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" |
| 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 | |
| 30 | namespace android { |
| 31 | |
Mathias Agopian | 8f938a5 | 2013-07-12 22:06:26 -0700 | [diff] [blame] | 32 | BufferItemConsumer::BufferItemConsumer(const sp<BufferQueue>& bq, |
Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 33 | uint32_t consumerUsage, int bufferCount, bool controlledByApp) : |
| 34 | ConsumerBase(bq, controlledByApp) |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 35 | { |
Igor Murashkin | 4be18a3 | 2013-11-18 12:26:56 -0800 | [diff] [blame] | 36 | if (bufferCount == MIN_UNDEQUEUED_BUFFERS) { |
| 37 | status_t res; |
| 38 | res = bq->query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &bufferCount); |
| 39 | LOG_ALWAYS_FATAL_IF(res != OK || bufferCount < 0, |
| 40 | "Failed to query min buffer count"); |
| 41 | } |
| 42 | |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 43 | mConsumer->setConsumerUsageBits(consumerUsage); |
| 44 | mConsumer->setMaxAcquiredBufferCount(bufferCount); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | BufferItemConsumer::~BufferItemConsumer() { |
| 48 | } |
| 49 | |
| 50 | void BufferItemConsumer::setName(const String8& name) { |
| 51 | Mutex::Autolock _l(mMutex); |
| 52 | mName = name; |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 53 | mConsumer->setConsumerName(name); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 56 | status_t BufferItemConsumer::acquireBuffer(BufferItem *item, |
| 57 | nsecs_t presentWhen, bool waitForFence) { |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 58 | status_t err; |
| 59 | |
| 60 | if (!item) return BAD_VALUE; |
| 61 | |
| 62 | Mutex::Autolock _l(mMutex); |
| 63 | |
Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 64 | err = acquireBufferLocked(item, presentWhen); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 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 | |
Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 72 | if (waitForFence) { |
Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 73 | err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer"); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 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 | |
| 86 | status_t BufferItemConsumer::releaseBuffer(const BufferItem &item, |
| 87 | const sp<Fence>& releaseFence) { |
| 88 | status_t err; |
| 89 | |
| 90 | Mutex::Autolock _l(mMutex); |
| 91 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 92 | err = addReleaseFenceLocked(item.mBuf, item.mGraphicBuffer, releaseFence); |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 93 | |
Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 94 | err = releaseBufferLocked(item.mBuf, item.mGraphicBuffer, EGL_NO_DISPLAY, |
Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 95 | EGL_NO_SYNC_KHR); |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 96 | if (err != OK) { |
| 97 | BI_LOGE("Failed to release buffer: %s (%d)", |
| 98 | strerror(-err), err); |
| 99 | } |
| 100 | return err; |
| 101 | } |
| 102 | |
Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 103 | status_t BufferItemConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 104 | Mutex::Autolock _l(mMutex); |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 105 | return mConsumer->setDefaultBufferSize(w, h); |
Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | status_t BufferItemConsumer::setDefaultBufferFormat(uint32_t defaultFormat) { |
| 109 | Mutex::Autolock _l(mMutex); |
Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 110 | return mConsumer->setDefaultBufferFormat(defaultFormat); |
Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 113 | } // namespace android |