summaryrefslogtreecommitdiff
path: root/libs/hostgraphics/include/gui/Surface.h
blob: e268ce6ca769006bc732772ed1feca8d66580f4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_GUI_SURFACE_H
#define ANDROID_GUI_SURFACE_H

#include <system/window.h>
#include <ui/ANativeObjectBase.h>
#include <utils/RefBase.h>

#include "gui/IGraphicBufferProducer.h"

namespace android {

class Surface : public ANativeObjectBase<ANativeWindow, Surface, RefBase> {
public:
    explicit Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false)
          : mBufferProducer(bufferProducer) {
        ANativeWindow::perform = hook_perform;
        ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
        ANativeWindow::query = hook_query;
    }

    sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
        return mBufferProducer;
    }

    static bool isValid(const sp<Surface>& surface) {
        return surface != nullptr;
    }

    void allocateBuffers() {}

    uint64_t getNextFrameNumber() const {
        return 0;
    }

    int setScalingMode(int mode) {
        return 0;
    }

    virtual int disconnect(int api,
                           IGraphicBufferProducer::DisconnectMode mode =
                                   IGraphicBufferProducer::DisconnectMode::Api) {
        return 0;
    }

    virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds) {
        // TODO: implement this
        return 0;
    }

    virtual int unlockAndPost() {
        return 0;
    }

    virtual int query(int what, int* value) const {
        return mBufferProducer->query(what, value);
    }

    status_t setDequeueTimeout(nsecs_t timeout) {
        return OK;
    }

    nsecs_t getLastDequeueStartTime() const {
        return 0;
    }

    virtual void destroy() {}

    int getBuffersDataSpace() {
        return 0;
    }

protected:
    virtual ~Surface() {}

    static int hook_perform(ANativeWindow* window, int operation, ...) {
        va_list args;
        va_start(args, operation);
        Surface* c = getSelf(window);
        int result = c->perform(operation, args);
        va_end(args);
        return result;
    }

    static int hook_query(const ANativeWindow* window, int what, int* value) {
        const Surface* c = getSelf(window);
        return c->query(what, value);
    }

    static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer,
                                  int* fenceFd) {
        Surface* c = getSelf(window);
        return c->dequeueBuffer(buffer, fenceFd);
    }

    virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd) {
        mBufferProducer->requestBuffer(0, &mBuffer);
        *buffer = mBuffer.get();
        return OK;
    }

    virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd) {
        return 0;
    }

    virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd) {
        return 0;
    }

    virtual int perform(int operation, va_list args) {
        return 0;
    }

    virtual int setSwapInterval(int interval) {
        return 0;
    }

    virtual int setBufferCount(int bufferCount) {
        return 0;
    }

private:
    // can't be copied
    Surface& operator=(const Surface& rhs);

    Surface(const Surface& rhs);

    const sp<IGraphicBufferProducer> mBufferProducer;
    sp<GraphicBuffer> mBuffer;
};

} // namespace android

#endif // ANDROID_GUI_SURFACE_H