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