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