Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_GUI_SURFACETEXTURECLIENT_H |
| 18 | #define ANDROID_GUI_SURFACETEXTURECLIENT_H |
| 19 | |
| 20 | #include <gui/ISurfaceTexture.h> |
| 21 | #include <gui/SurfaceTexture.h> |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 22 | #include <gui/BufferQueue.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 23 | |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 24 | #include <ui/ANativeObjectBase.h> |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 25 | #include <ui/Region.h> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 26 | |
| 27 | #include <utils/RefBase.h> |
| 28 | #include <utils/threads.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 32 | class Surface; |
| 33 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 34 | class SurfaceTextureClient |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 35 | : public ANativeObjectBase<ANativeWindow, SurfaceTextureClient, RefBase> |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 36 | { |
| 37 | public: |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 38 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 39 | SurfaceTextureClient(const sp<ISurfaceTexture>& surfaceTexture); |
| 40 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 41 | // SurfaceTextureClient is overloaded to assist in refactoring ST and BQ. |
| 42 | // SurfaceTexture is no longer an ISurfaceTexture, so client code |
| 43 | // calling the original constructor will fail. Thus this convenience method |
| 44 | // passes in the surfaceTexture's bufferQueue to the init method. |
| 45 | SurfaceTextureClient(const sp<SurfaceTexture>& surfaceTexture); |
| 46 | |
Jamie Gennis | bae774e | 2011-03-14 15:08:53 -0700 | [diff] [blame] | 47 | sp<ISurfaceTexture> getISurfaceTexture() const; |
| 48 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 49 | protected: |
| 50 | SurfaceTextureClient(); |
Mathias Agopian | a36bcd5 | 2011-11-17 18:46:09 -0800 | [diff] [blame] | 51 | virtual ~SurfaceTextureClient(); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 52 | void setISurfaceTexture(const sp<ISurfaceTexture>& surfaceTexture); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 53 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 54 | private: |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 55 | // can't be copied |
| 56 | SurfaceTextureClient& operator = (const SurfaceTextureClient& rhs); |
| 57 | SurfaceTextureClient(const SurfaceTextureClient& rhs); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 58 | void init(); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 59 | |
| 60 | // ANativeWindow hooks |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 61 | static int hook_cancelBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer); |
| 62 | static int hook_dequeueBuffer(ANativeWindow* window, ANativeWindowBuffer** buffer); |
| 63 | static int hook_lockBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer); |
| 64 | static int hook_perform(ANativeWindow* window, int operation, ...); |
| 65 | static int hook_query(const ANativeWindow* window, int what, int* value); |
| 66 | static int hook_queueBuffer(ANativeWindow* window, ANativeWindowBuffer* buffer); |
| 67 | static int hook_setSwapInterval(ANativeWindow* window, int interval); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 68 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 69 | int dispatchConnect(va_list args); |
| 70 | int dispatchDisconnect(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 71 | int dispatchSetBufferCount(va_list args); |
| 72 | int dispatchSetBuffersGeometry(va_list args); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 73 | int dispatchSetBuffersDimensions(va_list args); |
| 74 | int dispatchSetBuffersFormat(va_list args); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 75 | int dispatchSetScalingMode(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 76 | int dispatchSetBuffersTransform(va_list args); |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 77 | int dispatchSetBuffersTimestamp(va_list args); |
Jamie Gennis | 9d4d6c1 | 2011-02-27 14:10:20 -0800 | [diff] [blame] | 78 | int dispatchSetCrop(va_list args); |
| 79 | int dispatchSetUsage(va_list args); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 80 | int dispatchLock(va_list args); |
| 81 | int dispatchUnlockAndPost(va_list args); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 82 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 83 | protected: |
| 84 | virtual int cancelBuffer(ANativeWindowBuffer* buffer); |
| 85 | virtual int dequeueBuffer(ANativeWindowBuffer** buffer); |
| 86 | virtual int lockBuffer(ANativeWindowBuffer* buffer); |
| 87 | virtual int perform(int operation, va_list args); |
| 88 | virtual int query(int what, int* value) const; |
| 89 | virtual int queueBuffer(ANativeWindowBuffer* buffer); |
| 90 | virtual int setSwapInterval(int interval); |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 91 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 92 | virtual int connect(int api); |
| 93 | virtual int disconnect(int api); |
| 94 | virtual int setBufferCount(int bufferCount); |
| 95 | virtual int setBuffersDimensions(int w, int h); |
| 96 | virtual int setBuffersFormat(int format); |
Mathias Agopian | 7734ebf | 2011-07-13 15:24:42 -0700 | [diff] [blame] | 97 | virtual int setScalingMode(int mode); |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 98 | virtual int setBuffersTransform(int transform); |
| 99 | virtual int setBuffersTimestamp(int64_t timestamp); |
| 100 | virtual int setCrop(Rect const* rect); |
| 101 | virtual int setUsage(uint32_t reqUsage); |
| 102 | virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); |
| 103 | virtual int unlockAndPost(); |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 104 | |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 105 | enum { MIN_UNDEQUEUED_BUFFERS = BufferQueue::MIN_UNDEQUEUED_BUFFERS }; |
| 106 | enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS }; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 107 | enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 }; |
| 108 | |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 109 | private: |
| 110 | void freeAllBuffers(); |
| 111 | int getSlotFromBufferLocked(android_native_buffer_t* buffer) const; |
| 112 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 113 | // mSurfaceTexture is the interface to the surface texture server. All |
| 114 | // operations on the surface texture client ultimately translate into |
| 115 | // interactions with the server using this interface. |
| 116 | sp<ISurfaceTexture> mSurfaceTexture; |
| 117 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 118 | // mSlots stores the buffers that have been allocated for each buffer slot. |
| 119 | // It is initialized to null pointers, and gets filled in with the result of |
| 120 | // ISurfaceTexture::requestBuffer when the client dequeues a buffer from a |
| 121 | // slot that has not yet been used. The buffer allocated to a slot will also |
| 122 | // be replaced if the requested buffer usage or geometry differs from that |
| 123 | // of the buffer allocated to a slot. |
| 124 | sp<GraphicBuffer> mSlots[NUM_BUFFER_SLOTS]; |
| 125 | |
| 126 | // mReqWidth is the buffer width that will be requested at the next dequeue |
| 127 | // operation. It is initialized to 1. |
| 128 | uint32_t mReqWidth; |
| 129 | |
| 130 | // mReqHeight is the buffer height that will be requested at the next deuque |
| 131 | // operation. It is initialized to 1. |
| 132 | uint32_t mReqHeight; |
| 133 | |
| 134 | // mReqFormat is the buffer pixel format that will be requested at the next |
| 135 | // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888. |
| 136 | uint32_t mReqFormat; |
| 137 | |
| 138 | // mReqUsage is the set of buffer usage flags that will be requested |
| 139 | // at the next deuque operation. It is initialized to 0. |
| 140 | uint32_t mReqUsage; |
| 141 | |
Eino-Ville Talvala | 1d01a12 | 2011-02-18 11:02:42 -0800 | [diff] [blame] | 142 | // mTimestamp is the timestamp that will be used for the next buffer queue |
| 143 | // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that |
| 144 | // a timestamp is auto-generated when queueBuffer is called. |
| 145 | int64_t mTimestamp; |
| 146 | |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 147 | // mDefaultWidth is default width of the window, regardless of the |
Mathias Agopian | 3fbce7c | 2011-07-25 19:56:08 -0700 | [diff] [blame] | 148 | // native_window_set_buffers_dimensions call |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 149 | uint32_t mDefaultWidth; |
| 150 | |
| 151 | // mDefaultHeight is default width of the window, regardless of the |
Mathias Agopian | 3fbce7c | 2011-07-25 19:56:08 -0700 | [diff] [blame] | 152 | // native_window_set_buffers_dimensions call |
Mathias Agopian | 97c602c | 2011-07-19 15:24:46 -0700 | [diff] [blame] | 153 | uint32_t mDefaultHeight; |
| 154 | |
| 155 | // mTransformHint is the transform probably applied to buffers of this |
| 156 | // window. this is only a hint, actual transform may differ. |
| 157 | uint32_t mTransformHint; |
| 158 | |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 159 | // mMutex is the mutex used to prevent concurrent access to the member |
| 160 | // variables of SurfaceTexture objects. It must be locked whenever the |
| 161 | // member variables are accessed. |
Mathias Agopian | 7a042bf | 2011-04-11 21:19:55 -0700 | [diff] [blame] | 162 | mutable Mutex mMutex; |
Mathias Agopian | 8f9dbf9 | 2011-07-13 17:39:11 -0700 | [diff] [blame] | 163 | |
| 164 | // must be used from the lock/unlock thread |
| 165 | sp<GraphicBuffer> mLockedBuffer; |
| 166 | sp<GraphicBuffer> mPostedBuffer; |
| 167 | mutable Region mOldDirtyRegion; |
| 168 | bool mConnectedToCpu; |
Jamie Gennis | 8ba32fa | 2010-12-20 11:27:26 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | }; // namespace android |
| 172 | |
| 173 | #endif // ANDROID_GUI_SURFACETEXTURECLIENT_H |