blob: c0e90b32bbb5463d0928fd48d155e22eb0ecc34b [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>
29
Mathias Agopianb0e76f42012-03-23 14:15:44 -070030struct ANativeWindow_Buffer;
31
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032namespace android {
33
Mathias Agopian7a042bf2011-04-11 21:19:55 -070034class Surface;
35
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080036class SurfaceTextureClient
Mathias Agopian5f2165f2012-02-24 18:25:41 -080037 : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080038{
39public:
Daniel Lamb2675792012-02-23 14:35:13 -080040
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080041 SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture);
42
Daniel Lamb2675792012-02-23 14:35:13 -080043 // SurfaceTextureClient is overloaded to assist in refactoring ST and BQ.
44 // SurfaceTexture is no longer an ISurfaceTexture, so client code
45 // calling the original constructor will fail. Thus this convenience method
46 // passes in the surfaceTexture's bufferQueue to the init method.
47 SurfaceTextureClient(const sp<SurfaceTexture>& surfaceTexture);
48
Jamie Gennisbae774e2011-03-14 15:08:53 -070049 sp<ISurfaceTexture> getISurfaceTexture() const;
50
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070051protected:
52 SurfaceTextureClient();
Mathias Agopiana36bcd52011-11-17 18:46:09 -080053 virtual ~SurfaceTextureClient();
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070054 void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080055
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070056private:
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057 // can't be copied
58 SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs);
59 SurfaceTextureClient(const SurfaceTextureClient& rhs);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070060 void init();
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080061
62 // ANativeWindow hooks
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070063 static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
64 static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer);
65 static int hook_lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
66 static int hook_perform(ANativeWindow* window, int operation, ...);
67 static int hook_query(const ANativeWindow* window, int what, int* value);
68 static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer);
69 static int hook_setSwapInterval(ANativeWindow* window, int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080070
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080071 int dispatchConnect(va_list args);
72 int dispatchDisconnect(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073 int dispatchSetBufferCount(va_list args);
74 int dispatchSetBuffersGeometry(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070075 int dispatchSetBuffersDimensions(va_list args);
Michael I. Gold55a70142012-04-09 19:46:29 -070076 int dispatchSetBuffersUserDimensions(va_list args);
Jamie Gennisbee205f2011-07-01 13:12:07 -070077 int dispatchSetBuffersFormat(va_list args);
Mathias Agopian7734ebf2011-07-13 15:24:42 -070078 int dispatchSetScalingMode(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080079 int dispatchSetBuffersTransform(va_list args);
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -080080 int dispatchSetBuffersTimestamp(va_list args);
Jamie Gennis9d4d6c12011-02-27 14:10:20 -080081 int dispatchSetCrop(va_list args);
82 int dispatchSetUsage(va_list args);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070083 int dispatchLock(va_list args);
84 int dispatchUnlockAndPost(va_list args);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070086protected:
87 virtual int cancelBuffer(ANativeWindowBuffer* buffer);
88 virtual int dequeueBuffer(ANativeWindowBuffer** buffer);
89 virtual int lockBuffer(ANativeWindowBuffer* buffer);
90 virtual int perform(int operation, va_list args);
91 virtual int query(int what, int* value) const;
92 virtual int queueBuffer(ANativeWindowBuffer* buffer);
93 virtual int setSwapInterval(int interval);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080094
Mathias Agopian8f9dbf92011-07-13 17:39:11 -070095 virtual int connect(int api);
96 virtual int disconnect(int api);
97 virtual int setBufferCount(int bufferCount);
98 virtual int setBuffersDimensions(int w, int h);
Michael I. Gold55a70142012-04-09 19:46:29 -070099 virtual int setBuffersUserDimensions(int w, int h);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700100 virtual int setBuffersFormat(int format);
Mathias Agopian7734ebf2011-07-13 15:24:42 -0700101 virtual int setScalingMode(int mode);
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700102 virtual int setBuffersTransform(int transform);
103 virtual int setBuffersTimestamp(int64_t timestamp);
104 virtual int setCrop(Rect const* rect);
105 virtual int setUsage(uint32_t reqUsage);
106 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
107 virtual int unlockAndPost();
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700108
Daniel Lamb2675792012-02-23 14:35:13 -0800109 enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800110 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
111
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700112private:
113 void freeAllBuffers();
114 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
115
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800116 // mSurfaceTexture is the interface to the surface texture server. All
117 // operations on the surface texture client ultimately translate into
118 // interactions with the server using this interface.
119 sp<ISurfaceTexture> mSurfaceTexture;
120
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800121 // mSlots stores the buffers that have been allocated for each buffer slot.
122 // It is initialized to null pointers, and gets filled in with the result of
123 // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a
124 // slot that has not yet been used. The buffer allocated to a slot will also
125 // be replaced if the requested buffer usage or geometry differs from that
126 // of the buffer allocated to a slot.
127 sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS];
128
129 // mReqWidth is the buffer width that will be requested at the next dequeue
130 // operation. It is initialized to 1.
131 uint32_t mReqWidth;
132
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700133 // mReqHeight is the buffer height that will be requested at the next
134 // dequeue operation. It is initialized to 1.
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800135 uint32_t mReqHeight;
136
137 // mReqFormat is the buffer pixel format that will be requested at the next
138 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
139 uint32_t mReqFormat;
140
141 // mReqUsage is the set of buffer usage flags that will be requested
142 // at the next deuque operation. It is initialized to 0.
143 uint32_t mReqUsage;
144
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800145 // mTimestamp is the timestamp that will be used for the next buffer queue
146 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
147 // a timestamp is auto-generated when queueBuffer is called.
148 int64_t mTimestamp;
149
Mathias Agopian851ef8f2012-03-29 17:10:08 -0700150 // mCrop is the crop rectangle that will be used for the next buffer
151 // that gets queued. It is set by calling setCrop.
152 Rect mCrop;
153
154 // mScalingMode is the scaling mode that will be used for the next
155 // buffers that get queued. It is set by calling setScalingMode.
156 int mScalingMode;
157
158 // mTransform is the transform identifier that will be used for the next
159 // buffer that gets queued. It is set by calling setTransform.
160 uint32_t mTransform;
161
Michael I. Gold55a70142012-04-09 19:46:29 -0700162 // mDefaultWidth is default width of the buffers, regardless of the
163 // native_window_set_buffers_dimensions call.
164 uint32_t mDefaultWidth;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700165
Michael I. Gold55a70142012-04-09 19:46:29 -0700166 // mDefaultHeight is default height of the buffers, regardless of the
167 // native_window_set_buffers_dimensions call.
168 uint32_t mDefaultHeight;
169
170 // mUserWidth, if non-zero, is an application-specified override
171 // of mDefaultWidth. This is lower priority than the width set by
172 // native_window_set_buffers_dimensions.
173 uint32_t mUserWidth;
174
175 // mUserHeight, if non-zero, is an application-specified override
176 // of mDefaultHeight. This is lower priority than the height set
177 // by native_window_set_buffers_dimensions.
178 uint32_t mUserHeight;
Mathias Agopian97c602c2011-07-19 15:24:46 -0700179
180 // mTransformHint is the transform probably applied to buffers of this
181 // window. this is only a hint, actual transform may differ.
182 uint32_t mTransformHint;
183
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800184 // mMutex is the mutex used to prevent concurrent access to the member
185 // variables of SurfaceTexture objects. It must be locked whenever the
186 // member variables are accessed.
Mathias Agopian7a042bf2011-04-11 21:19:55 -0700187 mutable Mutex mMutex;
Mathias Agopian8f9dbf92011-07-13 17:39:11 -0700188
189 // must be used from the lock/unlock thread
190 sp<GraphicBuffer> mLockedBuffer;
191 sp<GraphicBuffer> mPostedBuffer;
192 mutable Region mOldDirtyRegion;
193 bool mConnectedToCpu;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800194};
195
196}; // namespace android
197
198#endif // ANDROID_GUI_SURFACETEXTURECLIENT_H