Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "BufferLayerConsumer" |
| 23 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 24 | //#define LOG_NDEBUG 0 |
| 25 | |
| 26 | #include "BufferLayerConsumer.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 27 | #include "Layer.h" |
Ady Abraham | 8cb2188 | 2020-08-26 18:22:05 -0700 | [diff] [blame] | 28 | #include "Scheduler/VsyncController.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 29 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 30 | #include <inttypes.h> |
| 31 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 32 | #include <cutils/compiler.h> |
| 33 | |
| 34 | #include <hardware/hardware.h> |
| 35 | |
| 36 | #include <math/mat4.h> |
| 37 | |
| 38 | #include <gui/BufferItem.h> |
| 39 | #include <gui/GLConsumer.h> |
| 40 | #include <gui/ISurfaceComposer.h> |
| 41 | #include <gui/SurfaceComposerClient.h> |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 42 | #include <private/gui/ComposerService.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 43 | #include <renderengine/RenderEngine.h> |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 44 | #include <renderengine/impl/ExternalTexture.h> |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 45 | #include <utils/Log.h> |
| 46 | #include <utils/String8.h> |
| 47 | #include <utils/Trace.h> |
| 48 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
| 51 | // Macros for including the BufferLayerConsumer name in log messages |
| 52 | #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 53 | #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 54 | //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 55 | #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 56 | #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 57 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 58 | static const mat4 mtxIdentity; |
| 59 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 60 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 61 | renderengine::RenderEngine& engine, uint32_t tex, |
| 62 | Layer* layer) |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 63 | : ConsumerBase(bq, false), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 64 | mCurrentCrop(Rect::EMPTY_RECT), |
| 65 | mCurrentTransform(0), |
| 66 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 67 | mCurrentFence(Fence::NO_FENCE), |
| 68 | mCurrentTimestamp(0), |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 69 | mCurrentDataSpace(ui::Dataspace::UNKNOWN), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 70 | mCurrentFrameNumber(0), |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 71 | mCurrentTransformToDisplayInverse(false), |
| 72 | mCurrentSurfaceDamage(), |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 73 | mCurrentApi(0), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 74 | mDefaultWidth(1), |
| 75 | mDefaultHeight(1), |
| 76 | mFilteringEnabled(true), |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 77 | mRE(engine), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 78 | mTexName(tex), |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 79 | mLayer(layer), |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 80 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 81 | BLC_LOGV("BufferLayerConsumer"); |
| 82 | |
| 83 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 84 | |
| 85 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 86 | } |
| 87 | |
| 88 | status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 89 | Mutex::Autolock lock(mMutex); |
| 90 | if (mAbandoned) { |
| 91 | BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!"); |
| 92 | return NO_INIT; |
| 93 | } |
| 94 | mDefaultWidth = w; |
| 95 | mDefaultHeight = h; |
| 96 | return mConsumer->setDefaultBufferSize(w, h); |
| 97 | } |
| 98 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 99 | void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) { |
| 100 | setFrameAvailableListener(listener); |
| 101 | Mutex::Autolock lock(mMutex); |
| 102 | mContentsChangedListener = listener; |
| 103 | } |
| 104 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 105 | status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime, |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 106 | bool* autoRefresh, bool* queuedBuffer, |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 107 | uint64_t maxFrameNumber) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 108 | ATRACE_CALL(); |
| 109 | BLC_LOGV("updateTexImage"); |
| 110 | Mutex::Autolock lock(mMutex); |
| 111 | |
| 112 | if (mAbandoned) { |
| 113 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 114 | return NO_INIT; |
| 115 | } |
| 116 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 117 | BufferItem item; |
| 118 | |
| 119 | // Acquire the next buffer. |
| 120 | // In asynchronous mode the list is guaranteed to be one buffer |
| 121 | // deep, while in synchronous mode we use the oldest buffer. |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 122 | status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 123 | if (err != NO_ERROR) { |
| 124 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 125 | err = NO_ERROR; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 126 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 127 | // return the error, without logging |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 128 | } else { |
| 129 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 130 | } |
| 131 | return err; |
| 132 | } |
| 133 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 134 | if (autoRefresh) { |
| 135 | *autoRefresh = item.mAutoRefresh; |
| 136 | } |
| 137 | |
| 138 | if (queuedBuffer) { |
| 139 | *queuedBuffer = item.mQueuedBuffer; |
| 140 | } |
| 141 | |
| 142 | // We call the rejecter here, in case the caller has a reason to |
| 143 | // not accept this buffer. This is used by SurfaceFlinger to |
| 144 | // reject buffers which have the wrong size |
| 145 | int slot = item.mSlot; |
| 146 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 147 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
| 148 | return BUFFER_REJECTED; |
| 149 | } |
| 150 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 151 | // Release the previous buffer. |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 152 | err = updateAndReleaseLocked(item, &mPendingRelease); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 153 | if (err != NO_ERROR) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 154 | return err; |
| 155 | } |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 156 | return err; |
| 157 | } |
| 158 | |
| 159 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 160 | if (!fence->isValid()) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture; |
| 165 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 166 | return; |
| 167 | } |
| 168 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 169 | auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 170 | : mCurrentTextureBuffer->getBuffer(); |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 171 | auto err = addReleaseFence(slot, buffer, fence); |
| 172 | if (err != OK) { |
| 173 | BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool BufferLayerConsumer::releasePendingBuffer() { |
| 178 | if (!mPendingRelease.isPending) { |
| 179 | BLC_LOGV("Pending buffer already released"); |
| 180 | return false; |
| 181 | } |
| 182 | BLC_LOGV("Releasing pending buffer"); |
| 183 | Mutex::Autolock lock(mMutex); |
| 184 | status_t result = |
| 185 | releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer); |
| 186 | if (result < NO_ERROR) { |
| 187 | BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result); |
| 188 | } |
| 189 | mPendingRelease = PendingRelease(); |
| 190 | return true; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 193 | sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const { |
| 194 | Mutex::Autolock lock(mMutex); |
| 195 | return ConsumerBase::mPrevFinalReleaseFence; |
| 196 | } |
| 197 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 198 | status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 199 | uint64_t maxFrameNumber) { |
| 200 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber); |
| 201 | if (err != NO_ERROR) { |
| 202 | return err; |
| 203 | } |
| 204 | |
| 205 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 206 | // before, so we need to clean up old references. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 207 | if (item->mGraphicBuffer != nullptr) { |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 208 | std::lock_guard<std::mutex> lock(mImagesMutex); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 209 | if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->getBuffer() == nullptr || |
| 210 | mImages[item->mSlot]->getBuffer()->getId() != item->mGraphicBuffer->getId()) { |
| 211 | mImages[item->mSlot] = std::make_shared< |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 212 | renderengine::impl::ExternalTexture>(item->mGraphicBuffer, mRE, |
| 213 | renderengine::impl::ExternalTexture:: |
| 214 | Usage::READABLE); |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 215 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return NO_ERROR; |
| 219 | } |
| 220 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 221 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 222 | PendingRelease* pendingRelease) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 223 | status_t err = NO_ERROR; |
| 224 | |
| 225 | int slot = item.mSlot; |
| 226 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 227 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 228 | (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->getBuffer() != nullptr) |
| 229 | ? mCurrentTextureBuffer->getBuffer()->handle |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 230 | : 0, |
| 231 | slot, mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 232 | |
| 233 | // Hang onto the pointer so that it isn't freed in the call to |
| 234 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 235 | // the same. |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 236 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 237 | std::shared_ptr<renderengine::ExternalTexture> nextTextureBuffer; |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 238 | { |
| 239 | std::lock_guard<std::mutex> lock(mImagesMutex); |
| 240 | nextTextureBuffer = mImages[slot]; |
| 241 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 242 | |
| 243 | // release old buffer |
| 244 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 245 | if (pendingRelease == nullptr) { |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 246 | status_t status = |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 247 | releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->getBuffer()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 248 | if (status < NO_ERROR) { |
| 249 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 250 | status); |
| 251 | err = status; |
| 252 | // keep going, with error raised [?] |
| 253 | } |
| 254 | } else { |
| 255 | pendingRelease->currentTexture = mCurrentTexture; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 256 | pendingRelease->graphicBuffer = mCurrentTextureBuffer->getBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 257 | pendingRelease->isPending = true; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Update the BufferLayerConsumer state. |
| 262 | mCurrentTexture = slot; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 263 | mCurrentTextureBuffer = nextTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 264 | mCurrentCrop = item.mCrop; |
| 265 | mCurrentTransform = item.mTransform; |
| 266 | mCurrentScalingMode = item.mScalingMode; |
| 267 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 268 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 269 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 270 | mCurrentFence = item.mFence; |
| 271 | mCurrentFenceTime = item.mFenceTime; |
| 272 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 273 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 274 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 275 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 276 | |
| 277 | computeCurrentTransformMatrixLocked(); |
| 278 | |
| 279 | return err; |
| 280 | } |
| 281 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 282 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 283 | Mutex::Autolock lock(mMutex); |
| 284 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 285 | } |
| 286 | |
| 287 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 288 | Mutex::Autolock lock(mMutex); |
| 289 | if (mAbandoned) { |
| 290 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 291 | return; |
| 292 | } |
| 293 | bool needsRecompute = mFilteringEnabled != enabled; |
| 294 | mFilteringEnabled = enabled; |
| 295 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 296 | if (needsRecompute && mCurrentTextureBuffer == nullptr) { |
| 297 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 300 | if (needsRecompute && mCurrentTextureBuffer != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 301 | computeCurrentTransformMatrixLocked(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 306 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 307 | if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->getBuffer() == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 308 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 309 | "mCurrentTextureBuffer is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 310 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 311 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, |
| 312 | mCurrentTextureBuffer == nullptr |
| 313 | ? nullptr |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 314 | : mCurrentTextureBuffer->getBuffer(), |
Alec Mouri | 2ee0dda | 2019-01-30 16:44:43 -0800 | [diff] [blame] | 315 | getCurrentCropLocked(), mCurrentTransform, |
| 316 | mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 319 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 320 | BLC_LOGV("getTimestamp"); |
| 321 | Mutex::Autolock lock(mMutex); |
| 322 | return mCurrentTimestamp; |
| 323 | } |
| 324 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 325 | ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 326 | BLC_LOGV("getCurrentDataSpace"); |
| 327 | Mutex::Autolock lock(mMutex); |
| 328 | return mCurrentDataSpace; |
| 329 | } |
| 330 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 331 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 332 | BLC_LOGV("getCurrentHdrMetadata"); |
| 333 | Mutex::Autolock lock(mMutex); |
| 334 | return mCurrentHdrMetadata; |
| 335 | } |
| 336 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 337 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 338 | BLC_LOGV("getFrameNumber"); |
| 339 | Mutex::Autolock lock(mMutex); |
| 340 | return mCurrentFrameNumber; |
| 341 | } |
| 342 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 343 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 344 | Mutex::Autolock lock(mMutex); |
| 345 | return mCurrentTransformToDisplayInverse; |
| 346 | } |
| 347 | |
| 348 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 349 | return mCurrentSurfaceDamage; |
| 350 | } |
| 351 | |
Steven Thomas | 44685cb | 2019-07-23 16:19:31 -0700 | [diff] [blame] | 352 | void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) { |
| 353 | if (damage.bounds() == Rect::INVALID_RECT || |
| 354 | mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) { |
| 355 | mCurrentSurfaceDamage = Region::INVALID_REGION; |
| 356 | } else { |
| 357 | mCurrentSurfaceDamage |= damage; |
| 358 | } |
| 359 | } |
| 360 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 361 | int BufferLayerConsumer::getCurrentApi() const { |
| 362 | Mutex::Autolock lock(mMutex); |
| 363 | return mCurrentApi; |
| 364 | } |
| 365 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 366 | std::shared_ptr<renderengine::ExternalTexture> BufferLayerConsumer::getCurrentBuffer( |
| 367 | int* outSlot, sp<Fence>* outFence) const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 368 | Mutex::Autolock lock(mMutex); |
| 369 | |
| 370 | if (outSlot != nullptr) { |
| 371 | *outSlot = mCurrentTexture; |
| 372 | } |
| 373 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 374 | if (outFence != nullptr) { |
| 375 | *outFence = mCurrentFence; |
| 376 | } |
| 377 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 378 | return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer; |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 381 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 382 | Mutex::Autolock lock(mMutex); |
Alec Mouri | 2ee0dda | 2019-01-30 16:44:43 -0800 | [diff] [blame] | 383 | return getCurrentCropLocked(); |
| 384 | } |
| 385 | |
| 386 | Rect BufferLayerConsumer::getCurrentCropLocked() const { |
Ana Krulec | 65d8780 | 2020-04-24 11:06:46 -0700 | [diff] [blame] | 387 | uint32_t width = mDefaultWidth; |
| 388 | uint32_t height = mDefaultHeight; |
| 389 | // If the buffer comes with a rotated bit for 90 (or 270) degrees, switch width/height in order |
| 390 | // to scale and crop correctly. |
| 391 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 392 | width = mDefaultHeight; |
| 393 | height = mDefaultWidth; |
| 394 | } |
| 395 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 396 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Ana Krulec | 65d8780 | 2020-04-24 11:06:46 -0700 | [diff] [blame] | 397 | ? GLConsumer::scaleDownCrop(mCurrentCrop, width, height) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 398 | : mCurrentCrop; |
| 399 | } |
| 400 | |
| 401 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 402 | Mutex::Autolock lock(mMutex); |
| 403 | return mCurrentTransform; |
| 404 | } |
| 405 | |
| 406 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 407 | Mutex::Autolock lock(mMutex); |
| 408 | return mCurrentScalingMode; |
| 409 | } |
| 410 | |
| 411 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 412 | Mutex::Autolock lock(mMutex); |
| 413 | return mCurrentFence; |
| 414 | } |
| 415 | |
| 416 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 417 | Mutex::Autolock lock(mMutex); |
| 418 | return mCurrentFenceTime; |
| 419 | } |
| 420 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 421 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 422 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 423 | std::lock_guard<std::mutex> lock(mImagesMutex); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 424 | if (slotIndex == mCurrentTexture) { |
| 425 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 426 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 427 | mImages[slotIndex] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 428 | ConsumerBase::freeBufferLocked(slotIndex); |
| 429 | } |
| 430 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 431 | void BufferLayerConsumer::onDisconnect() { |
chaviw | 1a4dba4 | 2020-05-27 15:16:15 -0700 | [diff] [blame] | 432 | Mutex::Autolock lock(mMutex); |
| 433 | |
| 434 | if (mAbandoned) { |
| 435 | // Nothing to do if we're already abandoned. |
| 436 | return; |
| 437 | } |
| 438 | |
chaviw | c65e864 | 2020-03-18 15:35:48 -0700 | [diff] [blame] | 439 | mLayer->onDisconnect(); |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 442 | void BufferLayerConsumer::onSidebandStreamChanged() { |
Dominik Laskowski | b4ba8f5 | 2021-09-27 18:20:58 -0700 | [diff] [blame] | 443 | [[maybe_unused]] FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 444 | { |
| 445 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 446 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 447 | } |
| 448 | sp<ContentsChangedListener> listener; |
| 449 | { // scope for the lock |
| 450 | Mutex::Autolock lock(mMutex); |
| 451 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 452 | listener = mContentsChangedListener.promote(); |
| 453 | } |
| 454 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 455 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 456 | listener->onSidebandStreamChanged(); |
| 457 | } |
| 458 | } |
| 459 | |
Alec Mouri | 485e4c3 | 2019-04-30 18:24:05 -0700 | [diff] [blame] | 460 | void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) { |
| 461 | if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) { |
| 462 | std::lock_guard<std::mutex> lock(mImagesMutex); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 463 | const std::shared_ptr<renderengine::ExternalTexture>& oldImage = mImages[item.mSlot]; |
| 464 | if (oldImage == nullptr || oldImage->getBuffer() == nullptr || |
| 465 | oldImage->getBuffer()->getId() != item.mGraphicBuffer->getId()) { |
| 466 | mImages[item.mSlot] = std::make_shared< |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 467 | renderengine::impl::ExternalTexture>(item.mGraphicBuffer, mRE, |
| 468 | renderengine::impl::ExternalTexture:: |
| 469 | Usage::READABLE); |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 470 | } |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 474 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 475 | FrameEventHistoryDelta* outDelta) { |
chaviw | 1a4dba4 | 2020-05-27 15:16:15 -0700 | [diff] [blame] | 476 | Mutex::Autolock lock(mMutex); |
| 477 | |
| 478 | if (mAbandoned) { |
| 479 | // Nothing to do if we're already abandoned. |
| 480 | return; |
| 481 | } |
| 482 | |
chaviw | c65e864 | 2020-03-18 15:35:48 -0700 | [diff] [blame] | 483 | mLayer->addAndGetFrameTimestamps(newTimestamps, outDelta); |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 486 | void BufferLayerConsumer::abandonLocked() { |
| 487 | BLC_LOGV("abandonLocked"); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 488 | mCurrentTextureBuffer = nullptr; |
| 489 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 490 | std::lock_guard<std::mutex> lock(mImagesMutex); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 491 | mImages[i] = nullptr; |
| 492 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 493 | ConsumerBase::abandonLocked(); |
| 494 | } |
| 495 | |
| 496 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 497 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 498 | } |
| 499 | |
| 500 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 501 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 502 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 503 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 504 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 505 | mCurrentTransform); |
| 506 | |
| 507 | ConsumerBase::dumpLocked(result, prefix); |
| 508 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 509 | }; // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 510 | |
| 511 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 512 | #pragma clang diagnostic pop // ignored "-Wconversion" |