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