Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2010 The Android Open Source Project |
michael@0 | 3 | * Copyright (C) 2013 Mozilla Foundation |
michael@0 | 4 | * |
michael@0 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 6 | * you may not use this file except in compliance with the License. |
michael@0 | 7 | * You may obtain a copy of the License at |
michael@0 | 8 | * |
michael@0 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 10 | * |
michael@0 | 11 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 14 | * See the License for the specific language governing permissions and |
michael@0 | 15 | * limitations under the License. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #ifndef NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H |
michael@0 | 19 | #define NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H |
michael@0 | 20 | |
michael@0 | 21 | #if ANDROID_VERSION == 17 |
michael@0 | 22 | #include <gui/ISurfaceTexture.h> |
michael@0 | 23 | #else |
michael@0 | 24 | #include <gui/IGraphicBufferProducer.h> |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | #include <ui/ANativeObjectBase.h> |
michael@0 | 28 | #include <ui/Region.h> |
michael@0 | 29 | |
michael@0 | 30 | #include <utils/RefBase.h> |
michael@0 | 31 | #include <utils/threads.h> |
michael@0 | 32 | #include <utils/KeyedVector.h> |
michael@0 | 33 | |
michael@0 | 34 | #include "GonkBufferQueue.h" |
michael@0 | 35 | |
michael@0 | 36 | struct ANativeWindow_Buffer; |
michael@0 | 37 | |
michael@0 | 38 | namespace android { |
michael@0 | 39 | |
michael@0 | 40 | /* |
michael@0 | 41 | * An implementation of ANativeWindow that feeds graphics buffers into a |
michael@0 | 42 | * BufferQueue. |
michael@0 | 43 | * |
michael@0 | 44 | * This is typically used by programs that want to render frames through |
michael@0 | 45 | * some means (maybe OpenGL, a software renderer, or a hardware decoder) |
michael@0 | 46 | * and have the frames they create forwarded to SurfaceFlinger for |
michael@0 | 47 | * compositing. For example, a video decoder could render a frame and call |
michael@0 | 48 | * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by |
michael@0 | 49 | * GonkNativeWindowClient. GonkNativeWindowClient then forwards the buffers through Binder IPC |
michael@0 | 50 | * to the BufferQueue's producer interface, providing the new frame to a |
michael@0 | 51 | * consumer such as GLConsumer. |
michael@0 | 52 | */ |
michael@0 | 53 | class GonkNativeWindowClient |
michael@0 | 54 | : public ANativeObjectBase<ANativeWindow, GonkNativeWindowClient, RefBase> |
michael@0 | 55 | { |
michael@0 | 56 | public: |
michael@0 | 57 | |
michael@0 | 58 | /* |
michael@0 | 59 | * creates a GonkNativeWindowClient from the given IGraphicBufferProducer (which concrete |
michael@0 | 60 | * implementation is a BufferQueue). |
michael@0 | 61 | * |
michael@0 | 62 | * GonkNativeWindowClient is mainly state-less while it's disconnected, it can be |
michael@0 | 63 | * viewed as a glorified IGraphicBufferProducer holder. It's therefore |
michael@0 | 64 | * safe to create other GonkNativeWindowClients from the same IGraphicBufferProducer. |
michael@0 | 65 | * |
michael@0 | 66 | * However, once a GonkNativeWindowClient is connected, it'll prevent other GonkNativeWindowClients |
michael@0 | 67 | * referring to the same IGraphicBufferProducer to become connected and |
michael@0 | 68 | * therefore prevent them to be used as actual producers of buffers. |
michael@0 | 69 | */ |
michael@0 | 70 | GonkNativeWindowClient(const sp<IGraphicBufferProducer>& bufferProducer); |
michael@0 | 71 | |
michael@0 | 72 | /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this |
michael@0 | 73 | * GonkNativeWindowClient was created with. Usually it's an error to use the |
michael@0 | 74 | * IGraphicBufferProducer while the GonkNativeWindowClient is connected. |
michael@0 | 75 | */ |
michael@0 | 76 | #if ANDROID_VERSION == 17 |
michael@0 | 77 | sp<IGraphicBufferProducer> getISurfaceTexture() const; |
michael@0 | 78 | #else |
michael@0 | 79 | sp<IGraphicBufferProducer> getIGraphicBufferProducer() const; |
michael@0 | 80 | #endif |
michael@0 | 81 | |
michael@0 | 82 | /* convenience function to check that the given surface is non NULL as |
michael@0 | 83 | * well as its IGraphicBufferProducer */ |
michael@0 | 84 | #if ANDROID_VERSION >= 18 |
michael@0 | 85 | static bool isValid(const sp<GonkNativeWindowClient>& surface) { |
michael@0 | 86 | return surface != NULL && surface->getIGraphicBufferProducer() != NULL; |
michael@0 | 87 | } |
michael@0 | 88 | #endif |
michael@0 | 89 | |
michael@0 | 90 | protected: |
michael@0 | 91 | virtual ~GonkNativeWindowClient(); |
michael@0 | 92 | |
michael@0 | 93 | private: |
michael@0 | 94 | // can't be copied |
michael@0 | 95 | GonkNativeWindowClient& operator = (const GonkNativeWindowClient& rhs); |
michael@0 | 96 | GonkNativeWindowClient(const GonkNativeWindowClient& rhs); |
michael@0 | 97 | |
michael@0 | 98 | // ANativeWindow hooks |
michael@0 | 99 | static int hook_cancelBuffer(ANativeWindow* window, |
michael@0 | 100 | ANativeWindowBuffer* buffer, int fenceFd); |
michael@0 | 101 | static int hook_dequeueBuffer(ANativeWindow* window, |
michael@0 | 102 | ANativeWindowBuffer** buffer, int* fenceFd); |
michael@0 | 103 | static int hook_perform(ANativeWindow* window, int operation, ...); |
michael@0 | 104 | static int hook_query(const ANativeWindow* window, int what, int* value); |
michael@0 | 105 | static int hook_queueBuffer(ANativeWindow* window, |
michael@0 | 106 | ANativeWindowBuffer* buffer, int fenceFd); |
michael@0 | 107 | static int hook_setSwapInterval(ANativeWindow* window, int interval); |
michael@0 | 108 | |
michael@0 | 109 | static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, |
michael@0 | 110 | ANativeWindowBuffer* buffer); |
michael@0 | 111 | static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, |
michael@0 | 112 | ANativeWindowBuffer** buffer); |
michael@0 | 113 | static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, |
michael@0 | 114 | ANativeWindowBuffer* buffer); |
michael@0 | 115 | static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, |
michael@0 | 116 | ANativeWindowBuffer* buffer); |
michael@0 | 117 | |
michael@0 | 118 | int dispatchConnect(va_list args); |
michael@0 | 119 | int dispatchDisconnect(va_list args); |
michael@0 | 120 | int dispatchSetBufferCount(va_list args); |
michael@0 | 121 | int dispatchSetBuffersGeometry(va_list args); |
michael@0 | 122 | int dispatchSetBuffersDimensions(va_list args); |
michael@0 | 123 | int dispatchSetBuffersUserDimensions(va_list args); |
michael@0 | 124 | int dispatchSetBuffersFormat(va_list args); |
michael@0 | 125 | int dispatchSetScalingMode(va_list args); |
michael@0 | 126 | int dispatchSetBuffersTransform(va_list args); |
michael@0 | 127 | int dispatchSetBuffersTimestamp(va_list args); |
michael@0 | 128 | int dispatchSetCrop(va_list args); |
michael@0 | 129 | int dispatchSetPostTransformCrop(va_list args); |
michael@0 | 130 | int dispatchSetUsage(va_list args); |
michael@0 | 131 | int dispatchLock(va_list args); |
michael@0 | 132 | int dispatchUnlockAndPost(va_list args); |
michael@0 | 133 | |
michael@0 | 134 | protected: |
michael@0 | 135 | virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); |
michael@0 | 136 | virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); |
michael@0 | 137 | virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); |
michael@0 | 138 | virtual int perform(int operation, va_list args); |
michael@0 | 139 | virtual int query(int what, int* value) const; |
michael@0 | 140 | virtual int setSwapInterval(int interval); |
michael@0 | 141 | |
michael@0 | 142 | virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer); |
michael@0 | 143 | |
michael@0 | 144 | virtual int connect(int api); |
michael@0 | 145 | virtual int disconnect(int api); |
michael@0 | 146 | virtual int setBufferCount(int bufferCount); |
michael@0 | 147 | virtual int setBuffersDimensions(int w, int h); |
michael@0 | 148 | virtual int setBuffersUserDimensions(int w, int h); |
michael@0 | 149 | virtual int setBuffersFormat(int format); |
michael@0 | 150 | virtual int setScalingMode(int mode); |
michael@0 | 151 | virtual int setBuffersTransform(int transform); |
michael@0 | 152 | virtual int setBuffersTimestamp(int64_t timestamp); |
michael@0 | 153 | virtual int setCrop(Rect const* rect); |
michael@0 | 154 | virtual int setUsage(uint32_t reqUsage); |
michael@0 | 155 | |
michael@0 | 156 | public: |
michael@0 | 157 | virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); |
michael@0 | 158 | virtual int unlockAndPost(); |
michael@0 | 159 | |
michael@0 | 160 | protected: |
michael@0 | 161 | enum { NUM_BUFFER_SLOTS = GonkBufferQueue::NUM_BUFFER_SLOTS }; |
michael@0 | 162 | enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; |
michael@0 | 163 | |
michael@0 | 164 | private: |
michael@0 | 165 | void freeAllBuffers(); |
michael@0 | 166 | int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; |
michael@0 | 167 | |
michael@0 | 168 | struct BufferSlot { |
michael@0 | 169 | sp<GraphicBuffer> buffer; |
michael@0 | 170 | Region dirtyRegion; |
michael@0 | 171 | }; |
michael@0 | 172 | |
michael@0 | 173 | // mSurfaceTexture is the interface to the surface texture server. All |
michael@0 | 174 | // operations on the surface texture client ultimately translate into |
michael@0 | 175 | // interactions with the server using this interface. |
michael@0 | 176 | sp<IGraphicBufferProducer> mBufferProducer; |
michael@0 | 177 | |
michael@0 | 178 | // mSlots stores the buffers that have been allocated for each buffer slot. |
michael@0 | 179 | // It is initialized to null pointers, and gets filled in with the result of |
michael@0 | 180 | // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a |
michael@0 | 181 | // slot that has not yet been used. The buffer allocated to a slot will also |
michael@0 | 182 | // be replaced if the requested buffer usage or geometry differs from that |
michael@0 | 183 | // of the buffer allocated to a slot. |
michael@0 | 184 | BufferSlot mSlots[NUM_BUFFER_SLOTS]; |
michael@0 | 185 | |
michael@0 | 186 | // mReqWidth is the buffer width that will be requested at the next dequeue |
michael@0 | 187 | // operation. It is initialized to 1. |
michael@0 | 188 | uint32_t mReqWidth; |
michael@0 | 189 | |
michael@0 | 190 | // mReqHeight is the buffer height that will be requested at the next |
michael@0 | 191 | // dequeue operation. It is initialized to 1. |
michael@0 | 192 | uint32_t mReqHeight; |
michael@0 | 193 | |
michael@0 | 194 | // mReqFormat is the buffer pixel format that will be requested at the next |
michael@0 | 195 | // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. |
michael@0 | 196 | uint32_t mReqFormat; |
michael@0 | 197 | |
michael@0 | 198 | // mReqUsage is the set of buffer usage flags that will be requested |
michael@0 | 199 | // at the next deuque operation. It is initialized to 0. |
michael@0 | 200 | uint32_t mReqUsage; |
michael@0 | 201 | |
michael@0 | 202 | // mTimestamp is the timestamp that will be used for the next buffer queue |
michael@0 | 203 | // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that |
michael@0 | 204 | // a timestamp is auto-generated when queueBuffer is called. |
michael@0 | 205 | int64_t mTimestamp; |
michael@0 | 206 | |
michael@0 | 207 | // mCrop is the crop rectangle that will be used for the next buffer |
michael@0 | 208 | // that gets queued. It is set by calling setCrop. |
michael@0 | 209 | Rect mCrop; |
michael@0 | 210 | |
michael@0 | 211 | // mScalingMode is the scaling mode that will be used for the next |
michael@0 | 212 | // buffers that get queued. It is set by calling setScalingMode. |
michael@0 | 213 | int mScalingMode; |
michael@0 | 214 | |
michael@0 | 215 | // mTransform is the transform identifier that will be used for the next |
michael@0 | 216 | // buffer that gets queued. It is set by calling setTransform. |
michael@0 | 217 | uint32_t mTransform; |
michael@0 | 218 | |
michael@0 | 219 | // mDefaultWidth is default width of the buffers, regardless of the |
michael@0 | 220 | // native_window_set_buffers_dimensions call. |
michael@0 | 221 | uint32_t mDefaultWidth; |
michael@0 | 222 | |
michael@0 | 223 | // mDefaultHeight is default height of the buffers, regardless of the |
michael@0 | 224 | // native_window_set_buffers_dimensions call. |
michael@0 | 225 | uint32_t mDefaultHeight; |
michael@0 | 226 | |
michael@0 | 227 | // mUserWidth, if non-zero, is an application-specified override |
michael@0 | 228 | // of mDefaultWidth. This is lower priority than the width set by |
michael@0 | 229 | // native_window_set_buffers_dimensions. |
michael@0 | 230 | uint32_t mUserWidth; |
michael@0 | 231 | |
michael@0 | 232 | // mUserHeight, if non-zero, is an application-specified override |
michael@0 | 233 | // of mDefaultHeight. This is lower priority than the height set |
michael@0 | 234 | // by native_window_set_buffers_dimensions. |
michael@0 | 235 | uint32_t mUserHeight; |
michael@0 | 236 | |
michael@0 | 237 | // mTransformHint is the transform probably applied to buffers of this |
michael@0 | 238 | // window. this is only a hint, actual transform may differ. |
michael@0 | 239 | uint32_t mTransformHint; |
michael@0 | 240 | |
michael@0 | 241 | // mConsumerRunningBehind whether the consumer is running more than |
michael@0 | 242 | // one buffer behind the producer. |
michael@0 | 243 | mutable bool mConsumerRunningBehind; |
michael@0 | 244 | |
michael@0 | 245 | // mMutex is the mutex used to prevent concurrent access to the member |
michael@0 | 246 | // variables of GonkNativeWindowClient objects. It must be locked whenever the |
michael@0 | 247 | // member variables are accessed. |
michael@0 | 248 | mutable Mutex mMutex; |
michael@0 | 249 | |
michael@0 | 250 | // must be used from the lock/unlock thread |
michael@0 | 251 | sp<GraphicBuffer> mLockedBuffer; |
michael@0 | 252 | sp<GraphicBuffer> mPostedBuffer; |
michael@0 | 253 | bool mConnectedToCpu; |
michael@0 | 254 | |
michael@0 | 255 | // must be accessed from lock/unlock thread only |
michael@0 | 256 | Region mDirtyRegion; |
michael@0 | 257 | }; |
michael@0 | 258 | |
michael@0 | 259 | }; // namespace android |
michael@0 | 260 | |
michael@0 | 261 | #endif // NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H |