SurfaceTextureClient.h revision 851ef8f1bfbb164d61b1528a529a464f0a60dbaf
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_GUI_SURFACETEXTURECLIENT_H
18#define ANDROID_GUI_SURFACETEXTURECLIENT_H
19
20#include <gui/ISurfaceTexture.h>
21#include <gui/SurfaceTexture.h>
22#include <gui/BufferQueue.h>
23
24#include <ui/ANativeObjectBase.h>
25#include <ui/Region.h>
26
27#include <utils/RefBase.h>
28#include <utils/threads.h>
29
30struct ANativeWindow_Buffer;
31
32namespace android {
33
34class Surface;
35
36class SurfaceTextureClient
37    : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase>
38{
39public:
40
41    SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
42
43    // SurfaceTextureClient is overloaded to assist in refactoring ST and BQ.
44    // SurfaceTexture is no longer an ISurfaceTexture, so client code
45    // calling the original constructor will fail. Thus this convenience method
46    // passes in the surfaceTexture's bufferQueue to the init method.
47    SurfaceTextureClient(const sp<SurfaceTexture>& surfaceTexture);
48
49    sp<ISurfaceTexture> getISurfaceTexture() const;
50
51protected:
52    SurfaceTextureClient();
53    virtual ~SurfaceTextureClient();
54    void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture);
55
56private:
57    // can't be copied
58    SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
59    SurfaceTextureClient(const SurfaceTextureClient& rhs);
60    void init();
61
62    // ANativeWindow hooks
63    static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
64    static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
65    static int hook_lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
66    static int hook_perform(ANativeWindow* window, int operation, ...);
67    static int hook_query(const ANativeWindow* window, int what, int* value);
68    static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
69    static int hook_setSwapInterval(ANativeWindow* window, int interval);
70
71    int dispatchConnect(va_list args);
72    int dispatchDisconnect(va_list args);
73    int dispatchSetBufferCount(va_list args);
74    int dispatchSetBuffersGeometry(va_list args);
75    int dispatchSetBuffersDimensions(va_list args);
76    int dispatchSetBuffersFormat(va_list args);
77    int dispatchSetScalingMode(va_list args);
78    int dispatchSetBuffersTransform(va_list args);
79    int dispatchSetBuffersTimestamp(va_list args);
80    int dispatchSetCrop(va_list args);
81    int dispatchSetUsage(va_list args);
82    int dispatchLock(va_list args);
83    int dispatchUnlockAndPost(va_list args);
84
85protected:
86    virtual int cancelBuffer(ANativeWindowBuffer* buffer);
87    virtual int dequeueBuffer(ANativeWindowBuffer** buffer);
88    virtual int lockBuffer(ANativeWindowBuffer* buffer);
89    virtual int perform(int operation, va_list args);
90    virtual int query(int what, int* value) const;
91    virtual int queueBuffer(ANativeWindowBuffer* buffer);
92    virtual int setSwapInterval(int interval);
93
94    virtual int connect(int api);
95    virtual int disconnect(int api);
96    virtual int setBufferCount(int bufferCount);
97    virtual int setBuffersDimensions(int w, int h);
98    virtual int setBuffersFormat(int format);
99    virtual int setScalingMode(int mode);
100    virtual int setBuffersTransform(int transform);
101    virtual int setBuffersTimestamp(int64_t timestamp);
102    virtual int setCrop(Rect const* rect);
103    virtual int setUsage(uint32_t reqUsage);
104    virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
105    virtual int unlockAndPost();
106
107    enum { MIN_UNDEQUEUED_BUFFERS = BufferQueue::MIN_UNDEQUEUED_BUFFERS };
108    enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
109    enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
110
111private:
112    void freeAllBuffers();
113    int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
114
115    // mSurfaceTexture is the interface to the surface texture server. All
116    // operations on the surface texture client ultimately translate into
117    // interactions with the server using this interface.
118    sp<ISurfaceTexture> mSurfaceTexture;
119
120    // mSlots stores the buffers that have been allocated for each buffer slot.
121    // It is initialized to null pointers, and gets filled in with the result of
122    // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
123    // slot that has not yet been used. The buffer allocated to a slot will also
124    // be replaced if the requested buffer usage or geometry differs from that
125    // of the buffer allocated to a slot.
126    sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS];
127
128    // mReqWidth is the buffer width that will be requested at the next dequeue
129    // operation. It is initialized to 1.
130    uint32_t mReqWidth;
131
132    // mReqHeight is the buffer height that will be requested at the next
133    // dequeue operation. It is initialized to 1.
134    uint32_t mReqHeight;
135
136    // mReqFormat is the buffer pixel format that will be requested at the next
137    // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
138    uint32_t mReqFormat;
139
140    // mReqUsage is the set of buffer usage flags that will be requested
141    // at the next deuque operation. It is initialized to 0.
142    uint32_t mReqUsage;
143
144    // mTimestamp is the timestamp that will be used for the next buffer queue
145    // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
146    // a timestamp is auto-generated when queueBuffer is called.
147    int64_t mTimestamp;
148
149    // mCrop is the crop rectangle that will be used for the next buffer
150    // that gets queued. It is set by calling setCrop.
151    Rect mCrop;
152
153    // mScalingMode is the scaling mode that will be used for the next
154    // buffers that get queued. It is set by calling setScalingMode.
155    int mScalingMode;
156
157    // mTransform is the transform identifier that will be used for the next
158    // buffer that gets queued. It is set by calling setTransform.
159    uint32_t mTransform;
160
161    // mDefaultWidth is default width of the window, regardless of the
162    // native_window_set_buffers_dimensions call
163    uint32_t mDefaultWidth;
164
165    // mDefaultHeight is default width of the window, regardless of the
166    // native_window_set_buffers_dimensions call
167    uint32_t mDefaultHeight;
168
169    // mTransformHint is the transform probably applied to buffers of this
170    // window. this is only a hint, actual transform may differ.
171    uint32_t mTransformHint;
172
173    // mMutex is the mutex used to prevent concurrent access to the member
174    // variables of SurfaceTexture objects. It must be locked whenever the
175    // member variables are accessed.
176    mutable Mutex mMutex;
177
178    // must be used from the lock/unlock thread
179    sp<GraphicBuffer>           mLockedBuffer;
180    sp<GraphicBuffer>           mPostedBuffer;
181    mutable Region              mOldDirtyRegion;
182    bool                        mConnectedToCpu;
183};
184
185}; // namespace android
186
187#endif  // ANDROID_GUI_SURFACETEXTURECLIENT_H
188