blob: 56d861a462e3d23e6c06487bb781f9f47df135d3 [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
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>
Daniel Lamb2675792012-02-23 14:35:13 -080022#include <gui/BufferQueue.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080023
Mathias Agopian5f2165f2012-02-24 18:25:41 -080024#include <ui/ANativeObjectBase.h>
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070025#include <ui/Region.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080026
27#include <utils/RefBase.h>
28#include <utils/threads.h>
Mathias Agopianac6035a2012-04-12 16:32:37 -070029#include <utils/KeyedVector.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080030
Mathias Agopianb0e76f42012-03-23 14:15:44 -070031struct ANativeWindow_Buffer;
32
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080033namespace android {
34
Mathias Agopian7a042bf2011-04-11 21:19:55 -070035class Surface;
36
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080037class SurfaceTextureClient
Mathias Agopian5f2165f2012-02-24 18:25:41 -080038 : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080039{
40public:
Daniel Lamb2675792012-02-23 14:35:13 -080041
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080042 SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
43
Jamie Gennisbae774e2011-03-14 15:08:53 -070044 sp<ISurfaceTexture> getISurfaceTexture() const;
45
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070046protected:
47 SurfaceTextureClient();
Mathias Agopiana36bcd52011-11-17 18:46:09 -080048 virtual ~SurfaceTextureClient();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070049 void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080050
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070051private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080052 // can't be copied
53 SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
54 SurfaceTextureClient(const SurfaceTextureClient& rhs);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070055 void init();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080056
57 // ANativeWindow hooks
Jamie Gennisd8e812c2012-06-13 16:32:25 -070058 static int hook_cancelBuffer(ANativeWindow* window,
59 ANativeWindowBuffer* buffer, int fenceFd);
60 static int hook_dequeueBuffer(ANativeWindow* window,
61 ANativeWindowBuffer** buffer, int* fenceFd);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070062 static int hook_perform(ANativeWindow* window, int operation, ...);
63 static int hook_query(const ANativeWindow* window, int what, int* value);
Jamie Gennisd8e812c2012-06-13 16:32:25 -070064 static int hook_queueBuffer(ANativeWindow* window,
65 ANativeWindowBuffer* buffer, int fenceFd);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070066 static int hook_setSwapInterval(ANativeWindow* window, int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080067
Jamie Gennisd8e812c2012-06-13 16:32:25 -070068 static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
69 ANativeWindowBuffer* buffer);
70 static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
71 ANativeWindowBuffer** buffer);
72 static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
73 ANativeWindowBuffer* buffer);
74 static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
75 ANativeWindowBuffer* buffer);
76
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080077 int dispatchConnect(va_list args);
78 int dispatchDisconnect(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080079 int dispatchSetBufferCount(va_list args);
80 int dispatchSetBuffersGeometry(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070081 int dispatchSetBuffersDimensions(va_list args);
Michael I. Gold55a70142012-04-09 19:46:29 -070082 int dispatchSetBuffersUserDimensions(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070083 int dispatchSetBuffersFormat(va_list args);
Mathias Agopian7734ebf2011-07-13 15:24:42 -070084 int dispatchSetScalingMode(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085 int dispatchSetBuffersTransform(va_list args);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080086 int dispatchSetBuffersTimestamp(va_list args);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080087 int dispatchSetCrop(va_list args);
Jamie Gennisd72f2332012-05-07 13:50:11 -070088 int dispatchSetPostTransformCrop(va_list args);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080089 int dispatchSetUsage(va_list args);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070090 int dispatchLock(va_list args);
91 int dispatchUnlockAndPost(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080092
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070093protected:
Jamie Gennisd8e812c2012-06-13 16:32:25 -070094 virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
95 virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
96 virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070097 virtual int perform(int operation, va_list args);
98 virtual int query(int what, int* value) const;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070099 virtual int setSwapInterval(int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800100
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700101 virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
102
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700103 virtual int connect(int api);
104 virtual int disconnect(int api);
105 virtual int setBufferCount(int bufferCount);
106 virtual int setBuffersDimensions(int w, int h);
Michael I. Gold55a70142012-04-09 19:46:29 -0700107 virtual int setBuffersUserDimensions(int w, int h);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700108 virtual int setBuffersFormat(int format);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700109 virtual int setScalingMode(int mode);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700110 virtual int setBuffersTransform(int transform);
111 virtual int setBuffersTimestamp(int64_t timestamp);
112 virtual int setCrop(Rect const* rect);
113 virtual int setUsage(uint32_t reqUsage);
114 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
115 virtual int unlockAndPost();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700116
Daniel Lamb2675792012-02-23 14:35:13 -0800117 enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800118 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
119
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700120private:
121 void freeAllBuffers();
122 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
123
Mathias Agopianac6035a2012-04-12 16:32:37 -0700124 struct BufferSlot {
125 sp<GraphicBuffer> buffer;
126 Region dirtyRegion;
127 };
128
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 // mSurfaceTexture is the interface to the surface texture server. All
130 // operations on the surface texture client ultimately translate into
131 // interactions with the server using this interface.
132 sp<ISurfaceTexture> mSurfaceTexture;
133
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800134 // mSlots stores the buffers that have been allocated for each buffer slot.
135 // It is initialized to null pointers, and gets filled in with the result of
136 // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
137 // slot that has not yet been used. The buffer allocated to a slot will also
138 // be replaced if the requested buffer usage or geometry differs from that
139 // of the buffer allocated to a slot.
Mathias Agopianac6035a2012-04-12 16:32:37 -0700140 BufferSlot mSlots[NUM_BUFFER_SLOTS];
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800141
142 // mReqWidth is the buffer width that will be requested at the next dequeue
143 // operation. It is initialized to 1.
144 uint32_t mReqWidth;
145
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700146 // mReqHeight is the buffer height that will be requested at the next
147 // dequeue operation. It is initialized to 1.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800148 uint32_t mReqHeight;
149
150 // mReqFormat is the buffer pixel format that will be requested at the next
151 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
152 uint32_t mReqFormat;
153
154 // mReqUsage is the set of buffer usage flags that will be requested
155 // at the next deuque operation. It is initialized to 0.
156 uint32_t mReqUsage;
157
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800158 // mTimestamp is the timestamp that will be used for the next buffer queue
159 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
160 // a timestamp is auto-generated when queueBuffer is called.
161 int64_t mTimestamp;
162
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700163 // mCrop is the crop rectangle that will be used for the next buffer
164 // that gets queued. It is set by calling setCrop.
165 Rect mCrop;
166
167 // mScalingMode is the scaling mode that will be used for the next
168 // buffers that get queued. It is set by calling setScalingMode.
169 int mScalingMode;
170
171 // mTransform is the transform identifier that will be used for the next
172 // buffer that gets queued. It is set by calling setTransform.
173 uint32_t mTransform;
174
Michael I. Gold55a70142012-04-09 19:46:29 -0700175 // mDefaultWidth is default width of the buffers, regardless of the
176 // native_window_set_buffers_dimensions call.
177 uint32_t mDefaultWidth;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700178
Michael I. Gold55a70142012-04-09 19:46:29 -0700179 // mDefaultHeight is default height of the buffers, regardless of the
180 // native_window_set_buffers_dimensions call.
181 uint32_t mDefaultHeight;
182
183 // mUserWidth, if non-zero, is an application-specified override
184 // of mDefaultWidth. This is lower priority than the width set by
185 // native_window_set_buffers_dimensions.
186 uint32_t mUserWidth;
187
188 // mUserHeight, if non-zero, is an application-specified override
189 // of mDefaultHeight. This is lower priority than the height set
190 // by native_window_set_buffers_dimensions.
191 uint32_t mUserHeight;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700192
193 // mTransformHint is the transform probably applied to buffers of this
194 // window. this is only a hint, actual transform may differ.
195 uint32_t mTransformHint;
196
Mathias Agopian2488b202012-04-20 17:19:28 -0700197 // mConsumerRunningBehind whether the consumer is running more than
198 // one buffer behind the producer.
199 mutable bool mConsumerRunningBehind;
200
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800201 // mMutex is the mutex used to prevent concurrent access to the member
202 // variables of SurfaceTexture objects. It must be locked whenever the
203 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700204 mutable Mutex mMutex;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700205
206 // must be used from the lock/unlock thread
207 sp<GraphicBuffer> mLockedBuffer;
208 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700209 bool mConnectedToCpu;
Mathias Agopianac6035a2012-04-12 16:32:37 -0700210
211 // must be accessed from lock/unlock thread only
212 Region mDirtyRegion;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800213};
214
215}; // namespace android
216
217#endif // ANDROID_GUI_SURFACETEXTURECLIENT_H