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