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/Image.h> |
| 44 | #include <renderengine/RenderEngine.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 |
| 170 | : mCurrentTextureBuffer->graphicBuffer(); |
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); |
| 209 | if (mImages[item->mSlot] == nullptr || mImages[item->mSlot]->graphicBuffer() == nullptr || |
| 210 | mImages[item->mSlot]->graphicBuffer()->getId() != item->mGraphicBuffer->getId()) { |
| 211 | mImages[item->mSlot] = std::make_shared<Image>(item->mGraphicBuffer, mRE); |
| 212 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return NO_ERROR; |
| 216 | } |
| 217 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 218 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 219 | PendingRelease* pendingRelease) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 220 | status_t err = NO_ERROR; |
| 221 | |
| 222 | int slot = item.mSlot; |
| 223 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 224 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 225 | (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr) |
| 226 | ? mCurrentTextureBuffer->graphicBuffer()->handle |
| 227 | : 0, |
| 228 | slot, mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 229 | |
| 230 | // Hang onto the pointer so that it isn't freed in the call to |
| 231 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 232 | // the same. |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 233 | |
| 234 | std::shared_ptr<Image> nextTextureBuffer; |
| 235 | { |
| 236 | std::lock_guard<std::mutex> lock(mImagesMutex); |
| 237 | nextTextureBuffer = mImages[slot]; |
| 238 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 239 | |
| 240 | // release old buffer |
| 241 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 242 | if (pendingRelease == nullptr) { |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 243 | status_t status = |
| 244 | releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->graphicBuffer()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 245 | if (status < NO_ERROR) { |
| 246 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 247 | status); |
| 248 | err = status; |
| 249 | // keep going, with error raised [?] |
| 250 | } |
| 251 | } else { |
| 252 | pendingRelease->currentTexture = mCurrentTexture; |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 253 | pendingRelease->graphicBuffer = mCurrentTextureBuffer->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 254 | pendingRelease->isPending = true; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Update the BufferLayerConsumer state. |
| 259 | mCurrentTexture = slot; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 260 | mCurrentTextureBuffer = nextTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 261 | mCurrentCrop = item.mCrop; |
| 262 | mCurrentTransform = item.mTransform; |
| 263 | mCurrentScalingMode = item.mScalingMode; |
| 264 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 265 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 266 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 267 | mCurrentFence = item.mFence; |
| 268 | mCurrentFenceTime = item.mFenceTime; |
| 269 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 270 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 271 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 272 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 273 | |
| 274 | computeCurrentTransformMatrixLocked(); |
| 275 | |
| 276 | return err; |
| 277 | } |
| 278 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 279 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 280 | Mutex::Autolock lock(mMutex); |
| 281 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 282 | } |
| 283 | |
| 284 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 285 | Mutex::Autolock lock(mMutex); |
| 286 | if (mAbandoned) { |
| 287 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 288 | return; |
| 289 | } |
| 290 | bool needsRecompute = mFilteringEnabled != enabled; |
| 291 | mFilteringEnabled = enabled; |
| 292 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 293 | if (needsRecompute && mCurrentTextureBuffer == nullptr) { |
| 294 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 297 | if (needsRecompute && mCurrentTextureBuffer != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 298 | computeCurrentTransformMatrixLocked(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 303 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 304 | if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->graphicBuffer() == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 305 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 306 | "mCurrentTextureBuffer is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 307 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 308 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, |
| 309 | mCurrentTextureBuffer == nullptr |
| 310 | ? nullptr |
| 311 | : mCurrentTextureBuffer->graphicBuffer(), |
Alec Mouri | 2ee0dda | 2019-01-30 16:44:43 -0800 | [diff] [blame] | 312 | getCurrentCropLocked(), mCurrentTransform, |
| 313 | mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 316 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 317 | BLC_LOGV("getTimestamp"); |
| 318 | Mutex::Autolock lock(mMutex); |
| 319 | return mCurrentTimestamp; |
| 320 | } |
| 321 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 322 | ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 323 | BLC_LOGV("getCurrentDataSpace"); |
| 324 | Mutex::Autolock lock(mMutex); |
| 325 | return mCurrentDataSpace; |
| 326 | } |
| 327 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 328 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 329 | BLC_LOGV("getCurrentHdrMetadata"); |
| 330 | Mutex::Autolock lock(mMutex); |
| 331 | return mCurrentHdrMetadata; |
| 332 | } |
| 333 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 334 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 335 | BLC_LOGV("getFrameNumber"); |
| 336 | Mutex::Autolock lock(mMutex); |
| 337 | return mCurrentFrameNumber; |
| 338 | } |
| 339 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 340 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 341 | Mutex::Autolock lock(mMutex); |
| 342 | return mCurrentTransformToDisplayInverse; |
| 343 | } |
| 344 | |
| 345 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 346 | return mCurrentSurfaceDamage; |
| 347 | } |
| 348 | |
Steven Thomas | 44685cb | 2019-07-23 16:19:31 -0700 | [diff] [blame] | 349 | void BufferLayerConsumer::mergeSurfaceDamage(const Region& damage) { |
| 350 | if (damage.bounds() == Rect::INVALID_RECT || |
| 351 | mCurrentSurfaceDamage.bounds() == Rect::INVALID_RECT) { |
| 352 | mCurrentSurfaceDamage = Region::INVALID_REGION; |
| 353 | } else { |
| 354 | mCurrentSurfaceDamage |= damage; |
| 355 | } |
| 356 | } |
| 357 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 358 | int BufferLayerConsumer::getCurrentApi() const { |
| 359 | Mutex::Autolock lock(mMutex); |
| 360 | return mCurrentApi; |
| 361 | } |
| 362 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 363 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 364 | Mutex::Autolock lock(mMutex); |
| 365 | |
| 366 | if (outSlot != nullptr) { |
| 367 | *outSlot = mCurrentTexture; |
| 368 | } |
| 369 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 370 | if (outFence != nullptr) { |
| 371 | *outFence = mCurrentFence; |
| 372 | } |
| 373 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 374 | return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer->graphicBuffer(); |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 377 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 378 | Mutex::Autolock lock(mMutex); |
Alec Mouri | 2ee0dda | 2019-01-30 16:44:43 -0800 | [diff] [blame] | 379 | return getCurrentCropLocked(); |
| 380 | } |
| 381 | |
| 382 | Rect BufferLayerConsumer::getCurrentCropLocked() const { |
Ana Krulec | 65d8780 | 2020-04-24 11:06:46 -0700 | [diff] [blame] | 383 | uint32_t width = mDefaultWidth; |
| 384 | uint32_t height = mDefaultHeight; |
| 385 | // If the buffer comes with a rotated bit for 90 (or 270) degrees, switch width/height in order |
| 386 | // to scale and crop correctly. |
| 387 | if (mCurrentTransform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 388 | width = mDefaultHeight; |
| 389 | height = mDefaultWidth; |
| 390 | } |
| 391 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 392 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Ana Krulec | 65d8780 | 2020-04-24 11:06:46 -0700 | [diff] [blame] | 393 | ? GLConsumer::scaleDownCrop(mCurrentCrop, width, height) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 394 | : mCurrentCrop; |
| 395 | } |
| 396 | |
| 397 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 398 | Mutex::Autolock lock(mMutex); |
| 399 | return mCurrentTransform; |
| 400 | } |
| 401 | |
| 402 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 403 | Mutex::Autolock lock(mMutex); |
| 404 | return mCurrentScalingMode; |
| 405 | } |
| 406 | |
| 407 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 408 | Mutex::Autolock lock(mMutex); |
| 409 | return mCurrentFence; |
| 410 | } |
| 411 | |
| 412 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 413 | Mutex::Autolock lock(mMutex); |
| 414 | return mCurrentFenceTime; |
| 415 | } |
| 416 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 417 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 418 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 419 | std::lock_guard<std::mutex> lock(mImagesMutex); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 420 | if (slotIndex == mCurrentTexture) { |
| 421 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 422 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 423 | mImages[slotIndex] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 424 | ConsumerBase::freeBufferLocked(slotIndex); |
| 425 | } |
| 426 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 427 | void BufferLayerConsumer::onDisconnect() { |
chaviw | 1a4dba4 | 2020-05-27 15:16:15 -0700 | [diff] [blame] | 428 | Mutex::Autolock lock(mMutex); |
| 429 | |
| 430 | if (mAbandoned) { |
| 431 | // Nothing to do if we're already abandoned. |
| 432 | return; |
| 433 | } |
| 434 | |
chaviw | c65e864 | 2020-03-18 15:35:48 -0700 | [diff] [blame] | 435 | mLayer->onDisconnect(); |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 438 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 439 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 440 | { |
| 441 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 442 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 443 | } |
| 444 | sp<ContentsChangedListener> listener; |
| 445 | { // scope for the lock |
| 446 | Mutex::Autolock lock(mMutex); |
| 447 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 448 | listener = mContentsChangedListener.promote(); |
| 449 | } |
| 450 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 451 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 452 | listener->onSidebandStreamChanged(); |
| 453 | } |
| 454 | } |
| 455 | |
Alec Mouri | 485e4c3 | 2019-04-30 18:24:05 -0700 | [diff] [blame] | 456 | void BufferLayerConsumer::onBufferAvailable(const BufferItem& item) { |
| 457 | if (item.mGraphicBuffer != nullptr && item.mSlot != BufferQueue::INVALID_BUFFER_SLOT) { |
| 458 | std::lock_guard<std::mutex> lock(mImagesMutex); |
| 459 | const std::shared_ptr<Image>& oldImage = mImages[item.mSlot]; |
| 460 | if (oldImage == nullptr || oldImage->graphicBuffer() == nullptr || |
| 461 | oldImage->graphicBuffer()->getId() != item.mGraphicBuffer->getId()) { |
| 462 | mImages[item.mSlot] = std::make_shared<Image>(item.mGraphicBuffer, mRE); |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 463 | } |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 467 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 468 | FrameEventHistoryDelta* outDelta) { |
chaviw | 1a4dba4 | 2020-05-27 15:16:15 -0700 | [diff] [blame] | 469 | Mutex::Autolock lock(mMutex); |
| 470 | |
| 471 | if (mAbandoned) { |
| 472 | // Nothing to do if we're already abandoned. |
| 473 | return; |
| 474 | } |
| 475 | |
chaviw | c65e864 | 2020-03-18 15:35:48 -0700 | [diff] [blame] | 476 | mLayer->addAndGetFrameTimestamps(newTimestamps, outDelta); |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 479 | void BufferLayerConsumer::abandonLocked() { |
| 480 | BLC_LOGV("abandonLocked"); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 481 | mCurrentTextureBuffer = nullptr; |
| 482 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 483 | std::lock_guard<std::mutex> lock(mImagesMutex); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 484 | mImages[i] = nullptr; |
| 485 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 486 | ConsumerBase::abandonLocked(); |
| 487 | } |
| 488 | |
| 489 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 490 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 491 | } |
| 492 | |
| 493 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 494 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 495 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 496 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 497 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 498 | mCurrentTransform); |
| 499 | |
| 500 | ConsumerBase::dumpLocked(result, prefix); |
| 501 | } |
| 502 | |
Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 503 | BufferLayerConsumer::Image::Image(const sp<GraphicBuffer>& graphicBuffer, |
| 504 | renderengine::RenderEngine& engine) |
| 505 | : mGraphicBuffer(graphicBuffer), mRE(engine) { |
Peiyong Lin | 637f4ac | 2020-10-22 00:39:39 -0700 | [diff] [blame] | 506 | if (graphicBuffer != nullptr && (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED)) { |
| 507 | return; |
| 508 | } |
Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 509 | mRE.cacheExternalTextureBuffer(mGraphicBuffer); |
| 510 | } |
| 511 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 512 | BufferLayerConsumer::Image::~Image() { |
| 513 | if (mGraphicBuffer != nullptr) { |
Alec Mouri | f190733 | 2019-03-07 13:05:26 -0800 | [diff] [blame] | 514 | ALOGV("Destroying buffer: %" PRId64, mGraphicBuffer->getId()); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 515 | mRE.unbindExternalTextureBuffer(mGraphicBuffer->getId()); |
| 516 | } |
| 517 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 518 | }; // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 519 | |
| 520 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 521 | #pragma clang diagnostic pop // ignored "-Wconversion" |