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; |
| 275 | mCurrentTextureImageFreed = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 276 | mCurrentCrop = item.mCrop; |
| 277 | mCurrentTransform = item.mTransform; |
| 278 | mCurrentScalingMode = item.mScalingMode; |
| 279 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 280 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 281 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 282 | mCurrentFence = item.mFence; |
| 283 | mCurrentFenceTime = item.mFenceTime; |
| 284 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 285 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 286 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 287 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 288 | |
| 289 | computeCurrentTransformMatrixLocked(); |
| 290 | |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 295 | ATRACE_CALL(); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 296 | |
Alec Mouri | 79108df | 2019-02-04 19:33:44 +0000 | [diff] [blame] | 297 | mRE.checkErrors(); |
| 298 | |
| 299 | // It is possible for the current slot's buffer to be freed before a new one |
| 300 | // is bound. In that scenario we still want to bind the image. |
| 301 | if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureBuffer == nullptr) { |
| 302 | BLC_LOGE("bindTextureImage: no currently-bound texture"); |
| 303 | mRE.bindExternalTextureImage(mTexName, *mRE.createImage()); |
| 304 | return NO_INIT; |
| 305 | } |
| 306 | |
| 307 | renderengine::Image* imageToRender; |
| 308 | |
| 309 | // mCurrentTextureImageFreed is non-null iff mCurrentTexture == |
| 310 | // BufferQueue::INVALID_BUFFER_SLOT, so we can omit that check. |
| 311 | if (mCurrentTextureImageFreed) { |
| 312 | imageToRender = mCurrentTextureImageFreed.get(); |
| 313 | } else if (mImages[mCurrentTexture]) { |
| 314 | imageToRender = mImages[mCurrentTexture].get(); |
| 315 | } else { |
| 316 | std::unique_ptr<renderengine::Image> image = mRE.createImage(); |
| 317 | bool success = image->setNativeWindowBuffer(mCurrentTextureBuffer->getNativeBuffer(), |
| 318 | mCurrentTextureBuffer->getUsage() & |
| 319 | GRALLOC_USAGE_PROTECTED); |
| 320 | if (!success) { |
| 321 | BLC_LOGE("bindTextureImage: Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 |
| 322 | " fmt=%d", |
| 323 | mCurrentTextureBuffer->getWidth(), mCurrentTextureBuffer->getHeight(), |
| 324 | mCurrentTextureBuffer->getStride(), mCurrentTextureBuffer->getUsage(), |
| 325 | mCurrentTextureBuffer->getPixelFormat()); |
| 326 | BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture); |
| 327 | mRE.bindExternalTextureImage(mTexName, *image); |
| 328 | return UNKNOWN_ERROR; |
| 329 | } |
| 330 | imageToRender = image.get(); |
| 331 | // Cache the image here so that we can reuse it. |
| 332 | mImages[mCurrentTexture] = std::move(image); |
| 333 | } |
| 334 | |
| 335 | mRE.bindExternalTextureImage(mTexName, *imageToRender); |
| 336 | |
| 337 | // Wait for the new buffer to be ready. |
| 338 | return doFenceWaitLocked(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 341 | status_t BufferLayerConsumer::syncForReleaseLocked(const sp<Fence>& releaseFence) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 342 | BLC_LOGV("syncForReleaseLocked"); |
| 343 | |
| 344 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 345 | if (mRE.useNativeFenceSync() && releaseFence != Fence::NO_FENCE) { |
| 346 | // TODO(alecmouri): fail further upstream if the fence is invalid |
| 347 | if (!releaseFence->isValid()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 348 | BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 349 | return UNKNOWN_ERROR; |
| 350 | } |
Alec Mouri | 86770e5 | 2018-09-24 22:40:58 +0000 | [diff] [blame] | 351 | status_t err = |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 352 | addReleaseFenceLocked(mCurrentTexture, mCurrentTextureBuffer, releaseFence); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 353 | if (err != OK) { |
| 354 | BLC_LOGE("syncForReleaseLocked: error adding release fence: " |
| 355 | "%s (%d)", |
| 356 | strerror(-err), err); |
| 357 | return err; |
| 358 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | return OK; |
| 363 | } |
| 364 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 365 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 366 | Mutex::Autolock lock(mMutex); |
| 367 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 368 | } |
| 369 | |
| 370 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 371 | Mutex::Autolock lock(mMutex); |
| 372 | if (mAbandoned) { |
| 373 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 374 | return; |
| 375 | } |
| 376 | bool needsRecompute = mFilteringEnabled != enabled; |
| 377 | mFilteringEnabled = enabled; |
| 378 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 379 | if (needsRecompute && mCurrentTextureBuffer == nullptr) { |
| 380 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 383 | if (needsRecompute && mCurrentTextureBuffer != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 384 | computeCurrentTransformMatrixLocked(); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 389 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 390 | if (mCurrentTextureBuffer == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 391 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 392 | "mCurrentTextureBuffer is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 393 | } |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 394 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, mCurrentTextureBuffer, mCurrentCrop, |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame] | 395 | mCurrentTransform, mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 398 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 399 | BLC_LOGV("getTimestamp"); |
| 400 | Mutex::Autolock lock(mMutex); |
| 401 | return mCurrentTimestamp; |
| 402 | } |
| 403 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 404 | ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 405 | BLC_LOGV("getCurrentDataSpace"); |
| 406 | Mutex::Autolock lock(mMutex); |
| 407 | return mCurrentDataSpace; |
| 408 | } |
| 409 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 410 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 411 | BLC_LOGV("getCurrentHdrMetadata"); |
| 412 | Mutex::Autolock lock(mMutex); |
| 413 | return mCurrentHdrMetadata; |
| 414 | } |
| 415 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 416 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 417 | BLC_LOGV("getFrameNumber"); |
| 418 | Mutex::Autolock lock(mMutex); |
| 419 | return mCurrentFrameNumber; |
| 420 | } |
| 421 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 422 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 423 | Mutex::Autolock lock(mMutex); |
| 424 | return mCurrentTransformToDisplayInverse; |
| 425 | } |
| 426 | |
| 427 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 428 | return mCurrentSurfaceDamage; |
| 429 | } |
| 430 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 431 | int BufferLayerConsumer::getCurrentApi() const { |
| 432 | Mutex::Autolock lock(mMutex); |
| 433 | return mCurrentApi; |
| 434 | } |
| 435 | |
Alec Mouri | 79108df | 2019-02-04 19:33:44 +0000 | [diff] [blame] | 436 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 437 | Mutex::Autolock lock(mMutex); |
| 438 | |
| 439 | if (outSlot != nullptr) { |
| 440 | *outSlot = mCurrentTexture; |
| 441 | } |
| 442 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 443 | return mCurrentTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 447 | Mutex::Autolock lock(mMutex); |
| 448 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 449 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 450 | : mCurrentCrop; |
| 451 | } |
| 452 | |
| 453 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 454 | Mutex::Autolock lock(mMutex); |
| 455 | return mCurrentTransform; |
| 456 | } |
| 457 | |
| 458 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 459 | Mutex::Autolock lock(mMutex); |
| 460 | return mCurrentScalingMode; |
| 461 | } |
| 462 | |
| 463 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 464 | Mutex::Autolock lock(mMutex); |
| 465 | return mCurrentFence; |
| 466 | } |
| 467 | |
| 468 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 469 | Mutex::Autolock lock(mMutex); |
| 470 | return mCurrentFenceTime; |
| 471 | } |
| 472 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 473 | status_t BufferLayerConsumer::doFenceWaitLocked() const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 474 | if (mCurrentFence->isValid()) { |
Lloyd Pique | 63f9dbf | 2018-06-21 13:13:07 -0700 | [diff] [blame] | 475 | if (mRE.useWaitSync()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 476 | base::unique_fd fenceFd(mCurrentFence->dup()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 477 | if (fenceFd == -1) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 478 | BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 479 | return -errno; |
| 480 | } |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 481 | if (!mRE.waitFence(std::move(fenceFd))) { |
| 482 | BLC_LOGE("doFenceWait: failed to wait on fence fd"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 483 | return UNKNOWN_ERROR; |
| 484 | } |
| 485 | } else { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 486 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 487 | if (err != NO_ERROR) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 488 | BLC_LOGE("doFenceWait: error waiting for fence: %d", err); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 489 | return err; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return NO_ERROR; |
| 495 | } |
| 496 | |
| 497 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 498 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 499 | if (slotIndex == mCurrentTexture) { |
| 500 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 501 | mCurrentTextureImageFreed = std::move(mImages[slotIndex]); |
| 502 | } else { |
| 503 | mImages[slotIndex] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 504 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 505 | ConsumerBase::freeBufferLocked(slotIndex); |
| 506 | } |
| 507 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 508 | void BufferLayerConsumer::onDisconnect() { |
| 509 | sp<Layer> l = mLayer.promote(); |
| 510 | if (l.get()) { |
| 511 | l->onDisconnect(); |
| 512 | } |
| 513 | } |
| 514 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 515 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 516 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 517 | { |
| 518 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 519 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 520 | } |
| 521 | sp<ContentsChangedListener> listener; |
| 522 | { // scope for the lock |
| 523 | Mutex::Autolock lock(mMutex); |
| 524 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 525 | listener = mContentsChangedListener.promote(); |
| 526 | } |
| 527 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 528 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 529 | listener->onSidebandStreamChanged(); |
| 530 | } |
| 531 | } |
| 532 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 533 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 534 | FrameEventHistoryDelta* outDelta) { |
| 535 | sp<Layer> l = mLayer.promote(); |
| 536 | if (l.get()) { |
| 537 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 538 | } |
| 539 | } |
| 540 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 541 | void BufferLayerConsumer::abandonLocked() { |
| 542 | BLC_LOGV("abandonLocked"); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 543 | mCurrentTextureBuffer.clear(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 544 | ConsumerBase::abandonLocked(); |
| 545 | } |
| 546 | |
| 547 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 548 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 549 | } |
| 550 | |
| 551 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 552 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 553 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 554 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 555 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 556 | mCurrentTransform); |
| 557 | |
| 558 | ConsumerBase::dumpLocked(result, prefix); |
| 559 | } |
| 560 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 561 | }; // namespace android |