SurfaceTextureClient.h revision bee205fd58a27c10a0895de5339e76025d429d2b
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 dispatchSetBuffersDimensions(va_list args);
69    int dispatchSetBuffersFormat(va_list args);
70    int dispatchSetBuffersTransform(va_list args);
71    int dispatchSetBuffersTimestamp(va_list args);
72    int dispatchSetCrop(va_list args);
73    int dispatchSetUsage(va_list args);
74
75    int connect(int api);
76    int disconnect(int api);
77    int setBufferCount(int bufferCount);
78    int setBuffersDimensions(int w, int h);
79    int setBuffersFormat(int format);
80    int setBuffersTransform(int transform);
81    int setBuffersTimestamp(int64_t timestamp);
82    int setCrop(Rect const* rect);
83    int setUsage(uint32_t reqUsage);
84
85    void freeAllBuffers();
86    int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
87
88    int getConnectedApi() const;
89
90    enum { MIN_UNDEQUEUED_BUFFERS = SurfaceTexture::MIN_UNDEQUEUED_BUFFERS };
91    enum { NUM_BUFFER_SLOTS = SurfaceTexture::NUM_BUFFER_SLOTS };
92    enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
93
94    // mSurfaceTexture is the interface to the surface texture server. All
95    // operations on the surface texture client ultimately translate into
96    // interactions with the server using this interface.
97    sp<ISurfaceTexture> mSurfaceTexture;
98
99    // mAllocator is the binder object that is referenced to prevent the
100    // dequeued buffers from being freed prematurely.
101    sp<IBinder> mAllocator;
102
103    // mSlots stores the buffers that have been allocated for each buffer slot.
104    // It is initialized to null pointers, and gets filled in with the result of
105    // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
106    // slot that has not yet been used. The buffer allocated to a slot will also
107    // be replaced if the requested buffer usage or geometry differs from that
108    // of the buffer allocated to a slot.
109    sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS];
110
111    // mReqWidth is the buffer width that will be requested at the next dequeue
112    // operation. It is initialized to 1.
113    uint32_t mReqWidth;
114
115    // mReqHeight is the buffer height that will be requested at the next deuque
116    // operation. It is initialized to 1.
117    uint32_t mReqHeight;
118
119    // mReqFormat is the buffer pixel format that will be requested at the next
120    // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
121    uint32_t mReqFormat;
122
123    // mReqUsage is the set of buffer usage flags that will be requested
124    // at the next deuque operation. It is initialized to 0.
125    uint32_t mReqUsage;
126
127    // mTimestamp is the timestamp that will be used for the next buffer queue
128    // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
129    // a timestamp is auto-generated when queueBuffer is called.
130    int64_t mTimestamp;
131
132    // mConnectedApi holds the currently connected API to this surface
133    int mConnectedApi;
134
135    // mQueryWidth is the width returned by query(). It is set to width
136    // of the last dequeued buffer or to mReqWidth if no buffer was dequeued.
137    uint32_t mQueryWidth;
138
139    // mQueryHeight is the height returned by query(). It is set to height
140    // of the last dequeued buffer or to mReqHeight if no buffer was dequeued.
141    uint32_t mQueryHeight;
142
143    // mQueryFormat is the format returned by query(). It is set to the last
144    // dequeued format or to mReqFormat if no buffer was dequeued.
145    uint32_t mQueryFormat;
146
147    // mMutex is the mutex used to prevent concurrent access to the member
148    // variables of SurfaceTexture objects. It must be locked whenever the
149    // member variables are accessed.
150    mutable Mutex mMutex;
151};
152
153}; // namespace android
154
155#endif  // ANDROID_GUI_SURFACETEXTURECLIENT_H
156