1 /*
2  * Copyright (C) 2013 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 #pragma once
18 
19 #include <binder/IInterface.h>
20 #include <binder/SafeInterface.h>
21 
22 #include <utils/Errors.h>
23 #include <utils/RefBase.h>
24 
25 #include <cstdint>
26 
27 #include <com_android_graphics_libgui_flags.h>
28 
29 namespace android {
30 
31 class BufferItem;
32 class FrameEventHistoryDelta;
33 struct NewFrameEventsEntry;
34 
35 // ConsumerListener is the interface through which the BufferQueue notifies the consumer of events
36 // that the consumer may wish to react to. Because the consumer will generally have a mutex that is
37 // locked during calls from the consumer to the BufferQueue, these calls from the BufferQueue to the
38 // consumer *MUST* be called only when the BufferQueue mutex is NOT locked.
39 
40 class ConsumerListener : public virtual RefBase {
41 public:
ConsumerListener()42     ConsumerListener() {}
43     virtual ~ConsumerListener();
44 
45     // onDisconnect is called when a producer disconnects from the BufferQueue.
onDisconnect()46     virtual void onDisconnect() {} /* Asynchronous */
47 
48     // onFrameDequeued is called when a call to the BufferQueueProducer::dequeueBuffer successfully
49     // returns a slot from the BufferQueue.
onFrameDequeued(const uint64_t)50     virtual void onFrameDequeued(const uint64_t) {}
51 
52     // onFrameCancelled is called when the client calls cancelBuffer, thereby releasing the slot
53     // back to the BufferQueue.
onFrameCancelled(const uint64_t)54     virtual void onFrameCancelled(const uint64_t) {}
55 
56     // onFrameDetached is called after a successful detachBuffer() call while in asynchronous mode.
onFrameDetached(const uint64_t)57     virtual void onFrameDetached(const uint64_t) {}
58 
59     // onFrameAvailable is called from queueBuffer each time an additional frame becomes available
60     // for consumption. This means that frames that are queued while in asynchronous mode only
61     // trigger the callback if no previous frames are pending. Frames queued while in synchronous
62     // mode always trigger the callback. The item passed to the callback will contain all of the
63     // information about the queued frame except for its GraphicBuffer pointer, which will always be
64     // null (except if the consumer is SurfaceFlinger).
65     //
66     // This is called without any lock held and can be called concurrently by multiple threads.
67     virtual void onFrameAvailable(const BufferItem& item) = 0; /* Asynchronous */
68 
69     // onFrameReplaced is called from queueBuffer if the frame being queued is replacing an existing
70     // slot in the queue. Any call to queueBuffer that doesn't call onFrameAvailable will call this
71     // callback instead. The item passed to the callback will contain all of the information about
72     // the queued frame except for its GraphicBuffer pointer, which will always be null.
73     //
74     // This is called without any lock held and can be called concurrently by multiple threads.
onFrameReplaced(const BufferItem &)75     virtual void onFrameReplaced(const BufferItem& /* item */) {} /* Asynchronous */
76 
77     // onBuffersReleased is called to notify the buffer consumer that the BufferQueue has released
78     // its references to one or more GraphicBuffers contained in its slots. The buffer consumer
79     // should then call BufferQueue::getReleasedBuffers to retrieve the list of buffers.
80     //
81     // This is called without any lock held and can be called concurrently by multiple threads.
82     virtual void onBuffersReleased() = 0; /* Asynchronous */
83 
84     // onSidebandStreamChanged is called to notify the buffer consumer that the BufferQueue's
85     // sideband buffer stream has changed. This is called when a stream is first attached and when
86     // it is either detached or replaced by a different stream.
87     virtual void onSidebandStreamChanged() = 0; /* Asynchronous */
88 
89     // Notifies the consumer of any new producer-side timestamps and returns the combined frame
90     // history that hasn't already been retrieved.
91     //
92     // WARNING: This method can only be called when the BufferQueue is in the consumer's process.
addAndGetFrameTimestamps(const NewFrameEventsEntry *,FrameEventHistoryDelta *)93     virtual void addAndGetFrameTimestamps(const NewFrameEventsEntry* /*newTimestamps*/,
94                                           FrameEventHistoryDelta* /*outDelta*/) {}
95 
96 #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BQ_SETFRAMERATE)
97     // Notifies the consumer of a setFrameRate call from the producer side.
onSetFrameRate(float,int8_t,int8_t)98     virtual void onSetFrameRate(float /*frameRate*/, int8_t /*compatibility*/,
99                                 int8_t /*changeFrameRateStrategy*/) {}
100 #endif
101 };
102 
103 #ifndef NO_BINDER
104 class IConsumerListener : public ConsumerListener, public IInterface {
105 public:
106     DECLARE_META_INTERFACE(ConsumerListener)
107 };
108 
109 class BnConsumerListener : public SafeBnInterface<IConsumerListener> {
110 public:
BnConsumerListener()111     BnConsumerListener() : SafeBnInterface<IConsumerListener>("BnConsumerListener") {}
112 
113     status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
114                         uint32_t flags = 0) override;
115 };
116 
117 #else
118 class IConsumerListener : public ConsumerListener {
119 };
120 class BnConsumerListener : public IConsumerListener {
121 };
122 #endif
123 
124 } // namespace android
125