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 | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 103 | uint64_t maxFrameNumber, |
| 104 | const sp<Fence>& releaseFence) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 105 | ATRACE_CALL(); |
| 106 | BLC_LOGV("updateTexImage"); |
| 107 | Mutex::Autolock lock(mMutex); |
| 108 | |
| 109 | if (mAbandoned) { |
| 110 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 111 | return NO_INIT; |
| 112 | } |
| 113 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 114 | BufferItem item; |
| 115 | |
| 116 | // Acquire the next buffer. |
| 117 | // In asynchronous mode the list is guaranteed to be one buffer |
| 118 | // deep, while in synchronous mode we use the oldest buffer. |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 119 | status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 120 | if (err != NO_ERROR) { |
| 121 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 122 | err = NO_ERROR; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 123 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 124 | // return the error, without logging |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 125 | } else { |
| 126 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 127 | } |
| 128 | return err; |
| 129 | } |
| 130 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 131 | if (autoRefresh) { |
| 132 | *autoRefresh = item.mAutoRefresh; |
| 133 | } |
| 134 | |
| 135 | if (queuedBuffer) { |
| 136 | *queuedBuffer = item.mQueuedBuffer; |
| 137 | } |
| 138 | |
| 139 | // We call the rejecter here, in case the caller has a reason to |
| 140 | // not accept this buffer. This is used by SurfaceFlinger to |
| 141 | // reject buffers which have the wrong size |
| 142 | int slot = item.mSlot; |
| 143 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 144 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
| 145 | return BUFFER_REJECTED; |
| 146 | } |
| 147 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 148 | // Release the previous buffer. |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 149 | err = updateAndReleaseLocked(item, &mPendingRelease, releaseFence); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 150 | if (err != NO_ERROR) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 151 | return err; |
| 152 | } |
| 153 | |
Alec Mouri | 3221a89 | 2018-09-28 20:45:06 +0000 | [diff] [blame] | 154 | if (!mRE.useNativeFenceSync()) { |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 155 | // Bind the new buffer to the GL texture. |
| 156 | // |
| 157 | // Older devices require the "implicit" synchronization provided |
| 158 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 159 | // devices will either call this in Layer::onDraw, or (if it's not |
| 160 | // a GL-composited layer) not at all. |
| 161 | err = bindTextureImageLocked(); |
| 162 | } |
| 163 | |
| 164 | return err; |
| 165 | } |
| 166 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 167 | status_t BufferLayerConsumer::bindTextureImage() { |
| 168 | Mutex::Autolock lock(mMutex); |
| 169 | return bindTextureImageLocked(); |
| 170 | } |
| 171 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 172 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 173 | if (!fence->isValid()) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture; |
| 178 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 179 | return; |
| 180 | } |
| 181 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 182 | auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer : mCurrentTextureBuffer; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 183 | auto err = addReleaseFence(slot, buffer, fence); |
| 184 | if (err != OK) { |
| 185 | BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | bool BufferLayerConsumer::releasePendingBuffer() { |
| 190 | if (!mPendingRelease.isPending) { |
| 191 | BLC_LOGV("Pending buffer already released"); |
| 192 | return false; |
| 193 | } |
| 194 | BLC_LOGV("Releasing pending buffer"); |
| 195 | Mutex::Autolock lock(mMutex); |
| 196 | status_t result = |
| 197 | releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer); |
| 198 | if (result < NO_ERROR) { |
| 199 | BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result); |
| 200 | } |
| 201 | mPendingRelease = PendingRelease(); |
| 202 | return true; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 205 | sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const { |
| 206 | Mutex::Autolock lock(mMutex); |
| 207 | return ConsumerBase::mPrevFinalReleaseFence; |
| 208 | } |
| 209 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 210 | status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 211 | uint64_t maxFrameNumber) { |
| 212 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber); |
| 213 | if (err != NO_ERROR) { |
| 214 | return err; |
| 215 | } |
| 216 | |
| 217 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 218 | // before. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 219 | if (item->mGraphicBuffer != nullptr) { |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 220 | mImages[item->mSlot] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return NO_ERROR; |
| 224 | } |
| 225 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 226 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 227 | PendingRelease* pendingRelease, |
| 228 | const sp<Fence>& releaseFence) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 229 | status_t err = NO_ERROR; |
| 230 | |
| 231 | int slot = item.mSlot; |
| 232 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 233 | // Do whatever sync ops we need to do before releasing the old slot. |
| 234 | if (slot != mCurrentTexture) { |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 235 | err = syncForReleaseLocked(releaseFence); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 236 | if (err != NO_ERROR) { |
| 237 | // Release the buffer we just acquired. It's not safe to |
| 238 | // release the old buffer, so instead we just drop the new frame. |
| 239 | // As we are still under lock since acquireBuffer, it is safe to |
| 240 | // release by slot. |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 241 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 242 | return err; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 247 | mCurrentTextureBuffer != nullptr ? mCurrentTextureBuffer->handle : 0, slot, |
| 248 | mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 249 | |
| 250 | // Hang onto the pointer so that it isn't freed in the call to |
| 251 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 252 | // the same. |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 253 | sp<GraphicBuffer> nextTextureBuffer = mSlots[slot].mGraphicBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 254 | |
| 255 | // release old buffer |
| 256 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 257 | if (pendingRelease == nullptr) { |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 258 | status_t status = releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 259 | if (status < NO_ERROR) { |
| 260 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 261 | status); |
| 262 | err = status; |
| 263 | // keep going, with error raised [?] |
| 264 | } |
| 265 | } else { |
| 266 | pendingRelease->currentTexture = mCurrentTexture; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 267 | pendingRelease->graphicBuffer = mCurrentTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 268 | pendingRelease->isPending = true; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Update the BufferLayerConsumer state. |
| 273 | mCurrentTexture = slot; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 274 | mCurrentTextureBuffer = nextTextureBuffer; |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 275 | mCurrentTextureBufferStaleForGpu = false; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 276 | mCurrentTextureImageFreed = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 277 | mCurrentCrop = item.mCrop; |
| 278 | mCurrentTransform = item.mTransform; |
| 279 | mCurrentScalingMode = item.mScalingMode; |
| 280 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 281 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 282 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 283 | mCurrentFence = item.mFence; |
| 284 | mCurrentFenceTime = item.mFenceTime; |
| 285 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 286 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 287 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 288 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 289 | |
| 290 | computeCurrentTransformMatrixLocked(); |
| 291 | |
| 292 | return err; |
| 293 | } |
| 294 | |
| 295 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 296 | ATRACE_CALL(); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 297 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 298 | return mRE.bindExternalTextureBuffer(mTexName, mCurrentTextureBuffer, mCurrentFence, false); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 301 | status_t BufferLayerConsumer::syncForReleaseLocked(const sp<Fence>& releaseFence) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 302 | BLC_LOGV("syncForReleaseLocked"); |
| 303 | |
| 304 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 305 | if (mRE.useNativeFenceSync() && releaseFence != Fence::NO_FENCE) { |
| 306 | // TODO(alecmouri): fail further upstream if the fence is invalid |
| 307 | if (!releaseFence->isValid()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 308 | BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 309 | return UNKNOWN_ERROR; |
| 310 | } |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 311 | status_t err = |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 312 | addReleaseFenceLocked(mCurrentTexture, mCurrentTextureBuffer, releaseFence); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 313 | if (err != OK) { |
| 314 | BLC_LOGE("syncForReleaseLocked: error adding release fence: " |
| 315 | "%s (%d)", |
| 316 | strerror(-err), err); |
| 317 | return err; |
| 318 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | return OK; |
| 323 | } |
| 324 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 325 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 326 | Mutex::Autolock lock(mMutex); |
| 327 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 328 | } |
| 329 | |
| 330 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 331 | Mutex::Autolock lock(mMutex); |
| 332 | if (mAbandoned) { |
| 333 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 334 | return; |
| 335 | } |
| 336 | bool needsRecompute = mFilteringEnabled != enabled; |
| 337 | mFilteringEnabled = enabled; |
| 338 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 339 | if (needsRecompute && mCurrentTextureBuffer == nullptr) { |
| 340 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 343 | if (needsRecompute && mCurrentTextureBuffer != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 344 | computeCurrentTransformMatrixLocked(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 349 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 350 | if (mCurrentTextureBuffer == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 351 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 352 | "mCurrentTextureBuffer is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 353 | } |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 354 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, mCurrentTextureBuffer, mCurrentCrop, |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 355 | mCurrentTransform, mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 358 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 359 | BLC_LOGV("getTimestamp"); |
| 360 | Mutex::Autolock lock(mMutex); |
| 361 | return mCurrentTimestamp; |
| 362 | } |
| 363 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 364 | ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 365 | BLC_LOGV("getCurrentDataSpace"); |
| 366 | Mutex::Autolock lock(mMutex); |
| 367 | return mCurrentDataSpace; |
| 368 | } |
| 369 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 370 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 371 | BLC_LOGV("getCurrentHdrMetadata"); |
| 372 | Mutex::Autolock lock(mMutex); |
| 373 | return mCurrentHdrMetadata; |
| 374 | } |
| 375 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 376 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 377 | BLC_LOGV("getFrameNumber"); |
| 378 | Mutex::Autolock lock(mMutex); |
| 379 | return mCurrentFrameNumber; |
| 380 | } |
| 381 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 382 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 383 | Mutex::Autolock lock(mMutex); |
| 384 | return mCurrentTransformToDisplayInverse; |
| 385 | } |
| 386 | |
| 387 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 388 | return mCurrentSurfaceDamage; |
| 389 | } |
| 390 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 391 | int BufferLayerConsumer::getCurrentApi() const { |
| 392 | Mutex::Autolock lock(mMutex); |
| 393 | return mCurrentApi; |
| 394 | } |
| 395 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 396 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 397 | Mutex::Autolock lock(mMutex); |
| 398 | |
| 399 | if (outSlot != nullptr) { |
| 400 | *outSlot = mCurrentTexture; |
| 401 | } |
| 402 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 403 | if (outFence != nullptr) { |
| 404 | *outFence = mCurrentFence; |
| 405 | } |
| 406 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 407 | return mCurrentTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame^] | 410 | bool BufferLayerConsumer::getAndSetCurrentBufferCacheHint() { |
| 411 | Mutex::Autolock lock(mMutex); |
| 412 | bool useCache = mCurrentTextureBufferStaleForGpu; |
| 413 | // Set the staleness bit here, as this function is only called during a |
| 414 | // client composition path. |
| 415 | mCurrentTextureBufferStaleForGpu = true; |
| 416 | return useCache; |
| 417 | } |
| 418 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 419 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 420 | Mutex::Autolock lock(mMutex); |
| 421 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 422 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 423 | : mCurrentCrop; |
| 424 | } |
| 425 | |
| 426 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 427 | Mutex::Autolock lock(mMutex); |
| 428 | return mCurrentTransform; |
| 429 | } |
| 430 | |
| 431 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 432 | Mutex::Autolock lock(mMutex); |
| 433 | return mCurrentScalingMode; |
| 434 | } |
| 435 | |
| 436 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 437 | Mutex::Autolock lock(mMutex); |
| 438 | return mCurrentFence; |
| 439 | } |
| 440 | |
| 441 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 442 | Mutex::Autolock lock(mMutex); |
| 443 | return mCurrentFenceTime; |
| 444 | } |
| 445 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 446 | status_t BufferLayerConsumer::doFenceWaitLocked() const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 447 | if (mCurrentFence->isValid()) { |
Lloyd Pique | 63f9dbf | 2018-06-21 13:13:07 -0700 | [diff] [blame] | 448 | if (mRE.useWaitSync()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 449 | base::unique_fd fenceFd(mCurrentFence->dup()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 450 | if (fenceFd == -1) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 451 | BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 452 | return -errno; |
| 453 | } |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 454 | if (!mRE.waitFence(std::move(fenceFd))) { |
| 455 | BLC_LOGE("doFenceWait: failed to wait on fence fd"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 456 | return UNKNOWN_ERROR; |
| 457 | } |
| 458 | } else { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 459 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 460 | if (err != NO_ERROR) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 461 | BLC_LOGE("doFenceWait: error waiting for fence: %d", err); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 462 | return err; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return NO_ERROR; |
| 468 | } |
| 469 | |
| 470 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 471 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 472 | if (slotIndex == mCurrentTexture) { |
| 473 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 474 | mCurrentTextureImageFreed = std::move(mImages[slotIndex]); |
| 475 | } else { |
| 476 | mImages[slotIndex] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 477 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 478 | ConsumerBase::freeBufferLocked(slotIndex); |
| 479 | } |
| 480 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 481 | void BufferLayerConsumer::onDisconnect() { |
| 482 | sp<Layer> l = mLayer.promote(); |
| 483 | if (l.get()) { |
| 484 | l->onDisconnect(); |
| 485 | } |
| 486 | } |
| 487 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 488 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 489 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 490 | { |
| 491 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 492 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 493 | } |
| 494 | sp<ContentsChangedListener> listener; |
| 495 | { // scope for the lock |
| 496 | Mutex::Autolock lock(mMutex); |
| 497 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 498 | listener = mContentsChangedListener.promote(); |
| 499 | } |
| 500 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 501 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 502 | listener->onSidebandStreamChanged(); |
| 503 | } |
| 504 | } |
| 505 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 506 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 507 | FrameEventHistoryDelta* outDelta) { |
| 508 | sp<Layer> l = mLayer.promote(); |
| 509 | if (l.get()) { |
| 510 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 511 | } |
| 512 | } |
| 513 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 514 | void BufferLayerConsumer::abandonLocked() { |
| 515 | BLC_LOGV("abandonLocked"); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 516 | mCurrentTextureBuffer.clear(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 517 | ConsumerBase::abandonLocked(); |
| 518 | } |
| 519 | |
| 520 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 521 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 522 | } |
| 523 | |
| 524 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 525 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 526 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 527 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 528 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 529 | mCurrentTransform); |
| 530 | |
| 531 | ConsumerBase::dumpLocked(result, prefix); |
| 532 | } |
| 533 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 534 | }; // namespace android |