1 /*
2  * Copyright (C) 2012 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 #define LOG_TAG "BufferQueue"
18 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19 //#define LOG_NDEBUG 0
20 
21 #ifndef NO_BUFFERHUB
22 #include <gui/BufferHubConsumer.h>
23 #include <gui/BufferHubProducer.h>
24 #endif
25 
26 #include <gui/BufferQueue.h>
27 #include <gui/BufferQueueConsumer.h>
28 #include <gui/BufferQueueCore.h>
29 #include <gui/BufferQueueProducer.h>
30 
31 namespace android {
32 
ProxyConsumerListener(const wp<ConsumerListener> & consumerListener)33 BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
34         const wp<ConsumerListener>& consumerListener):
35         mConsumerListener(consumerListener) {}
36 
~ProxyConsumerListener()37 BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
38 
onDisconnect()39 void BufferQueue::ProxyConsumerListener::onDisconnect() {
40     sp<ConsumerListener> listener(mConsumerListener.promote());
41     if (listener != nullptr) {
42         listener->onDisconnect();
43     }
44 }
45 
onFrameDequeued(const uint64_t bufferId)46 void BufferQueue::ProxyConsumerListener::onFrameDequeued(const uint64_t bufferId) {
47     sp<ConsumerListener> listener(mConsumerListener.promote());
48     if (listener != nullptr) {
49         listener->onFrameDequeued(bufferId);
50     }
51 }
52 
onFrameCancelled(const uint64_t bufferId)53 void BufferQueue::ProxyConsumerListener::onFrameCancelled(const uint64_t bufferId) {
54     sp<ConsumerListener> listener(mConsumerListener.promote());
55     if (listener != nullptr) {
56         listener->onFrameCancelled(bufferId);
57     }
58 }
59 
onFrameDetached(const uint64_t bufferId)60 void BufferQueue::ProxyConsumerListener::onFrameDetached(const uint64_t bufferId) {
61     sp<ConsumerListener> listener(mConsumerListener.promote());
62     if (listener != nullptr) {
63         listener->onFrameDetached(bufferId);
64     }
65 }
66 
onFrameAvailable(const BufferItem & item)67 void BufferQueue::ProxyConsumerListener::onFrameAvailable(
68         const BufferItem& item) {
69     sp<ConsumerListener> listener(mConsumerListener.promote());
70     if (listener != nullptr) {
71         listener->onFrameAvailable(item);
72     }
73 }
74 
onFrameReplaced(const BufferItem & item)75 void BufferQueue::ProxyConsumerListener::onFrameReplaced(
76         const BufferItem& item) {
77     sp<ConsumerListener> listener(mConsumerListener.promote());
78     if (listener != nullptr) {
79         listener->onFrameReplaced(item);
80     }
81 }
82 
onBuffersReleased()83 void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
84     sp<ConsumerListener> listener(mConsumerListener.promote());
85     if (listener != nullptr) {
86         listener->onBuffersReleased();
87     }
88 }
89 
onSidebandStreamChanged()90 void BufferQueue::ProxyConsumerListener::onSidebandStreamChanged() {
91     sp<ConsumerListener> listener(mConsumerListener.promote());
92     if (listener != nullptr) {
93         listener->onSidebandStreamChanged();
94     }
95 }
96 
addAndGetFrameTimestamps(const NewFrameEventsEntry * newTimestamps,FrameEventHistoryDelta * outDelta)97 void BufferQueue::ProxyConsumerListener::addAndGetFrameTimestamps(
98         const NewFrameEventsEntry* newTimestamps,
99         FrameEventHistoryDelta* outDelta) {
100     sp<ConsumerListener> listener(mConsumerListener.promote());
101     if (listener != nullptr) {
102         listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
103     }
104 }
105 
createBufferQueue(sp<IGraphicBufferProducer> * outProducer,sp<IGraphicBufferConsumer> * outConsumer,bool consumerIsSurfaceFlinger)106 void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
107         sp<IGraphicBufferConsumer>* outConsumer,
108         bool consumerIsSurfaceFlinger) {
109     LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
110             "BufferQueue: outProducer must not be NULL");
111     LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
112             "BufferQueue: outConsumer must not be NULL");
113 
114     sp<BufferQueueCore> core(new BufferQueueCore());
115     LOG_ALWAYS_FATAL_IF(core == nullptr,
116             "BufferQueue: failed to create BufferQueueCore");
117 
118     sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
119     LOG_ALWAYS_FATAL_IF(producer == nullptr,
120             "BufferQueue: failed to create BufferQueueProducer");
121 
122     sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
123     LOG_ALWAYS_FATAL_IF(consumer == nullptr,
124             "BufferQueue: failed to create BufferQueueConsumer");
125 
126     *outProducer = producer;
127     *outConsumer = consumer;
128 }
129 
130 #ifndef NO_BUFFERHUB
createBufferHubQueue(sp<IGraphicBufferProducer> * outProducer,sp<IGraphicBufferConsumer> * outConsumer)131 void BufferQueue::createBufferHubQueue(sp<IGraphicBufferProducer>* outProducer,
132                                        sp<IGraphicBufferConsumer>* outConsumer) {
133     LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BufferQueue: outProducer must not be NULL");
134     LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BufferQueue: outConsumer must not be NULL");
135 
136     sp<IGraphicBufferProducer> producer;
137     sp<IGraphicBufferConsumer> consumer;
138 
139     dvr::ProducerQueueConfigBuilder configBuilder;
140     std::shared_ptr<dvr::ProducerQueue> producerQueue =
141             dvr::ProducerQueue::Create(configBuilder.Build(), dvr::UsagePolicy{});
142     LOG_ALWAYS_FATAL_IF(producerQueue == nullptr, "BufferQueue: failed to create ProducerQueue.");
143 
144     std::shared_ptr<dvr::ConsumerQueue> consumerQueue = producerQueue->CreateConsumerQueue();
145     LOG_ALWAYS_FATAL_IF(consumerQueue == nullptr, "BufferQueue: failed to create ConsumerQueue.");
146 
147     producer = BufferHubProducer::Create(producerQueue);
148     consumer = BufferHubConsumer::Create(consumerQueue);
149 
150     LOG_ALWAYS_FATAL_IF(producer == nullptr, "BufferQueue: failed to create BufferQueueProducer");
151     LOG_ALWAYS_FATAL_IF(consumer == nullptr, "BufferQueue: failed to create BufferQueueConsumer");
152 
153     *outProducer = producer;
154     *outConsumer = consumer;
155 }
156 #endif
157 
158 }; // namespace android
159