Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 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" |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 19 | //#define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 20 | #include <utils/Log.h> |
| 21 | |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 22 | #include <inttypes.h> |
| 23 | |
Dan Stoza | dd26416 | 2015-03-12 13:58:47 -0700 | [diff] [blame] | 24 | #include <gui/BufferItem.h> |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 25 | #include <gui/BufferItemConsumer.h> |
| 26 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 27 | #define BI_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 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__) |
Dan Albert | 8b49125 | 2014-09-08 18:53:39 -0700 | [diff] [blame] | 31 | #define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 35 | BufferItemConsumer::BufferItemConsumer( |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 36 | const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage, |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 37 | int bufferCount, bool controlledByApp) : |
| 38 | ConsumerBase(consumer, controlledByApp) |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 39 | { |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 40 | status_t err = mConsumer->setConsumerUsageBits(consumerUsage); |
| 41 | LOG_ALWAYS_FATAL_IF(err != OK, |
Chia-I Wu | e2786ea | 2017-08-07 10:36:08 -0700 | [diff] [blame] | 42 | "Failed to set consumer usage bits to %#" PRIx64, consumerUsage); |
Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 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); |
Igor Murashkin | 4be18a3 | 2013-11-18 12:26:56 -0800 | [diff] [blame] | 47 | } |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 50 | BufferItemConsumer::~BufferItemConsumer() {} |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 51 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 52 | void BufferItemConsumer::setBufferFreedListener( |
| 53 | const wp<BufferFreedListener>& listener) { |
| 54 | Mutex::Autolock _l(mMutex); |
| 55 | mBufferFreedListener = listener; |
| 56 | } |
| 57 | |
Dan Stoza | 5471631 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 58 | status_t BufferItemConsumer::acquireBuffer(BufferItem *item, |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 59 | nsecs_t presentWhen, bool waitForFence) { |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 60 | status_t err; |
| 61 | |
| 62 | if (!item) return BAD_VALUE; |
| 63 | |
| 64 | Mutex::Autolock _l(mMutex); |
| 65 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 66 | err = acquireBufferLocked(item, presentWhen); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 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 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 74 | if (waitForFence) { |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 75 | err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer"); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 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 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 83 | item->mGraphicBuffer = mSlots[item->mSlot].mGraphicBuffer; |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 84 | |
| 85 | return OK; |
| 86 | } |
| 87 | |
| 88 | status_t BufferItemConsumer::releaseBuffer(const BufferItem &item, |
| 89 | const sp<Fence>& releaseFence) { |
| 90 | status_t err; |
| 91 | |
| 92 | Mutex::Autolock _l(mMutex); |
| 93 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 94 | err = addReleaseFenceLocked(item.mSlot, item.mGraphicBuffer, releaseFence); |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 95 | |
Pablo Ceballos | 47650f4 | 2015-08-04 16:38:17 -0700 | [diff] [blame] | 96 | err = releaseBufferLocked(item.mSlot, item.mGraphicBuffer, EGL_NO_DISPLAY, |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 97 | EGL_NO_SYNC_KHR); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 98 | if (err != OK) { |
| 99 | BI_LOGE("Failed to release buffer: %s (%d)", |
| 100 | strerror(-err), err); |
| 101 | } |
| 102 | return err; |
| 103 | } |
| 104 | |
Jiwen 'Steve' Cai | 021d92e | 2017-03-16 21:07:00 -0700 | [diff] [blame] | 105 | void BufferItemConsumer::freeBufferLocked(int slotIndex) { |
| 106 | sp<BufferFreedListener> listener = mBufferFreedListener.promote(); |
| 107 | if (listener != NULL && mSlots[slotIndex].mGraphicBuffer != NULL) { |
| 108 | // Fire callback if we have a listener registered and the buffer being freed is valid. |
| 109 | BI_LOGV("actually calling onBufferFreed"); |
| 110 | listener->onBufferFreed(mSlots[slotIndex].mGraphicBuffer); |
| 111 | } |
| 112 | ConsumerBase::freeBufferLocked(slotIndex); |
| 113 | } |
| 114 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 115 | } // namespace android |