| 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 | 3be1c6b | 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 |  | 
|  | 22 | #include <gui/BufferItemConsumer.h> | 
|  | 23 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 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__) | 
| Dan Albert | 8b49125 | 2014-09-08 18:53:39 -0700 | [diff] [blame] | 28 | #define BI_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
| Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 32 | BufferItemConsumer::BufferItemConsumer( | 
|  | 33 | const sp<IGraphicBufferConsumer>& consumer, uint32_t consumerUsage, | 
|  | 34 | int bufferCount, bool controlledByApp) : | 
|  | 35 | ConsumerBase(consumer, controlledByApp) | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 36 | { | 
| Dan Stoza | fe50d2a | 2014-03-12 14:32:29 -0700 | [diff] [blame] | 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); | 
| Igor Murashkin | 4be18a3 | 2013-11-18 12:26:56 -0800 | [diff] [blame] | 44 | } | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 47 | BufferItemConsumer::~BufferItemConsumer() {} | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | void BufferItemConsumer::setName(const String8& name) { | 
|  | 50 | Mutex::Autolock _l(mMutex); | 
|  | 51 | mName = name; | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 52 | mConsumer->setConsumerName(name); | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 55 | status_t BufferItemConsumer::acquireBuffer(BufferItem *item, | 
|  | 56 | nsecs_t presentWhen, bool waitForFence) { | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 57 | status_t err; | 
|  | 58 |  | 
|  | 59 | if (!item) return BAD_VALUE; | 
|  | 60 |  | 
|  | 61 | Mutex::Autolock _l(mMutex); | 
|  | 62 |  | 
| Andy McFadden | 1585c4d | 2013-06-28 13:52:40 -0700 | [diff] [blame] | 63 | err = acquireBufferLocked(item, presentWhen); | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 64 | if (err != OK) { | 
|  | 65 | if (err != NO_BUFFER_AVAILABLE) { | 
|  | 66 | BI_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err); | 
|  | 67 | } | 
|  | 68 | return err; | 
|  | 69 | } | 
|  | 70 |  | 
| Jamie Gennis | 1df8c34 | 2012-12-20 14:05:45 -0800 | [diff] [blame] | 71 | if (waitForFence) { | 
| Mathias Agopian | ea74d3b | 2013-05-16 18:03:22 -0700 | [diff] [blame] | 72 | err = item->mFence->waitForever("BufferItemConsumer::acquireBuffer"); | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 73 | if (err != OK) { | 
|  | 74 | BI_LOGE("Failed to wait for fence of acquired buffer: %s (%d)", | 
|  | 75 | strerror(-err), err); | 
|  | 76 | return err; | 
|  | 77 | } | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | item->mGraphicBuffer = mSlots[item->mBuf].mGraphicBuffer; | 
|  | 81 |  | 
|  | 82 | return OK; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | status_t BufferItemConsumer::releaseBuffer(const BufferItem &item, | 
|  | 86 | const sp<Fence>& releaseFence) { | 
|  | 87 | status_t err; | 
|  | 88 |  | 
|  | 89 | Mutex::Autolock _l(mMutex); | 
|  | 90 |  | 
| Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 91 | err = addReleaseFenceLocked(item.mBuf, item.mGraphicBuffer, releaseFence); | 
| Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 92 |  | 
| Lajos Molnar | c5d7b7d | 2013-05-03 14:50:50 -0700 | [diff] [blame] | 93 | err = releaseBufferLocked(item.mBuf, item.mGraphicBuffer, EGL_NO_DISPLAY, | 
| Jamie Gennis | b272541 | 2012-09-05 20:09:05 -0700 | [diff] [blame] | 94 | EGL_NO_SYNC_KHR); | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 95 | if (err != OK) { | 
|  | 96 | BI_LOGE("Failed to release buffer: %s (%d)", | 
|  | 97 | strerror(-err), err); | 
|  | 98 | } | 
|  | 99 | return err; | 
|  | 100 | } | 
|  | 101 |  | 
| Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 102 | status_t BufferItemConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { | 
|  | 103 | Mutex::Autolock _l(mMutex); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 104 | return mConsumer->setDefaultBufferSize(w, h); | 
| Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 107 | status_t BufferItemConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) { | 
| Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 108 | Mutex::Autolock _l(mMutex); | 
| Mathias Agopian | db89edc | 2013-08-02 01:40:18 -0700 | [diff] [blame] | 109 | return mConsumer->setDefaultBufferFormat(defaultFormat); | 
| Igor Murashkin | 87d1e34 | 2013-04-16 11:24:40 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Eino-Ville Talvala | e232fdc | 2012-08-21 13:37:35 -0700 | [diff] [blame] | 112 | } // namespace android |