SurfaceTextureClient.h revision 697526bc9e44ce61c88614f98387ae8bbf0a187e
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
25#include <utils/RefBase.h>
26#include <utils/threads.h>
27
28namespace android {
29
30class Surface;
31
32class SurfaceTextureClient
33    : public EGLNativeBase<ANativeWindow, SurfaceTextureClient, RefBase>
34{
35public:
36    SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
37
38    sp<ISurfaceTexture> getISurfaceTexture() const;
39
40private:
41    friend class Surface;
42
43    // can't be copied
44    SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
45    SurfaceTextureClient(const SurfaceTextureClient& rhs);
46
47    // ANativeWindow hooks
48    static int cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
49    static int dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
50    static int lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
51    static int perform(ANativeWindow* window, int operation, ...);
52    static int query(const ANativeWindow* window, int what, int* value);
53    static int queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
54    static int setSwapInterval(ANativeWindow* window, int interval);
55
56    int cancelBuffer(ANativeWindowBuffer* buffer);
57    int dequeueBuffer(ANativeWindowBuffer** buffer);
58    int lockBuffer(ANativeWindowBuffer* buffer);
59    int perform(int operation, va_list args);
60    int query(int what, int* value) const;
61    int queueBuffer(ANativeWindowBuffer* buffer);
62    int setSwapInterval(int interval);
63
64    int dispatchConnect(va_list args);
65    int dispatchDisconnect(va_list args);
66    int dispatchSetBufferCount(va_list args);
67    int dispatchSetBuffersGeometry(va_list args);
68    int dispatchSetBuffersTransform(va_list args);
69    int dispatchSetBuffersTimestamp(va_list args);
70    int dispatchSetCrop(va_list args);
71    int dispatchSetUsage(va_list args);
72
73    int connect(int api);
74    int disconnect(int api);
75    int setBufferCount(int bufferCount);
76    int setBuffersGeometry(int w, int h, int format);
77    int setBuffersTransform(int transform);
78    int setBuffersTimestamp(int64_t timestamp);
79    int setCrop(Rect const* rect);
80    int setUsage(uint32_t reqUsage);
81
82    void freeAllBuffers();
83
84    int getConnectedApi() const;
85
86    enum { MIN_UNDEQUEUED_BUFFERS = SurfaceTexture::MIN_UNDEQUEUED_BUFFERS };
87    enum { MIN_BUFFER_SLOTS = SurfaceTexture::MIN_BUFFER_SLOTS };
88    enum { NUM_BUFFER_SLOTS = SurfaceTexture::NUM_BUFFER_SLOTS };
89    enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
90
91    // mSurfaceTexture is the interface to the surface texture server. All
92    // operations on the surface texture client ultimately translate into
93    // interactions with the server using this interface.
94    sp<ISurfaceTexture> mSurfaceTexture;
95
96    // mAllocator is the binder object that is referenced to prevent the
97    // dequeued buffers from being freed prematurely.
98    sp<IBinder> mAllocator;
99
100    // mSlots stores the buffers that have been allocated for each buffer slot.
101    // It is initialized to null pointers, and gets filled in with the result of
102    // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
103    // slot that has not yet been used. The buffer allocated to a slot will also
104    // be replaced if the requested buffer usage or geometry differs from that
105    // of the buffer allocated to a slot.
106    sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS];
107
108    // mReqWidth is the buffer width that will be requested at the next dequeue
109    // operation. It is initialized to 1.
110    uint32_t mReqWidth;
111
112    // mReqHeight is the buffer height that will be requested at the next deuque
113    // operation. It is initialized to 1.
114    uint32_t mReqHeight;
115
116    // mReqFormat is the buffer pixel format that will be requested at the next
117    // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
118    uint32_t mReqFormat;
119
120    // mReqUsage is the set of buffer usage flags that will be requested
121    // at the next deuque operation. It is initialized to 0.
122    uint32_t mReqUsage;
123
124    // mTimestamp is the timestamp that will be used for the next buffer queue
125    // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
126    // a timestamp is auto-generated when queueBuffer is called.
127    int64_t mTimestamp;
128
129    // mConnectedApi holds the currently connected API to this surface
130    int mConnectedApi;
131
132    // mQueryWidth is the width returned by query(). It is set to width
133    // of the last dequeued buffer or to mReqWidth if no buffer was dequeued.
134    uint32_t mQueryWidth;
135
136    // mQueryHeight is the height returned by query(). It is set to height
137    // of the last dequeued buffer or to mReqHeight if no buffer was dequeued.
138    uint32_t mQueryHeight;
139
140    // mQueryFormat is the format returned by query(). It is set to the last
141    // dequeued format or to mReqFormat if no buffer was dequeued.
142    uint32_t mQueryFormat;
143
144    // mMutex is the mutex used to prevent concurrent access to the member
145    // variables of SurfaceTexture objects. It must be locked whenever the
146    // member variables are accessed.
147    mutable Mutex mMutex;
148};
149
150}; // namespace android
151
152#endif  // ANDROID_GUI_SURFACETEXTURECLIENT_H
153