Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 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 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 20 | #include <gui/IGraphicBufferProducer.h> |
| 21 | #include <gui/GLConsumer.h> |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 22 | #include <gui/BufferQueue.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 23 | |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 24 | #include <ui/ANativeObjectBase.h> |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 25 | #include <ui/Region.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 26 | |
| 27 | #include <utils/RefBase.h> |
| 28 | #include <utils/threads.h> |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 29 | #include <utils/KeyedVector.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | b0e76f4 | 2012-03-23 14:15:44 -0700 | [diff] [blame] | 31 | struct ANativeWindow_Buffer; |
| 32 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 35 | class Surface; |
| 36 | |
Andy McFadden | 0273adb | 2012-12-12 17:10:08 -0800 | [diff] [blame] | 37 | /* |
| 38 | * An implementation of ANativeWindow that also behaves as the producer |
| 39 | * side of a BufferQueue. |
| 40 | * |
| 41 | * This is typically used by programs that want to render frames through |
| 42 | * some means (maybe OpenGL, a software renderer, or a hardware decoder) |
| 43 | * and have the frames they create forwarded to SurfaceFlinger for |
| 44 | * compositing. For example, a video decoder could render a frame and call |
| 45 | * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by |
| 46 | * SurfaceTextureClient. STC then acts as the BufferQueue producer, |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 47 | * providing the new frame to a consumer such as GLConsumer. |
Andy McFadden | 0273adb | 2012-12-12 17:10:08 -0800 | [diff] [blame] | 48 | * |
| 49 | * TODO: rename to Surface. The existing Surface class wraps STC with |
| 50 | * some Binder goodies, which most users of Surface class don't care about. |
| 51 | */ |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 52 | class SurfaceTextureClient |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 53 | : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 54 | { |
| 55 | public: |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 56 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 57 | SurfaceTextureClient(const sp<IGraphicBufferProducer>& bufferProducer); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 58 | |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 59 | sp<IGraphicBufferProducer> getISurfaceTexture() const; // TODO: rename |
Jamie Gennis | bae774e | 2011-03-14 15:08:53 -0700 | [diff] [blame] | 60 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 61 | protected: |
| 62 | SurfaceTextureClient(); |
Mathias Agopian | a36bcd5 | 2011-11-17 18:46:09 -0800 | [diff] [blame] | 63 | virtual ~SurfaceTextureClient(); |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 64 | void setISurfaceTexture(const sp<IGraphicBufferProducer>& bufferProducer); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 65 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 66 | private: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 67 | // can't be copied |
| 68 | SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs); |
| 69 | SurfaceTextureClient(const SurfaceTextureClient& rhs); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 70 | void init(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 71 | |
| 72 | // ANativeWindow hooks |
Jamie Gennis | d8e812c | 2012-06-13 16:32:25 -0700 | [diff] [blame] | 73 | static int hook_cancelBuffer(ANativeWindow* window, |
| 74 | ANativeWindowBuffer* buffer, int fenceFd); |
| 75 | static int hook_dequeueBuffer(ANativeWindow* window, |
| 76 | ANativeWindowBuffer** buffer, int* fenceFd); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 77 | static int hook_perform(ANativeWindow* window, int operation, ...); |
| 78 | static int hook_query(const ANativeWindow* window, int what, int* value); |
Jamie Gennis | d8e812c | 2012-06-13 16:32:25 -0700 | [diff] [blame] | 79 | static int hook_queueBuffer(ANativeWindow* window, |
| 80 | ANativeWindowBuffer* buffer, int fenceFd); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 81 | static int hook_setSwapInterval(ANativeWindow* window, int interval); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 82 | |
Jamie Gennis | d8e812c | 2012-06-13 16:32:25 -0700 | [diff] [blame] | 83 | static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, |
| 84 | ANativeWindowBuffer* buffer); |
| 85 | static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, |
| 86 | ANativeWindowBuffer** buffer); |
| 87 | static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, |
| 88 | ANativeWindowBuffer* buffer); |
| 89 | static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, |
| 90 | ANativeWindowBuffer* buffer); |
| 91 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 92 | int dispatchConnect(va_list args); |
| 93 | int dispatchDisconnect(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 94 | int dispatchSetBufferCount(va_list args); |
| 95 | int dispatchSetBuffersGeometry(va_list args); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 96 | int dispatchSetBuffersDimensions(va_list args); |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 97 | int dispatchSetBuffersUserDimensions(va_list args); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 98 | int dispatchSetBuffersFormat(va_list args); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 99 | int dispatchSetScalingMode(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 100 | int dispatchSetBuffersTransform(va_list args); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 101 | int dispatchSetBuffersTimestamp(va_list args); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 102 | int dispatchSetCrop(va_list args); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame] | 103 | int dispatchSetPostTransformCrop(va_list args); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 104 | int dispatchSetUsage(va_list args); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 105 | int dispatchLock(va_list args); |
| 106 | int dispatchUnlockAndPost(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 107 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 108 | protected: |
Jamie Gennis | d8e812c | 2012-06-13 16:32:25 -0700 | [diff] [blame] | 109 | virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); |
| 110 | virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); |
| 111 | virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 112 | virtual int perform(int operation, va_list args); |
| 113 | virtual int query(int what, int* value) const; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 114 | virtual int setSwapInterval(int interval); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 115 | |
Jamie Gennis | d8e812c | 2012-06-13 16:32:25 -0700 | [diff] [blame] | 116 | virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer); |
| 117 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 118 | virtual int connect(int api); |
| 119 | virtual int disconnect(int api); |
| 120 | virtual int setBufferCount(int bufferCount); |
| 121 | virtual int setBuffersDimensions(int w, int h); |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 122 | virtual int setBuffersUserDimensions(int w, int h); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 123 | virtual int setBuffersFormat(int format); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 124 | virtual int setScalingMode(int mode); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 125 | virtual int setBuffersTransform(int transform); |
| 126 | virtual int setBuffersTimestamp(int64_t timestamp); |
| 127 | virtual int setCrop(Rect const* rect); |
| 128 | virtual int setUsage(uint32_t reqUsage); |
| 129 | virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); |
| 130 | virtual int unlockAndPost(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 131 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 132 | enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS }; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 133 | enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; |
| 134 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 135 | private: |
| 136 | void freeAllBuffers(); |
| 137 | int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; |
| 138 | |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 139 | struct BufferSlot { |
| 140 | sp<GraphicBuffer> buffer; |
| 141 | Region dirtyRegion; |
| 142 | }; |
| 143 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 144 | // mSurfaceTexture is the interface to the surface texture server. All |
| 145 | // operations on the surface texture client ultimately translate into |
| 146 | // interactions with the server using this interface. |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 147 | // TODO: rename to mBufferProducer |
| 148 | sp<IGraphicBufferProducer> mSurfaceTexture; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 149 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 150 | // mSlots stores the buffers that have been allocated for each buffer slot. |
| 151 | // It is initialized to null pointers, and gets filled in with the result of |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 152 | // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 153 | // slot that has not yet been used. The buffer allocated to a slot will also |
| 154 | // be replaced if the requested buffer usage or geometry differs from that |
| 155 | // of the buffer allocated to a slot. |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 156 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 157 | |
| 158 | // mReqWidth is the buffer width that will be requested at the next dequeue |
| 159 | // operation. It is initialized to 1. |
| 160 | uint32_t mReqWidth; |
| 161 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 162 | // mReqHeight is the buffer height that will be requested at the next |
| 163 | // dequeue operation. It is initialized to 1. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 164 | uint32_t mReqHeight; |
| 165 | |
| 166 | // mReqFormat is the buffer pixel format that will be requested at the next |
| 167 | // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. |
| 168 | uint32_t mReqFormat; |
| 169 | |
| 170 | // mReqUsage is the set of buffer usage flags that will be requested |
| 171 | // at the next deuque operation. It is initialized to 0. |
| 172 | uint32_t mReqUsage; |
| 173 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 174 | // mTimestamp is the timestamp that will be used for the next buffer queue |
| 175 | // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that |
| 176 | // a timestamp is auto-generated when queueBuffer is called. |
| 177 | int64_t mTimestamp; |
| 178 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 179 | // mCrop is the crop rectangle that will be used for the next buffer |
| 180 | // that gets queued. It is set by calling setCrop. |
| 181 | Rect mCrop; |
| 182 | |
| 183 | // mScalingMode is the scaling mode that will be used for the next |
| 184 | // buffers that get queued. It is set by calling setScalingMode. |
| 185 | int mScalingMode; |
| 186 | |
| 187 | // mTransform is the transform identifier that will be used for the next |
| 188 | // buffer that gets queued. It is set by calling setTransform. |
| 189 | uint32_t mTransform; |
| 190 | |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 191 | // mDefaultWidth is default width of the buffers, regardless of the |
| 192 | // native_window_set_buffers_dimensions call. |
| 193 | uint32_t mDefaultWidth; |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 194 | |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 195 | // mDefaultHeight is default height of the buffers, regardless of the |
| 196 | // native_window_set_buffers_dimensions call. |
| 197 | uint32_t mDefaultHeight; |
| 198 | |
| 199 | // mUserWidth, if non-zero, is an application-specified override |
| 200 | // of mDefaultWidth. This is lower priority than the width set by |
| 201 | // native_window_set_buffers_dimensions. |
| 202 | uint32_t mUserWidth; |
| 203 | |
| 204 | // mUserHeight, if non-zero, is an application-specified override |
| 205 | // of mDefaultHeight. This is lower priority than the height set |
| 206 | // by native_window_set_buffers_dimensions. |
| 207 | uint32_t mUserHeight; |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 208 | |
| 209 | // mTransformHint is the transform probably applied to buffers of this |
| 210 | // window. this is only a hint, actual transform may differ. |
| 211 | uint32_t mTransformHint; |
| 212 | |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 213 | // mConsumerRunningBehind whether the consumer is running more than |
| 214 | // one buffer behind the producer. |
| 215 | mutable bool mConsumerRunningBehind; |
| 216 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 217 | // mMutex is the mutex used to prevent concurrent access to the member |
Andy McFadden | 2adaf04 | 2012-12-18 09:49:45 -0800 | [diff] [blame^] | 218 | // variables of SurfaceTextureClient objects. It must be locked whenever the |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 219 | // member variables are accessed. |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 220 | mutable Mutex mMutex; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 221 | |
| 222 | // must be used from the lock/unlock thread |
| 223 | sp<GraphicBuffer> mLockedBuffer; |
| 224 | sp<GraphicBuffer> mPostedBuffer; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 225 | bool mConnectedToCpu; |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 226 | |
| 227 | // must be accessed from lock/unlock thread only |
| 228 | Region mDirtyRegion; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | }; // namespace android |
| 232 | |
| 233 | #endif // ANDROID_GUI_SURFACETEXTURECLIENT_H |