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" |
| 23 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 24 | #include "DispSync.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 25 | #include "Layer.h" |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 26 | #include "RenderEngine/Image.h" |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 27 | #include "RenderEngine/RenderEngine.h" |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 28 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 29 | #include <inttypes.h> |
| 30 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 31 | #include <cutils/compiler.h> |
| 32 | |
| 33 | #include <hardware/hardware.h> |
| 34 | |
| 35 | #include <math/mat4.h> |
| 36 | |
| 37 | #include <gui/BufferItem.h> |
| 38 | #include <gui/GLConsumer.h> |
| 39 | #include <gui/ISurfaceComposer.h> |
| 40 | #include <gui/SurfaceComposerClient.h> |
| 41 | |
| 42 | #include <private/gui/ComposerService.h> |
| 43 | #include <private/gui/SyncFeatures.h> |
| 44 | |
| 45 | #include <utils/Log.h> |
| 46 | #include <utils/String8.h> |
| 47 | #include <utils/Trace.h> |
| 48 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
| 51 | // Macros for including the BufferLayerConsumer name in log messages |
| 52 | #define BLC_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 53 | #define BLC_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 54 | //#define BLC_LOGI(x, ...) ALOGI("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 55 | #define BLC_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 56 | #define BLC_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__) |
| 57 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 58 | static const mat4 mtxIdentity; |
| 59 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 60 | BufferLayerConsumer::BufferLayerConsumer(const sp<IGraphicBufferConsumer>& bq, |
| 61 | RE::RenderEngine& engine, uint32_t tex, Layer* layer) |
Chia-I Wu | bd854bf | 2017-11-27 13:41:26 -0800 | [diff] [blame] | 62 | : ConsumerBase(bq, false), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 63 | mCurrentCrop(Rect::EMPTY_RECT), |
| 64 | mCurrentTransform(0), |
| 65 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 66 | mCurrentFence(Fence::NO_FENCE), |
| 67 | mCurrentTimestamp(0), |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 68 | mCurrentDataSpace(ui::Dataspace::UNKNOWN), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 69 | mCurrentFrameNumber(0), |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 70 | mCurrentTransformToDisplayInverse(false), |
| 71 | mCurrentSurfaceDamage(), |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 72 | mCurrentApi(0), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 73 | mDefaultWidth(1), |
| 74 | mDefaultHeight(1), |
| 75 | mFilteringEnabled(true), |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 76 | mRE(engine), |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 77 | mTexName(tex), |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 78 | mLayer(layer), |
Chia-I Wu | c91077c | 2017-11-27 13:32:04 -0800 | [diff] [blame] | 79 | mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 80 | BLC_LOGV("BufferLayerConsumer"); |
| 81 | |
| 82 | memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix)); |
| 83 | |
| 84 | mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS); |
| 85 | } |
| 86 | |
| 87 | status_t BufferLayerConsumer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
| 88 | Mutex::Autolock lock(mMutex); |
| 89 | if (mAbandoned) { |
| 90 | BLC_LOGE("setDefaultBufferSize: BufferLayerConsumer is abandoned!"); |
| 91 | return NO_INIT; |
| 92 | } |
| 93 | mDefaultWidth = w; |
| 94 | mDefaultHeight = h; |
| 95 | return mConsumer->setDefaultBufferSize(w, h); |
| 96 | } |
| 97 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 98 | void BufferLayerConsumer::setContentsChangedListener(const wp<ContentsChangedListener>& listener) { |
| 99 | setFrameAvailableListener(listener); |
| 100 | Mutex::Autolock lock(mMutex); |
| 101 | mContentsChangedListener = listener; |
| 102 | } |
| 103 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 104 | // We need to determine the time when a buffer acquired now will be |
| 105 | // displayed. This can be calculated: |
| 106 | // time when previous buffer's actual-present fence was signaled |
| 107 | // + current display refresh rate * HWC latency |
| 108 | // + a little extra padding |
| 109 | // |
| 110 | // Buffer producers are expected to set their desired presentation time |
| 111 | // based on choreographer time stamps, which (coming from vsync events) |
| 112 | // will be slightly later then the actual-present timing. If we get a |
| 113 | // desired-present time that is unintentionally a hair after the next |
| 114 | // vsync, we'll hold the frame when we really want to display it. We |
| 115 | // need to take the offset between actual-present and reported-vsync |
| 116 | // into account. |
| 117 | // |
| 118 | // If the system is configured without a DispSync phase offset for the app, |
| 119 | // we also want to throw in a bit of padding to avoid edge cases where we |
| 120 | // just barely miss. We want to do it here, not in every app. A major |
| 121 | // source of trouble is the app's use of the display's ideal refresh time |
| 122 | // (via Display.getRefreshRate()), which could be off of the actual refresh |
| 123 | // by a few percent, with the error multiplied by the number of frames |
| 124 | // between now and when the buffer should be displayed. |
| 125 | // |
| 126 | // If the refresh reported to the app has a phase offset, we shouldn't need |
| 127 | // to tweak anything here. |
| 128 | nsecs_t BufferLayerConsumer::computeExpectedPresent(const DispSync& dispSync) { |
| 129 | // The HWC doesn't currently have a way to report additional latency. |
| 130 | // Assume that whatever we submit now will appear right after the flip. |
| 131 | // For a smart panel this might be 1. This is expressed in frames, |
| 132 | // rather than time, because we expect to have a constant frame delay |
| 133 | // regardless of the refresh rate. |
| 134 | const uint32_t hwcLatency = 0; |
| 135 | |
| 136 | // Ask DispSync when the next refresh will be (CLOCK_MONOTONIC). |
| 137 | const nsecs_t nextRefresh = dispSync.computeNextRefresh(hwcLatency); |
| 138 | |
| 139 | // The DispSync time is already adjusted for the difference between |
| 140 | // vsync and reported-vsync (SurfaceFlinger::dispSyncPresentTimeOffset), so |
| 141 | // we don't need to factor that in here. Pad a little to avoid |
| 142 | // weird effects if apps might be requesting times right on the edge. |
| 143 | nsecs_t extraPadding = 0; |
| 144 | if (SurfaceFlinger::vsyncPhaseOffsetNs == 0) { |
| 145 | extraPadding = 1000000; // 1ms (6% of 60Hz) |
| 146 | } |
| 147 | |
| 148 | return nextRefresh + extraPadding; |
| 149 | } |
| 150 | |
| 151 | status_t BufferLayerConsumer::updateTexImage(BufferRejecter* rejecter, const DispSync& dispSync, |
| 152 | bool* autoRefresh, bool* queuedBuffer, |
| 153 | uint64_t maxFrameNumber) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 154 | ATRACE_CALL(); |
| 155 | BLC_LOGV("updateTexImage"); |
| 156 | Mutex::Autolock lock(mMutex); |
| 157 | |
| 158 | if (mAbandoned) { |
| 159 | BLC_LOGE("updateTexImage: BufferLayerConsumer is abandoned!"); |
| 160 | return NO_INIT; |
| 161 | } |
| 162 | |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 163 | // Make sure RenderEngine is current |
| 164 | if (!mRE.isCurrent()) { |
| 165 | BLC_LOGE("updateTexImage: RenderEngine is not current"); |
| 166 | return INVALID_OPERATION; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | BufferItem item; |
| 170 | |
| 171 | // Acquire the next buffer. |
| 172 | // In asynchronous mode the list is guaranteed to be one buffer |
| 173 | // deep, while in synchronous mode we use the oldest buffer. |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 174 | status_t err = acquireBufferLocked(&item, computeExpectedPresent(dispSync), maxFrameNumber); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 175 | if (err != NO_ERROR) { |
| 176 | if (err == BufferQueue::NO_BUFFER_AVAILABLE) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 177 | err = NO_ERROR; |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 178 | } else if (err == BufferQueue::PRESENT_LATER) { |
| 179 | // return the error, without logging |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 180 | } else { |
| 181 | BLC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err); |
| 182 | } |
| 183 | return err; |
| 184 | } |
| 185 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 186 | if (autoRefresh) { |
| 187 | *autoRefresh = item.mAutoRefresh; |
| 188 | } |
| 189 | |
| 190 | if (queuedBuffer) { |
| 191 | *queuedBuffer = item.mQueuedBuffer; |
| 192 | } |
| 193 | |
| 194 | // We call the rejecter here, in case the caller has a reason to |
| 195 | // not accept this buffer. This is used by SurfaceFlinger to |
| 196 | // reject buffers which have the wrong size |
| 197 | int slot = item.mSlot; |
| 198 | if (rejecter && rejecter->reject(mSlots[slot].mGraphicBuffer, item)) { |
| 199 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
| 200 | return BUFFER_REJECTED; |
| 201 | } |
| 202 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 203 | // Release the previous buffer. |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 204 | err = updateAndReleaseLocked(item, &mPendingRelease); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 205 | if (err != NO_ERROR) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 206 | return err; |
| 207 | } |
| 208 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 209 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
| 210 | // Bind the new buffer to the GL texture. |
| 211 | // |
| 212 | // Older devices require the "implicit" synchronization provided |
| 213 | // by glEGLImageTargetTexture2DOES, which this method calls. Newer |
| 214 | // devices will either call this in Layer::onDraw, or (if it's not |
| 215 | // a GL-composited layer) not at all. |
| 216 | err = bindTextureImageLocked(); |
| 217 | } |
| 218 | |
| 219 | return err; |
| 220 | } |
| 221 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 222 | status_t BufferLayerConsumer::bindTextureImage() { |
| 223 | Mutex::Autolock lock(mMutex); |
| 224 | return bindTextureImageLocked(); |
| 225 | } |
| 226 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 227 | void BufferLayerConsumer::setReleaseFence(const sp<Fence>& fence) { |
| 228 | if (!fence->isValid()) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | auto slot = mPendingRelease.isPending ? mPendingRelease.currentTexture : mCurrentTexture; |
| 233 | if (slot == BufferQueue::INVALID_BUFFER_SLOT) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | auto buffer = mPendingRelease.isPending ? mPendingRelease.graphicBuffer |
| 238 | : mCurrentTextureImage->graphicBuffer(); |
| 239 | auto err = addReleaseFence(slot, buffer, fence); |
| 240 | if (err != OK) { |
| 241 | BLC_LOGE("setReleaseFence: failed to add the fence: %s (%d)", strerror(-err), err); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | bool BufferLayerConsumer::releasePendingBuffer() { |
| 246 | if (!mPendingRelease.isPending) { |
| 247 | BLC_LOGV("Pending buffer already released"); |
| 248 | return false; |
| 249 | } |
| 250 | BLC_LOGV("Releasing pending buffer"); |
| 251 | Mutex::Autolock lock(mMutex); |
| 252 | status_t result = |
| 253 | releaseBufferLocked(mPendingRelease.currentTexture, mPendingRelease.graphicBuffer); |
| 254 | if (result < NO_ERROR) { |
| 255 | BLC_LOGE("releasePendingBuffer failed: %s (%d)", strerror(-result), result); |
| 256 | } |
| 257 | mPendingRelease = PendingRelease(); |
| 258 | return true; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 261 | sp<Fence> BufferLayerConsumer::getPrevFinalReleaseFence() const { |
| 262 | Mutex::Autolock lock(mMutex); |
| 263 | return ConsumerBase::mPrevFinalReleaseFence; |
| 264 | } |
| 265 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 266 | status_t BufferLayerConsumer::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen, |
| 267 | uint64_t maxFrameNumber) { |
| 268 | status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber); |
| 269 | if (err != NO_ERROR) { |
| 270 | return err; |
| 271 | } |
| 272 | |
| 273 | // If item->mGraphicBuffer is not null, this buffer has not been acquired |
| 274 | // before, so any prior EglImage created is using a stale buffer. This |
| 275 | // replaces any old EglImage with a new one (using the new buffer). |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 276 | if (item->mGraphicBuffer != nullptr) { |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 277 | mImages[item->mSlot] = new Image(item->mGraphicBuffer, mRE); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | return NO_ERROR; |
| 281 | } |
| 282 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 283 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
| 284 | PendingRelease* pendingRelease) { |
| 285 | status_t err = NO_ERROR; |
| 286 | |
| 287 | int slot = item.mSlot; |
| 288 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 289 | // Do whatever sync ops we need to do before releasing the old slot. |
| 290 | if (slot != mCurrentTexture) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 291 | err = syncForReleaseLocked(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 292 | if (err != NO_ERROR) { |
| 293 | // Release the buffer we just acquired. It's not safe to |
| 294 | // release the old buffer, so instead we just drop the new frame. |
| 295 | // As we are still under lock since acquireBuffer, it is safe to |
| 296 | // release by slot. |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 297 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 298 | return err; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 303 | mCurrentTextureImage != nullptr ? mCurrentTextureImage->graphicBufferHandle() : 0, |
| 304 | slot, mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 305 | |
| 306 | // Hang onto the pointer so that it isn't freed in the call to |
| 307 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 308 | // the same. |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 309 | sp<Image> nextTextureImage = mImages[slot]; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 310 | |
| 311 | // release old buffer |
| 312 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 313 | if (pendingRelease == nullptr) { |
| 314 | status_t status = |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 315 | releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 316 | if (status < NO_ERROR) { |
| 317 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 318 | status); |
| 319 | err = status; |
| 320 | // keep going, with error raised [?] |
| 321 | } |
| 322 | } else { |
| 323 | pendingRelease->currentTexture = mCurrentTexture; |
| 324 | pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 325 | pendingRelease->isPending = true; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Update the BufferLayerConsumer state. |
| 330 | mCurrentTexture = slot; |
| 331 | mCurrentTextureImage = nextTextureImage; |
| 332 | mCurrentCrop = item.mCrop; |
| 333 | mCurrentTransform = item.mTransform; |
| 334 | mCurrentScalingMode = item.mScalingMode; |
| 335 | mCurrentTimestamp = item.mTimestamp; |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 336 | mCurrentDataSpace = static_cast<ui::Dataspace>(item.mDataSpace); |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 337 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 338 | mCurrentFence = item.mFence; |
| 339 | mCurrentFenceTime = item.mFenceTime; |
| 340 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 341 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 342 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 343 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 344 | |
| 345 | computeCurrentTransformMatrixLocked(); |
| 346 | |
| 347 | return err; |
| 348 | } |
| 349 | |
| 350 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 351 | ATRACE_CALL(); |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 352 | mRE.checkErrors(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 353 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 354 | if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 355 | BLC_LOGE("bindTextureImage: no currently-bound texture"); |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 356 | mRE.bindExternalTextureImage(mTexName, *mRE.createImage()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 357 | return NO_INIT; |
| 358 | } |
| 359 | |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 360 | status_t err = mCurrentTextureImage->createIfNeeded(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 361 | if (err != NO_ERROR) { |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 362 | BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture); |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 363 | mRE.bindExternalTextureImage(mTexName, *mRE.createImage()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 364 | return UNKNOWN_ERROR; |
| 365 | } |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 366 | |
| 367 | mRE.bindExternalTextureImage(mTexName, mCurrentTextureImage->image()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 368 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 369 | // Wait for the new buffer to be ready. |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 370 | return doFenceWaitLocked(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 373 | status_t BufferLayerConsumer::syncForReleaseLocked() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 374 | BLC_LOGV("syncForReleaseLocked"); |
| 375 | |
| 376 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 377 | if (SyncFeatures::getInstance().useNativeFenceSync()) { |
Chia-I Wu | df1badd | 2017-12-27 11:10:20 -0800 | [diff] [blame] | 378 | base::unique_fd fenceFd = mRE.flush(); |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 379 | if (fenceFd == -1) { |
| 380 | BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 381 | return UNKNOWN_ERROR; |
| 382 | } |
Chia-I Wu | df1badd | 2017-12-27 11:10:20 -0800 | [diff] [blame] | 383 | sp<Fence> fence(new Fence(std::move(fenceFd))); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 384 | status_t err = addReleaseFenceLocked(mCurrentTexture, |
| 385 | mCurrentTextureImage->graphicBuffer(), fence); |
| 386 | if (err != OK) { |
| 387 | BLC_LOGE("syncForReleaseLocked: error adding release fence: " |
| 388 | "%s (%d)", |
| 389 | strerror(-err), err); |
| 390 | return err; |
| 391 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | return OK; |
| 396 | } |
| 397 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 398 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 399 | Mutex::Autolock lock(mMutex); |
| 400 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 401 | } |
| 402 | |
| 403 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 404 | Mutex::Autolock lock(mMutex); |
| 405 | if (mAbandoned) { |
| 406 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 407 | return; |
| 408 | } |
| 409 | bool needsRecompute = mFilteringEnabled != enabled; |
| 410 | mFilteringEnabled = enabled; |
| 411 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 412 | if (needsRecompute && mCurrentTextureImage == nullptr) { |
| 413 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 416 | if (needsRecompute && mCurrentTextureImage != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 417 | computeCurrentTransformMatrixLocked(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 422 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
| 423 | sp<GraphicBuffer> buf = |
| 424 | (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer(); |
| 425 | if (buf == nullptr) { |
| 426 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 427 | "mCurrentTextureImage is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 428 | } |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 429 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop, |
| 430 | mCurrentTransform, mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 433 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 434 | BLC_LOGV("getTimestamp"); |
| 435 | Mutex::Autolock lock(mMutex); |
| 436 | return mCurrentTimestamp; |
| 437 | } |
| 438 | |
Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 439 | ui::Dataspace BufferLayerConsumer::getCurrentDataSpace() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 440 | BLC_LOGV("getCurrentDataSpace"); |
| 441 | Mutex::Autolock lock(mMutex); |
| 442 | return mCurrentDataSpace; |
| 443 | } |
| 444 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 445 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 446 | BLC_LOGV("getCurrentHdrMetadata"); |
| 447 | Mutex::Autolock lock(mMutex); |
| 448 | return mCurrentHdrMetadata; |
| 449 | } |
| 450 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 451 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 452 | BLC_LOGV("getFrameNumber"); |
| 453 | Mutex::Autolock lock(mMutex); |
| 454 | return mCurrentFrameNumber; |
| 455 | } |
| 456 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 457 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 458 | Mutex::Autolock lock(mMutex); |
| 459 | return mCurrentTransformToDisplayInverse; |
| 460 | } |
| 461 | |
| 462 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 463 | return mCurrentSurfaceDamage; |
| 464 | } |
| 465 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 466 | int BufferLayerConsumer::getCurrentApi() const { |
| 467 | Mutex::Autolock lock(mMutex); |
| 468 | return mCurrentApi; |
| 469 | } |
| 470 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 471 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const { |
| 472 | Mutex::Autolock lock(mMutex); |
| 473 | |
| 474 | if (outSlot != nullptr) { |
| 475 | *outSlot = mCurrentTexture; |
| 476 | } |
| 477 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 478 | return (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 482 | Mutex::Autolock lock(mMutex); |
| 483 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 484 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 485 | : mCurrentCrop; |
| 486 | } |
| 487 | |
| 488 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 489 | Mutex::Autolock lock(mMutex); |
| 490 | return mCurrentTransform; |
| 491 | } |
| 492 | |
| 493 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 494 | Mutex::Autolock lock(mMutex); |
| 495 | return mCurrentScalingMode; |
| 496 | } |
| 497 | |
| 498 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 499 | Mutex::Autolock lock(mMutex); |
| 500 | return mCurrentFence; |
| 501 | } |
| 502 | |
| 503 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 504 | Mutex::Autolock lock(mMutex); |
| 505 | return mCurrentFenceTime; |
| 506 | } |
| 507 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 508 | status_t BufferLayerConsumer::doFenceWaitLocked() const { |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 509 | if (!mRE.isCurrent()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 510 | BLC_LOGE("doFenceWait: RenderEngine is not current"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 511 | return INVALID_OPERATION; |
| 512 | } |
| 513 | |
| 514 | if (mCurrentFence->isValid()) { |
| 515 | if (SyncFeatures::getInstance().useWaitSync()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 516 | base::unique_fd fenceFd(mCurrentFence->dup()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 517 | if (fenceFd == -1) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 518 | BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 519 | return -errno; |
| 520 | } |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 521 | if (!mRE.waitFence(std::move(fenceFd))) { |
| 522 | BLC_LOGE("doFenceWait: failed to wait on fence fd"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 523 | return UNKNOWN_ERROR; |
| 524 | } |
| 525 | } else { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 526 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 527 | if (err != NO_ERROR) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 528 | BLC_LOGE("doFenceWait: error waiting for fence: %d", err); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 529 | return err; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return NO_ERROR; |
| 535 | } |
| 536 | |
| 537 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 538 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 539 | if (slotIndex == mCurrentTexture) { |
| 540 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 541 | } |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 542 | mImages[slotIndex].clear(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 543 | ConsumerBase::freeBufferLocked(slotIndex); |
| 544 | } |
| 545 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 546 | void BufferLayerConsumer::onDisconnect() { |
| 547 | sp<Layer> l = mLayer.promote(); |
| 548 | if (l.get()) { |
| 549 | l->onDisconnect(); |
| 550 | } |
| 551 | } |
| 552 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 553 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 554 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 555 | { |
| 556 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 557 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 558 | } |
| 559 | sp<ContentsChangedListener> listener; |
| 560 | { // scope for the lock |
| 561 | Mutex::Autolock lock(mMutex); |
| 562 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 563 | listener = mContentsChangedListener.promote(); |
| 564 | } |
| 565 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 566 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 567 | listener->onSidebandStreamChanged(); |
| 568 | } |
| 569 | } |
| 570 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 571 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 572 | FrameEventHistoryDelta* outDelta) { |
| 573 | sp<Layer> l = mLayer.promote(); |
| 574 | if (l.get()) { |
| 575 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 576 | } |
| 577 | } |
| 578 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 579 | void BufferLayerConsumer::abandonLocked() { |
| 580 | BLC_LOGV("abandonLocked"); |
| 581 | mCurrentTextureImage.clear(); |
| 582 | ConsumerBase::abandonLocked(); |
| 583 | } |
| 584 | |
| 585 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 586 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 587 | } |
| 588 | |
| 589 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 590 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 591 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 592 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 593 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 594 | mCurrentTransform); |
| 595 | |
| 596 | ConsumerBase::dumpLocked(result, prefix); |
| 597 | } |
| 598 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 599 | BufferLayerConsumer::Image::Image(sp<GraphicBuffer> graphicBuffer, RE::RenderEngine& engine) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 600 | : mGraphicBuffer(graphicBuffer), |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 601 | mImage{engine.createImage()}, |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 602 | mCreated(false), |
| 603 | mCropWidth(0), |
| 604 | mCropHeight(0) {} |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 605 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 606 | BufferLayerConsumer::Image::~Image() = default; |
| 607 | |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 608 | status_t BufferLayerConsumer::Image::createIfNeeded() { |
| 609 | if (mCreated) return OK; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 610 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 611 | mCreated = mImage->setNativeWindowBuffer(mGraphicBuffer->getNativeBuffer(), |
Krzysztof Kosiński | 7108c31 | 2018-06-27 19:12:54 -0700 | [diff] [blame^] | 612 | mGraphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 613 | if (!mCreated) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 614 | const sp<GraphicBuffer>& buffer = mGraphicBuffer; |
| 615 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", |
| 616 | buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(), |
| 617 | buffer->getPixelFormat()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 620 | return mCreated ? OK : UNKNOWN_ERROR; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | }; // namespace android |