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_SURFACE_H
18 #define ANDROID_GUI_SURFACE_H
19 
20 #include <gui/IGraphicBufferProducer.h>
21 #include <gui/BufferQueue.h>
22 
23 #include <ui/ANativeObjectBase.h>
24 #include <ui/Region.h>
25 
26 #include <binder/Parcelable.h>
27 
28 #include <utils/RefBase.h>
29 #include <utils/threads.h>
30 #include <utils/KeyedVector.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  * Surface.  Surface 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 Surface
50     : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
51 {
52 public:
53 
54     /*
55      * creates a Surface from the given IGraphicBufferProducer (which concrete
56      * implementation is a BufferQueue).
57      *
58      * Surface 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 Surfaces from the same IGraphicBufferProducer.
61      *
62      * However, once a Surface is connected, it'll prevent other Surfaces
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     Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false);
70 
71     /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this
72      * Surface was created with. Usually it's an error to use the
73      * IGraphicBufferProducer while the Surface 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 */
isValid(const sp<Surface> & surface)79     static bool isValid(const sp<Surface>& surface) {
80         return surface != NULL && surface->getIGraphicBufferProducer() != NULL;
81     }
82 
83     /* Attaches a sideband buffer stream to the Surface's IGraphicBufferProducer.
84      *
85      * A sideband stream is a device-specific mechanism for passing buffers
86      * from the producer to the consumer without using dequeueBuffer/
87      * queueBuffer. If a sideband stream is present, the consumer can choose
88      * whether to acquire buffers from the sideband stream or from the queued
89      * buffers.
90      *
91      * Passing NULL or a different stream handle will detach the previous
92      * handle if any.
93      */
94     void setSidebandStream(const sp<NativeHandle>& stream);
95 
96     /* Allocates buffers based on the current dimensions/format.
97      *
98      * This function will allocate up to the maximum number of buffers
99      * permitted by the current BufferQueue configuration. It will use the
100      * default format and dimensions. This is most useful to avoid an allocation
101      * delay during dequeueBuffer. If there are already the maximum number of
102      * buffers allocated, this function has no effect.
103      */
104     void allocateBuffers();
105 
106     /* Sets the generation number on the IGraphicBufferProducer and updates the
107      * generation number on any buffers attached to the Surface after this call.
108      * See IGBP::setGenerationNumber for more information. */
109     status_t setGenerationNumber(uint32_t generationNumber);
110 
111     // See IGraphicBufferProducer::getConsumerName
112     String8 getConsumerName() const;
113 
114     // See IGraphicBufferProducer::getNextFrameNumber
115     uint64_t getNextFrameNumber() const;
116 
117     /* Set the scaling mode to be used with a Surface.
118      * See NATIVE_WINDOW_SET_SCALING_MODE and its parameters
119      * in <system/window.h>. */
120     int setScalingMode(int mode);
121 
122     // See IGraphicBufferProducer::setDequeueTimeout
123     status_t setDequeueTimeout(nsecs_t timeout);
124 
125     /*
126      * Wait for frame number to increase past lastFrame for at most
127      * timeoutNs. Useful for one thread to wait for another unknown
128      * thread to queue a buffer.
129      */
130     bool waitForNextFrame(uint64_t lastFrame, nsecs_t timeout);
131 
132     // See IGraphicBufferProducer::getLastQueuedBuffer
133     // See GLConsumer::getTransformMatrix for outTransformMatrix format
134     status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
135             sp<Fence>* outFence, float outTransformMatrix[16]);
136 
137     status_t getUniqueId(uint64_t* outId) const;
138 
139 protected:
140     virtual ~Surface();
141 
142 private:
143     // can't be copied
144     Surface& operator = (const Surface& rhs);
145     Surface(const Surface& rhs);
146 
147     // ANativeWindow hooks
148     static int hook_cancelBuffer(ANativeWindow* window,
149             ANativeWindowBuffer* buffer, int fenceFd);
150     static int hook_dequeueBuffer(ANativeWindow* window,
151             ANativeWindowBuffer** buffer, int* fenceFd);
152     static int hook_perform(ANativeWindow* window, int operation, ...);
153     static int hook_query(const ANativeWindow* window, int what, int* value);
154     static int hook_queueBuffer(ANativeWindow* window,
155             ANativeWindowBuffer* buffer, int fenceFd);
156     static int hook_setSwapInterval(ANativeWindow* window, int interval);
157 
158     static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
159             ANativeWindowBuffer* buffer);
160     static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
161             ANativeWindowBuffer** buffer);
162     static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
163             ANativeWindowBuffer* buffer);
164     static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
165             ANativeWindowBuffer* buffer);
166 
167     int dispatchConnect(va_list args);
168     int dispatchDisconnect(va_list args);
169     int dispatchSetBufferCount(va_list args);
170     int dispatchSetBuffersGeometry(va_list args);
171     int dispatchSetBuffersDimensions(va_list args);
172     int dispatchSetBuffersUserDimensions(va_list args);
173     int dispatchSetBuffersFormat(va_list args);
174     int dispatchSetScalingMode(va_list args);
175     int dispatchSetBuffersTransform(va_list args);
176     int dispatchSetBuffersStickyTransform(va_list args);
177     int dispatchSetBuffersTimestamp(va_list args);
178     int dispatchSetCrop(va_list args);
179     int dispatchSetPostTransformCrop(va_list args);
180     int dispatchSetUsage(va_list args);
181     int dispatchLock(va_list args);
182     int dispatchUnlockAndPost(va_list args);
183     int dispatchSetSidebandStream(va_list args);
184     int dispatchSetBuffersDataSpace(va_list args);
185     int dispatchSetSurfaceDamage(va_list args);
186     int dispatchSetSharedBufferMode(va_list args);
187     int dispatchSetAutoRefresh(va_list args);
188 
189 protected:
190     virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
191     virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
192     virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
193     virtual int perform(int operation, va_list args);
194     virtual int query(int what, int* value) const;
195     virtual int setSwapInterval(int interval);
196 
197     virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
198 
199     virtual int connect(int api);
200     virtual int disconnect(int api);
201     virtual int setBufferCount(int bufferCount);
202     virtual int setBuffersDimensions(uint32_t width, uint32_t height);
203     virtual int setBuffersUserDimensions(uint32_t width, uint32_t height);
204     virtual int setBuffersFormat(PixelFormat format);
205     virtual int setBuffersTransform(uint32_t transform);
206     virtual int setBuffersStickyTransform(uint32_t transform);
207     virtual int setBuffersTimestamp(int64_t timestamp);
208     virtual int setBuffersDataSpace(android_dataspace dataSpace);
209     virtual int setCrop(Rect const* rect);
210     virtual int setUsage(uint32_t reqUsage);
211     virtual void setSurfaceDamage(android_native_rect_t* rects, size_t numRects);
212 
213 public:
214     virtual int setMaxDequeuedBufferCount(int maxDequeuedBuffers);
215     virtual int setAsyncMode(bool async);
216     virtual int setSharedBufferMode(bool sharedBufferMode);
217     virtual int setAutoRefresh(bool autoRefresh);
218     virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
219     virtual int unlockAndPost();
220 
221     virtual int connect(int api, const sp<IProducerListener>& listener);
222     virtual int detachNextBuffer(sp<GraphicBuffer>* outBuffer,
223             sp<Fence>* outFence);
224     virtual int attachBuffer(ANativeWindowBuffer*);
225 
226 protected:
227     enum { NUM_BUFFER_SLOTS = BufferQueue::NUM_BUFFER_SLOTS };
228     enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
229 
230 private:
231     void freeAllBuffers();
232     int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
233 
234     struct BufferSlot {
235         sp<GraphicBuffer> buffer;
236         Region dirtyRegion;
237     };
238 
239     // mSurfaceTexture is the interface to the surface texture server. All
240     // operations on the surface texture client ultimately translate into
241     // interactions with the server using this interface.
242     // TODO: rename to mBufferProducer
243     sp<IGraphicBufferProducer> mGraphicBufferProducer;
244 
245     // mSlots stores the buffers that have been allocated for each buffer slot.
246     // It is initialized to null pointers, and gets filled in with the result of
247     // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
248     // slot that has not yet been used. The buffer allocated to a slot will also
249     // be replaced if the requested buffer usage or geometry differs from that
250     // of the buffer allocated to a slot.
251     BufferSlot mSlots[NUM_BUFFER_SLOTS];
252 
253     // mReqWidth is the buffer width that will be requested at the next dequeue
254     // operation. It is initialized to 1.
255     uint32_t mReqWidth;
256 
257     // mReqHeight is the buffer height that will be requested at the next
258     // dequeue operation. It is initialized to 1.
259     uint32_t mReqHeight;
260 
261     // mReqFormat is the buffer pixel format that will be requested at the next
262     // deuque operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
263     PixelFormat mReqFormat;
264 
265     // mReqUsage is the set of buffer usage flags that will be requested
266     // at the next deuque operation. It is initialized to 0.
267     uint32_t mReqUsage;
268 
269     // mTimestamp is the timestamp that will be used for the next buffer queue
270     // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
271     // a timestamp is auto-generated when queueBuffer is called.
272     int64_t mTimestamp;
273 
274     // mDataSpace is the buffer dataSpace that will be used for the next buffer
275     // queue operation. It defaults to HAL_DATASPACE_UNKNOWN, which
276     // means that the buffer contains some type of color data.
277     android_dataspace mDataSpace;
278 
279     // mCrop is the crop rectangle that will be used for the next buffer
280     // that gets queued. It is set by calling setCrop.
281     Rect mCrop;
282 
283     // mScalingMode is the scaling mode that will be used for the next
284     // buffers that get queued. It is set by calling setScalingMode.
285     int mScalingMode;
286 
287     // mTransform is the transform identifier that will be used for the next
288     // buffer that gets queued. It is set by calling setTransform.
289     uint32_t mTransform;
290 
291     // mStickyTransform is a transform that is applied on top of mTransform
292     // in each buffer that is queued.  This is typically used to force the
293     // compositor to apply a transform, and will prevent the transform hint
294     // from being set by the compositor.
295     uint32_t mStickyTransform;
296 
297     // mDefaultWidth is default width of the buffers, regardless of the
298     // native_window_set_buffers_dimensions call.
299     uint32_t mDefaultWidth;
300 
301     // mDefaultHeight is default height of the buffers, regardless of the
302     // native_window_set_buffers_dimensions call.
303     uint32_t mDefaultHeight;
304 
305     // mUserWidth, if non-zero, is an application-specified override
306     // of mDefaultWidth.  This is lower priority than the width set by
307     // native_window_set_buffers_dimensions.
308     uint32_t mUserWidth;
309 
310     // mUserHeight, if non-zero, is an application-specified override
311     // of mDefaultHeight.  This is lower priority than the height set
312     // by native_window_set_buffers_dimensions.
313     uint32_t mUserHeight;
314 
315     // mTransformHint is the transform probably applied to buffers of this
316     // window. this is only a hint, actual transform may differ.
317     uint32_t mTransformHint;
318 
319     // mProducerControlledByApp whether this buffer producer is controlled
320     // by the application
321     bool mProducerControlledByApp;
322 
323     // mSwapIntervalZero set if we should drop buffers at queue() time to
324     // achieve an asynchronous swap interval
325     bool mSwapIntervalZero;
326 
327     // mConsumerRunningBehind whether the consumer is running more than
328     // one buffer behind the producer.
329     mutable bool mConsumerRunningBehind;
330 
331     // mMutex is the mutex used to prevent concurrent access to the member
332     // variables of Surface objects. It must be locked whenever the
333     // member variables are accessed.
334     mutable Mutex mMutex;
335 
336     // must be used from the lock/unlock thread
337     sp<GraphicBuffer>           mLockedBuffer;
338     sp<GraphicBuffer>           mPostedBuffer;
339     bool                        mConnectedToCpu;
340 
341     // When a CPU producer is attached, this reflects the region that the
342     // producer wished to update as well as whether the Surface was able to copy
343     // the previous buffer back to allow a partial update.
344     //
345     // When a non-CPU producer is attached, this reflects the surface damage
346     // (the change since the previous frame) passed in by the producer.
347     Region mDirtyRegion;
348 
349     // Stores the current generation number. See setGenerationNumber and
350     // IGraphicBufferProducer::setGenerationNumber for more information.
351     uint32_t mGenerationNumber;
352 
353     // Caches the values that have been passed to the producer.
354     bool mSharedBufferMode;
355     bool mAutoRefresh;
356 
357     // If in shared buffer mode and auto refresh is enabled, store the shared
358     // buffer slot and return it for all calls to queue/dequeue without going
359     // over Binder.
360     int mSharedBufferSlot;
361 
362     // This is true if the shared buffer has already been queued/canceled. It's
363     // used to prevent a mismatch between the number of queue/dequeue calls.
364     bool mSharedBufferHasBeenQueued;
365 
366     Condition mQueueBufferCondition;
367 };
368 
369 namespace view {
370 
371 /**
372  * A simple holder for an IGraphicBufferProducer, to match the managed-side
373  * android.view.Surface parcelable behavior.
374  *
375  * This implements android/view/Surface.aidl
376  *
377  * TODO: Convert IGraphicBufferProducer into AIDL so that it can be directly
378  * used in managed Binder calls.
379  */
380 class Surface : public Parcelable {
381   public:
382 
383     String16 name;
384     sp<IGraphicBufferProducer> graphicBufferProducer;
385 
386     virtual status_t writeToParcel(Parcel* parcel) const override;
387     virtual status_t readFromParcel(const Parcel* parcel) override;
388 
389     // nameAlreadyWritten set to true by Surface.java, because it splits
390     // Parceling itself between managed and native code, so it only wants a part
391     // of the full parceling to happen on its native side.
392     status_t writeToParcel(Parcel* parcel, bool nameAlreadyWritten) const;
393 
394     // nameAlreadyRead set to true by Surface.java, because it splits
395     // Parceling itself between managed and native code, so it only wants a part
396     // of the full parceling to happen on its native side.
397     status_t readFromParcel(const Parcel* parcel, bool nameAlreadyRead);
398 
399   private:
400 
401     static String16 readMaybeEmptyString16(const Parcel* parcel);
402 };
403 
404 } // namespace view
405 
406 }; // namespace android
407 
408 #endif  // ANDROID_GUI_SURFACE_H
409