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> 22 #include <gui/BufferQueue.h> 23 24 #include <ui/ANativeObjectBase.h> 25 #include <ui/Region.h> 26 27 #include <utils/RefBase.h> 28 #include <utils/threads.h> 29 #include <utils/KeyedVector.h> 30 31 struct ANativeWindow_Buffer; 32 33 namespace android { 34 35 class Surface; 36 37 class SurfaceTextureClient 38 : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase> 39 { 40 public: 41 42 SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture); 43 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 50 sp<ISurfaceTexture> getISurfaceTexture() const; 51 52 protected: 53 SurfaceTextureClient(); 54 virtual ~SurfaceTextureClient(); 55 void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture); 56 57 private: 58 // can't be copied 59 SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs); 60 SurfaceTextureClient(const SurfaceTextureClient& rhs); 61 void init(); 62 63 // ANativeWindow hooks 64 static int hook_cancelBuffer(ANativeWindow* window, 65 ANativeWindowBuffer* buffer, int fenceFd); 66 static int hook_dequeueBuffer(ANativeWindow* window, 67 ANativeWindowBuffer** buffer, int* fenceFd); 68 static int hook_perform(ANativeWindow* window, int operation, ...); 69 static int hook_query(const ANativeWindow* window, int what, int* value); 70 static int hook_queueBuffer(ANativeWindow* window, 71 ANativeWindowBuffer* buffer, int fenceFd); 72 static int hook_setSwapInterval(ANativeWindow* window, int interval); 73 74 static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, 75 ANativeWindowBuffer* buffer); 76 static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, 77 ANativeWindowBuffer** buffer); 78 static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, 79 ANativeWindowBuffer* buffer); 80 static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, 81 ANativeWindowBuffer* buffer); 82 83 int dispatchConnect(va_list args); 84 int dispatchDisconnect(va_list args); 85 int dispatchSetBufferCount(va_list args); 86 int dispatchSetBuffersGeometry(va_list args); 87 int dispatchSetBuffersDimensions(va_list args); 88 int dispatchSetBuffersUserDimensions(va_list args); 89 int dispatchSetBuffersFormat(va_list args); 90 int dispatchSetScalingMode(va_list args); 91 int dispatchSetBuffersTransform(va_list args); 92 int dispatchSetBuffersTimestamp(va_list args); 93 int dispatchSetCrop(va_list args); 94 int dispatchSetPostTransformCrop(va_list args); 95 int dispatchSetUsage(va_list args); 96 int dispatchLock(va_list args); 97 int dispatchUnlockAndPost(va_list args); 98 99 protected: 100 virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); 101 virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); 102 virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); 103 virtual int perform(int operation, va_list args); 104 virtual int query(int what, int* value) const; 105 virtual int setSwapInterval(int interval); 106 107 virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer); 108 109 virtual int connect(int api); 110 virtual int disconnect(int api); 111 virtual int setBufferCount(int bufferCount); 112 virtual int setBuffersDimensions(int w, int h); 113 virtual int setBuffersUserDimensions(int w, int h); 114 virtual int setBuffersFormat(int format); 115 virtual int setScalingMode(int mode); 116 virtual int setBuffersTransform(int transform); 117 virtual int setBuffersTimestamp(int64_t timestamp); 118 virtual int setCrop(Rect const* rect); 119 virtual int setUsage(uint32_t reqUsage); 120 virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); 121 virtual int unlockAndPost(); 122 123 enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS }; 124 enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; 125 126 private: 127 void freeAllBuffers(); 128 int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; 129 130 struct BufferSlot { 131 sp<GraphicBuffer> buffer; 132 Region dirtyRegion; 133 }; 134 135 // mSurfaceTexture is the interface to the surface texture server. All 136 // operations on the surface texture client ultimately translate into 137 // interactions with the server using this interface. 138 sp<ISurfaceTexture> mSurfaceTexture; 139 140 // mSlots stores the buffers that have been allocated for each buffer slot. 141 // It is initialized to null pointers, and gets filled in with the result of 142 // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a 143 // slot that has not yet been used. The buffer allocated to a slot will also 144 // be replaced if the requested buffer usage or geometry differs from that 145 // of the buffer allocated to a slot. 146 BufferSlot mSlots[NUM_BUFFER_SLOTS]; 147 148 // mReqWidth is the buffer width that will be requested at the next dequeue 149 // operation. It is initialized to 1. 150 uint32_t mReqWidth; 151 152 // mReqHeight is the buffer height that will be requested at the next 153 // dequeue operation. It is initialized to 1. 154 uint32_t mReqHeight; 155 156 // mReqFormat is the buffer pixel format that will be requested at the next 157 // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. 158 uint32_t mReqFormat; 159 160 // mReqUsage is the set of buffer usage flags that will be requested 161 // at the next deuque operation. It is initialized to 0. 162 uint32_t mReqUsage; 163 164 // mTimestamp is the timestamp that will be used for the next buffer queue 165 // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that 166 // a timestamp is auto-generated when queueBuffer is called. 167 int64_t mTimestamp; 168 169 // mCrop is the crop rectangle that will be used for the next buffer 170 // that gets queued. It is set by calling setCrop. 171 Rect mCrop; 172 173 // mScalingMode is the scaling mode that will be used for the next 174 // buffers that get queued. It is set by calling setScalingMode. 175 int mScalingMode; 176 177 // mTransform is the transform identifier that will be used for the next 178 // buffer that gets queued. It is set by calling setTransform. 179 uint32_t mTransform; 180 181 // mDefaultWidth is default width of the buffers, regardless of the 182 // native_window_set_buffers_dimensions call. 183 uint32_t mDefaultWidth; 184 185 // mDefaultHeight is default height of the buffers, regardless of the 186 // native_window_set_buffers_dimensions call. 187 uint32_t mDefaultHeight; 188 189 // mUserWidth, if non-zero, is an application-specified override 190 // of mDefaultWidth. This is lower priority than the width set by 191 // native_window_set_buffers_dimensions. 192 uint32_t mUserWidth; 193 194 // mUserHeight, if non-zero, is an application-specified override 195 // of mDefaultHeight. This is lower priority than the height set 196 // by native_window_set_buffers_dimensions. 197 uint32_t mUserHeight; 198 199 // mTransformHint is the transform probably applied to buffers of this 200 // window. this is only a hint, actual transform may differ. 201 uint32_t mTransformHint; 202 203 // mConsumerRunningBehind whether the consumer is running more than 204 // one buffer behind the producer. 205 mutable bool mConsumerRunningBehind; 206 207 // mMutex is the mutex used to prevent concurrent access to the member 208 // variables of SurfaceTexture objects. It must be locked whenever the 209 // member variables are accessed. 210 mutable Mutex mMutex; 211 212 // must be used from the lock/unlock thread 213 sp<GraphicBuffer> mLockedBuffer; 214 sp<GraphicBuffer> mPostedBuffer; 215 bool mConnectedToCpu; 216 217 // must be accessed from lock/unlock thread only 218 Region mDirtyRegion; 219 }; 220 221 }; // namespace android 222 223 #endif // ANDROID_GUI_SURFACETEXTURECLIENT_H 224