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