1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/nativewindow/GonkNativeWindowICS.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,296 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 The Android Open Source Project 1.6 + * Copyright (C) 2012 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_GONKNATIVEWINDOW_ICS_H 1.22 +#define NATIVEWINDOW_GONKNATIVEWINDOW_ICS_H 1.23 + 1.24 +#include <stdint.h> 1.25 +#include <sys/types.h> 1.26 + 1.27 +#include <gui/ISurfaceTexture.h> 1.28 +#include <ui/egl/android_natives.h> 1.29 +#include <ui/GraphicBuffer.h> 1.30 +#include <ui/Rect.h> 1.31 +#include <utils/Errors.h> 1.32 +#include <utils/RefBase.h> 1.33 +#include <utils/String8.h> 1.34 +#include <utils/threads.h> 1.35 + 1.36 +#include "CameraCommon.h" 1.37 +#include "GrallocImages.h" 1.38 +#include "mozilla/layers/LayersSurfaces.h" 1.39 +#include "mozilla/layers/TextureClient.h" 1.40 + 1.41 +namespace android { 1.42 + 1.43 +// The user of GonkNativeWindow who wants to receive notification of 1.44 +// new frames should implement this interface. 1.45 +class GonkNativeWindowNewFrameCallback { 1.46 +public: 1.47 + virtual void OnNewFrame() = 0; 1.48 +}; 1.49 + 1.50 +class GonkNativeWindow : public BnSurfaceTexture 1.51 +{ 1.52 + friend class GonkNativeWindowClient; 1.53 + 1.54 + typedef mozilla::layers::TextureClient TextureClient; 1.55 + 1.56 +public: 1.57 + enum { MIN_UNDEQUEUED_BUFFERS = 2 }; 1.58 + enum { MIN_BUFFER_SLOTS = MIN_UNDEQUEUED_BUFFERS }; 1.59 + enum { NUM_BUFFER_SLOTS = 32 }; 1.60 + enum { NO_CONNECTED_API = 0 }; 1.61 + enum { NATIVE_WINDOW_SET_BUFFERS_SIZE = 0x10000000 }; 1.62 + 1.63 + GonkNativeWindow(); 1.64 + ~GonkNativeWindow(); // this class cannot be overloaded 1.65 + 1.66 + // Get next frame from the queue and mark it as RENDERING, caller 1.67 + // owns the returned buffer. 1.68 + mozilla::TemporaryRef<TextureClient> getCurrentBuffer(); 1.69 + 1.70 + // Return the buffer to the queue and mark it as FREE. After that 1.71 + // the buffer is useable again for the decoder. 1.72 + void returnBuffer(TextureClient* client); 1.73 + 1.74 + // setBufferCount updates the number of available buffer slots. After 1.75 + // calling this all buffer slots are owned by the GonkNativeWindow object 1.76 + // (i.e. they are not owned by the client). 1.77 + virtual status_t setBufferCount(int bufferCount); 1.78 + 1.79 + // requestBuffer requests a new buffer for the given index. 1.80 + virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf); 1.81 + 1.82 + // dequeueBuffer gets the next buffer slot index for the client to use. If a 1.83 + // buffer slot is available then that slot index is written to the location 1.84 + // pointed to by the buf argument and a status of OK is returned. If no 1.85 + // slot is available then a status of -EBUSY is returned and buf is 1.86 + // unmodified. 1.87 + virtual status_t dequeueBuffer(int *buf, uint32_t width, uint32_t height, 1.88 + uint32_t format, uint32_t usage); 1.89 + 1.90 + // queueBuffer returns a filled buffer to the GonkNativeWindow. In addition, 1.91 + // a timestamp must be provided for the buffer. The timestamp is in 1.92 + // nanoseconds, and must be monotonically increasing. Its other semantics 1.93 + // (zero point, etc) are client-dependent and should be documented by the 1.94 + // client. 1.95 + virtual status_t queueBuffer(int buf, int64_t timestamp, 1.96 + uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); 1.97 + virtual void cancelBuffer(int buf); 1.98 + virtual status_t setCrop(const Rect& reg); 1.99 + virtual status_t setTransform(uint32_t transform); 1.100 + virtual status_t setScalingMode(int mode); 1.101 + 1.102 + virtual int query(int what, int* value); 1.103 + 1.104 + // Qcom specific function 1.105 + virtual int performQcomOperation(int operation, int arg1, int arg2, int arg3) { 1.106 + return OK; 1.107 + } 1.108 + 1.109 + // GonkNativeWindow do not implement the function and always works in 1.110 + // synchronous mode. 1.111 + virtual status_t setSynchronousMode(bool enabled); 1.112 + 1.113 + // connect attempts to connect a client API to the GonkNativeWindow. 1.114 + // 1.115 + // This method will fail if the connect was previously called on the 1.116 + // GonkNativeWindow and no corresponding disconnect call was made. 1.117 + virtual status_t connect(int api, 1.118 + uint32_t* outWidth, uint32_t* outHeight, uint32_t* outTransform); 1.119 + 1.120 + // disconnect attempts to disconnect a client API from the GonkNativeWindow. 1.121 + // This method will fail if the the GonkNativeWindow is not currently 1.122 + // connected to the specified client API. 1.123 + virtual status_t disconnect(int api); 1.124 + 1.125 + void setNewFrameCallback(GonkNativeWindowNewFrameCallback* aCallback); 1.126 + 1.127 + // setDefaultBufferSize is used to set the size of buffers returned by 1.128 + // requestBuffers when a with and height of zero is requested. 1.129 + // A call to setDefaultBufferSize() may trigger requestBuffers() to 1.130 + // be called from the client. 1.131 + status_t setDefaultBufferSize(uint32_t width, uint32_t height); 1.132 + 1.133 + // abandon frees all the buffers and puts the GonkNativeWindow into the 1.134 + // 'abandoned' state. Once put in this state the GonkNativeWindow can never 1.135 + // leave it. When in the 'abandoned' state, all methods of the 1.136 + // ISurfaceTexture interface will fail with the NO_INIT error. 1.137 + // 1.138 + // Note that while calling this method causes all the buffers to be freed 1.139 + // from the perspective of the the GonkNativeWindow, if there are additional 1.140 + // references on the buffers (e.g. if a buffer is referenced by a client) 1.141 + // then those buffer will remain allocated. 1.142 + void abandon(); 1.143 + 1.144 + mozilla::TemporaryRef<TextureClient> getTextureClientFromBuffer(ANativeWindowBuffer* buffer); 1.145 + 1.146 + static void RecycleCallback(TextureClient* client, void* closure); 1.147 + 1.148 +protected: 1.149 + 1.150 + // freeAllBuffersLocked frees the resources (both GraphicBuffer and 1.151 + // EGLImage) for all slots by removing them from the slots and appending 1.152 + // then to the freeList. This must be called with mMutex locked. 1.153 + void freeAllBuffersLocked(); 1.154 + 1.155 + // clearRenderingStateBuffersLocked clear the resources in RENDERING state; 1.156 + // But do not destroy the gralloc buffer. It is still in the video stream 1.157 + // awaiting rendering. 1.158 + // this must be called with mMutex locked. 1.159 + void clearRenderingStateBuffersLocked(); 1.160 + 1.161 +private: 1.162 + void init(); 1.163 + 1.164 + int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; 1.165 + 1.166 + int getSlotFromTextureClientLocked(TextureClient* client) const; 1.167 + 1.168 + enum { INVALID_BUFFER_SLOT = -1 }; 1.169 + 1.170 + struct BufferSlot { 1.171 + 1.172 + BufferSlot() 1.173 + : mBufferState(BufferSlot::FREE), 1.174 + mTimestamp(0), 1.175 + mFrameNumber(0){ 1.176 + } 1.177 + 1.178 + // mGraphicBuffer points to the buffer allocated for this slot or is NULL 1.179 + // if no buffer has been allocated. 1.180 + sp<GraphicBuffer> mGraphicBuffer; 1.181 + 1.182 + // mTextureClient is a thin abstraction over remotely allocated GraphicBuffer. 1.183 + mozilla::RefPtr<TextureClient> mTextureClient; 1.184 + 1.185 + // BufferState represents the different states in which a buffer slot 1.186 + // can be. 1.187 + enum BufferState { 1.188 + // FREE indicates that the buffer is not currently being used and 1.189 + // will not be used in the future until it gets dequeued and 1.190 + // subsequently queued by the client. 1.191 + FREE = 0, 1.192 + 1.193 + // DEQUEUED indicates that the buffer has been dequeued by the 1.194 + // client, but has not yet been queued or canceled. The buffer is 1.195 + // considered 'owned' by the client, and the server should not use 1.196 + // it for anything. 1.197 + // 1.198 + // Note that when in synchronous-mode (mSynchronousMode == true), 1.199 + // the buffer that's currently attached to the texture may be 1.200 + // dequeued by the client. That means that the current buffer can 1.201 + // be in either the DEQUEUED or QUEUED state. In asynchronous mode, 1.202 + // however, the current buffer is always in the QUEUED state. 1.203 + DEQUEUED = 1, 1.204 + 1.205 + // QUEUED indicates that the buffer has been queued by the client, 1.206 + // and has not since been made available for the client to dequeue. 1.207 + // Attaching the buffer to the texture does NOT transition the 1.208 + // buffer away from the QUEUED state. However, in Synchronous mode 1.209 + // the current buffer may be dequeued by the client under some 1.210 + // circumstances. See the note about the current buffer in the 1.211 + // documentation for DEQUEUED. 1.212 + QUEUED = 2, 1.213 + 1.214 + // RENDERING indicates that the buffer has been sent to 1.215 + // the compositor, and has not yet available for the 1.216 + // client to dequeue. When the compositor has finished its 1.217 + // job, the buffer will be returned to FREE state. 1.218 + RENDERING = 3, 1.219 + }; 1.220 + 1.221 + // mBufferState is the current state of this buffer slot. 1.222 + BufferState mBufferState; 1.223 + 1.224 + // mRequestBufferCalled is used for validating that the client did 1.225 + // call requestBuffer() when told to do so. Technically this is not 1.226 + // needed but useful for debugging and catching client bugs. 1.227 + bool mRequestBufferCalled; 1.228 + 1.229 + // mTimestamp is the current timestamp for this buffer slot. This gets 1.230 + // to set by queueBuffer each time this slot is queued. 1.231 + int64_t mTimestamp; 1.232 + 1.233 + // mFrameNumber is the number of the queued frame for this slot. 1.234 + uint64_t mFrameNumber; 1.235 + }; 1.236 + 1.237 + // mSlots is the array of buffer slots that must be mirrored on the client 1.238 + // side. This allows buffer ownership to be transferred between the client 1.239 + // and server without sending a GraphicBuffer over binder. The entire array 1.240 + // is initialized to NULL at construction time, and buffers are allocated 1.241 + // for a slot when requestBuffer is called with that slot's index. 1.242 + BufferSlot mSlots[NUM_BUFFER_SLOTS]; 1.243 + 1.244 + // mDequeueCondition condition used for dequeueBuffer in synchronous mode 1.245 + mutable Condition mDequeueCondition; 1.246 + 1.247 + // mAbandoned indicates that the GonkNativeWindow will no longer be used to 1.248 + // consume buffers pushed to it. 1.249 + // It is initialized to false, and set to true in the abandon method. A 1.250 + // GonkNativeWindow that has been abandoned will return the NO_INIT error 1.251 + // from all control methods capable of returning an error. 1.252 + bool mAbandoned; 1.253 + 1.254 + // mTimestamp is the timestamp that will be used for the next buffer queue 1.255 + // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that 1.256 + // a timestamp is auto-generated when queueBuffer is called. 1.257 + int64_t mTimestamp; 1.258 + 1.259 + // mDefaultWidth holds the default width of allocated buffers. It is used 1.260 + // in requestBuffers() if a width and height of zero is specified. 1.261 + uint32_t mDefaultWidth; 1.262 + 1.263 + // mDefaultHeight holds the default height of allocated buffers. It is used 1.264 + // in requestBuffers() if a width and height of zero is specified. 1.265 + uint32_t mDefaultHeight; 1.266 + 1.267 + // mPixelFormat holds the pixel format of allocated buffers. It is used 1.268 + // in requestBuffers() if a format of zero is specified. 1.269 + uint32_t mPixelFormat; 1.270 + 1.271 + // mBufferCount is the number of buffer slots that the client and server 1.272 + // must maintain. It defaults to MIN_BUFFER_SLOTS + 1 and can be changed 1.273 + // by calling setBufferCount or setBufferCountServer 1.274 + int mBufferCount; 1.275 + 1.276 + // mConnectedApi indicates the API that is currently connected to this 1.277 + // GonkNativeWindow. It defaults to NO_CONNECTED_API (= 0), and gets updated 1.278 + // by the connect and disconnect methods. 1.279 + int mConnectedApi; 1.280 + 1.281 + // mQueue is a FIFO of queued buffers used in synchronous mode 1.282 + // GonkNativeWindow always works in synchronous mode 1.283 + typedef Vector<int> Fifo; 1.284 + Fifo mQueue; 1.285 + 1.286 + // mMutex is the mutex used to prevent concurrent access to the member 1.287 + // variables of GonkNativeWindow objects. It must be locked whenever the 1.288 + // member variables are accessed. 1.289 + mutable Mutex mMutex; 1.290 + 1.291 + // mFrameCounter is the free running counter, incremented for every buffer queued 1.292 + uint64_t mFrameCounter; 1.293 + 1.294 + GonkNativeWindowNewFrameCallback* mNewFrameCallback; 1.295 +}; 1.296 + 1.297 +}; // namespace android 1.298 + 1.299 +#endif // NATIVEWINDOW_GONKNATIVEWINDOW_ICS_H