1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/nativewindow/GonkNativeWindowClientJB.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,261 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 The Android Open Source Project 1.6 + * Copyright (C) 2013 Mozilla Foundation 1.7 + * 1.8 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.9 + * you may not use this file except in compliance with the License. 1.10 + * You may obtain a copy of the License at 1.11 + * 1.12 + * http://www.apache.org/licenses/LICENSE-2.0 1.13 + * 1.14 + * Unless required by applicable law or agreed to in writing, software 1.15 + * distributed under the License is distributed on an "AS IS" BASIS, 1.16 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.17 + * See the License for the specific language governing permissions and 1.18 + * limitations under the License. 1.19 + */ 1.20 + 1.21 +#ifndef NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H 1.22 +#define NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H 1.23 + 1.24 +#if ANDROID_VERSION == 17 1.25 +#include <gui/ISurfaceTexture.h> 1.26 +#else 1.27 +#include <gui/IGraphicBufferProducer.h> 1.28 +#endif 1.29 + 1.30 +#include <ui/ANativeObjectBase.h> 1.31 +#include <ui/Region.h> 1.32 + 1.33 +#include <utils/RefBase.h> 1.34 +#include <utils/threads.h> 1.35 +#include <utils/KeyedVector.h> 1.36 + 1.37 +#include "GonkBufferQueue.h" 1.38 + 1.39 +struct ANativeWindow_Buffer; 1.40 + 1.41 +namespace android { 1.42 + 1.43 +/* 1.44 + * An implementation of ANativeWindow that feeds graphics buffers into a 1.45 + * BufferQueue. 1.46 + * 1.47 + * This is typically used by programs that want to render frames through 1.48 + * some means (maybe OpenGL, a software renderer, or a hardware decoder) 1.49 + * and have the frames they create forwarded to SurfaceFlinger for 1.50 + * compositing. For example, a video decoder could render a frame and call 1.51 + * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by 1.52 + * GonkNativeWindowClient. GonkNativeWindowClient then forwards the buffers through Binder IPC 1.53 + * to the BufferQueue's producer interface, providing the new frame to a 1.54 + * consumer such as GLConsumer. 1.55 + */ 1.56 +class GonkNativeWindowClient 1.57 + : public ANativeObjectBase<ANativeWindow, GonkNativeWindowClient, RefBase> 1.58 +{ 1.59 +public: 1.60 + 1.61 + /* 1.62 + * creates a GonkNativeWindowClient from the given IGraphicBufferProducer (which concrete 1.63 + * implementation is a BufferQueue). 1.64 + * 1.65 + * GonkNativeWindowClient is mainly state-less while it's disconnected, it can be 1.66 + * viewed as a glorified IGraphicBufferProducer holder. It's therefore 1.67 + * safe to create other GonkNativeWindowClients from the same IGraphicBufferProducer. 1.68 + * 1.69 + * However, once a GonkNativeWindowClient is connected, it'll prevent other GonkNativeWindowClients 1.70 + * referring to the same IGraphicBufferProducer to become connected and 1.71 + * therefore prevent them to be used as actual producers of buffers. 1.72 + */ 1.73 + GonkNativeWindowClient(const sp<IGraphicBufferProducer>& bufferProducer); 1.74 + 1.75 + /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this 1.76 + * GonkNativeWindowClient was created with. Usually it's an error to use the 1.77 + * IGraphicBufferProducer while the GonkNativeWindowClient is connected. 1.78 + */ 1.79 +#if ANDROID_VERSION == 17 1.80 + sp<IGraphicBufferProducer> getISurfaceTexture() const; 1.81 +#else 1.82 + sp<IGraphicBufferProducer> getIGraphicBufferProducer() const; 1.83 +#endif 1.84 + 1.85 + /* convenience function to check that the given surface is non NULL as 1.86 + * well as its IGraphicBufferProducer */ 1.87 +#if ANDROID_VERSION >= 18 1.88 + static bool isValid(const sp<GonkNativeWindowClient>& surface) { 1.89 + return surface != NULL && surface->getIGraphicBufferProducer() != NULL; 1.90 + } 1.91 +#endif 1.92 + 1.93 +protected: 1.94 + virtual ~GonkNativeWindowClient(); 1.95 + 1.96 +private: 1.97 + // can't be copied 1.98 + GonkNativeWindowClient& operator = (const GonkNativeWindowClient& rhs); 1.99 + GonkNativeWindowClient(const GonkNativeWindowClient& rhs); 1.100 + 1.101 + // ANativeWindow hooks 1.102 + static int hook_cancelBuffer(ANativeWindow* window, 1.103 + ANativeWindowBuffer* buffer, int fenceFd); 1.104 + static int hook_dequeueBuffer(ANativeWindow* window, 1.105 + ANativeWindowBuffer** buffer, int* fenceFd); 1.106 + static int hook_perform(ANativeWindow* window, int operation, ...); 1.107 + static int hook_query(const ANativeWindow* window, int what, int* value); 1.108 + static int hook_queueBuffer(ANativeWindow* window, 1.109 + ANativeWindowBuffer* buffer, int fenceFd); 1.110 + static int hook_setSwapInterval(ANativeWindow* window, int interval); 1.111 + 1.112 + static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, 1.113 + ANativeWindowBuffer* buffer); 1.114 + static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, 1.115 + ANativeWindowBuffer** buffer); 1.116 + static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, 1.117 + ANativeWindowBuffer* buffer); 1.118 + static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, 1.119 + ANativeWindowBuffer* buffer); 1.120 + 1.121 + int dispatchConnect(va_list args); 1.122 + int dispatchDisconnect(va_list args); 1.123 + int dispatchSetBufferCount(va_list args); 1.124 + int dispatchSetBuffersGeometry(va_list args); 1.125 + int dispatchSetBuffersDimensions(va_list args); 1.126 + int dispatchSetBuffersUserDimensions(va_list args); 1.127 + int dispatchSetBuffersFormat(va_list args); 1.128 + int dispatchSetScalingMode(va_list args); 1.129 + int dispatchSetBuffersTransform(va_list args); 1.130 + int dispatchSetBuffersTimestamp(va_list args); 1.131 + int dispatchSetCrop(va_list args); 1.132 + int dispatchSetPostTransformCrop(va_list args); 1.133 + int dispatchSetUsage(va_list args); 1.134 + int dispatchLock(va_list args); 1.135 + int dispatchUnlockAndPost(va_list args); 1.136 + 1.137 +protected: 1.138 + virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); 1.139 + virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); 1.140 + virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); 1.141 + virtual int perform(int operation, va_list args); 1.142 + virtual int query(int what, int* value) const; 1.143 + virtual int setSwapInterval(int interval); 1.144 + 1.145 + virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer); 1.146 + 1.147 + virtual int connect(int api); 1.148 + virtual int disconnect(int api); 1.149 + virtual int setBufferCount(int bufferCount); 1.150 + virtual int setBuffersDimensions(int w, int h); 1.151 + virtual int setBuffersUserDimensions(int w, int h); 1.152 + virtual int setBuffersFormat(int format); 1.153 + virtual int setScalingMode(int mode); 1.154 + virtual int setBuffersTransform(int transform); 1.155 + virtual int setBuffersTimestamp(int64_t timestamp); 1.156 + virtual int setCrop(Rect const* rect); 1.157 + virtual int setUsage(uint32_t reqUsage); 1.158 + 1.159 +public: 1.160 + virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); 1.161 + virtual int unlockAndPost(); 1.162 + 1.163 +protected: 1.164 + enum { NUM_BUFFER_SLOTS = GonkBufferQueue::NUM_BUFFER_SLOTS }; 1.165 + enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; 1.166 + 1.167 +private: 1.168 + void freeAllBuffers(); 1.169 + int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; 1.170 + 1.171 + struct BufferSlot { 1.172 + sp<GraphicBuffer> buffer; 1.173 + Region dirtyRegion; 1.174 + }; 1.175 + 1.176 + // mSurfaceTexture is the interface to the surface texture server. All 1.177 + // operations on the surface texture client ultimately translate into 1.178 + // interactions with the server using this interface. 1.179 + sp<IGraphicBufferProducer> mBufferProducer; 1.180 + 1.181 + // mSlots stores the buffers that have been allocated for each buffer slot. 1.182 + // It is initialized to null pointers, and gets filled in with the result of 1.183 + // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a 1.184 + // slot that has not yet been used. The buffer allocated to a slot will also 1.185 + // be replaced if the requested buffer usage or geometry differs from that 1.186 + // of the buffer allocated to a slot. 1.187 + BufferSlot mSlots[NUM_BUFFER_SLOTS]; 1.188 + 1.189 + // mReqWidth is the buffer width that will be requested at the next dequeue 1.190 + // operation. It is initialized to 1. 1.191 + uint32_t mReqWidth; 1.192 + 1.193 + // mReqHeight is the buffer height that will be requested at the next 1.194 + // dequeue operation. It is initialized to 1. 1.195 + uint32_t mReqHeight; 1.196 + 1.197 + // mReqFormat is the buffer pixel format that will be requested at the next 1.198 + // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. 1.199 + uint32_t mReqFormat; 1.200 + 1.201 + // mReqUsage is the set of buffer usage flags that will be requested 1.202 + // at the next deuque operation. It is initialized to 0. 1.203 + uint32_t mReqUsage; 1.204 + 1.205 + // mTimestamp is the timestamp that will be used for the next buffer queue 1.206 + // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that 1.207 + // a timestamp is auto-generated when queueBuffer is called. 1.208 + int64_t mTimestamp; 1.209 + 1.210 + // mCrop is the crop rectangle that will be used for the next buffer 1.211 + // that gets queued. It is set by calling setCrop. 1.212 + Rect mCrop; 1.213 + 1.214 + // mScalingMode is the scaling mode that will be used for the next 1.215 + // buffers that get queued. It is set by calling setScalingMode. 1.216 + int mScalingMode; 1.217 + 1.218 + // mTransform is the transform identifier that will be used for the next 1.219 + // buffer that gets queued. It is set by calling setTransform. 1.220 + uint32_t mTransform; 1.221 + 1.222 + // mDefaultWidth is default width of the buffers, regardless of the 1.223 + // native_window_set_buffers_dimensions call. 1.224 + uint32_t mDefaultWidth; 1.225 + 1.226 + // mDefaultHeight is default height of the buffers, regardless of the 1.227 + // native_window_set_buffers_dimensions call. 1.228 + uint32_t mDefaultHeight; 1.229 + 1.230 + // mUserWidth, if non-zero, is an application-specified override 1.231 + // of mDefaultWidth. This is lower priority than the width set by 1.232 + // native_window_set_buffers_dimensions. 1.233 + uint32_t mUserWidth; 1.234 + 1.235 + // mUserHeight, if non-zero, is an application-specified override 1.236 + // of mDefaultHeight. This is lower priority than the height set 1.237 + // by native_window_set_buffers_dimensions. 1.238 + uint32_t mUserHeight; 1.239 + 1.240 + // mTransformHint is the transform probably applied to buffers of this 1.241 + // window. this is only a hint, actual transform may differ. 1.242 + uint32_t mTransformHint; 1.243 + 1.244 + // mConsumerRunningBehind whether the consumer is running more than 1.245 + // one buffer behind the producer. 1.246 + mutable bool mConsumerRunningBehind; 1.247 + 1.248 + // mMutex is the mutex used to prevent concurrent access to the member 1.249 + // variables of GonkNativeWindowClient objects. It must be locked whenever the 1.250 + // member variables are accessed. 1.251 + mutable Mutex mMutex; 1.252 + 1.253 + // must be used from the lock/unlock thread 1.254 + sp<GraphicBuffer> mLockedBuffer; 1.255 + sp<GraphicBuffer> mPostedBuffer; 1.256 + bool mConnectedToCpu; 1.257 + 1.258 + // must be accessed from lock/unlock thread only 1.259 + Region mDirtyRegion; 1.260 +}; 1.261 + 1.262 +}; // namespace android 1.263 + 1.264 +#endif // NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H