| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 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 |  | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 17 | #undef LOG_TAG | 
|  | 18 | #define LOG_TAG "BLASTBufferQueue" | 
|  | 19 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 20 | #include <gui/BLASTBufferQueue.h> | 
|  | 21 | #include <gui/BufferItemConsumer.h> | 
| Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 22 | #include <gui/GLConsumer.h> | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | #include <chrono> | 
|  | 25 |  | 
|  | 26 | using namespace std::chrono_literals; | 
|  | 27 |  | 
|  | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | BLASTBufferQueue::BLASTBufferQueue(const sp<SurfaceControl>& surface, int width, int height) | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 31 | : mSurfaceControl(surface), | 
|  | 32 | mPendingCallbacks(0), | 
|  | 33 | mWidth(width), | 
|  | 34 | mHeight(height), | 
|  | 35 | mNextTransaction(nullptr) { | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 36 | BufferQueue::createBufferQueue(&mProducer, &mConsumer); | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 37 | mConsumer->setMaxBufferCount(MAX_BUFFERS); | 
|  | 38 | mProducer->setMaxDequeuedBufferCount(MAX_BUFFERS - 1); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 39 | mBufferItemConsumer = | 
|  | 40 | new BufferItemConsumer(mConsumer, AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, 1, true); | 
|  | 41 | mBufferItemConsumer->setName(String8("BLAST Consumer")); | 
|  | 42 | mBufferItemConsumer->setFrameAvailableListener(this); | 
|  | 43 | mBufferItemConsumer->setBufferFreedListener(this); | 
|  | 44 | mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight); | 
|  | 45 | mBufferItemConsumer->setDefaultBufferFormat(PIXEL_FORMAT_RGBA_8888); | 
| Valerie Hau | 8cee3f9 | 2019-11-06 10:06:28 -0800 | [diff] [blame] | 46 | mBufferItemConsumer->setTransformHint(mSurfaceControl->getTransformHint()); | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 47 |  | 
|  | 48 | mAcquired = false; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | void BLASTBufferQueue::update(const sp<SurfaceControl>& surface, int width, int height) { | 
|  | 52 | std::unique_lock _lock{mMutex}; | 
|  | 53 | mSurfaceControl = surface; | 
|  | 54 | mWidth = width; | 
|  | 55 | mHeight = height; | 
|  | 56 | mBufferItemConsumer->setDefaultBufferSize(mWidth, mHeight); | 
| Valerie Hau | 8cee3f9 | 2019-11-06 10:06:28 -0800 | [diff] [blame] | 57 | mBufferItemConsumer->setTransformHint(mSurfaceControl->getTransformHint()); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | static void transactionCallbackThunk(void* context, nsecs_t latchTime, | 
|  | 61 | const sp<Fence>& presentFence, | 
|  | 62 | const std::vector<SurfaceControlStats>& stats) { | 
|  | 63 | if (context == nullptr) { | 
|  | 64 | return; | 
|  | 65 | } | 
|  | 66 | BLASTBufferQueue* bq = static_cast<BLASTBufferQueue*>(context); | 
|  | 67 | bq->transactionCallback(latchTime, presentFence, stats); | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | void BLASTBufferQueue::transactionCallback(nsecs_t /*latchTime*/, const sp<Fence>& /*presentFence*/, | 
|  | 71 | const std::vector<SurfaceControlStats>& stats) { | 
|  | 72 | std::unique_lock _lock{mMutex}; | 
|  | 73 |  | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 74 | if (stats.size() > 0 && !mShadowQueue.empty()) { | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 75 | mBufferItemConsumer->releaseBuffer(mNextCallbackBufferItem, | 
|  | 76 | stats[0].previousReleaseFence | 
|  | 77 | ? stats[0].previousReleaseFence | 
|  | 78 | : Fence::NO_FENCE); | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 79 | mAcquired = false; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 80 | mNextCallbackBufferItem = BufferItem(); | 
| Valerie Hau | 8cee3f9 | 2019-11-06 10:06:28 -0800 | [diff] [blame] | 81 | mBufferItemConsumer->setTransformHint(stats[0].transformHint); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 82 | } | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 83 | mPendingCallbacks--; | 
|  | 84 | processNextBufferLocked(); | 
|  | 85 | mCallbackCV.notify_all(); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 86 | decStrong((void*)transactionCallbackThunk); | 
|  | 87 | } | 
|  | 88 |  | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 89 | void BLASTBufferQueue::processNextBufferLocked() { | 
|  | 90 | if (mShadowQueue.empty()) { | 
|  | 91 | return; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | if (mAcquired) { | 
|  | 95 | return; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | BufferItem item = std::move(mShadowQueue.front()); | 
|  | 99 | mShadowQueue.pop(); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | SurfaceComposerClient::Transaction localTransaction; | 
|  | 102 | bool applyTransaction = true; | 
|  | 103 | SurfaceComposerClient::Transaction* t = &localTransaction; | 
|  | 104 | if (mNextTransaction != nullptr) { | 
|  | 105 | t = mNextTransaction; | 
|  | 106 | mNextTransaction = nullptr; | 
|  | 107 | applyTransaction = false; | 
|  | 108 | } | 
|  | 109 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 110 | mNextCallbackBufferItem = mLastSubmittedBufferItem; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 111 | mLastSubmittedBufferItem = BufferItem(); | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 112 |  | 
|  | 113 | status_t status = mBufferItemConsumer->acquireBuffer(&mLastSubmittedBufferItem, -1, false); | 
|  | 114 | mAcquired = true; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 115 | if (status != OK) { | 
|  | 116 | ALOGE("Failed to acquire?"); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | auto buffer = mLastSubmittedBufferItem.mGraphicBuffer; | 
|  | 120 |  | 
|  | 121 | if (buffer == nullptr) { | 
|  | 122 | ALOGE("Null buffer"); | 
|  | 123 | return; | 
|  | 124 | } | 
|  | 125 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 126 | // Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback. | 
|  | 127 | incStrong((void*)transactionCallbackThunk); | 
|  | 128 |  | 
|  | 129 | t->setBuffer(mSurfaceControl, buffer); | 
|  | 130 | t->setAcquireFence(mSurfaceControl, | 
|  | 131 | item.mFence ? new Fence(item.mFence->dup()) : Fence::NO_FENCE); | 
|  | 132 | t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this)); | 
|  | 133 |  | 
|  | 134 | t->setFrame(mSurfaceControl, {0, 0, (int32_t)buffer->getWidth(), (int32_t)buffer->getHeight()}); | 
| Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 135 | t->setCrop(mSurfaceControl, computeCrop(mLastSubmittedBufferItem)); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 136 |  | 
|  | 137 | if (applyTransaction) { | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 138 | t->apply(); | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
| Valerie Hau | 45e4b3b | 2019-12-03 10:49:17 -0800 | [diff] [blame] | 142 | Rect BLASTBufferQueue::computeCrop(const BufferItem& item) { | 
|  | 143 | if (item.mScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) { | 
|  | 144 | return GLConsumer::scaleDownCrop(item.mCrop, mWidth, mHeight); | 
|  | 145 | } | 
|  | 146 | return item.mCrop; | 
|  | 147 | } | 
|  | 148 |  | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 149 | void BLASTBufferQueue::onFrameAvailable(const BufferItem& item) { | 
|  | 150 | std::lock_guard _lock{mMutex}; | 
|  | 151 |  | 
|  | 152 | // add to shadow queue | 
|  | 153 | mShadowQueue.push(item); | 
|  | 154 | processNextBufferLocked(); | 
|  | 155 | mPendingCallbacks++; | 
|  | 156 | } | 
|  | 157 |  | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 158 | void BLASTBufferQueue::setNextTransaction(SurfaceComposerClient::Transaction* t) { | 
| Valerie Hau | d3b90d2 | 2019-11-06 09:37:31 -0800 | [diff] [blame] | 159 | std::lock_guard _lock{mMutex}; | 
| Robert Carr | 78c25dd | 2019-08-15 14:10:33 -0700 | [diff] [blame] | 160 | mNextTransaction = t; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | } // namespace android |