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 | 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 | |
Chia-I Wu | da5c730 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 208 | if (!SyncFeatures::getInstance().useNativeFenceSync()) { |
| 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 | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 282 | bool BufferLayerConsumer::canUseImageCrop(const Rect& crop) const { |
| 283 | // If the crop rect is not at the origin, we can't set the crop on the |
| 284 | // EGLImage because that's not allowed by the EGL_ANDROID_image_crop |
| 285 | // extension. In the future we can add a layered extension that |
| 286 | // removes this restriction if there is hardware that can support it. |
| 287 | return mRE.supportsImageCrop() && crop.left == 0 && crop.top == 0; |
| 288 | } |
| 289 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 290 | status_t BufferLayerConsumer::updateAndReleaseLocked(const BufferItem& item, |
| 291 | PendingRelease* pendingRelease) { |
| 292 | status_t err = NO_ERROR; |
| 293 | |
| 294 | int slot = item.mSlot; |
| 295 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 296 | // Do whatever sync ops we need to do before releasing the old slot. |
| 297 | if (slot != mCurrentTexture) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 298 | err = syncForReleaseLocked(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 299 | if (err != NO_ERROR) { |
| 300 | // Release the buffer we just acquired. It's not safe to |
| 301 | // release the old buffer, so instead we just drop the new frame. |
| 302 | // As we are still under lock since acquireBuffer, it is safe to |
| 303 | // release by slot. |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 304 | releaseBufferLocked(slot, mSlots[slot].mGraphicBuffer); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 305 | return err; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | BLC_LOGV("updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", mCurrentTexture, |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 310 | mCurrentTextureImage != nullptr ? mCurrentTextureImage->graphicBufferHandle() : 0, |
| 311 | slot, mSlots[slot].mGraphicBuffer->handle); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 312 | |
| 313 | // Hang onto the pointer so that it isn't freed in the call to |
| 314 | // releaseBufferLocked() if we're in shared buffer mode and both buffers are |
| 315 | // the same. |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 316 | sp<Image> nextTextureImage = mImages[slot]; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 317 | |
| 318 | // release old buffer |
| 319 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 320 | if (pendingRelease == nullptr) { |
| 321 | status_t status = |
Chia-I Wu | 6aff69b | 2017-11-27 14:08:48 -0800 | [diff] [blame] | 322 | releaseBufferLocked(mCurrentTexture, mCurrentTextureImage->graphicBuffer()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 323 | if (status < NO_ERROR) { |
| 324 | BLC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status), |
| 325 | status); |
| 326 | err = status; |
| 327 | // keep going, with error raised [?] |
| 328 | } |
| 329 | } else { |
| 330 | pendingRelease->currentTexture = mCurrentTexture; |
| 331 | pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 332 | pendingRelease->isPending = true; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Update the BufferLayerConsumer state. |
| 337 | mCurrentTexture = slot; |
| 338 | mCurrentTextureImage = nextTextureImage; |
| 339 | mCurrentCrop = item.mCrop; |
| 340 | mCurrentTransform = item.mTransform; |
| 341 | mCurrentScalingMode = item.mScalingMode; |
| 342 | mCurrentTimestamp = item.mTimestamp; |
| 343 | mCurrentDataSpace = item.mDataSpace; |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 344 | mCurrentHdrMetadata = item.mHdrMetadata; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 345 | mCurrentFence = item.mFence; |
| 346 | mCurrentFenceTime = item.mFenceTime; |
| 347 | mCurrentFrameNumber = item.mFrameNumber; |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 348 | mCurrentTransformToDisplayInverse = item.mTransformToDisplayInverse; |
| 349 | mCurrentSurfaceDamage = item.mSurfaceDamage; |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame^] | 350 | mCurrentApi = item.mApi; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 351 | |
| 352 | computeCurrentTransformMatrixLocked(); |
| 353 | |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | status_t BufferLayerConsumer::bindTextureImageLocked() { |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 358 | mRE.checkErrors(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 359 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 360 | if (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 361 | BLC_LOGE("bindTextureImage: no currently-bound texture"); |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 362 | mRE.bindExternalTextureImage(mTexName, RE::Image(mRE)); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 363 | return NO_INIT; |
| 364 | } |
| 365 | |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 366 | const Rect& imageCrop = canUseImageCrop(mCurrentCrop) ? mCurrentCrop : Rect::EMPTY_RECT; |
| 367 | status_t err = mCurrentTextureImage->createIfNeeded(imageCrop); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 368 | if (err != NO_ERROR) { |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 369 | BLC_LOGW("bindTextureImage: can't create image on slot=%d", mCurrentTexture); |
| 370 | mRE.bindExternalTextureImage(mTexName, RE::Image(mRE)); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 371 | return UNKNOWN_ERROR; |
| 372 | } |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 373 | |
| 374 | mRE.bindExternalTextureImage(mTexName, mCurrentTextureImage->image()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 375 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 376 | // Wait for the new buffer to be ready. |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 377 | return doFenceWaitLocked(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 380 | status_t BufferLayerConsumer::syncForReleaseLocked() { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 381 | BLC_LOGV("syncForReleaseLocked"); |
| 382 | |
| 383 | if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) { |
| 384 | if (SyncFeatures::getInstance().useNativeFenceSync()) { |
Chia-I Wu | df1badd | 2017-12-27 11:10:20 -0800 | [diff] [blame] | 385 | base::unique_fd fenceFd = mRE.flush(); |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 386 | if (fenceFd == -1) { |
| 387 | BLC_LOGE("syncForReleaseLocked: failed to flush RenderEngine"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 388 | return UNKNOWN_ERROR; |
| 389 | } |
Chia-I Wu | df1badd | 2017-12-27 11:10:20 -0800 | [diff] [blame] | 390 | sp<Fence> fence(new Fence(std::move(fenceFd))); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 391 | status_t err = addReleaseFenceLocked(mCurrentTexture, |
| 392 | mCurrentTextureImage->graphicBuffer(), fence); |
| 393 | if (err != OK) { |
| 394 | BLC_LOGE("syncForReleaseLocked: error adding release fence: " |
| 395 | "%s (%d)", |
| 396 | strerror(-err), err); |
| 397 | return err; |
| 398 | } |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | return OK; |
| 403 | } |
| 404 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 405 | void BufferLayerConsumer::getTransformMatrix(float mtx[16]) { |
| 406 | Mutex::Autolock lock(mMutex); |
| 407 | memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix)); |
| 408 | } |
| 409 | |
| 410 | void BufferLayerConsumer::setFilteringEnabled(bool enabled) { |
| 411 | Mutex::Autolock lock(mMutex); |
| 412 | if (mAbandoned) { |
| 413 | BLC_LOGE("setFilteringEnabled: BufferLayerConsumer is abandoned!"); |
| 414 | return; |
| 415 | } |
| 416 | bool needsRecompute = mFilteringEnabled != enabled; |
| 417 | mFilteringEnabled = enabled; |
| 418 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 419 | if (needsRecompute && mCurrentTextureImage == nullptr) { |
| 420 | BLC_LOGD("setFilteringEnabled called with mCurrentTextureImage == nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 423 | if (needsRecompute && mCurrentTextureImage != nullptr) { |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 424 | computeCurrentTransformMatrixLocked(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void BufferLayerConsumer::computeCurrentTransformMatrixLocked() { |
| 429 | BLC_LOGV("computeCurrentTransformMatrixLocked"); |
| 430 | sp<GraphicBuffer> buf = |
| 431 | (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer(); |
| 432 | if (buf == nullptr) { |
| 433 | BLC_LOGD("computeCurrentTransformMatrixLocked: " |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 434 | "mCurrentTextureImage is nullptr"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 435 | } |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 436 | const Rect& cropRect = canUseImageCrop(mCurrentCrop) ? Rect::EMPTY_RECT : mCurrentCrop; |
| 437 | GLConsumer::computeTransformMatrix(mCurrentTransformMatrix, buf, cropRect, mCurrentTransform, |
| 438 | mFilteringEnabled); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 441 | nsecs_t BufferLayerConsumer::getTimestamp() { |
| 442 | BLC_LOGV("getTimestamp"); |
| 443 | Mutex::Autolock lock(mMutex); |
| 444 | return mCurrentTimestamp; |
| 445 | } |
| 446 | |
| 447 | android_dataspace BufferLayerConsumer::getCurrentDataSpace() { |
| 448 | BLC_LOGV("getCurrentDataSpace"); |
| 449 | Mutex::Autolock lock(mMutex); |
| 450 | return mCurrentDataSpace; |
| 451 | } |
| 452 | |
Courtney Goeltzenleuchter | 9bad0d7 | 2017-12-19 12:34:34 -0700 | [diff] [blame] | 453 | const HdrMetadata& BufferLayerConsumer::getCurrentHdrMetadata() const { |
| 454 | BLC_LOGV("getCurrentHdrMetadata"); |
| 455 | Mutex::Autolock lock(mMutex); |
| 456 | return mCurrentHdrMetadata; |
| 457 | } |
| 458 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 459 | uint64_t BufferLayerConsumer::getFrameNumber() { |
| 460 | BLC_LOGV("getFrameNumber"); |
| 461 | Mutex::Autolock lock(mMutex); |
| 462 | return mCurrentFrameNumber; |
| 463 | } |
| 464 | |
Chia-I Wu | 67dcc69 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 465 | bool BufferLayerConsumer::getTransformToDisplayInverse() const { |
| 466 | Mutex::Autolock lock(mMutex); |
| 467 | return mCurrentTransformToDisplayInverse; |
| 468 | } |
| 469 | |
| 470 | const Region& BufferLayerConsumer::getSurfaceDamage() const { |
| 471 | return mCurrentSurfaceDamage; |
| 472 | } |
| 473 | |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame^] | 474 | int BufferLayerConsumer::getCurrentApi() const { |
| 475 | Mutex::Autolock lock(mMutex); |
| 476 | return mCurrentApi; |
| 477 | } |
| 478 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 479 | sp<GraphicBuffer> BufferLayerConsumer::getCurrentBuffer(int* outSlot) const { |
| 480 | Mutex::Autolock lock(mMutex); |
| 481 | |
| 482 | if (outSlot != nullptr) { |
| 483 | *outSlot = mCurrentTexture; |
| 484 | } |
| 485 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 486 | return (mCurrentTextureImage == nullptr) ? nullptr : mCurrentTextureImage->graphicBuffer(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | Rect BufferLayerConsumer::getCurrentCrop() const { |
| 490 | Mutex::Autolock lock(mMutex); |
| 491 | return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) |
Chia-I Wu | e1e1187 | 2017-12-01 09:21:59 -0800 | [diff] [blame] | 492 | ? GLConsumer::scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 493 | : mCurrentCrop; |
| 494 | } |
| 495 | |
| 496 | uint32_t BufferLayerConsumer::getCurrentTransform() const { |
| 497 | Mutex::Autolock lock(mMutex); |
| 498 | return mCurrentTransform; |
| 499 | } |
| 500 | |
| 501 | uint32_t BufferLayerConsumer::getCurrentScalingMode() const { |
| 502 | Mutex::Autolock lock(mMutex); |
| 503 | return mCurrentScalingMode; |
| 504 | } |
| 505 | |
| 506 | sp<Fence> BufferLayerConsumer::getCurrentFence() const { |
| 507 | Mutex::Autolock lock(mMutex); |
| 508 | return mCurrentFence; |
| 509 | } |
| 510 | |
| 511 | std::shared_ptr<FenceTime> BufferLayerConsumer::getCurrentFenceTime() const { |
| 512 | Mutex::Autolock lock(mMutex); |
| 513 | return mCurrentFenceTime; |
| 514 | } |
| 515 | |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 516 | status_t BufferLayerConsumer::doFenceWaitLocked() const { |
Chia-I Wu | 9f2db77 | 2017-11-30 21:06:50 -0800 | [diff] [blame] | 517 | if (!mRE.isCurrent()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 518 | BLC_LOGE("doFenceWait: RenderEngine is not current"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 519 | return INVALID_OPERATION; |
| 520 | } |
| 521 | |
| 522 | if (mCurrentFence->isValid()) { |
| 523 | if (SyncFeatures::getInstance().useWaitSync()) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 524 | base::unique_fd fenceFd(mCurrentFence->dup()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 525 | if (fenceFd == -1) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 526 | BLC_LOGE("doFenceWait: error dup'ing fence fd: %d", errno); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 527 | return -errno; |
| 528 | } |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 529 | if (!mRE.waitFence(std::move(fenceFd))) { |
| 530 | BLC_LOGE("doFenceWait: failed to wait on fence fd"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 531 | return UNKNOWN_ERROR; |
| 532 | } |
| 533 | } else { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 534 | status_t err = mCurrentFence->waitForever("BufferLayerConsumer::doFenceWaitLocked"); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 535 | if (err != NO_ERROR) { |
Chia-I Wu | 3498e3c | 2017-12-01 10:19:38 -0800 | [diff] [blame] | 536 | BLC_LOGE("doFenceWait: error waiting for fence: %d", err); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 537 | return err; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return NO_ERROR; |
| 543 | } |
| 544 | |
| 545 | void BufferLayerConsumer::freeBufferLocked(int slotIndex) { |
| 546 | BLC_LOGV("freeBufferLocked: slotIndex=%d", slotIndex); |
| 547 | if (slotIndex == mCurrentTexture) { |
| 548 | mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT; |
| 549 | } |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 550 | mImages[slotIndex].clear(); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 551 | ConsumerBase::freeBufferLocked(slotIndex); |
| 552 | } |
| 553 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 554 | void BufferLayerConsumer::onDisconnect() { |
| 555 | sp<Layer> l = mLayer.promote(); |
| 556 | if (l.get()) { |
| 557 | l->onDisconnect(); |
| 558 | } |
| 559 | } |
| 560 | |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 561 | void BufferLayerConsumer::onSidebandStreamChanged() { |
| 562 | FrameAvailableListener* unsafeFrameAvailableListener = nullptr; |
| 563 | { |
| 564 | Mutex::Autolock lock(mFrameAvailableMutex); |
| 565 | unsafeFrameAvailableListener = mFrameAvailableListener.unsafe_get(); |
| 566 | } |
| 567 | sp<ContentsChangedListener> listener; |
| 568 | { // scope for the lock |
| 569 | Mutex::Autolock lock(mMutex); |
| 570 | ALOG_ASSERT(unsafeFrameAvailableListener == mContentsChangedListener.unsafe_get()); |
| 571 | listener = mContentsChangedListener.promote(); |
| 572 | } |
| 573 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 574 | if (listener != nullptr) { |
Chia-I Wu | fd257f8 | 2017-11-27 14:51:06 -0800 | [diff] [blame] | 575 | listener->onSidebandStreamChanged(); |
| 576 | } |
| 577 | } |
| 578 | |
Chia-I Wu | c75c44d | 2017-11-27 14:32:57 -0800 | [diff] [blame] | 579 | void BufferLayerConsumer::addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps, |
| 580 | FrameEventHistoryDelta* outDelta) { |
| 581 | sp<Layer> l = mLayer.promote(); |
| 582 | if (l.get()) { |
| 583 | l->addAndGetFrameTimestamps(newTimestamps, outDelta); |
| 584 | } |
| 585 | } |
| 586 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 587 | void BufferLayerConsumer::abandonLocked() { |
| 588 | BLC_LOGV("abandonLocked"); |
| 589 | mCurrentTextureImage.clear(); |
| 590 | ConsumerBase::abandonLocked(); |
| 591 | } |
| 592 | |
| 593 | status_t BufferLayerConsumer::setConsumerUsageBits(uint64_t usage) { |
| 594 | return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS); |
| 595 | } |
| 596 | |
| 597 | void BufferLayerConsumer::dumpLocked(String8& result, const char* prefix) const { |
| 598 | result.appendFormat("%smTexName=%d mCurrentTexture=%d\n" |
| 599 | "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n", |
| 600 | prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, |
| 601 | mCurrentCrop.top, mCurrentCrop.right, mCurrentCrop.bottom, |
| 602 | mCurrentTransform); |
| 603 | |
| 604 | ConsumerBase::dumpLocked(result, prefix); |
| 605 | } |
| 606 | |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 607 | BufferLayerConsumer::Image::Image(sp<GraphicBuffer> graphicBuffer, const RenderEngine& engine) |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 608 | : mGraphicBuffer(graphicBuffer), |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 609 | mImage{engine}, |
| 610 | mCreated(false), |
| 611 | mCropWidth(0), |
| 612 | mCropHeight(0) {} |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 613 | |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 614 | status_t BufferLayerConsumer::Image::createIfNeeded(const Rect& imageCrop) { |
| 615 | const int32_t cropWidth = imageCrop.width(); |
| 616 | const int32_t cropHeight = imageCrop.height(); |
| 617 | if (mCreated && mCropWidth == cropWidth && mCropHeight == cropHeight) { |
| 618 | return OK; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 619 | } |
| 620 | |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 621 | mCreated = mImage.setNativeWindowBuffer(mGraphicBuffer->getNativeBuffer(), |
| 622 | mGraphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED, |
| 623 | cropWidth, cropHeight); |
| 624 | if (mCreated) { |
| 625 | mCropWidth = cropWidth; |
| 626 | mCropHeight = cropHeight; |
| 627 | } else { |
| 628 | mCropWidth = 0; |
| 629 | mCropHeight = 0; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 630 | |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 631 | const sp<GraphicBuffer>& buffer = mGraphicBuffer; |
| 632 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", |
| 633 | buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(), |
| 634 | buffer->getPixelFormat()); |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Chia-I Wu | 6748db4 | 2017-12-01 10:53:53 -0800 | [diff] [blame] | 637 | return mCreated ? OK : UNKNOWN_ERROR; |
Chia-I Wu | f140518 | 2017-11-27 11:29:21 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | }; // namespace android |