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