SurfaceTextureClient.h revision 976f494d53cb239307fd810ce592b5b5de1cbc41
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
23#include <ui/egl/android_natives.h>
24#include <ui/Region.h>
25
26#include <utils/RefBase.h>
27#include <utils/threads.h>
28
29namespace android {
30
31class Surface;
32
33class SurfaceTextureClient
34    : public EGLNativeBase<ANativeWindow, SurfaceTextureClient, RefBase>
35{
36public:
37    SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
38
39    sp<ISurfaceTexture> getISurfaceTexture() const;
40
41protected:
42    SurfaceTextureClient();
43    void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture);
44
45private:
46    // can't be copied
47    SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
48    SurfaceTextureClient(const SurfaceTextureClient& rhs);
49    void init();
50
51    // ANativeWindow hooks
52    static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
53    static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
54    static int hook_lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
55    static int hook_perform(ANativeWindow* window, int operation, ...);
56    static int hook_query(const ANativeWindow* window, int what, int* value);
57    static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
58    static int hook_setSwapInterval(ANativeWindow* window, int interval);
59
60    int dispatchConnect(va_list args);
61    int dispatchDisconnect(va_list args);
62    int dispatchSetBufferCount(va_list args);
63    int dispatchSetBuffersGeometry(va_list args);
64    int dispatchSetBuffersDimensions(va_list args);
65    int dispatchSetBuffersFormat(va_list args);
66    int dispatchSetScalingMode(va_list args);
67    int dispatchSetBuffersTransform(va_list args);
68    int dispatchSetBuffersTimestamp(va_list args);
69    int dispatchSetCrop(va_list args);
70    int dispatchSetUsage(va_list args);
71    int dispatchLock(va_list args);
72    int dispatchUnlockAndPost(va_list args);
73
74protected:
75    virtual int cancelBuffer(ANativeWindowBuffer* buffer);
76    virtual int dequeueBuffer(ANativeWindowBuffer** buffer);
77    virtual int lockBuffer(ANativeWindowBuffer* buffer);
78    virtual int perform(int operation, va_list args);
79    virtual int query(int what, int* value) const;
80    virtual int queueBuffer(ANativeWindowBuffer* buffer);
81    virtual int setSwapInterval(int interval);
82
83    virtual int connect(int api);
84    virtual int disconnect(int api);
85    virtual int setBufferCount(int bufferCount);
86    virtual int setBuffersDimensions(int w, int h);
87    virtual int setBuffersFormat(int format);
88    virtual int setScalingMode(int mode);
89    virtual int setBuffersTransform(int transform);
90    virtual int setBuffersTimestamp(int64_t timestamp);
91    virtual int setCrop(Rect const* rect);
92    virtual int setUsage(uint32_t reqUsage);
93    virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
94    virtual int unlockAndPost();
95
96    enum { MIN_UNDEQUEUED_BUFFERS = SurfaceTexture::MIN_UNDEQUEUED_BUFFERS };
97    enum { NUM_BUFFER_SLOTS = SurfaceTexture::NUM_BUFFER_SLOTS };
98    enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
99
100private:
101    void freeAllBuffers();
102    int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
103
104    // mSurfaceTexture is the interface to the surface texture server. All
105    // operations on the surface texture client ultimately translate into
106    // interactions with the server using this interface.
107    sp<ISurfaceTexture> mSurfaceTexture;
108
109    // mSlots stores the buffers that have been allocated for each buffer slot.
110    // It is initialized to null pointers, and gets filled in with the result of
111    // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
112    // slot that has not yet been used. The buffer allocated to a slot will also
113    // be replaced if the requested buffer usage or geometry differs from that
114    // of the buffer allocated to a slot.
115    sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS];
116
117    // mReqWidth is the buffer width that will be requested at the next dequeue
118    // operation. It is initialized to 1.
119    uint32_t mReqWidth;
120
121    // mReqHeight is the buffer height that will be requested at the next deuque
122    // operation. It is initialized to 1.
123    uint32_t mReqHeight;
124
125    // mReqFormat is the buffer pixel format that will be requested at the next
126    // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
127    uint32_t mReqFormat;
128
129    // mReqUsage is the set of buffer usage flags that will be requested
130    // at the next deuque operation. It is initialized to 0.
131    uint32_t mReqUsage;
132
133    // mTimestamp is the timestamp that will be used for the next buffer queue
134    // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
135    // a timestamp is auto-generated when queueBuffer is called.
136    int64_t mTimestamp;
137
138    // mQueryWidth is the width returned by query(). It is set to width
139    // of the last dequeued buffer or to mReqWidth if no buffer was dequeued.
140    uint32_t mQueryWidth;
141
142    // mQueryHeight is the height returned by query(). It is set to height
143    // of the last dequeued buffer or to mReqHeight if no buffer was dequeued.
144    uint32_t mQueryHeight;
145
146    // mQueryFormat is the format returned by query(). It is set to the last
147    // dequeued format or to mReqFormat if no buffer was dequeued.
148    uint32_t mQueryFormat;
149
150    // mDefaultWidth is default width of the window, regardless of the
151    // set_dimension call
152    uint32_t mDefaultWidth;
153
154    // mDefaultHeight is default width of the window, regardless of the
155    // set_dimension call
156    uint32_t mDefaultHeight;
157
158    // mTransformHint is the transform probably applied to buffers of this
159    // window. this is only a hint, actual transform may differ.
160    uint32_t mTransformHint;
161
162    // mMutex is the mutex used to prevent concurrent access to the member
163    // variables of SurfaceTexture objects. It must be locked whenever the
164    // member variables are accessed.
165    mutable Mutex mMutex;
166
167    // must be used from the lock/unlock thread
168    sp<GraphicBuffer>           mLockedBuffer;
169    sp<GraphicBuffer>           mPostedBuffer;
170    mutable Region              mOldDirtyRegion;
171    bool                        mConnectedToCpu;
172};
173
174}; // namespace android
175
176#endif  // ANDROID_GUI_SURFACETEXTURECLIENT_H
177