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