1 /*
2 * Copyright 2014 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 #include "MessageQueue.h"
18 #include "MonitoredProducer.h"
19 #include "SurfaceFlinger.h"
20 #include "Layer.h"
21
22 namespace android {
23
MonitoredProducer(const sp<IGraphicBufferProducer> & producer,const sp<SurfaceFlinger> & flinger,const wp<Layer> & layer)24 MonitoredProducer::MonitoredProducer(const sp<IGraphicBufferProducer>& producer,
25 const sp<SurfaceFlinger>& flinger,
26 const wp<Layer>& layer) :
27 mProducer(producer),
28 mFlinger(flinger),
29 mLayer(layer) {}
30
~MonitoredProducer()31 MonitoredProducer::~MonitoredProducer() {
32 // Remove ourselves from SurfaceFlinger's list. We do this asynchronously
33 // because we don't know where this destructor is called from. It could be
34 // called with the mStateLock held, leading to a dead-lock (it actually
35 // happens).
36 class MessageCleanUpList : public MessageBase {
37 public:
38 MessageCleanUpList(const sp<SurfaceFlinger>& flinger,
39 const wp<IBinder>& producer)
40 : mFlinger(flinger), mProducer(producer) {}
41
42 virtual ~MessageCleanUpList() {}
43
44 virtual bool handler() {
45 Mutex::Autolock _l(mFlinger->mStateLock);
46 mFlinger->mGraphicBufferProducerList.remove(mProducer);
47 return true;
48 }
49
50 private:
51 sp<SurfaceFlinger> mFlinger;
52 wp<IBinder> mProducer;
53 };
54
55 mFlinger->postMessageAsync(new MessageCleanUpList(mFlinger, asBinder(mProducer)));
56 }
57
requestBuffer(int slot,sp<GraphicBuffer> * buf)58 status_t MonitoredProducer::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
59 return mProducer->requestBuffer(slot, buf);
60 }
61
setMaxDequeuedBufferCount(int maxDequeuedBuffers)62 status_t MonitoredProducer::setMaxDequeuedBufferCount(
63 int maxDequeuedBuffers) {
64 return mProducer->setMaxDequeuedBufferCount(maxDequeuedBuffers);
65 }
66
setAsyncMode(bool async)67 status_t MonitoredProducer::setAsyncMode(bool async) {
68 return mProducer->setAsyncMode(async);
69 }
70
dequeueBuffer(int * slot,sp<Fence> * fence,uint32_t w,uint32_t h,PixelFormat format,uint64_t usage,uint64_t * outBufferAge,FrameEventHistoryDelta * outTimestamps)71 status_t MonitoredProducer::dequeueBuffer(int* slot, sp<Fence>* fence, uint32_t w, uint32_t h,
72 PixelFormat format, uint64_t usage,
73 uint64_t* outBufferAge,
74 FrameEventHistoryDelta* outTimestamps) {
75 return mProducer->dequeueBuffer(slot, fence, w, h, format, usage, outBufferAge, outTimestamps);
76 }
77
detachBuffer(int slot)78 status_t MonitoredProducer::detachBuffer(int slot) {
79 return mProducer->detachBuffer(slot);
80 }
81
detachNextBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence)82 status_t MonitoredProducer::detachNextBuffer(sp<GraphicBuffer>* outBuffer,
83 sp<Fence>* outFence) {
84 return mProducer->detachNextBuffer(outBuffer, outFence);
85 }
86
attachBuffer(int * outSlot,const sp<GraphicBuffer> & buffer)87 status_t MonitoredProducer::attachBuffer(int* outSlot,
88 const sp<GraphicBuffer>& buffer) {
89 return mProducer->attachBuffer(outSlot, buffer);
90 }
91
queueBuffer(int slot,const QueueBufferInput & input,QueueBufferOutput * output)92 status_t MonitoredProducer::queueBuffer(int slot, const QueueBufferInput& input,
93 QueueBufferOutput* output) {
94 return mProducer->queueBuffer(slot, input, output);
95 }
96
cancelBuffer(int slot,const sp<Fence> & fence)97 status_t MonitoredProducer::cancelBuffer(int slot, const sp<Fence>& fence) {
98 return mProducer->cancelBuffer(slot, fence);
99 }
100
query(int what,int * value)101 int MonitoredProducer::query(int what, int* value) {
102 return mProducer->query(what, value);
103 }
104
connect(const sp<IProducerListener> & listener,int api,bool producerControlledByApp,QueueBufferOutput * output)105 status_t MonitoredProducer::connect(const sp<IProducerListener>& listener,
106 int api, bool producerControlledByApp, QueueBufferOutput* output) {
107 return mProducer->connect(listener, api, producerControlledByApp, output);
108 }
109
disconnect(int api,DisconnectMode mode)110 status_t MonitoredProducer::disconnect(int api, DisconnectMode mode) {
111 return mProducer->disconnect(api, mode);
112 }
113
setSidebandStream(const sp<NativeHandle> & stream)114 status_t MonitoredProducer::setSidebandStream(const sp<NativeHandle>& stream) {
115 return mProducer->setSidebandStream(stream);
116 }
117
allocateBuffers(uint32_t width,uint32_t height,PixelFormat format,uint64_t usage)118 void MonitoredProducer::allocateBuffers(uint32_t width, uint32_t height,
119 PixelFormat format, uint64_t usage) {
120 mProducer->allocateBuffers(width, height, format, usage);
121 }
122
allowAllocation(bool allow)123 status_t MonitoredProducer::allowAllocation(bool allow) {
124 return mProducer->allowAllocation(allow);
125 }
126
setGenerationNumber(uint32_t generationNumber)127 status_t MonitoredProducer::setGenerationNumber(uint32_t generationNumber) {
128 return mProducer->setGenerationNumber(generationNumber);
129 }
130
getConsumerName() const131 String8 MonitoredProducer::getConsumerName() const {
132 return mProducer->getConsumerName();
133 }
134
setSharedBufferMode(bool sharedBufferMode)135 status_t MonitoredProducer::setSharedBufferMode(bool sharedBufferMode) {
136 return mProducer->setSharedBufferMode(sharedBufferMode);
137 }
138
setAutoRefresh(bool autoRefresh)139 status_t MonitoredProducer::setAutoRefresh(bool autoRefresh) {
140 return mProducer->setAutoRefresh(autoRefresh);
141 }
142
setDequeueTimeout(nsecs_t timeout)143 status_t MonitoredProducer::setDequeueTimeout(nsecs_t timeout) {
144 return mProducer->setDequeueTimeout(timeout);
145 }
146
getLastQueuedBuffer(sp<GraphicBuffer> * outBuffer,sp<Fence> * outFence,float outTransformMatrix[16])147 status_t MonitoredProducer::getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
148 sp<Fence>* outFence, float outTransformMatrix[16]) {
149 return mProducer->getLastQueuedBuffer(outBuffer, outFence,
150 outTransformMatrix);
151 }
152
getFrameTimestamps(FrameEventHistoryDelta * outDelta)153 void MonitoredProducer::getFrameTimestamps(FrameEventHistoryDelta* outDelta) {
154 mProducer->getFrameTimestamps(outDelta);
155 }
156
getUniqueId(uint64_t * outId) const157 status_t MonitoredProducer::getUniqueId(uint64_t* outId) const {
158 return mProducer->getUniqueId(outId);
159 }
160
getConsumerUsage(uint64_t * outUsage) const161 status_t MonitoredProducer::getConsumerUsage(uint64_t* outUsage) const {
162 return mProducer->getConsumerUsage(outUsage);
163 }
164
onAsBinder()165 IBinder* MonitoredProducer::onAsBinder() {
166 return this;
167 }
168
getLayer() const169 sp<Layer> MonitoredProducer::getLayer() const {
170 return mLayer.promote();
171 }
172
173 // ---------------------------------------------------------------------------
174 }; // namespace android
175