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