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 
onFrameAvailable(const BufferItem & item)46 void BufferQueue::ProxyConsumerListener::onFrameAvailable(
47         const BufferItem& item) {
48     sp<ConsumerListener> listener(mConsumerListener.promote());
49     if (listener != nullptr) {
50         listener->onFrameAvailable(item);
51     }
52 }
53 
onFrameReplaced(const BufferItem & item)54 void BufferQueue::ProxyConsumerListener::onFrameReplaced(
55         const BufferItem& item) {
56     sp<ConsumerListener> listener(mConsumerListener.promote());
57     if (listener != nullptr) {
58         listener->onFrameReplaced(item);
59     }
60 }
61 
onBuffersReleased()62 void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
63     sp<ConsumerListener> listener(mConsumerListener.promote());
64     if (listener != nullptr) {
65         listener->onBuffersReleased();
66     }
67 }
68 
onSidebandStreamChanged()69 void BufferQueue::ProxyConsumerListener::onSidebandStreamChanged() {
70     sp<ConsumerListener> listener(mConsumerListener.promote());
71     if (listener != nullptr) {
72         listener->onSidebandStreamChanged();
73     }
74 }
75 
addAndGetFrameTimestamps(const NewFrameEventsEntry * newTimestamps,FrameEventHistoryDelta * outDelta)76 void BufferQueue::ProxyConsumerListener::addAndGetFrameTimestamps(
77         const NewFrameEventsEntry* newTimestamps,
78         FrameEventHistoryDelta* outDelta) {
79     sp<ConsumerListener> listener(mConsumerListener.promote());
80     if (listener != nullptr) {
81         listener->addAndGetFrameTimestamps(newTimestamps, outDelta);
82     }
83 }
84 
createBufferQueue(sp<IGraphicBufferProducer> * outProducer,sp<IGraphicBufferConsumer> * outConsumer,bool consumerIsSurfaceFlinger)85 void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
86         sp<IGraphicBufferConsumer>* outConsumer,
87         bool consumerIsSurfaceFlinger) {
88     LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
89             "BufferQueue: outProducer must not be NULL");
90     LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
91             "BufferQueue: outConsumer must not be NULL");
92 
93     sp<BufferQueueCore> core(new BufferQueueCore());
94     LOG_ALWAYS_FATAL_IF(core == nullptr,
95             "BufferQueue: failed to create BufferQueueCore");
96 
97     sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
98     LOG_ALWAYS_FATAL_IF(producer == nullptr,
99             "BufferQueue: failed to create BufferQueueProducer");
100 
101     sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
102     LOG_ALWAYS_FATAL_IF(consumer == nullptr,
103             "BufferQueue: failed to create BufferQueueConsumer");
104 
105     *outProducer = producer;
106     *outConsumer = consumer;
107 }
108 
109 #ifndef NO_BUFFERHUB
createBufferHubQueue(sp<IGraphicBufferProducer> * outProducer,sp<IGraphicBufferConsumer> * outConsumer)110 void BufferQueue::createBufferHubQueue(sp<IGraphicBufferProducer>* outProducer,
111                                        sp<IGraphicBufferConsumer>* outConsumer) {
112     LOG_ALWAYS_FATAL_IF(outProducer == nullptr, "BufferQueue: outProducer must not be NULL");
113     LOG_ALWAYS_FATAL_IF(outConsumer == nullptr, "BufferQueue: outConsumer must not be NULL");
114 
115     sp<IGraphicBufferProducer> producer;
116     sp<IGraphicBufferConsumer> consumer;
117 
118     dvr::ProducerQueueConfigBuilder configBuilder;
119     std::shared_ptr<dvr::ProducerQueue> producerQueue =
120             dvr::ProducerQueue::Create(configBuilder.Build(), dvr::UsagePolicy{});
121     LOG_ALWAYS_FATAL_IF(producerQueue == nullptr, "BufferQueue: failed to create ProducerQueue.");
122 
123     std::shared_ptr<dvr::ConsumerQueue> consumerQueue = producerQueue->CreateConsumerQueue();
124     LOG_ALWAYS_FATAL_IF(consumerQueue == nullptr, "BufferQueue: failed to create ConsumerQueue.");
125 
126     producer = BufferHubProducer::Create(producerQueue);
127     consumer = BufferHubConsumer::Create(consumerQueue);
128 
129     LOG_ALWAYS_FATAL_IF(producer == nullptr, "BufferQueue: failed to create BufferQueueProducer");
130     LOG_ALWAYS_FATAL_IF(consumer == nullptr, "BufferQueue: failed to create BufferQueueConsumer");
131 
132     *outProducer = producer;
133     *outConsumer = consumer;
134 }
135 #endif
136 
137 }; // namespace android
138