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