Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "BufferLayerConsumer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include "BufferLayerConsumer.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 23 | #include "Layer.h" |
Ana Krulec | fefcb58 | 2018-08-07 14:22:37 -0700 | [diff] [blame] | 24 | #include "Scheduler/DispSync.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 25 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
| 27 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 28 | #include <cutils/compiler.h> |
| 29 | |
| 30 | #include <hardware/hardware.h> |
| 31 | |
| 32 | #include <math/mat4.h> |
| 33 | |
| 34 | #include <gui/BufferItem.h> |
| 35 | #include <gui/GLConsumer.h> |
| 36 | #include <gui/ISurfaceComposer.h> |
| 37 | #include <gui/SurfaceComposerClient.h> |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 38 | #include <private/gui/ComposerService.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 39 | #include <renderengine/Image.h> |
| 40 | #include <renderengine/RenderEngine.h> |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 41 | #include <utils/Log.h> |
| 42 | #include <utils/String8.h> |
| 43 | #include <utils/Trace.h> |
| 44 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | |
| 47 | // Macros for including the BufferLayerConsumer name in log messages |
| 48 | #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 49 | #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 50 | //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 51 | #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 52 | #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 53 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 54 | static const mat4 mtxIdentity; |
| 55 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 56 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 57 | renderengine::RenderEngine& engine, uint32_t tex, |
| 58 | Layer* layer) |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 59 | : ConsumerBase(bq, false), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 60 | mCurrentCrop(Rect::EMPTY_RECT), |
| 61 | mCurrentTransform(0), |
| 62 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 63 | mCurrentFence(Fence::NO_FENCE), |
| 64 | mCurrentTimestamp(0), |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 65 | mCurrentDataSpace(ui::Dataspace::UNKNOWN), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 66 | mCurrentFrameNumber(0), |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 67 | mCurrentTransformToDisplayInverse(false), |
| 68 | mCurrentSurfaceDamage(), |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 69 | mCurrentApi(0), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 70 | mDefaultWidth(1), |
| 71 | mDefaultHeight(1), |
| 72 | mFilteringEnabled(true), |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 73 | mRE(engine), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 74 | mTexName(tex), |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 75 | mLayer(layer), |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 76 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 77 | BLC_LOGV("BufferLayerConsumer"); |
| 78 | |
| 79 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 80 | |
| 81 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 82 | } |
| 83 | |
| 84 | status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 85 | Mutex::Autolock lock(mMutex); |
| 86 | if (mAbandoned) { |
| 87 | BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!"); |
| 88 | return NO_INIT; |
| 89 | } |
| 90 | mDefaultWidth = w; |
| 91 | mDefaultHeight = h; |
| 92 | return mConsumer->setDefaultBufferSize(w, h); |
| 93 | } |
| 94 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 95 | void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) { |
| 96 | setFrameAvailableListener(listener); |
| 97 | Mutex::Autolock lock(mMutex); |
| 98 | mContentsChangedListener = listener; |
| 99 | } |
| 100 | |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 101 | status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, nsecs_t expectedPresentTime, |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 102 | bool* autoRefresh, bool* queuedBuffer, |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 103 | uint64_t maxFrameNumber) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 104 | ATRACE_CALL(); |
| 105 | BLC_LOGV("updateTexImage"); |
| 106 | Mutex::Autolock lock(mMutex); |
| 107 | |
| 108 | if (mAbandoned) { |
| 109 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 110 | return NO_INIT; |
| 111 | } |
| 112 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 113 | BufferItem item; |
| 114 | |
| 115 | // Acquire the next buffer. |
| 116 | // In asynchronous mode the list is guaranteed to be one buffer |
| 117 | // deep, while in synchronous mode we use the oldest buffer. |
Ana Krulec | 010d219 | 2018-10-08 06:29:54 -0700 | [diff] [blame] | 118 | status_t err = acquireBufferLocked(&item, expectedPresentTime, maxFrameNumber); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 119 | if (err != NO_ERROR) { |
| 120 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 121 | err = NO_ERROR; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 122 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 123 | // return the error, without logging |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 124 | } else { |
| 125 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 126 | } |
| 127 | return err; |
| 128 | } |
| 129 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 130 | if (autoRefresh) { |
| 131 | *autoRefresh = item.mAutoRefresh; |
| 132 | } |
| 133 | |
| 134 | if (queuedBuffer) { |
| 135 | *queuedBuffer = item.mQueuedBuffer; |
| 136 | } |
| 137 | |
| 138 | // We call the rejecter here, in case the caller has a reason to |
| 139 | // not accept this buffer. This is used by SurfaceFlinger to |
| 140 | // reject buffers which have the wrong size |
| 141 | int slot = item.mSlot; |
| 142 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 143 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
| 144 | return BUFFER_REJECTED; |
| 145 | } |
| 146 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 147 | // Release the previous buffer. |
Alec Mouri | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 148 | err = updateAndReleaseLocked(item, &mPendingRelease); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 149 | if (err != NO_ERROR) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 150 | return err; |
| 151 | } |
| 152 | |
Alec Mouri | 3221a89 | 2018-09-28 20:45:06 +0000 | [diff] [blame] | 153 | if (!mRE.useNativeFenceSync()) { |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 154 | // Bind the new buffer to the GL texture. |
| 155 | // |
| 156 | // Older devices require the "implicit" synchronization provided |
| 157 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 158 | // devices will either call this in Layer::onDraw, or (if it's not |
| 159 | // a GL-composited layer) not at all. |
| 160 | err = bindTextureImageLocked(); |
| 161 | } |
| 162 | |
| 163 | return err; |
| 164 | } |
| 165 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 166 | status_t BufferLayerConsumer::bindTextureImage() { |
| 167 | Mutex::Autolock lock(mMutex); |
| 168 | return bindTextureImageLocked(); |
| 169 | } |
| 170 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 171 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 172 | if (!fence->isValid()) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture; |
| 177 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 178 | return; |
| 179 | } |
| 180 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 181 | auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer |
| 182 | : mCurrentTextureBuffer->graphicBuffer(); |
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 | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 218 | // before, so we need to clean up old references. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 219 | if (item->mGraphicBuffer != nullptr) { |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 220 | mImages[item->mSlot] = std::make_shared<Image>(item->mGraphicBuffer, mRE); |
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 | 56e538f | 2019-01-14 15:22:01 -0800 | [diff] [blame] | 227 | PendingRelease* pendingRelease) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 228 | status_t err = NO_ERROR; |
| 229 | |
| 230 | int slot = item.mSlot; |
| 231 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 232 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 233 | (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr) |
| 234 | ? mCurrentTextureBuffer->graphicBuffer()->handle |
| 235 | : 0, |
| 236 | slot, mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 237 | |
| 238 | // Hang onto the pointer so that it isn't freed in the call to |
| 239 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 240 | // the same. |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 241 | std::shared_ptr<Image> nextTextureBuffer = mImages[slot]; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 242 | |
| 243 | // release old buffer |
| 244 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 245 | if (pendingRelease == nullptr) { |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 246 | status_t status = |
| 247 | releaseBufferLocked(mCurrentTexture, mCurrentTextureBuffer->graphicBuffer()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 248 | if (status < NO_ERROR) { |
| 249 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 250 | status); |
| 251 | err = status; |
| 252 | // keep going, with error raised [?] |
| 253 | } |
| 254 | } else { |
| 255 | pendingRelease->currentTexture = mCurrentTexture; |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 256 | pendingRelease->graphicBuffer = mCurrentTextureBuffer->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 257 | pendingRelease->isPending = true; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Update the BufferLayerConsumer state. |
| 262 | mCurrentTexture = slot; |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 263 | mCurrentTextureBuffer = nextTextureBuffer; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 264 | mCurrentCrop = item.mCrop; |
| 265 | mCurrentTransform = item.mTransform; |
| 266 | mCurrentScalingMode = item.mScalingMode; |
| 267 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 268 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 269 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 270 | mCurrentFence = item.mFence; |
| 271 | mCurrentFenceTime = item.mFenceTime; |
| 272 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 273 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 274 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 275 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 276 | |
| 277 | computeCurrentTransformMatrixLocked(); |
| 278 | |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 283 | ATRACE_CALL(); |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 284 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 285 | if (mCurrentTextureBuffer != nullptr && mCurrentTextureBuffer->graphicBuffer() != nullptr) { |
| 286 | return mRE.bindExternalTextureBuffer(mTexName, mCurrentTextureBuffer->graphicBuffer(), |
| 287 | mCurrentFence); |
| 288 | } |
| 289 | |
| 290 | return NO_INIT; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 293 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 294 | Mutex::Autolock lock(mMutex); |
| 295 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 296 | } |
| 297 | |
| 298 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 299 | Mutex::Autolock lock(mMutex); |
| 300 | if (mAbandoned) { |
| 301 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 302 | return; |
| 303 | } |
| 304 | bool needsRecompute = mFilteringEnabled != enabled; |
| 305 | mFilteringEnabled = enabled; |
| 306 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 307 | if (needsRecompute && mCurrentTextureBuffer == nullptr) { |
| 308 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureBuffer == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 311 | if (needsRecompute && mCurrentTextureBuffer != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 312 | computeCurrentTransformMatrixLocked(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 317 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 318 | if (mCurrentTextureBuffer == nullptr || mCurrentTextureBuffer->graphicBuffer() == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 319 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Alec Mouri | 39801c0 | 2018-10-10 10:44:47 -0700 | [diff] [blame] | 320 | "mCurrentTextureBuffer is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 321 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 322 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, |
| 323 | mCurrentTextureBuffer == nullptr |
| 324 | ? nullptr |
| 325 | : mCurrentTextureBuffer->graphicBuffer(), |
| 326 | mCurrentCrop, mCurrentTransform, mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 329 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 330 | BLC_LOGV("getTimestamp"); |
| 331 | Mutex::Autolock lock(mMutex); |
| 332 | return mCurrentTimestamp; |
| 333 | } |
| 334 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 335 | ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 336 | BLC_LOGV("getCurrentDataSpace"); |
| 337 | Mutex::Autolock lock(mMutex); |
| 338 | return mCurrentDataSpace; |
| 339 | } |
| 340 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 341 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 342 | BLC_LOGV("getCurrentHdrMetadata"); |
| 343 | Mutex::Autolock lock(mMutex); |
| 344 | return mCurrentHdrMetadata; |
| 345 | } |
| 346 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 347 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 348 | BLC_LOGV("getFrameNumber"); |
| 349 | Mutex::Autolock lock(mMutex); |
| 350 | return mCurrentFrameNumber; |
| 351 | } |
| 352 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 353 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 354 | Mutex::Autolock lock(mMutex); |
| 355 | return mCurrentTransformToDisplayInverse; |
| 356 | } |
| 357 | |
| 358 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 359 | return mCurrentSurfaceDamage; |
| 360 | } |
| 361 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 362 | int BufferLayerConsumer::getCurrentApi() const { |
| 363 | Mutex::Autolock lock(mMutex); |
| 364 | return mCurrentApi; |
| 365 | } |
| 366 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 367 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot, sp<Fence>* outFence) const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 368 | Mutex::Autolock lock(mMutex); |
| 369 | |
| 370 | if (outSlot != nullptr) { |
| 371 | *outSlot = mCurrentTexture; |
| 372 | } |
| 373 | |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 374 | if (outFence != nullptr) { |
| 375 | *outFence = mCurrentFence; |
| 376 | } |
| 377 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 378 | return mCurrentTextureBuffer == nullptr ? nullptr : mCurrentTextureBuffer->graphicBuffer(); |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 381 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 382 | Mutex::Autolock lock(mMutex); |
| 383 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 384 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 385 | : mCurrentCrop; |
| 386 | } |
| 387 | |
| 388 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 389 | Mutex::Autolock lock(mMutex); |
| 390 | return mCurrentTransform; |
| 391 | } |
| 392 | |
| 393 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 394 | Mutex::Autolock lock(mMutex); |
| 395 | return mCurrentScalingMode; |
| 396 | } |
| 397 | |
| 398 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 399 | Mutex::Autolock lock(mMutex); |
| 400 | return mCurrentFence; |
| 401 | } |
| 402 | |
| 403 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 404 | Mutex::Autolock lock(mMutex); |
| 405 | return mCurrentFenceTime; |
| 406 | } |
| 407 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 408 | status_t BufferLayerConsumer::doFenceWaitLocked() const { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 409 | if (mCurrentFence->isValid()) { |
Lloyd Pique | 63f9dbf | 2018-06-21 13:13:07 -0700 | [diff] [blame] | 410 | if (mRE.useWaitSync()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 411 | base::unique_fd fenceFd(mCurrentFence->dup()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 412 | if (fenceFd == -1) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 413 | BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 414 | return -errno; |
| 415 | } |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 416 | if (!mRE.waitFence(std::move(fenceFd))) { |
| 417 | BLC_LOGE("doFenceWait: failed to wait on fence fd"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 418 | return UNKNOWN_ERROR; |
| 419 | } |
| 420 | } else { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 421 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 422 | if (err != NO_ERROR) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 423 | BLC_LOGE("doFenceWait: error waiting for fence: %d", err); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 424 | return err; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return NO_ERROR; |
| 430 | } |
| 431 | |
| 432 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 433 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 434 | if (slotIndex == mCurrentTexture) { |
| 435 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 436 | } |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 437 | mImages[slotIndex] = nullptr; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 438 | ConsumerBase::freeBufferLocked(slotIndex); |
| 439 | } |
| 440 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 441 | void BufferLayerConsumer::onDisconnect() { |
| 442 | sp<Layer> l = mLayer.promote(); |
| 443 | if (l.get()) { |
| 444 | l->onDisconnect(); |
| 445 | } |
| 446 | } |
| 447 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 448 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 449 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 450 | { |
| 451 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 452 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 453 | } |
| 454 | sp<ContentsChangedListener> listener; |
| 455 | { // scope for the lock |
| 456 | Mutex::Autolock lock(mMutex); |
| 457 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 458 | listener = mContentsChangedListener.promote(); |
| 459 | } |
| 460 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 461 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 462 | listener->onSidebandStreamChanged(); |
| 463 | } |
| 464 | } |
| 465 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 466 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 467 | FrameEventHistoryDelta* outDelta) { |
| 468 | sp<Layer> l = mLayer.promote(); |
| 469 | if (l.get()) { |
| 470 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 471 | } |
| 472 | } |
| 473 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 474 | void BufferLayerConsumer::abandonLocked() { |
| 475 | BLC_LOGV("abandonLocked"); |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 476 | mCurrentTextureBuffer = nullptr; |
| 477 | for (int i = 0; i < BufferQueue::NUM_BUFFER_SLOTS; i++) { |
| 478 | mImages[i] = nullptr; |
| 479 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 480 | ConsumerBase::abandonLocked(); |
| 481 | } |
| 482 | |
| 483 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 484 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 485 | } |
| 486 | |
| 487 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 488 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 489 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 490 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 491 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 492 | mCurrentTransform); |
| 493 | |
| 494 | ConsumerBase::dumpLocked(result, prefix); |
| 495 | } |
| 496 | |
Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame^] | 497 | BufferLayerConsumer::Image::~Image() { |
| 498 | if (mGraphicBuffer != nullptr) { |
| 499 | ALOGE("Destroying buffer: %" PRId64, mGraphicBuffer->getId()); |
| 500 | mRE.unbindExternalTextureBuffer(mGraphicBuffer->getId()); |
| 501 | } |
| 502 | } |
| 503 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 504 | }; // namespace android |