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 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "BufferLayerConsumer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include "BufferLayerConsumer.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 23 | #include "Layer.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 24 | #include "Scheduler/DispSync.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 25 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
| 27 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 28 | #include <cutils/compiler.h> |
| 29 | |
| 30 | #include <hardware/hardware.h> |
| 31 | |
| 32 | #include <math/mat4.h> |
| 33 | |
| 34 | #include <gui/BufferItem.h> |
| 35 | #include <gui/GLConsumer.h> |
| 36 | #include <gui/ISurfaceComposer.h> |
| 37 | #include <gui/SurfaceComposerClient.h> |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 38 | #include <private/gui/ComposerService.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 39 | #include <renderengine/Image.h> |
| 40 | #include <renderengine/RenderEngine.h> |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 41 | #include <utils/Log.h> |
| 42 | #include <utils/String8.h> |
| 43 | #include <utils/Trace.h> |
| 44 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
| 47 | // Macros for including the BufferLayerConsumer name in log messages |
| 48 | #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 49 | #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 50 | //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 51 | #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 52 | #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 53 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 54 | static const mat4 mtxIdentity; |
| 55 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 56 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 57 | renderengine::RenderEngine& engine, uint32_t tex, |
| 58 | Layer* layer) |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 59 | : ConsumerBase(bq, false), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 60 | mCurrentCrop(Rect::EMPTY_RECT), |
| 61 | mCurrentTransform(0), |
| 62 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 63 | mCurrentFence(Fence::NO_FENCE), |
| 64 | mCurrentTimestamp(0), |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 65 | mCurrentDataSpace(ui::Dataspace::UNKNOWN), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 66 | mCurrentFrameNumber(0), |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 67 | mCurrentTransformToDisplayInverse(false), |
| 68 | mCurrentSurfaceDamage(), |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 69 | mCurrentApi(0), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 70 | mDefaultWidth(1), |
| 71 | mDefaultHeight(1), |
| 72 | mFilteringEnabled(true), |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 73 | mRE(engine), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 74 | mTexName(tex), |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 75 | mLayer(layer), |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 76 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 77 | BLC_LOGV("BufferLayerConsumer"); |
| 78 | |
| 79 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 80 | |
| 81 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 82 | } |
| 83 | |
| 84 | status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 85 | Mutex::Autolock lock(mMutex); |
| 86 | if (mAbandoned) { |
| 87 | BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!"); |
| 88 | return NO_INIT; |
| 89 | } |
| 90 | mDefaultWidth = w; |
| 91 | mDefaultHeight = h; |
| 92 | return mConsumer->setDefaultBufferSize(w, h); |
| 93 | } |
| 94 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 95 | void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) { |
| 96 | setFrameAvailableListener(listener); |
| 97 | Mutex::Autolock lock(mMutex); |
| 98 | mContentsChangedListener = listener; |
| 99 | } |
| 100 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 101 | status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime, |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 102 | bool* autoRefresh, bool* queuedBuffer, |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame^] | 103 | uint64_t maxFrameNumber) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 104 | ATRACE_CALL(); |
| 105 | BLC_LOGV("updateTexImage"); |
| 106 | Mutex::Autolock lock(mMutex); |
| 107 | |
| 108 | if (mAbandoned) { |
| 109 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 110 | return NO_INIT; |
| 111 | } |
| 112 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 113 | BufferItem item; |
| 114 | |
| 115 | // Acquire the next buffer. |
| 116 | // In asynchronous mode the list is guaranteed to be one buffer |
| 117 | // deep, while in synchronous mode we use the oldest buffer. |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 118 | status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 119 | if (err != NO_ERROR) { |
| 120 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 121 | err = NO_ERROR; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 122 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 123 | // return the error, without logging |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 124 | } else { |
| 125 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 126 | } |
| 127 | return err; |
| 128 | } |
| 129 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 130 | if (autoRefresh) { |
| 131 | *autoRefresh = item.mAutoRefresh; |
| 132 | } |
| 133 | |
| 134 | if (queuedBuffer) { |
| 135 | *queuedBuffer = item.mQueuedBuffer; |
| 136 | } |
| 137 | |
| 138 | // We call the rejecter here, in case the caller has a reason to |
| 139 | // not accept this buffer. This is used by SurfaceFlinger to |
| 140 | // reject buffers which have the wrong size |
| 141 | int slot = item.mSlot; |
| 142 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 143 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
| 144 | return BUFFER_REJECTED; |
| 145 | } |
| 146 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 147 | // Release the previous buffer. |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame^] | 148 | err = updateAndReleaseLocked(item, &mPendingRelease); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 149 | if (err != NO_ERROR) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 150 | return err; |
| 151 | } |
| 152 | |
Alec Mouri | 3221a89 | 2018-09-28 20:45:06 +0000 | [diff] [blame] | 153 | if (!mRE.useNativeFenceSync()) { |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 154 | // Bind the new buffer to the GL texture. |
| 155 | // |
| 156 | // Older devices require the "implicit" synchronization provided |
| 157 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 158 | // devices will either call this in Layer::onDraw, or (if it's not |
| 159 | // a GL-composited layer) not at all. |
| 160 | err = bindTextureImageLocked(); |
| 161 | } |
| 162 | |
| 163 | return err; |
| 164 | } |
| 165 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 166 | status_t BufferLayerConsumer::bindTextureImage() { |
| 167 | Mutex::Autolock lock(mMutex); |
| 168 | return bindTextureImageLocked(); |
| 169 | } |
| 170 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 171 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 172 | if (!fence->isValid()) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture; |
| 177 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 178 | return; |
| 179 | } |
| 180 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 181 | auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer : mCurrentTextureBuffer; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 182 | auto err = addReleaseFence(slot, buffer, fence); |
| 183 | if (err != OK) { |
| 184 | BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool BufferLayerConsumer::releasePendingBuffer() { |
| 189 | if (!mPendingRelease.isPending) { |
| 190 | BLC_LOGV("Pending buffer already released"); |
| 191 | return false; |
| 192 | } |
| 193 | BLC_LOGV("Releasing pending buffer"); |
| 194 | Mutex::Autolock lock(mMutex); |
| 195 | status_t result = |
| 196 | releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer); |
| 197 | if (result < NO_ERROR) { |
| 198 | BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result); |
| 199 | } |
| 200 | mPendingRelease = PendingRelease(); |
| 201 | return true; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 204 | sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const { |
| 205 | Mutex::Autolock lock(mMutex); |
| 206 | return ConsumerBase::mPrevFinalReleaseFence; |
| 207 | } |
| 208 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 209 | status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 210 | uint64_t maxFrameNumber) { |
| 211 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber); |
| 212 | if (err != NO_ERROR) { |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 217 | // before. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 218 | if (item->mGraphicBuffer != nullptr) { |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 219 | mImages[item->mSlot] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return NO_ERROR; |
| 223 | } |
| 224 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 225 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame^] | 226 | PendingRelease* pendingRelease) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 227 | status_t err = NO_ERROR; |
| 228 | |
| 229 | int slot = item.mSlot; |
| 230 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 231 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 232 | mCurrentTextureBuffer != nullptr ? mCurrentTextureBuffer->handle : 0, slot, |
| 233 | mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 234 | |
| 235 | // Hang onto the pointer so that it isn't freed in the call to |
| 236 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 237 | // the same. |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 238 | sp<GraphicBuffer> nextTextureBuffer = mSlots[slot].mGraphicBuffer; |
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 | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 243 | status_t status = releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 244 | if (status < NO_ERROR) { |
| 245 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 246 | status); |
| 247 | err = status; |
| 248 | // keep going, with error raised [?] |
| 249 | } |
| 250 | } else { |
| 251 | pendingRelease->currentTexture = mCurrentTexture; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 252 | pendingRelease->graphicBuffer = mCurrentTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 253 | pendingRelease->isPending = true; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Update the BufferLayerConsumer state. |
| 258 | mCurrentTexture = slot; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 259 | mCurrentTextureBuffer = nextTextureBuffer; |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 260 | mCurrentTextureBufferStaleForGpu = false; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 261 | mCurrentTextureImageFreed = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 262 | mCurrentCrop = item.mCrop; |
| 263 | mCurrentTransform = item.mTransform; |
| 264 | mCurrentScalingMode = item.mScalingMode; |
| 265 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 266 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 267 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 268 | mCurrentFence = item.mFence; |
| 269 | mCurrentFenceTime = item.mFenceTime; |
| 270 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 271 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 272 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 273 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 274 | |
| 275 | computeCurrentTransformMatrixLocked(); |
| 276 | |
| 277 | return err; |
| 278 | } |
| 279 | |
| 280 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 281 | ATRACE_CALL(); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 282 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 283 | return mRE.bindExternalTextureBuffer(mTexName, mCurrentTextureBuffer, mCurrentFence, false); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 286 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 287 | Mutex::Autolock lock(mMutex); |
| 288 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 289 | } |
| 290 | |
| 291 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 292 | Mutex::Autolock lock(mMutex); |
| 293 | if (mAbandoned) { |
| 294 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 295 | return; |
| 296 | } |
| 297 | bool needsRecompute = mFilteringEnabled != enabled; |
| 298 | mFilteringEnabled = enabled; |
| 299 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 300 | if (needsRecompute && mCurrentTextureBuffer == nullptr) { |
| 301 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 304 | if (needsRecompute && mCurrentTextureBuffer != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 305 | computeCurrentTransformMatrixLocked(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 310 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 311 | if (mCurrentTextureBuffer == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 312 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 313 | "mCurrentTextureBuffer is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 314 | } |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 315 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, mCurrentTextureBuffer, mCurrentCrop, |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 316 | mCurrentTransform, 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 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 352 | int BufferLayerConsumer::getCurrentApi() const { |
| 353 | Mutex::Autolock lock(mMutex); |
| 354 | return mCurrentApi; |
| 355 | } |
| 356 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 357 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 358 | Mutex::Autolock lock(mMutex); |
| 359 | |
| 360 | if (outSlot != nullptr) { |
| 361 | *outSlot = mCurrentTexture; |
| 362 | } |
| 363 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 364 | if (outFence != nullptr) { |
| 365 | *outFence = mCurrentFence; |
| 366 | } |
| 367 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 368 | return mCurrentTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 371 | bool BufferLayerConsumer::getAndSetCurrentBufferCacheHint() { |
| 372 | Mutex::Autolock lock(mMutex); |
| 373 | bool useCache = mCurrentTextureBufferStaleForGpu; |
| 374 | // Set the staleness bit here, as this function is only called during a |
| 375 | // client composition path. |
| 376 | mCurrentTextureBufferStaleForGpu = true; |
| 377 | return useCache; |
| 378 | } |
| 379 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 380 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 381 | Mutex::Autolock lock(mMutex); |
| 382 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 383 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 384 | : mCurrentCrop; |
| 385 | } |
| 386 | |
| 387 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 388 | Mutex::Autolock lock(mMutex); |
| 389 | return mCurrentTransform; |
| 390 | } |
| 391 | |
| 392 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 393 | Mutex::Autolock lock(mMutex); |
| 394 | return mCurrentScalingMode; |
| 395 | } |
| 396 | |
| 397 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 398 | Mutex::Autolock lock(mMutex); |
| 399 | return mCurrentFence; |
| 400 | } |
| 401 | |
| 402 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 403 | Mutex::Autolock lock(mMutex); |
| 404 | return mCurrentFenceTime; |
| 405 | } |
| 406 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 407 | status_t BufferLayerConsumer::doFenceWaitLocked() const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 408 | if (mCurrentFence->isValid()) { |
Lloyd Pique | 63f9dbf | 2018-06-21 13:13:07 -0700 | [diff] [blame] | 409 | if (mRE.useWaitSync()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 410 | base::unique_fd fenceFd(mCurrentFence->dup()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 411 | if (fenceFd == -1) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 412 | BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 413 | return -errno; |
| 414 | } |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 415 | if (!mRE.waitFence(std::move(fenceFd))) { |
| 416 | BLC_LOGE("doFenceWait: failed to wait on fence fd"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 417 | return UNKNOWN_ERROR; |
| 418 | } |
| 419 | } else { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 420 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 421 | if (err != NO_ERROR) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 422 | BLC_LOGE("doFenceWait: error waiting for fence: %d", err); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 423 | return err; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return NO_ERROR; |
| 429 | } |
| 430 | |
| 431 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 432 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 433 | if (slotIndex == mCurrentTexture) { |
| 434 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 435 | mCurrentTextureImageFreed = std::move(mImages[slotIndex]); |
| 436 | } else { |
| 437 | mImages[slotIndex] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 438 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 439 | ConsumerBase::freeBufferLocked(slotIndex); |
| 440 | } |
| 441 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 442 | void BufferLayerConsumer::onDisconnect() { |
| 443 | sp<Layer> l = mLayer.promote(); |
| 444 | if (l.get()) { |
| 445 | l->onDisconnect(); |
| 446 | } |
| 447 | } |
| 448 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 449 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 450 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 451 | { |
| 452 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 453 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 454 | } |
| 455 | sp<ContentsChangedListener> listener; |
| 456 | { // scope for the lock |
| 457 | Mutex::Autolock lock(mMutex); |
| 458 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 459 | listener = mContentsChangedListener.promote(); |
| 460 | } |
| 461 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 462 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 463 | listener->onSidebandStreamChanged(); |
| 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) { |
| 469 | sp<Layer> l = mLayer.promote(); |
| 470 | if (l.get()) { |
| 471 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 472 | } |
| 473 | } |
| 474 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 475 | void BufferLayerConsumer::abandonLocked() { |
| 476 | BLC_LOGV("abandonLocked"); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 477 | mCurrentTextureBuffer.clear(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 478 | ConsumerBase::abandonLocked(); |
| 479 | } |
| 480 | |
| 481 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 482 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 483 | } |
| 484 | |
| 485 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 486 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 487 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 488 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 489 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 490 | mCurrentTransform); |
| 491 | |
| 492 | ConsumerBase::dumpLocked(result, prefix); |
| 493 | } |
| 494 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 495 | }; // namespace android |