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 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 50 BufferItemConsumer::~BufferItemConsumer() {} 51 52 void BufferItemConsumer::setName(const String8& name) { 53 Mutex::Autolock _l(mMutex); 54 if (mAbandoned) { 55 BI_LOGE("setName: BufferItemConsumer is abandoned!"); 56 return; 57 } 58 mName = name; 59 mConsumer->setConsumerName(name); 60 } 61 62 void BufferItemConsumer::setBufferFreedListener( 63 const wp<BufferFreedListener>& listener) { 64 Mutex::Autolock _l(mMutex); 65 mBufferFreedListener = listener; 66 } 67 68 status_t BufferItemConsumer::acquireBuffer(BufferItem *item, 69 nsecs_t presentWhen, bool waitForFence) { 70 status_t err; 71 72 if (!item) return BAD_VALUE; 73 74 Mutex::Autolock _l(mMutex); 75 76 err = acquireBufferLocked(item, presentWhen); 77 if (err != OK) { 78 if (err != NO_BUFFER_AVAILABLE) { 79 BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); 80 } 81 return err; 82 } 83 84 if (waitForFence) { 85 err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer"); 86 if (err != OK) { 87 BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", 88 strerror(-err), err); 89 return err; 90 } 91 } 92 93 item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer; 94 95 return OK; 96 } 97 98 status_t BufferItemConsumer::releaseBuffer(const BufferItem &item, 99 const sp<Fence>& releaseFence) { 100 status_t err; 101 102 Mutex::Autolock _l(mMutex); 103 104 err = addReleaseFenceLocked(item.mSlot, item.mGraphicBuffer, releaseFence); 105 106 err = releaseBufferLocked(item.mSlot, item.mGraphicBuffer, EGL_NO_DISPLAY, 107 EGL_NO_SYNC_KHR); 108 if (err != OK) { 109 BI_LOGE("Failed to release buffer: %s (%d)", 110 strerror(-err), err); 111 } 112 return err; 113 } 114 115 void BufferItemConsumer::freeBufferLocked(int slotIndex) { 116 sp<BufferFreedListener> listener = mBufferFreedListener.promote(); 117 if (listener != NULL && mSlots[slotIndex].mGraphicBuffer != NULL) { 118 // Fire callback if we have a listener registered and the buffer being freed is valid. 119 BI_LOGV("actually calling onBufferFreed"); 120 listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer); 121 } 122 ConsumerBase::freeBufferLocked(slotIndex); 123 } 124 125 } // namespace android 126