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 | |
| 20 | #include <gui/ISurfaceTexture.h> |
| 21 | #include <gui/SurfaceTexture.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 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 37 | class SurfaceTextureClient |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 38 | : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 39 | { |
| 40 | public: |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 41 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 42 | SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture); |
| 43 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 44 | // 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 Gennis | bae774e | 2011-03-14 15:08:53 -0700 | [diff] [blame] | 50 | sp<ISurfaceTexture> getISurfaceTexture() const; |
| 51 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 52 | protected: |
| 53 | SurfaceTextureClient(); |
Mathias Agopian | a36bcd5 | 2011-11-17 18:46:09 -0800 | [diff] [blame] | 54 | virtual ~SurfaceTextureClient(); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 55 | void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 56 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 57 | private: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 58 | // can't be copied |
| 59 | SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs); |
| 60 | SurfaceTextureClient(const SurfaceTextureClient& rhs); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 61 | void init(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 62 | |
| 63 | // ANativeWindow hooks |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 64 | 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 71 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 72 | int dispatchConnect(va_list args); |
| 73 | int dispatchDisconnect(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 74 | int dispatchSetBufferCount(va_list args); |
| 75 | int dispatchSetBuffersGeometry(va_list args); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 76 | int dispatchSetBuffersDimensions(va_list args); |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 77 | int dispatchSetBuffersUserDimensions(va_list args); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 78 | int dispatchSetBuffersFormat(va_list args); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 79 | int dispatchSetScalingMode(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 80 | int dispatchSetBuffersTransform(va_list args); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 81 | int dispatchSetBuffersTimestamp(va_list args); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 82 | int dispatchSetCrop(va_list args); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame^] | 83 | int dispatchSetPostTransformCrop(va_list args); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 84 | int dispatchSetUsage(va_list args); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 85 | int dispatchLock(va_list args); |
| 86 | int dispatchUnlockAndPost(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 87 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 88 | protected: |
| 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 Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 96 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 97 | 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. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 101 | virtual int setBuffersUserDimensions(int w, int h); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 102 | virtual int setBuffersFormat(int format); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 103 | virtual int setScalingMode(int mode); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 104 | virtual int setBuffersTransform(int transform); |
| 105 | virtual int setBuffersTimestamp(int64_t timestamp); |
| 106 | virtual int setCrop(Rect const* rect); |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame^] | 107 | virtual int setPostTransformCrop(Rect const* rect); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 108 | virtual int setUsage(uint32_t reqUsage); |
| 109 | virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); |
| 110 | virtual int unlockAndPost(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 111 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 112 | enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS }; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 113 | enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; |
| 114 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 115 | private: |
| 116 | void freeAllBuffers(); |
| 117 | int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; |
| 118 | |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 119 | struct BufferSlot { |
| 120 | sp<GraphicBuffer> buffer; |
| 121 | Region dirtyRegion; |
| 122 | }; |
| 123 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 124 | // mSurfaceTexture is the interface to the surface texture server. All |
| 125 | // operations on the surface texture client ultimately translate into |
| 126 | // interactions with the server using this interface. |
| 127 | sp<ISurfaceTexture> mSurfaceTexture; |
| 128 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 129 | // mSlots stores the buffers that have been allocated for each buffer slot. |
| 130 | // It is initialized to null pointers, and gets filled in with the result of |
| 131 | // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a |
| 132 | // slot that has not yet been used. The buffer allocated to a slot will also |
| 133 | // be replaced if the requested buffer usage or geometry differs from that |
| 134 | // of the buffer allocated to a slot. |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 135 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 136 | |
| 137 | // mReqWidth is the buffer width that will be requested at the next dequeue |
| 138 | // operation. It is initialized to 1. |
| 139 | uint32_t mReqWidth; |
| 140 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 141 | // mReqHeight is the buffer height that will be requested at the next |
| 142 | // dequeue operation. It is initialized to 1. |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 143 | uint32_t mReqHeight; |
| 144 | |
| 145 | // mReqFormat is the buffer pixel format that will be requested at the next |
| 146 | // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. |
| 147 | uint32_t mReqFormat; |
| 148 | |
| 149 | // mReqUsage is the set of buffer usage flags that will be requested |
| 150 | // at the next deuque operation. It is initialized to 0. |
| 151 | uint32_t mReqUsage; |
| 152 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 153 | // mTimestamp is the timestamp that will be used for the next buffer queue |
| 154 | // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that |
| 155 | // a timestamp is auto-generated when queueBuffer is called. |
| 156 | int64_t mTimestamp; |
| 157 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 158 | // mCrop is the crop rectangle that will be used for the next buffer |
| 159 | // that gets queued. It is set by calling setCrop. |
| 160 | Rect mCrop; |
| 161 | |
Jamie Gennis | d72f233 | 2012-05-07 13:50:11 -0700 | [diff] [blame^] | 162 | // mCropNeedsTransform indicates whether mCrop is in post-transform |
| 163 | // coordinates and must be transformed using the inverse of mTransform |
| 164 | // before being queued with a buffer. Otherwise the crop is passed |
| 165 | // untransformed. It is initialized to false, is set to true by |
| 166 | // setPostTransformCrop, and set to false by setCrop. |
| 167 | bool mCropNeedsTransform; |
| 168 | |
Mathias Agopian | 851ef8f | 2012-03-29 17:10:08 -0700 | [diff] [blame] | 169 | // mScalingMode is the scaling mode that will be used for the next |
| 170 | // buffers that get queued. It is set by calling setScalingMode. |
| 171 | int mScalingMode; |
| 172 | |
| 173 | // mTransform is the transform identifier that will be used for the next |
| 174 | // buffer that gets queued. It is set by calling setTransform. |
| 175 | uint32_t mTransform; |
| 176 | |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 177 | // mDefaultWidth is default width of the buffers, regardless of the |
| 178 | // native_window_set_buffers_dimensions call. |
| 179 | uint32_t mDefaultWidth; |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 180 | |
Michael I. Gold | 55a7014 | 2012-04-09 19:46:29 -0700 | [diff] [blame] | 181 | // mDefaultHeight is default height of the buffers, regardless of the |
| 182 | // native_window_set_buffers_dimensions call. |
| 183 | uint32_t mDefaultHeight; |
| 184 | |
| 185 | // mUserWidth, if non-zero, is an application-specified override |
| 186 | // of mDefaultWidth. This is lower priority than the width set by |
| 187 | // native_window_set_buffers_dimensions. |
| 188 | uint32_t mUserWidth; |
| 189 | |
| 190 | // mUserHeight, if non-zero, is an application-specified override |
| 191 | // of mDefaultHeight. This is lower priority than the height set |
| 192 | // by native_window_set_buffers_dimensions. |
| 193 | uint32_t mUserHeight; |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 194 | |
| 195 | // mTransformHint is the transform probably applied to buffers of this |
| 196 | // window. this is only a hint, actual transform may differ. |
| 197 | uint32_t mTransformHint; |
| 198 | |
Mathias Agopian | 2488b20 | 2012-04-20 17:19:28 -0700 | [diff] [blame] | 199 | // mConsumerRunningBehind whether the consumer is running more than |
| 200 | // one buffer behind the producer. |
| 201 | mutable bool mConsumerRunningBehind; |
| 202 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 203 | // mMutex is the mutex used to prevent concurrent access to the member |
| 204 | // variables of SurfaceTexture objects. It must be locked whenever the |
| 205 | // member variables are accessed. |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 206 | mutable Mutex mMutex; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 207 | |
| 208 | // must be used from the lock/unlock thread |
| 209 | sp<GraphicBuffer> mLockedBuffer; |
| 210 | sp<GraphicBuffer> mPostedBuffer; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 211 | bool mConnectedToCpu; |
Mathias Agopian | ac6035a | 2012-04-12 16:32:37 -0700 | [diff] [blame] | 212 | |
| 213 | // must be accessed from lock/unlock thread only |
| 214 | Region mDirtyRegion; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | }; // namespace android |
| 218 | |
| 219 | #endif // ANDROID_GUI_SURFACETEXTURECLIENT_H |