blob: 8fa85b67e68d477ca03fe77beb616df56eca8945 [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
Daniel Lamb2675792012-02-23 14:35:13 -080044 // SurfaceTextureClient is overloaded to assist in refactoring ST and BQ.
45 // SurfaceTexture is no longer an ISurfaceTexture, so client code
46 // calling the original constructor will fail. Thus this convenience method
47 // passes in the surfaceTexture's bufferQueue to the init method.
48 SurfaceTextureClient(const sp<SurfaceTexture>& surfaceTexture);
49
Jamie Gennisbae774e2011-03-14 15:08:53 -070050 sp<ISurfaceTexture> getISurfaceTexture() const;
51
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070052protected:
53 SurfaceTextureClient();
Mathias Agopiana36bcd52011-11-17 18:46:09 -080054 virtual ~SurfaceTextureClient();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070055 void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080056
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070057private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080058 // can't be copied
59 SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
60 SurfaceTextureClient(const SurfaceTextureClient& rhs);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070061 void init();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080062
63 // ANativeWindow hooks
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070064 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
65 static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
66 static int hook_lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
67 static int hook_perform(ANativeWindow* window, int operation, ...);
68 static int hook_query(const ANativeWindow* window, int what, int* value);
69 static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
70 static int hook_setSwapInterval(ANativeWindow* window, int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080071
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080072 int dispatchConnect(va_list args);
73 int dispatchDisconnect(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080074 int dispatchSetBufferCount(va_list args);
75 int dispatchSetBuffersGeometry(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070076 int dispatchSetBuffersDimensions(va_list args);
Michael I. Gold55a70142012-04-09 19:46:29 -070077 int dispatchSetBuffersUserDimensions(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070078 int dispatchSetBuffersFormat(va_list args);
Mathias Agopian7734ebf2011-07-13 15:24:42 -070079 int dispatchSetScalingMode(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080080 int dispatchSetBuffersTransform(va_list args);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080081 int dispatchSetBuffersTimestamp(va_list args);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080082 int dispatchSetCrop(va_list args);
Jamie Gennisd72f2332012-05-07 13:50:11 -070083 int dispatchSetPostTransformCrop(va_list args);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080084 int dispatchSetUsage(va_list args);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070085 int dispatchLock(va_list args);
86 int dispatchUnlockAndPost(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080087
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070088protected:
89 virtual int cancelBuffer(ANativeWindowBuffer* buffer);
90 virtual int dequeueBuffer(ANativeWindowBuffer** buffer);
91 virtual int lockBuffer(ANativeWindowBuffer* buffer);
92 virtual int perform(int operation, va_list args);
93 virtual int query(int what, int* value) const;
94 virtual int queueBuffer(ANativeWindowBuffer* buffer);
95 virtual int setSwapInterval(int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080096
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070097 virtual int connect(int api);
98 virtual int disconnect(int api);
99 virtual int setBufferCount(int bufferCount);
100 virtual int setBuffersDimensions(int w, int h);
Michael I. Gold55a70142012-04-09 19:46:29 -0700101 virtual int setBuffersUserDimensions(int w, int h);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700102 virtual int setBuffersFormat(int format);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700103 virtual int setScalingMode(int mode);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700104 virtual int setBuffersTransform(int transform);
105 virtual int setBuffersTimestamp(int64_t timestamp);
106 virtual int setCrop(Rect const* rect);
107 virtual int setUsage(uint32_t reqUsage);
108 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
109 virtual int unlockAndPost();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700110
Daniel Lamb2675792012-02-23 14:35:13 -0800111 enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800112 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
113
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700114private:
115 void freeAllBuffers();
116 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
117
Mathias Agopianac6035a2012-04-12 16:32:37 -0700118 struct BufferSlot {
119 sp<GraphicBuffer> buffer;
120 Region dirtyRegion;
121 };
122
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800123 // mSurfaceTexture is the interface to the surface texture server. All
124 // operations on the surface texture client ultimately translate into
125 // interactions with the server using this interface.
126 sp<ISurfaceTexture> mSurfaceTexture;
127
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128 // mSlots stores the buffers that have been allocated for each buffer slot.
129 // It is initialized to null pointers, and gets filled in with the result of
130 // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
131 // slot that has not yet been used. The buffer allocated to a slot will also
132 // be replaced if the requested buffer usage or geometry differs from that
133 // of the buffer allocated to a slot.
Mathias Agopianac6035a2012-04-12 16:32:37 -0700134 BufferSlot mSlots[NUM_BUFFER_SLOTS];
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135
136 // mReqWidth is the buffer width that will be requested at the next dequeue
137 // operation. It is initialized to 1.
138 uint32_t mReqWidth;
139
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700140 // mReqHeight is the buffer height that will be requested at the next
141 // dequeue operation. It is initialized to 1.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800142 uint32_t mReqHeight;
143
144 // mReqFormat is the buffer pixel format that will be requested at the next
145 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
146 uint32_t mReqFormat;
147
148 // mReqUsage is the set of buffer usage flags that will be requested
149 // at the next deuque operation. It is initialized to 0.
150 uint32_t mReqUsage;
151
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800152 // mTimestamp is the timestamp that will be used for the next buffer queue
153 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
154 // a timestamp is auto-generated when queueBuffer is called.
155 int64_t mTimestamp;
156
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700157 // mCrop is the crop rectangle that will be used for the next buffer
158 // that gets queued. It is set by calling setCrop.
159 Rect mCrop;
160
161 // mScalingMode is the scaling mode that will be used for the next
162 // buffers that get queued. It is set by calling setScalingMode.
163 int mScalingMode;
164
165 // mTransform is the transform identifier that will be used for the next
166 // buffer that gets queued. It is set by calling setTransform.
167 uint32_t mTransform;
168
Michael I. Gold55a70142012-04-09 19:46:29 -0700169 // mDefaultWidth is default width of the buffers, regardless of the
170 // native_window_set_buffers_dimensions call.
171 uint32_t mDefaultWidth;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700172
Michael I. Gold55a70142012-04-09 19:46:29 -0700173 // mDefaultHeight is default height of the buffers, regardless of the
174 // native_window_set_buffers_dimensions call.
175 uint32_t mDefaultHeight;
176
177 // mUserWidth, if non-zero, is an application-specified override
178 // of mDefaultWidth. This is lower priority than the width set by
179 // native_window_set_buffers_dimensions.
180 uint32_t mUserWidth;
181
182 // mUserHeight, if non-zero, is an application-specified override
183 // of mDefaultHeight. This is lower priority than the height set
184 // by native_window_set_buffers_dimensions.
185 uint32_t mUserHeight;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700186
187 // mTransformHint is the transform probably applied to buffers of this
188 // window. this is only a hint, actual transform may differ.
189 uint32_t mTransformHint;
190
Mathias Agopian2488b202012-04-20 17:19:28 -0700191 // mConsumerRunningBehind whether the consumer is running more than
192 // one buffer behind the producer.
193 mutable bool mConsumerRunningBehind;
194
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195 // mMutex is the mutex used to prevent concurrent access to the member
196 // variables of SurfaceTexture objects. It must be locked whenever the
197 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700198 mutable Mutex mMutex;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700199
200 // must be used from the lock/unlock thread
201 sp<GraphicBuffer> mLockedBuffer;
202 sp<GraphicBuffer> mPostedBuffer;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700203 bool mConnectedToCpu;
Mathias Agopianac6035a2012-04-12 16:32:37 -0700204
205 // must be accessed from lock/unlock thread only
206 Region mDirtyRegion;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800207};
208
209}; // namespace android
210
211#endif // ANDROID_GUI_SURFACETEXTURECLIENT_H