• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android/gui/FrameTimelineInfo.h>
21 #include <gui/BufferQueueDefs.h>
22 #include <gui/HdrMetadata.h>
23 #include <gui/IGraphicBufferProducer.h>
24 #include <gui/IProducerListener.h>
25 #include <system/window.h>
26 #include <ui/ANativeObjectBase.h>
27 #include <ui/GraphicTypes.h>
28 #include <ui/Region.h>
29 #include <utils/Condition.h>
30 #include <utils/Mutex.h>
31 #include <utils/RefBase.h>
32 
33 #include <shared_mutex>
34 #include <unordered_set>
35 
36 namespace android {
37 
38 namespace gui {
39 class ISurfaceComposer;
40 } // namespace gui
41 
42 class ISurfaceComposer;
43 
44 using gui::FrameTimelineInfo;
45 
46 /* This is the same as ProducerListener except that onBuffersDiscarded is
47  * called with a vector of graphic buffers instead of buffer slots.
48  */
49 class SurfaceListener : public virtual RefBase
50 {
51 public:
52     SurfaceListener() = default;
53     virtual ~SurfaceListener() = default;
54 
55     virtual void onBufferReleased() = 0;
56     virtual bool needsReleaseNotify() = 0;
57 
58     virtual void onBuffersDiscarded(const std::vector<sp<GraphicBuffer>>& buffers) = 0;
59 };
60 
61 /*
62  * An implementation of ANativeWindow that feeds graphics buffers into a
63  * BufferQueue.
64  *
65  * This is typically used by programs that want to render frames through
66  * some means (maybe OpenGL, a software renderer, or a hardware decoder)
67  * and have the frames they create forwarded to SurfaceFlinger for
68  * compositing.  For example, a video decoder could render a frame and call
69  * eglSwapBuffers(), which invokes ANativeWindow callbacks defined by
70  * Surface.  Surface then forwards the buffers through Binder IPC
71  * to the BufferQueue's producer interface, providing the new frame to a
72  * consumer such as GLConsumer.
73  */
74 class Surface
75     : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
76 {
77 public:
78     /*
79      * creates a Surface from the given IGraphicBufferProducer (which concrete
80      * implementation is a BufferQueue).
81      *
82      * Surface is mainly state-less while it's disconnected, it can be
83      * viewed as a glorified IGraphicBufferProducer holder. It's therefore
84      * safe to create other Surfaces from the same IGraphicBufferProducer.
85      *
86      * However, once a Surface is connected, it'll prevent other Surfaces
87      * referring to the same IGraphicBufferProducer to become connected and
88      * therefore prevent them to be used as actual producers of buffers.
89      *
90      * the controlledByApp flag indicates that this Surface (producer) is
91      * controlled by the application. This flag is used at connect time.
92      *
93      * Pass in the SurfaceControlHandle to store a weak reference to the layer
94      * that the Surface was created from. This handle can be used to create a
95      * child surface without using the IGBP to identify the layer. This is used
96      * for surfaces created by the BlastBufferQueue whose IGBP is created on the
97      * client and cannot be verified in SF.
98      */
99     explicit Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp = false,
100                      const sp<IBinder>& surfaceControlHandle = nullptr);
101 
102     /* getIGraphicBufferProducer() returns the IGraphicBufferProducer this
103      * Surface was created with. Usually it's an error to use the
104      * IGraphicBufferProducer while the Surface is connected.
105      */
106     sp<IGraphicBufferProducer> getIGraphicBufferProducer() const;
107 
108     sp<IBinder> getSurfaceControlHandle() const;
109 
110     /* convenience function to check that the given surface is non NULL as
111      * well as its IGraphicBufferProducer */
isValid(const sp<Surface> & surface)112     static bool isValid(const sp<Surface>& surface) {
113         return surface != nullptr && surface->getIGraphicBufferProducer() != nullptr;
114     }
115 
getIGraphicBufferProducer(ANativeWindow * window)116     static sp<IGraphicBufferProducer> getIGraphicBufferProducer(ANativeWindow* window) {
117         int val;
118         if (window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &val) >= 0 &&
119             val == NATIVE_WINDOW_SURFACE) {
120             return ((Surface*) window)->mGraphicBufferProducer;
121         }
122         return nullptr;
123     }
124 
getSurfaceControlHandle(ANativeWindow * window)125     static sp<IBinder> getSurfaceControlHandle(ANativeWindow* window) {
126         int val;
127         if (window->query(window, NATIVE_WINDOW_CONCRETE_TYPE, &val) >= 0 &&
128             val == NATIVE_WINDOW_SURFACE) {
129             return ((Surface*) window)->mSurfaceControlHandle;
130         }
131         return nullptr;
132     }
133 
134     /* Attaches a sideband buffer stream to the Surface's IGraphicBufferProducer.
135      *
136      * A sideband stream is a device-specific mechanism for passing buffers
137      * from the producer to the consumer without using dequeueBuffer/
138      * queueBuffer. If a sideband stream is present, the consumer can choose
139      * whether to acquire buffers from the sideband stream or from the queued
140      * buffers.
141      *
142      * Passing NULL or a different stream handle will detach the previous
143      * handle if any.
144      */
145     void setSidebandStream(const sp<NativeHandle>& stream);
146 
147     /* Allocates buffers based on the current dimensions/format.
148      *
149      * This function will allocate up to the maximum number of buffers
150      * permitted by the current BufferQueue configuration. It will use the
151      * default format and dimensions. This is most useful to avoid an allocation
152      * delay during dequeueBuffer. If there are already the maximum number of
153      * buffers allocated, this function has no effect.
154      */
155     virtual void allocateBuffers();
156 
157     /* Sets the generation number on the IGraphicBufferProducer and updates the
158      * generation number on any buffers attached to the Surface after this call.
159      * See IGBP::setGenerationNumber for more information. */
160     status_t setGenerationNumber(uint32_t generationNumber);
161 
162     // See IGraphicBufferProducer::getConsumerName
163     String8 getConsumerName() const;
164 
165     // See IGraphicBufferProducer::getNextFrameNumber
166     uint64_t getNextFrameNumber() const;
167 
168     /* Set the scaling mode to be used with a Surface.
169      * See NATIVE_WINDOW_SET_SCALING_MODE and its parameters
170      * in <system/window.h>. */
171     int setScalingMode(int mode);
172 
173     // See IGraphicBufferProducer::setDequeueTimeout
174     status_t setDequeueTimeout(nsecs_t timeout);
175 
176     /*
177      * Wait for frame number to increase past lastFrame for at most
178      * timeoutNs. Useful for one thread to wait for another unknown
179      * thread to queue a buffer.
180      */
181     bool waitForNextFrame(uint64_t lastFrame, nsecs_t timeout);
182 
183     // See IGraphicBufferProducer::getLastQueuedBuffer
184     // See GLConsumer::getTransformMatrix for outTransformMatrix format
185     status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
186             sp<Fence>* outFence, float outTransformMatrix[16]);
187 
188     status_t getDisplayRefreshCycleDuration(nsecs_t* outRefreshDuration);
189 
190     /* Enables or disables frame timestamp tracking. It is disabled by default
191      * to avoid overhead during queue and dequeue for applications that don't
192      * need the feature. If disabled, calls to getFrameTimestamps will fail.
193      */
194     void enableFrameTimestamps(bool enable);
195 
196     status_t getCompositorTiming(
197             nsecs_t* compositeDeadline, nsecs_t* compositeInterval,
198             nsecs_t* compositeToPresentLatency);
199 
200     // See IGraphicBufferProducer::getFrameTimestamps
201     status_t getFrameTimestamps(uint64_t frameNumber,
202             nsecs_t* outRequestedPresentTime, nsecs_t* outAcquireTime,
203             nsecs_t* outLatchTime, nsecs_t* outFirstRefreshStartTime,
204             nsecs_t* outLastRefreshStartTime, nsecs_t* outGlCompositionDoneTime,
205             nsecs_t* outDisplayPresentTime, nsecs_t* outDequeueReadyTime,
206             nsecs_t* outReleaseTime);
207 
208     status_t getWideColorSupport(bool* supported) __attribute__((__deprecated__));
209     status_t getHdrSupport(bool* supported) __attribute__((__deprecated__));
210 
211     status_t getUniqueId(uint64_t* outId) const;
212     status_t getConsumerUsage(uint64_t* outUsage) const;
213 
214     virtual status_t setFrameRate(float frameRate, int8_t compatibility,
215                                   int8_t changeFrameRateStrategy);
216     virtual status_t setFrameTimelineInfo(uint64_t frameNumber, const FrameTimelineInfo& info);
217 
218 protected:
219     virtual ~Surface();
220 
221     // Virtual for testing.
222     virtual sp<ISurfaceComposer> composerService() const;
223     virtual sp<gui::ISurfaceComposer> composerServiceAIDL() const;
224     virtual nsecs_t now() const;
225 
226 private:
227     // can't be copied
228     Surface& operator = (const Surface& rhs);
229     Surface(const Surface& rhs);
230 
231     // ANativeWindow hooks
232     static int hook_cancelBuffer(ANativeWindow* window,
233             ANativeWindowBuffer* buffer, int fenceFd);
234     static int hook_dequeueBuffer(ANativeWindow* window,
235             ANativeWindowBuffer** buffer, int* fenceFd);
236     static int hook_perform(ANativeWindow* window, int operation, ...);
237     static int hook_query(const ANativeWindow* window, int what, int* value);
238     static int hook_queueBuffer(ANativeWindow* window,
239             ANativeWindowBuffer* buffer, int fenceFd);
240     static int hook_setSwapInterval(ANativeWindow* window, int interval);
241 
242     static int cancelBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer,
243                                     int fenceFd);
244     static int dequeueBufferInternal(ANativeWindow* window, ANativeWindowBuffer** buffer,
245                                      int* fenceFd);
246     static int performInternal(ANativeWindow* window, int operation, va_list args);
247     static int queueBufferInternal(ANativeWindow* window, ANativeWindowBuffer* buffer, int fenceFd);
248     static int queryInternal(const ANativeWindow* window, int what, int* value);
249 
250     static int hook_cancelBuffer_DEPRECATED(ANativeWindow* window,
251             ANativeWindowBuffer* buffer);
252     static int hook_dequeueBuffer_DEPRECATED(ANativeWindow* window,
253             ANativeWindowBuffer** buffer);
254     static int hook_lockBuffer_DEPRECATED(ANativeWindow* window,
255             ANativeWindowBuffer* buffer);
256     static int hook_queueBuffer_DEPRECATED(ANativeWindow* window,
257             ANativeWindowBuffer* buffer);
258 
259     int dispatchConnect(va_list args);
260     int dispatchDisconnect(va_list args);
261     int dispatchSetBufferCount(va_list args);
262     int dispatchSetBuffersGeometry(va_list args);
263     int dispatchSetBuffersDimensions(va_list args);
264     int dispatchSetBuffersUserDimensions(va_list args);
265     int dispatchSetBuffersFormat(va_list args);
266     int dispatchSetScalingMode(va_list args);
267     int dispatchSetBuffersTransform(va_list args);
268     int dispatchSetBuffersStickyTransform(va_list args);
269     int dispatchSetBuffersTimestamp(va_list args);
270     int dispatchSetCrop(va_list args);
271     int dispatchSetUsage(va_list args);
272     int dispatchSetUsage64(va_list args);
273     int dispatchLock(va_list args);
274     int dispatchUnlockAndPost(va_list args);
275     int dispatchSetSidebandStream(va_list args);
276     int dispatchSetBuffersDataSpace(va_list args);
277     int dispatchSetBuffersSmpte2086Metadata(va_list args);
278     int dispatchSetBuffersCta8613Metadata(va_list args);
279     int dispatchSetBuffersHdr10PlusMetadata(va_list args);
280     int dispatchSetSurfaceDamage(va_list args);
281     int dispatchSetSharedBufferMode(va_list args);
282     int dispatchSetAutoRefresh(va_list args);
283     int dispatchGetDisplayRefreshCycleDuration(va_list args);
284     int dispatchGetNextFrameId(va_list args);
285     int dispatchEnableFrameTimestamps(va_list args);
286     int dispatchGetCompositorTiming(va_list args);
287     int dispatchGetFrameTimestamps(va_list args);
288     int dispatchGetWideColorSupport(va_list args);
289     int dispatchGetHdrSupport(va_list args);
290     int dispatchGetConsumerUsage64(va_list args);
291     int dispatchSetAutoPrerotation(va_list args);
292     int dispatchGetLastDequeueStartTime(va_list args);
293     int dispatchSetDequeueTimeout(va_list args);
294     int dispatchGetLastDequeueDuration(va_list args);
295     int dispatchGetLastQueueDuration(va_list args);
296     int dispatchSetFrameRate(va_list args);
297     int dispatchAddCancelInterceptor(va_list args);
298     int dispatchAddDequeueInterceptor(va_list args);
299     int dispatchAddPerformInterceptor(va_list args);
300     int dispatchAddQueueInterceptor(va_list args);
301     int dispatchAddQueryInterceptor(va_list args);
302     int dispatchGetLastQueuedBuffer(va_list args);
303     int dispatchGetLastQueuedBuffer2(va_list args);
304     int dispatchSetFrameTimelineInfo(va_list args);
305 
306     std::mutex mNameMutex;
307     std::string mName;
308     const char* getDebugName();
309 
310 protected:
311     virtual int dequeueBuffer(ANativeWindowBuffer** buffer, int* fenceFd);
312     virtual int cancelBuffer(ANativeWindowBuffer* buffer, int fenceFd);
313     virtual int queueBuffer(ANativeWindowBuffer* buffer, int fenceFd);
314     virtual int perform(int operation, va_list args);
315     virtual int setSwapInterval(int interval);
316 
317     virtual int lockBuffer_DEPRECATED(ANativeWindowBuffer* buffer);
318 
319     virtual int connect(int api);
320     virtual int setBufferCount(int bufferCount);
321     virtual int setBuffersUserDimensions(uint32_t width, uint32_t height);
322     virtual int setBuffersFormat(PixelFormat format);
323     virtual int setBuffersTransform(uint32_t transform);
324     virtual int setBuffersStickyTransform(uint32_t transform);
325     virtual int setBuffersTimestamp(int64_t timestamp);
326     virtual int setBuffersDataSpace(ui::Dataspace dataSpace);
327     virtual int setBuffersSmpte2086Metadata(const android_smpte2086_metadata* metadata);
328     virtual int setBuffersCta8613Metadata(const android_cta861_3_metadata* metadata);
329     virtual int setBuffersHdr10PlusMetadata(const size_t size, const uint8_t* metadata);
330     virtual int setCrop(Rect const* rect);
331     virtual int setUsage(uint64_t reqUsage);
332     virtual void setSurfaceDamage(android_native_rect_t* rects, size_t numRects);
333 
334 public:
335     virtual int disconnect(int api,
336             IGraphicBufferProducer::DisconnectMode mode =
337                     IGraphicBufferProducer::DisconnectMode::Api);
338 
339     virtual int setMaxDequeuedBufferCount(int maxDequeuedBuffers);
340     virtual int setAsyncMode(bool async);
341     virtual int setSharedBufferMode(bool sharedBufferMode);
342     virtual int setAutoRefresh(bool autoRefresh);
343     virtual int setAutoPrerotation(bool autoPrerotation);
344     virtual int setBuffersDimensions(uint32_t width, uint32_t height);
345     virtual int lock(ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds);
346     virtual int unlockAndPost();
347     virtual int query(int what, int* value) const;
348 
349     virtual int connect(int api, const sp<IProducerListener>& listener);
350 
351     // When reportBufferRemoval is true, clients must call getAndFlushRemovedBuffers to fetch
352     // GraphicBuffers removed from this surface after a dequeueBuffer, detachNextBuffer or
353     // attachBuffer call. This allows clients with their own buffer caches to free up buffers no
354     // longer in use by this surface.
355     virtual int connect(
356             int api, const sp<IProducerListener>& listener,
357             bool reportBufferRemoval);
358     virtual int detachNextBuffer(sp<GraphicBuffer>* outBuffer,
359             sp<Fence>* outFence);
360     virtual int attachBuffer(ANativeWindowBuffer*);
361 
362     virtual int connect(
363             int api, bool reportBufferRemoval,
364             const sp<SurfaceListener>& sListener);
365     virtual void destroy();
366 
367     // When client connects to Surface with reportBufferRemoval set to true, any buffers removed
368     // from this Surface will be collected and returned here. Once this method returns, these
369     // buffers will no longer be referenced by this Surface unless they are attached to this
370     // Surface later. The list of removed buffers will only be stored until the next dequeueBuffer,
371     // detachNextBuffer, or attachBuffer call.
372     status_t getAndFlushRemovedBuffers(std::vector<sp<GraphicBuffer>>* out);
373 
374     ui::Dataspace getBuffersDataSpace();
375 
376     static status_t attachAndQueueBufferWithDataspace(Surface* surface, sp<GraphicBuffer> buffer,
377                                                       ui::Dataspace dataspace);
378 
379     // Batch version of dequeueBuffer, cancelBuffer and queueBuffer
380     // Note that these batched operations are not supported when shared buffer mode is being used.
381     struct BatchBuffer {
382         ANativeWindowBuffer* buffer = nullptr;
383         int fenceFd = -1;
384     };
385     virtual int dequeueBuffers(std::vector<BatchBuffer>* buffers);
386     virtual int cancelBuffers(const std::vector<BatchBuffer>& buffers);
387 
388     struct BatchQueuedBuffer {
389         ANativeWindowBuffer* buffer = nullptr;
390         int fenceFd = -1;
391         nsecs_t timestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
392     };
393     virtual int queueBuffers(
394             const std::vector<BatchQueuedBuffer>& buffers);
395 
396 protected:
397     enum { NUM_BUFFER_SLOTS = BufferQueueDefs::NUM_BUFFER_SLOTS };
398     enum { DEFAULT_FORMAT = PIXEL_FORMAT_RGBA_8888 };
399 
400     class ProducerListenerProxy : public BnProducerListener {
401     public:
ProducerListenerProxy(wp<Surface> parent,sp<SurfaceListener> listener)402         ProducerListenerProxy(wp<Surface> parent, sp<SurfaceListener> listener)
403                : mParent(parent), mSurfaceListener(listener) {}
~ProducerListenerProxy()404         virtual ~ProducerListenerProxy() {}
405 
onBufferReleased()406         virtual void onBufferReleased() {
407             mSurfaceListener->onBufferReleased();
408         }
409 
needsReleaseNotify()410         virtual bool needsReleaseNotify() {
411             return mSurfaceListener->needsReleaseNotify();
412         }
413 
414         virtual void onBuffersDiscarded(const std::vector<int32_t>& slots);
415     private:
416         wp<Surface> mParent;
417         sp<SurfaceListener> mSurfaceListener;
418     };
419 
420     void querySupportedTimestampsLocked() const;
421 
422     void freeAllBuffers();
423     int getSlotFromBufferLocked(android_native_buffer_t* buffer) const;
424 
425     void getDequeueBufferInputLocked(IGraphicBufferProducer::DequeueBufferInput* dequeueInput);
426 
427     void getQueueBufferInputLocked(android_native_buffer_t* buffer, int fenceFd, nsecs_t timestamp,
428             IGraphicBufferProducer::QueueBufferInput* out);
429 
430     // For easing in adoption of gralloc4 metadata by vendor components, as well as for supporting
431     // the public ANativeWindow api, allow setting relevant metadata when queueing a buffer through
432     // a native window
433     void applyGrallocMetadataLocked(
434             android_native_buffer_t* buffer,
435             const IGraphicBufferProducer::QueueBufferInput& queueBufferInput);
436 
437     void onBufferQueuedLocked(int slot, sp<Fence> fence,
438             const IGraphicBufferProducer::QueueBufferOutput& output);
439 
440     struct BufferSlot {
441         sp<GraphicBuffer> buffer;
442         Region dirtyRegion;
443     };
444 
445     // mSurfaceTexture is the interface to the surface texture server. All
446     // operations on the surface texture client ultimately translate into
447     // interactions with the server using this interface.
448     // TODO: rename to mBufferProducer
449     sp<IGraphicBufferProducer> mGraphicBufferProducer;
450 
451     // mSlots stores the buffers that have been allocated for each buffer slot.
452     // It is initialized to null pointers, and gets filled in with the result of
453     // IGraphicBufferProducer::requestBuffer when the client dequeues a buffer from a
454     // slot that has not yet been used. The buffer allocated to a slot will also
455     // be replaced if the requested buffer usage or geometry differs from that
456     // of the buffer allocated to a slot.
457     BufferSlot mSlots[NUM_BUFFER_SLOTS];
458 
459     // mReqWidth is the buffer width that will be requested at the next dequeue
460     // operation. It is initialized to 1.
461     uint32_t mReqWidth;
462 
463     // mReqHeight is the buffer height that will be requested at the next
464     // dequeue operation. It is initialized to 1.
465     uint32_t mReqHeight;
466 
467     // mReqFormat is the buffer pixel format that will be requested at the next
468     // dequeue operation. It is initialized to PIXEL_FORMAT_RGBA_8888.
469     PixelFormat mReqFormat;
470 
471     // mReqUsage is the set of buffer usage flags that will be requested
472     // at the next dequeue operation. It is initialized to 0.
473     uint64_t mReqUsage;
474 
475     // mTimestamp is the timestamp that will be used for the next buffer queue
476     // operation. It defaults to NATIVE_WINDOW_TIMESTAMP_AUTO, which means that
477     // a timestamp is auto-generated when queueBuffer is called.
478     int64_t mTimestamp;
479 
480     // mDataSpace is the buffer dataSpace that will be used for the next buffer
481     // queue operation. It defaults to Dataspace::UNKNOWN, which
482     // means that the buffer contains some type of color data.
483     ui::Dataspace mDataSpace;
484 
485     // mHdrMetadata is the HDR metadata that will be used for the next buffer
486     // queue operation.  There is no HDR metadata by default.
487     HdrMetadata mHdrMetadata;
488 
489     // mHdrMetadataIsSet is a bitfield to track which HDR metadata has been set.
490     // Prevent Surface from resetting HDR metadata that was set on a bufer when
491     // HDR metadata is not set on this Surface.
492     uint32_t mHdrMetadataIsSet{0};
493 
494     // mCrop is the crop rectangle that will be used for the next buffer
495     // that gets queued. It is set by calling setCrop.
496     Rect mCrop;
497 
498     // mScalingMode is the scaling mode that will be used for the next
499     // buffers that get queued. It is set by calling setScalingMode.
500     int mScalingMode;
501 
502     // mTransform is the transform identifier that will be used for the next
503     // buffer that gets queued. It is set by calling setTransform.
504     uint32_t mTransform;
505 
506     // mStickyTransform is a transform that is applied on top of mTransform
507     // in each buffer that is queued.  This is typically used to force the
508     // compositor to apply a transform, and will prevent the transform hint
509     // from being set by the compositor.
510     uint32_t mStickyTransform;
511 
512     // mDefaultWidth is default width of the buffers, regardless of the
513     // native_window_set_buffers_dimensions call.
514     uint32_t mDefaultWidth;
515 
516     // mDefaultHeight is default height of the buffers, regardless of the
517     // native_window_set_buffers_dimensions call.
518     uint32_t mDefaultHeight;
519 
520     // mUserWidth, if non-zero, is an application-specified override
521     // of mDefaultWidth.  This is lower priority than the width set by
522     // native_window_set_buffers_dimensions.
523     uint32_t mUserWidth;
524 
525     // mUserHeight, if non-zero, is an application-specified override
526     // of mDefaultHeight.  This is lower priority than the height set
527     // by native_window_set_buffers_dimensions.
528     uint32_t mUserHeight;
529 
530     // mTransformHint is the transform probably applied to buffers of this
531     // window. this is only a hint, actual transform may differ.
532     uint32_t mTransformHint;
getTransformHint()533     virtual uint32_t getTransformHint() const { return mTransformHint; }
534     bool transformToDisplayInverse() const;
535 
536     // mProducerControlledByApp whether this buffer producer is controlled
537     // by the application
538     bool mProducerControlledByApp;
539 
540     // mSwapIntervalZero set if we should drop buffers at queue() time to
541     // achieve an asynchronous swap interval
542     bool mSwapIntervalZero;
543 
544     // mConsumerRunningBehind whether the consumer is running more than
545     // one buffer behind the producer.
546     mutable bool mConsumerRunningBehind;
547 
548     // mMutex is the mutex used to prevent concurrent access to the member
549     // variables of Surface objects. It must be locked whenever the
550     // member variables are accessed.
551     mutable Mutex mMutex;
552 
553     // mInterceptorMutex is the mutex guarding interceptors.
554     mutable std::shared_mutex mInterceptorMutex;
555 
556     ANativeWindow_cancelBufferInterceptor mCancelInterceptor = nullptr;
557     void* mCancelInterceptorData = nullptr;
558     ANativeWindow_dequeueBufferInterceptor mDequeueInterceptor = nullptr;
559     void* mDequeueInterceptorData = nullptr;
560     ANativeWindow_performInterceptor mPerformInterceptor = nullptr;
561     void* mPerformInterceptorData = nullptr;
562     ANativeWindow_queueBufferInterceptor mQueueInterceptor = nullptr;
563     void* mQueueInterceptorData = nullptr;
564     ANativeWindow_queryInterceptor mQueryInterceptor = nullptr;
565     void* mQueryInterceptorData = nullptr;
566 
567     // must be used from the lock/unlock thread
568     sp<GraphicBuffer>           mLockedBuffer;
569     sp<GraphicBuffer>           mPostedBuffer;
570     bool                        mConnectedToCpu;
571 
572     // When a CPU producer is attached, this reflects the region that the
573     // producer wished to update as well as whether the Surface was able to copy
574     // the previous buffer back to allow a partial update.
575     //
576     // When a non-CPU producer is attached, this reflects the surface damage
577     // (the change since the previous frame) passed in by the producer.
578     Region mDirtyRegion;
579 
580     // mBufferAge tracks the age of the contents of the most recently dequeued
581     // buffer as the number of frames that have elapsed since it was last queued
582     uint64_t mBufferAge;
583 
584     // Stores the current generation number. See setGenerationNumber and
585     // IGraphicBufferProducer::setGenerationNumber for more information.
586     uint32_t mGenerationNumber;
587 
588     // Caches the values that have been passed to the producer.
589     bool mSharedBufferMode;
590     bool mAutoRefresh;
591     bool mAutoPrerotation;
592 
593     // If in shared buffer mode and auto refresh is enabled, store the shared
594     // buffer slot and return it for all calls to queue/dequeue without going
595     // over Binder.
596     int mSharedBufferSlot;
597 
598     // This is true if the shared buffer has already been queued/canceled. It's
599     // used to prevent a mismatch between the number of queue/dequeue calls.
600     bool mSharedBufferHasBeenQueued;
601 
602     // These are used to satisfy the NATIVE_WINDOW_LAST_*_DURATION queries
603     nsecs_t mLastDequeueDuration = 0;
604     nsecs_t mLastQueueDuration = 0;
605 
606     // Stores the time right before we call IGBP::dequeueBuffer
607     nsecs_t mLastDequeueStartTime = 0;
608 
609     Condition mQueueBufferCondition;
610 
611     uint64_t mNextFrameNumber = 1;
612     uint64_t mLastFrameNumber = 0;
613 
614     // Mutable because ANativeWindow::query needs this class const.
615     mutable bool mQueriedSupportedTimestamps;
616     mutable bool mFrameTimestampsSupportsPresent;
617 
618     // A cached copy of the FrameEventHistory maintained by the consumer.
619     bool mEnableFrameTimestamps = false;
620     std::unique_ptr<ProducerFrameEventHistory> mFrameEventHistory;
621 
622     // Reference to the SurfaceFlinger layer that was used to create this
623     // surface. This is only populated when the Surface is created from
624     // a BlastBufferQueue.
625     sp<IBinder> mSurfaceControlHandle;
626 
627     bool mReportRemovedBuffers = false;
628     std::vector<sp<GraphicBuffer>> mRemovedBuffers;
629     int mMaxBufferCount;
630 
631     sp<IProducerListener> mListenerProxy;
632 
633     // Get and flush the buffers of given slots, if the buffer in the slot
634     // is currently dequeued then it won't be flushed and won't be returned
635     // in outBuffers.
636     status_t getAndFlushBuffersFromSlots(const std::vector<int32_t>& slots,
637             std::vector<sp<GraphicBuffer>>* outBuffers);
638 
639     // Buffers that are successfully dequeued/attached and handed to clients
640     std::unordered_set<int> mDequeuedSlots;
641 };
642 
643 } // namespace android
644 
645 #endif  // ANDROID_GUI_SURFACE_H
646