widget/gonk/nativewindow/GonkNativeWindowClientKK.h

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

mercurial