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