michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * Copyright (C) 2013 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_KK_H michael@0: #define NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_KK_H michael@0: michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "GonkBufferQueue.h" michael@0: michael@0: struct ANativeWindow_Buffer; michael@0: michael@0: namespace android { michael@0: michael@0: /* michael@0: * An implementation of ANativeWindow that feeds graphics buffers into a michael@0: * BufferQueue. michael@0: * michael@0: * This is typically used by programs that want to render frames through michael@0: * some means (maybe OpenGL, a software renderer, or a hardware decoder) michael@0: * and have the frames they create forwarded to SurfaceFlinger for michael@0: * compositing. For example, a video decoder could render a frame and call michael@0: * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by michael@0: * GonkNativeWindowClient. GonkNativeWindowClient then forwards the buffers through Binder IPC michael@0: * to the BufferQueue's producer interface, providing the new frame to a michael@0: * consumer such as GLConsumer. michael@0: */ michael@0: class GonkNativeWindowClient michael@0: : public ANativeObjectBase michael@0: { michael@0: public: michael@0: michael@0: /* michael@0: * creates a GonkNativeWindowClient from the given IGraphicBufferProducer (which concrete michael@0: * implementation is a BufferQueue). michael@0: * michael@0: * GonkNativeWindowClient is mainly state-less while it's disconnected, it can be michael@0: * viewed as a glorified IGraphicBufferProducer holder. It's therefore michael@0: * safe to create other GonkNativeWindowClients from the same IGraphicBufferProducer. michael@0: * michael@0: * However, once a GonkNativeWindowClient is connected, it'll prevent other GonkNativeWindowClients michael@0: * referring to the same IGraphicBufferProducer to become connected and michael@0: * therefore prevent them to be used as actual producers of buffers. michael@0: * michael@0: * the controlledByApp flag indicates that this Surface (producer) is michael@0: * controlled by the application. This flag is used at connect time. michael@0: */ michael@0: GonkNativeWindowClient(const sp& bufferProducer, bool controlledByApp = false); michael@0: michael@0: /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this michael@0: * GonkNativeWindowClient was created with. Usually it's an error to use the michael@0: * IGraphicBufferProducer while the GonkNativeWindowClient is connected. michael@0: */ michael@0: sp getIGraphicBufferProducer() const; michael@0: michael@0: /* convenience function to check that the given surface is non NULL as michael@0: * well as its IGraphicBufferProducer */ michael@0: static bool isValid(const sp& surface) { michael@0: return surface != NULL && surface->getIGraphicBufferProducer() != NULL; michael@0: } michael@0: michael@0: protected: michael@0: virtual ~GonkNativeWindowClient(); michael@0: michael@0: private: michael@0: // can't be copied michael@0: GonkNativeWindowClient& operator = (const GonkNativeWindowClient& rhs); michael@0: GonkNativeWindowClient(const GonkNativeWindowClient& rhs); michael@0: michael@0: // ANativeWindow hooks michael@0: static int hook_cancelBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer, int fenceFd); michael@0: static int hook_dequeueBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer** buffer, int* fenceFd); michael@0: static int hook_perform(ANativeWindow* window, int operation, ...); michael@0: static int hook_query(const ANativeWindow* window, int what, int* value); michael@0: static int hook_queueBuffer(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer, int fenceFd); michael@0: static int hook_setSwapInterval(ANativeWindow* window, int interval); michael@0: michael@0: static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer); michael@0: static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window, michael@0: ANativeWindowBuffer** buffer); michael@0: static int hook_lockBuffer_DEPRECATED(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer); michael@0: static int hook_queueBuffer_DEPRECATED(ANativeWindow* window, michael@0: ANativeWindowBuffer* buffer); michael@0: michael@0: int dispatchConnect(va_list args); michael@0: int dispatchDisconnect(va_list args); michael@0: int dispatchSetBufferCount(va_list args); michael@0: int dispatchSetBuffersGeometry(va_list args); michael@0: int dispatchSetBuffersDimensions(va_list args); michael@0: int dispatchSetBuffersUserDimensions(va_list args); michael@0: int dispatchSetBuffersFormat(va_list args); michael@0: int dispatchSetScalingMode(va_list args); michael@0: int dispatchSetBuffersTransform(va_list args); michael@0: int dispatchSetBuffersTimestamp(va_list args); michael@0: int dispatchSetCrop(va_list args); michael@0: int dispatchSetPostTransformCrop(va_list args); michael@0: int dispatchSetUsage(va_list args); michael@0: int dispatchLock(va_list args); michael@0: int dispatchUnlockAndPost(va_list args); michael@0: michael@0: protected: michael@0: virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd); michael@0: virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd); michael@0: virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd); michael@0: virtual int perform(int operation, va_list args); michael@0: virtual int query(int what, int* value) const; michael@0: virtual int setSwapInterval(int interval); michael@0: michael@0: virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer); michael@0: michael@0: virtual int connect(int api); michael@0: virtual int disconnect(int api); michael@0: virtual int setBufferCount(int bufferCount); michael@0: virtual int setBuffersDimensions(int w, int h); michael@0: virtual int setBuffersUserDimensions(int w, int h); michael@0: virtual int setBuffersFormat(int format); michael@0: virtual int setScalingMode(int mode); michael@0: virtual int setBuffersTransform(int transform); michael@0: virtual int setBuffersTimestamp(int64_t timestamp); michael@0: virtual int setCrop(Rect const* rect); michael@0: virtual int setUsage(uint32_t reqUsage); michael@0: michael@0: public: michael@0: virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); michael@0: virtual int unlockAndPost(); michael@0: michael@0: protected: michael@0: enum { NUM_BUFFER_SLOTS = GonkBufferQueue::NUM_BUFFER_SLOTS }; michael@0: enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; michael@0: michael@0: private: michael@0: void freeAllBuffers(); michael@0: int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; michael@0: michael@0: struct BufferSlot { michael@0: sp buffer; michael@0: Region dirtyRegion; michael@0: }; michael@0: michael@0: // mSurfaceTexture is the interface to the surface texture server. All michael@0: // operations on the surface texture client ultimately translate into michael@0: // interactions with the server using this interface. michael@0: // TODO: rename to mBufferProducer michael@0: sp mGraphicBufferProducer; michael@0: michael@0: // mSlots stores the buffers that have been allocated for each buffer slot. michael@0: // It is initialized to null pointers, and gets filled in with the result of michael@0: // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a michael@0: // slot that has not yet been used. The buffer allocated to a slot will also michael@0: // be replaced if the requested buffer usage or geometry differs from that michael@0: // of the buffer allocated to a slot. michael@0: BufferSlot mSlots[NUM_BUFFER_SLOTS]; michael@0: michael@0: // mReqWidth is the buffer width that will be requested at the next dequeue michael@0: // operation. It is initialized to 1. michael@0: uint32_t mReqWidth; michael@0: michael@0: // mReqHeight is the buffer height that will be requested at the next michael@0: // dequeue operation. It is initialized to 1. michael@0: uint32_t mReqHeight; michael@0: michael@0: // mReqFormat is the buffer pixel format that will be requested at the next michael@0: // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. michael@0: uint32_t mReqFormat; michael@0: michael@0: // mReqUsage is the set of buffer usage flags that will be requested michael@0: // at the next deuque operation. It is initialized to 0. michael@0: uint32_t mReqUsage; michael@0: michael@0: // mTimestamp is the timestamp that will be used for the next buffer queue michael@0: // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that michael@0: // a timestamp is auto-generated when queueBuffer is called. michael@0: int64_t mTimestamp; michael@0: michael@0: // mCrop is the crop rectangle that will be used for the next buffer michael@0: // that gets queued. It is set by calling setCrop. michael@0: Rect mCrop; michael@0: michael@0: // mScalingMode is the scaling mode that will be used for the next michael@0: // buffers that get queued. It is set by calling setScalingMode. michael@0: int mScalingMode; michael@0: michael@0: // mTransform is the transform identifier that will be used for the next michael@0: // buffer that gets queued. It is set by calling setTransform. michael@0: uint32_t mTransform; michael@0: michael@0: // mDefaultWidth is default width of the buffers, regardless of the michael@0: // native_window_set_buffers_dimensions call. michael@0: uint32_t mDefaultWidth; michael@0: michael@0: // mDefaultHeight is default height of the buffers, regardless of the michael@0: // native_window_set_buffers_dimensions call. michael@0: uint32_t mDefaultHeight; michael@0: michael@0: // mUserWidth, if non-zero, is an application-specified override michael@0: // of mDefaultWidth. This is lower priority than the width set by michael@0: // native_window_set_buffers_dimensions. michael@0: uint32_t mUserWidth; michael@0: michael@0: // mUserHeight, if non-zero, is an application-specified override michael@0: // of mDefaultHeight. This is lower priority than the height set michael@0: // by native_window_set_buffers_dimensions. michael@0: uint32_t mUserHeight; michael@0: michael@0: // mTransformHint is the transform probably applied to buffers of this michael@0: // window. this is only a hint, actual transform may differ. michael@0: uint32_t mTransformHint; michael@0: michael@0: // mProducerControlledByApp whether this buffer producer is controlled michael@0: // by the application michael@0: bool mProducerControlledByApp; michael@0: michael@0: // mSwapIntervalZero set if we should drop buffers at queue() time to michael@0: // achieve an asynchronous swap interval michael@0: bool mSwapIntervalZero; michael@0: michael@0: // mConsumerRunningBehind whether the consumer is running more than michael@0: // one buffer behind the producer. michael@0: mutable bool mConsumerRunningBehind; michael@0: michael@0: // mMutex is the mutex used to prevent concurrent access to the member michael@0: // variables of GonkNativeWindowClient objects. It must be locked whenever the michael@0: // member variables are accessed. michael@0: mutable Mutex mMutex; michael@0: michael@0: // must be used from the lock/unlock thread michael@0: sp mLockedBuffer; michael@0: sp mPostedBuffer; michael@0: bool mConnectedToCpu; michael@0: michael@0: // must be accessed from lock/unlock thread only michael@0: Region mDirtyRegion; michael@0: }; michael@0: michael@0: }; // namespace android michael@0: michael@0: #endif // NATIVEWINDOW_GONKNATIVEWINDOWCLIENT_JB_H