blob: 349104381129dbcd6425275b18b9c6d5d745d2b2 [file] [log] [blame]
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -07001/*
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"
Dan Stozad723bd72014-11-18 10:24:03 -080019//#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070020#include <utils/Log.h>
21
Dan Stozadd264162015-03-12 13:58:47 -070022#include <gui/BufferItem.h>
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070023#include <gui/BufferItemConsumer.h>
24
Dan Stozad723bd72014-11-18 10:24:03 -080025//#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__)
Dan Albert8b491252014-09-08 18:53:39 -070029#define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070030
31namespace android {
32
Dan Stozafe50d2a2014-03-12 14:32:29 -070033BufferItemConsumer::BufferItemConsumer(
34 const sp<IGraphicBufferConsumer>& consumer, uint32_t consumerUsage,
35 int bufferCount, bool controlledByApp) :
36 ConsumerBase(consumer, controlledByApp)
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070037{
Dan Stozafe50d2a2014-03-12 14:32:29 -070038 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);
Igor Murashkin4be18a32013-11-18 12:26:56 -080045 }
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070046}
47
Dan Stozad723bd72014-11-18 10:24:03 -080048BufferItemConsumer::~BufferItemConsumer() {}
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070049
50void BufferItemConsumer::setName(const String8& name) {
51 Mutex::Autolock _l(mMutex);
Pablo Ceballos65d9f6d2016-05-04 13:59:35 -070052 if (mAbandoned) {
53 BI_LOGE("setName: BufferItemConsumer is abandoned!");
54 return;
55 }
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070056 mName = name;
Mathias Agopiandb89edc2013-08-02 01:40:18 -070057 mConsumer->setConsumerName(name);
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070058}
59
Dan Stoza54716312015-03-13 14:40:34 -070060status_t BufferItemConsumer::acquireBuffer(BufferItem *item,
Andy McFadden1585c4d2013-06-28 13:52:40 -070061 nsecs_t presentWhen, bool waitForFence) {
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070062 status_t err;
63
64 if (!item) return BAD_VALUE;
65
66 Mutex::Autolock _l(mMutex);
67
Andy McFadden1585c4d2013-06-28 13:52:40 -070068 err = acquireBufferLocked(item, presentWhen);
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070069 if (err != OK) {
70 if (err != NO_BUFFER_AVAILABLE) {
71 BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
72 }
73 return err;
74 }
75
Jamie Gennis1df8c342012-12-20 14:05:45 -080076 if (waitForFence) {
Mathias Agopianea74d3b2013-05-16 18:03:22 -070077 err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer");
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070078 if (err != OK) {
79 BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)",
80 strerror(-err), err);
81 return err;
82 }
83 }
84
Pablo Ceballos47650f42015-08-04 16:38:17 -070085 item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer;
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -070086
87 return OK;
88}
89
90status_t BufferItemConsumer::releaseBuffer(const BufferItem &item,
91 const sp<Fence>& releaseFence) {
92 status_t err;
93
94 Mutex::Autolock _l(mMutex);
95
Pablo Ceballos47650f42015-08-04 16:38:17 -070096 err = addReleaseFenceLocked(item.mSlot, item.mGraphicBuffer, releaseFence);
Jamie Gennisb2725412012-09-05 20:09:05 -070097
Pablo Ceballos47650f42015-08-04 16:38:17 -070098 err = releaseBufferLocked(item.mSlot, item.mGraphicBuffer, EGL_NO_DISPLAY,
Jamie Gennisb2725412012-09-05 20:09:05 -070099 EGL_NO_SYNC_KHR);
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700100 if (err != OK) {
101 BI_LOGE("Failed to release buffer: %s (%d)",
102 strerror(-err), err);
103 }
104 return err;
105}
106
Eino-Ville Talvalae232fdc2012-08-21 13:37:35 -0700107} // namespace android