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