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_NDEBUG 0
18 #define LOG_TAG "BufferItemConsumer"
19 //#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20 #include <utils/Log.h>
21 
22 #include <inttypes.h>
23 
24 #include <gui/BufferItem.h>
25 #include <gui/BufferItemConsumer.h>
26 
27 #define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
28 //#define BI_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
29 //#define BI_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__)
30 //#define BI_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
31 #define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
32 
33 namespace android {
34 
BufferItemConsumer(const sp<IGraphicBufferConsumer> & consumer,uint64_t consumerUsage,int bufferCount,bool controlledByApp)35 BufferItemConsumer::BufferItemConsumer(
36         const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
37         int bufferCount, bool controlledByApp) :
38     ConsumerBase(consumer, controlledByApp)
39 {
40     status_t err = mConsumer->setConsumerUsageBits(consumerUsage);
41     LOG_ALWAYS_FATAL_IF(err != OK,
42             "Failed to set consumer usage bits to %#" PRIx64, consumerUsage);
43     if (bufferCount != DEFAULT_MAX_BUFFERS) {
44         err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
45         LOG_ALWAYS_FATAL_IF(err != OK,
46                 "Failed to set max acquired buffer count to %d", bufferCount);
47     }
48 }
49 
~BufferItemConsumer()50 BufferItemConsumer::~BufferItemConsumer() {}
51 
setBufferFreedListener(const wp<BufferFreedListener> & listener)52 void BufferItemConsumer::setBufferFreedListener(
53         const wp<BufferFreedListener>& listener) {
54     Mutex::Autolock _l(mMutex);
55     mBufferFreedListener = listener;
56 }
57 
acquireBuffer(BufferItem * item,nsecs_t presentWhen,bool waitForFence)58 status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
59         nsecs_t presentWhen, bool waitForFence) {
60     status_t err;
61 
62     if (!item) return BAD_VALUE;
63 
64     Mutex::Autolock _l(mMutex);
65 
66     err = acquireBufferLocked(item, presentWhen);
67     if (err != OK) {
68         if (err != NO_BUFFER_AVAILABLE) {
69             BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
70         }
71         return err;
72     }
73 
74     if (waitForFence) {
75         err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
76         if (err != OK) {
77             BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
78                     strerror(-err), err);
79             return err;
80         }
81     }
82 
83     item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer;
84 
85     return OK;
86 }
87 
releaseBuffer(const BufferItem & item,const sp<Fence> & releaseFence)88 status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
89         const sp<Fence>& releaseFence) {
90     status_t err;
91 
92     Mutex::Autolock _l(mMutex);
93 
94     err = addReleaseFenceLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
95     if (err != OK) {
96         BI_LOGE("Failed to addReleaseFenceLocked");
97     }
98 
99     err = releaseBufferLocked(item.mSlot, item.mGraphicBuffer, EGL_NO_DISPLAY,
100             EGL_NO_SYNC_KHR);
101     if (err != OK && err != IGraphicBufferConsumer::STALE_BUFFER_SLOT) {
102         BI_LOGE("Failed to release buffer: %s (%d)",
103                 strerror(-err), err);
104     }
105     return err;
106 }
107 
freeBufferLocked(int slotIndex)108 void BufferItemConsumer::freeBufferLocked(int slotIndex) {
109     sp<BufferFreedListener> listener = mBufferFreedListener.promote();
110     if (listener != nullptr && mSlots[slotIndex].mGraphicBuffer != nullptr) {
111         // Fire callback if we have a listener registered and the buffer being freed is valid.
112         BI_LOGV("actually calling onBufferFreed");
113         listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer);
114     }
115     ConsumerBase::freeBufferLocked(slotIndex);
116 }
117 
118 } // namespace android
119