• 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 #include <inttypes.h>
18 
19 #define LOG_TAG "ConsumerBase"
20 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21 //#define LOG_NDEBUG 0
22 
23 #define EGL_EGLEXT_PROTOTYPES
24 
25 #include <EGL/egl.h>
26 #include <EGL/eglext.h>
27 
28 #include <hardware/hardware.h>
29 
30 #include <cutils/atomic.h>
31 
32 #include <gui/BufferItem.h>
33 #include <gui/ISurfaceComposer.h>
34 #include <gui/SurfaceComposerClient.h>
35 #include <gui/ConsumerBase.h>
36 
37 #include <private/gui/ComposerService.h>
38 
39 #include <utils/Log.h>
40 #include <utils/String8.h>
41 #include <utils/Trace.h>
42 
43 // Macros for including the ConsumerBase name in log messages
44 #define CB_LOGV(x, ...) ALOGV("[%s] " x, mName.c_str(), ##__VA_ARGS__)
45 // #define CB_LOGD(x, ...) ALOGD("[%s] " x, mName.c_str(), ##__VA_ARGS__)
46 // #define CB_LOGI(x, ...) ALOGI("[%s] " x, mName.c_str(), ##__VA_ARGS__)
47 // #define CB_LOGW(x, ...) ALOGW("[%s] " x, mName.c_str(), ##__VA_ARGS__)
48 #define CB_LOGE(x, ...) ALOGE("[%s] " x, mName.c_str(), ##__VA_ARGS__)
49 
50 namespace android {
51 
52 // Get an ID that's unique within this process.
createProcessUniqueId()53 static int32_t createProcessUniqueId() {
54     static volatile int32_t globalCounter = 0;
55     return android_atomic_inc(&globalCounter);
56 }
57 
ConsumerBase(const sp<IGraphicBufferConsumer> & bufferQueue,bool controlledByApp)58 ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
59         mAbandoned(false),
60         mConsumer(bufferQueue),
61         mPrevFinalReleaseFence(Fence::NO_FENCE) {
62     // Choose a name using the PID and a process-unique ID.
63     mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId());
64 
65     // Note that we can't create an sp<...>(this) in a ctor that will not keep a
66     // reference once the ctor ends, as that would cause the refcount of 'this'
67     // dropping to 0 at the end of the ctor.  Since all we need is a wp<...>
68     // that's what we create.
69     wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
70     sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);
71 
72     status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
73     if (err != NO_ERROR) {
74         CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)",
75                 strerror(-err), err);
76     } else {
77         mConsumer->setConsumerName(mName);
78     }
79 }
80 
~ConsumerBase()81 ConsumerBase::~ConsumerBase() {
82     CB_LOGV("~ConsumerBase");
83     Mutex::Autolock lock(mMutex);
84 
85     // Verify that abandon() has been called before we get here.  This should
86     // be done by ConsumerBase::onLastStrongRef(), but it's possible for a
87     // derived class to override that method and not call
88     // ConsumerBase::onLastStrongRef().
89     LOG_ALWAYS_FATAL_IF(!mAbandoned,
90                         "[%s] ~ConsumerBase was called, but the "
91                         "consumer is not abandoned!",
92                         mName.c_str());
93 }
94 
onLastStrongRef(const void * id)95 void ConsumerBase::onLastStrongRef(const void* id __attribute__((unused))) {
96     abandon();
97 }
98 
freeBufferLocked(int slotIndex)99 void ConsumerBase::freeBufferLocked(int slotIndex) {
100     CB_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
101     mSlots[slotIndex].mGraphicBuffer = nullptr;
102     mSlots[slotIndex].mFence = Fence::NO_FENCE;
103     mSlots[slotIndex].mFrameNumber = 0;
104 }
105 
onFrameDequeued(const uint64_t bufferId)106 void ConsumerBase::onFrameDequeued(const uint64_t bufferId) {
107     CB_LOGV("onFrameDequeued");
108 
109     sp<FrameAvailableListener> listener;
110     {
111         Mutex::Autolock lock(mFrameAvailableMutex);
112         listener = mFrameAvailableListener.promote();
113     }
114 
115     if (listener != nullptr) {
116         listener->onFrameDequeued(bufferId);
117     }
118 }
119 
onFrameCancelled(const uint64_t bufferId)120 void ConsumerBase::onFrameCancelled(const uint64_t bufferId) {
121     CB_LOGV("onFrameCancelled");
122 
123     sp<FrameAvailableListener> listener;
124     {
125         Mutex::Autolock lock(mFrameAvailableMutex);
126         listener = mFrameAvailableListener.promote();
127     }
128 
129     if (listener != nullptr) {
130         listener->onFrameCancelled(bufferId);
131     }
132 }
133 
onFrameDetached(const uint64_t bufferId)134 void ConsumerBase::onFrameDetached(const uint64_t bufferId) {
135     CB_LOGV("onFrameDetached");
136 
137     sp<FrameAvailableListener> listener;
138     {
139         Mutex::Autolock lock(mFrameAvailableMutex);
140         listener = mFrameAvailableListener.promote();
141     }
142 
143     if (listener != nullptr) {
144         listener->onFrameDetached(bufferId);
145     }
146 }
147 
onFrameAvailable(const BufferItem & item)148 void ConsumerBase::onFrameAvailable(const BufferItem& item) {
149     CB_LOGV("onFrameAvailable");
150 
151     sp<FrameAvailableListener> listener;
152     { // scope for the lock
153         Mutex::Autolock lock(mFrameAvailableMutex);
154         listener = mFrameAvailableListener.promote();
155     }
156 
157     if (listener != nullptr) {
158         CB_LOGV("actually calling onFrameAvailable");
159         listener->onFrameAvailable(item);
160     }
161 }
162 
onFrameReplaced(const BufferItem & item)163 void ConsumerBase::onFrameReplaced(const BufferItem &item) {
164     CB_LOGV("onFrameReplaced");
165 
166     sp<FrameAvailableListener> listener;
167     {
168         Mutex::Autolock lock(mFrameAvailableMutex);
169         listener = mFrameAvailableListener.promote();
170     }
171 
172     if (listener != nullptr) {
173         CB_LOGV("actually calling onFrameReplaced");
174         listener->onFrameReplaced(item);
175     }
176 }
177 
onBuffersReleased()178 void ConsumerBase::onBuffersReleased() {
179     Mutex::Autolock lock(mMutex);
180 
181     CB_LOGV("onBuffersReleased");
182 
183     if (mAbandoned) {
184         // Nothing to do if we're already abandoned.
185         return;
186     }
187 
188     uint64_t mask = 0;
189     mConsumer->getReleasedBuffers(&mask);
190     for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
191         if (mask & (1ULL << i)) {
192             freeBufferLocked(i);
193         }
194     }
195 }
196 
onSidebandStreamChanged()197 void ConsumerBase::onSidebandStreamChanged() {
198 }
199 
abandon()200 void ConsumerBase::abandon() {
201     CB_LOGV("abandon");
202     Mutex::Autolock lock(mMutex);
203 
204     if (!mAbandoned) {
205         abandonLocked();
206         mAbandoned = true;
207     }
208 }
209 
abandonLocked()210 void ConsumerBase::abandonLocked() {
211     CB_LOGV("abandonLocked");
212     if (mAbandoned) {
213         CB_LOGE("abandonLocked: ConsumerBase is abandoned!");
214         return;
215     }
216     for (int i =0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
217         freeBufferLocked(i);
218     }
219     // disconnect from the BufferQueue
220     mConsumer->consumerDisconnect();
221     mConsumer.clear();
222 }
223 
isAbandoned()224 bool ConsumerBase::isAbandoned() {
225     Mutex::Autolock _l(mMutex);
226     return mAbandoned;
227 }
228 
setName(const String8 & name)229 void ConsumerBase::setName(const String8& name) {
230     Mutex::Autolock _l(mMutex);
231     if (mAbandoned) {
232         CB_LOGE("setName: ConsumerBase is abandoned!");
233         return;
234     }
235     mName = name;
236     mConsumer->setConsumerName(name);
237 }
238 
setFrameAvailableListener(const wp<FrameAvailableListener> & listener)239 void ConsumerBase::setFrameAvailableListener(
240         const wp<FrameAvailableListener>& listener) {
241     CB_LOGV("setFrameAvailableListener");
242     Mutex::Autolock lock(mFrameAvailableMutex);
243     mFrameAvailableListener = listener;
244 }
245 
detachBuffer(int slot)246 status_t ConsumerBase::detachBuffer(int slot) {
247     CB_LOGV("detachBuffer");
248     Mutex::Autolock lock(mMutex);
249 
250     if (mAbandoned) {
251         CB_LOGE("detachBuffer: ConsumerBase is abandoned!");
252         return NO_INIT;
253     }
254 
255     status_t result = mConsumer->detachBuffer(slot);
256     if (result != NO_ERROR) {
257         CB_LOGE("Failed to detach buffer: %d", result);
258         return result;
259     }
260 
261     freeBufferLocked(slot);
262 
263     return result;
264 }
265 
setDefaultBufferSize(uint32_t width,uint32_t height)266 status_t ConsumerBase::setDefaultBufferSize(uint32_t width, uint32_t height) {
267     Mutex::Autolock _l(mMutex);
268     if (mAbandoned) {
269         CB_LOGE("setDefaultBufferSize: ConsumerBase is abandoned!");
270         return NO_INIT;
271     }
272     return mConsumer->setDefaultBufferSize(width, height);
273 }
274 
setDefaultBufferFormat(PixelFormat defaultFormat)275 status_t ConsumerBase::setDefaultBufferFormat(PixelFormat defaultFormat) {
276     Mutex::Autolock _l(mMutex);
277     if (mAbandoned) {
278         CB_LOGE("setDefaultBufferFormat: ConsumerBase is abandoned!");
279         return NO_INIT;
280     }
281     return mConsumer->setDefaultBufferFormat(defaultFormat);
282 }
283 
setDefaultBufferDataSpace(android_dataspace defaultDataSpace)284 status_t ConsumerBase::setDefaultBufferDataSpace(
285         android_dataspace defaultDataSpace) {
286     Mutex::Autolock _l(mMutex);
287     if (mAbandoned) {
288         CB_LOGE("setDefaultBufferDataSpace: ConsumerBase is abandoned!");
289         return NO_INIT;
290     }
291     return mConsumer->setDefaultBufferDataSpace(defaultDataSpace);
292 }
293 
setConsumerUsageBits(uint64_t usage)294 status_t ConsumerBase::setConsumerUsageBits(uint64_t usage) {
295     Mutex::Autolock lock(mMutex);
296     if (mAbandoned) {
297         CB_LOGE("setConsumerUsageBits: ConsumerBase is abandoned!");
298         return NO_INIT;
299     }
300     return mConsumer->setConsumerUsageBits(usage);
301 }
302 
setTransformHint(uint32_t hint)303 status_t ConsumerBase::setTransformHint(uint32_t hint) {
304     Mutex::Autolock lock(mMutex);
305     if (mAbandoned) {
306         CB_LOGE("setTransformHint: ConsumerBase is abandoned!");
307         return NO_INIT;
308     }
309     return mConsumer->setTransformHint(hint);
310 }
311 
setMaxAcquiredBufferCount(int maxAcquiredBuffers)312 status_t ConsumerBase::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
313     Mutex::Autolock lock(mMutex);
314     if (mAbandoned) {
315         CB_LOGE("setMaxAcquiredBufferCount: ConsumerBase is abandoned!");
316         return NO_INIT;
317     }
318     return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
319 }
320 
getSidebandStream() const321 sp<NativeHandle> ConsumerBase::getSidebandStream() const {
322     Mutex::Autolock _l(mMutex);
323     if (mAbandoned) {
324         CB_LOGE("getSidebandStream: ConsumerBase is abandoned!");
325         return nullptr;
326     }
327 
328     sp<NativeHandle> stream;
329     status_t err = mConsumer->getSidebandStream(&stream);
330     if (err != NO_ERROR) {
331         CB_LOGE("failed to get sideband stream: %d", err);
332         return nullptr;
333     }
334 
335     return stream;
336 }
337 
getOccupancyHistory(bool forceFlush,std::vector<OccupancyTracker::Segment> * outHistory)338 status_t ConsumerBase::getOccupancyHistory(bool forceFlush,
339         std::vector<OccupancyTracker::Segment>* outHistory) {
340     Mutex::Autolock _l(mMutex);
341     if (mAbandoned) {
342         CB_LOGE("getOccupancyHistory: ConsumerBase is abandoned!");
343         return NO_INIT;
344     }
345     return mConsumer->getOccupancyHistory(forceFlush, outHistory);
346 }
347 
discardFreeBuffers()348 status_t ConsumerBase::discardFreeBuffers() {
349     Mutex::Autolock _l(mMutex);
350     if (mAbandoned) {
351         CB_LOGE("discardFreeBuffers: ConsumerBase is abandoned!");
352         return NO_INIT;
353     }
354     status_t err = mConsumer->discardFreeBuffers();
355     if (err != OK) {
356         return err;
357     }
358     uint64_t mask;
359     mConsumer->getReleasedBuffers(&mask);
360     for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) {
361         if (mask & (1ULL << i)) {
362             freeBufferLocked(i);
363         }
364     }
365     return OK;
366 }
367 
dumpState(String8 & result) const368 void ConsumerBase::dumpState(String8& result) const {
369     dumpState(result, "");
370 }
371 
dumpState(String8 & result,const char * prefix) const372 void ConsumerBase::dumpState(String8& result, const char* prefix) const {
373     Mutex::Autolock _l(mMutex);
374     dumpLocked(result, prefix);
375 }
376 
dumpLocked(String8 & result,const char * prefix) const377 void ConsumerBase::dumpLocked(String8& result, const char* prefix) const {
378     result.appendFormat("%smAbandoned=%d\n", prefix, int(mAbandoned));
379 
380     if (!mAbandoned) {
381         String8 consumerState;
382         mConsumer->dumpState(String8(prefix), &consumerState);
383         result.append(consumerState);
384     }
385 }
386 
acquireBufferLocked(BufferItem * item,nsecs_t presentWhen,uint64_t maxFrameNumber)387 status_t ConsumerBase::acquireBufferLocked(BufferItem *item,
388         nsecs_t presentWhen, uint64_t maxFrameNumber) {
389     if (mAbandoned) {
390         CB_LOGE("acquireBufferLocked: ConsumerBase is abandoned!");
391         return NO_INIT;
392     }
393 
394     status_t err = mConsumer->acquireBuffer(item, presentWhen, maxFrameNumber);
395     if (err != NO_ERROR) {
396         return err;
397     }
398 
399     if (item->mGraphicBuffer != nullptr) {
400         if (mSlots[item->mSlot].mGraphicBuffer != nullptr) {
401             freeBufferLocked(item->mSlot);
402         }
403         mSlots[item->mSlot].mGraphicBuffer = item->mGraphicBuffer;
404     }
405 
406     mSlots[item->mSlot].mFrameNumber = item->mFrameNumber;
407     mSlots[item->mSlot].mFence = item->mFence;
408 
409     CB_LOGV("acquireBufferLocked: -> slot=%d/%" PRIu64,
410             item->mSlot, item->mFrameNumber);
411 
412     return OK;
413 }
414 
addReleaseFence(int slot,const sp<GraphicBuffer> graphicBuffer,const sp<Fence> & fence)415 status_t ConsumerBase::addReleaseFence(int slot,
416         const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
417     Mutex::Autolock lock(mMutex);
418     return addReleaseFenceLocked(slot, graphicBuffer, fence);
419 }
420 
addReleaseFenceLocked(int slot,const sp<GraphicBuffer> graphicBuffer,const sp<Fence> & fence)421 status_t ConsumerBase::addReleaseFenceLocked(int slot,
422         const sp<GraphicBuffer> graphicBuffer, const sp<Fence>& fence) {
423     CB_LOGV("addReleaseFenceLocked: slot=%d", slot);
424 
425     // If consumer no longer tracks this graphicBuffer, we can safely
426     // drop this fence, as it will never be received by the producer.
427     if (!stillTracking(slot, graphicBuffer)) {
428         return OK;
429     }
430 
431     if (!mSlots[slot].mFence.get()) {
432         mSlots[slot].mFence = fence;
433         return OK;
434     }
435 
436     // Check status of fences first because merging is expensive.
437     // Merging an invalid fence with any other fence results in an
438     // invalid fence.
439     auto currentStatus = mSlots[slot].mFence->getStatus();
440     if (currentStatus == Fence::Status::Invalid) {
441         CB_LOGE("Existing fence has invalid state");
442         return BAD_VALUE;
443     }
444 
445     auto incomingStatus = fence->getStatus();
446     if (incomingStatus == Fence::Status::Invalid) {
447         CB_LOGE("New fence has invalid state");
448         mSlots[slot].mFence = fence;
449         return BAD_VALUE;
450     }
451 
452     // If both fences are signaled or both are unsignaled, we need to merge
453     // them to get an accurate timestamp.
454     if (currentStatus == incomingStatus) {
455         char fenceName[32] = {};
456         snprintf(fenceName, 32, "%.28s:%d", mName.c_str(), slot);
457         sp<Fence> mergedFence = Fence::merge(
458                 fenceName, mSlots[slot].mFence, fence);
459         if (!mergedFence.get()) {
460             CB_LOGE("failed to merge release fences");
461             // synchronization is broken, the best we can do is hope fences
462             // signal in order so the new fence will act like a union
463             mSlots[slot].mFence = fence;
464             return BAD_VALUE;
465         }
466         mSlots[slot].mFence = mergedFence;
467     } else if (incomingStatus == Fence::Status::Unsignaled) {
468         // If one fence has signaled and the other hasn't, the unsignaled
469         // fence will approximately correspond with the correct timestamp.
470         // There's a small race if both fences signal at about the same time
471         // and their statuses are retrieved with unfortunate timing. However,
472         // by this point, they will have both signaled and only the timestamp
473         // will be slightly off; any dependencies after this point will
474         // already have been met.
475         mSlots[slot].mFence = fence;
476     }
477     // else if (currentStatus == Fence::Status::Unsignaled) is a no-op.
478 
479     return OK;
480 }
481 
releaseBufferLocked(int slot,const sp<GraphicBuffer> graphicBuffer,EGLDisplay display,EGLSyncKHR eglFence)482 status_t ConsumerBase::releaseBufferLocked(
483         int slot, const sp<GraphicBuffer> graphicBuffer,
484         EGLDisplay display, EGLSyncKHR eglFence) {
485     if (mAbandoned) {
486         CB_LOGE("releaseBufferLocked: ConsumerBase is abandoned!");
487         return NO_INIT;
488     }
489     // If consumer no longer tracks this graphicBuffer (we received a new
490     // buffer on the same slot), the buffer producer is definitely no longer
491     // tracking it.
492     if (!stillTracking(slot, graphicBuffer)) {
493         return OK;
494     }
495 
496     CB_LOGV("releaseBufferLocked: slot=%d/%" PRIu64,
497             slot, mSlots[slot].mFrameNumber);
498     status_t err = mConsumer->releaseBuffer(slot, mSlots[slot].mFrameNumber,
499             display, eglFence, mSlots[slot].mFence);
500     if (err == IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
501         freeBufferLocked(slot);
502     }
503 
504     mPrevFinalReleaseFence = mSlots[slot].mFence;
505     mSlots[slot].mFence = Fence::NO_FENCE;
506 
507     return err;
508 }
509 
stillTracking(int slot,const sp<GraphicBuffer> graphicBuffer)510 bool ConsumerBase::stillTracking(int slot,
511         const sp<GraphicBuffer> graphicBuffer) {
512     if (slot < 0 || slot >= BufferQueue::NUM_BUFFER_SLOTS) {
513         return false;
514     }
515     return (mSlots[slot].mGraphicBuffer != nullptr &&
516             mSlots[slot].mGraphicBuffer->handle == graphicBuffer->handle);
517 }
518 
519 } // namespace android
520