David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | //#define LOG_NDEBUG 0 |
| 18 | #undef LOG_TAG |
| 19 | #define LOG_TAG "BufferLayer" |
| 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 21 | |
| 22 | #include "BufferLayer.h" |
| 23 | #include "Colorizer.h" |
| 24 | #include "DisplayDevice.h" |
| 25 | #include "LayerRejecter.h" |
| 26 | #include "clz.h" |
| 27 | |
| 28 | #include "RenderEngine/RenderEngine.h" |
| 29 | |
| 30 | #include <gui/BufferItem.h> |
| 31 | #include <gui/BufferQueue.h> |
| 32 | #include <gui/LayerDebugInfo.h> |
| 33 | #include <gui/Surface.h> |
| 34 | |
| 35 | #include <ui/DebugUtils.h> |
| 36 | |
| 37 | #include <utils/Errors.h> |
| 38 | #include <utils/Log.h> |
| 39 | #include <utils/NativeHandle.h> |
| 40 | #include <utils/StopWatch.h> |
| 41 | #include <utils/Trace.h> |
| 42 | |
| 43 | #include <cutils/compiler.h> |
| 44 | #include <cutils/native_handle.h> |
| 45 | #include <cutils/properties.h> |
| 46 | |
| 47 | #include <math.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <mutex> |
| 50 | |
| 51 | namespace android { |
| 52 | |
| 53 | BufferLayer::BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, |
| 54 | uint32_t w, uint32_t h, uint32_t flags) |
| 55 | : Layer(flinger, client, name, w, h, flags), |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 56 | mConsumer(nullptr), |
Ivan Lozano | eb13f9e | 2017-11-09 12:39:31 -0800 | [diff] [blame] | 57 | mTextureName(UINT32_MAX), |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 58 | mFormat(PIXEL_FORMAT_NONE), |
| 59 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 60 | mBufferLatched(false), |
| 61 | mPreviousFrameNumber(0), |
| 62 | mUpdateTexImageFailed(false), |
| 63 | mRefreshPending(false) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 64 | ALOGV("Creating Layer %s", name.string()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 65 | |
| 66 | mFlinger->getRenderEngine().genTextures(1, &mTextureName); |
| 67 | mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); |
| 68 | |
| 69 | if (flags & ISurfaceComposerClient::eNonPremultiplied) mPremultipliedAlpha = false; |
| 70 | |
| 71 | mCurrentState.requested = mCurrentState.active; |
| 72 | |
| 73 | // drawing state & current state are identical |
| 74 | mDrawingState = mCurrentState; |
| 75 | } |
| 76 | |
| 77 | BufferLayer::~BufferLayer() { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 78 | mFlinger->deleteTextureAsync(mTextureName); |
| 79 | |
David Sodman | 6f65f3e | 2017-11-03 14:28:09 -0700 | [diff] [blame] | 80 | if (!getBE().mHwcLayers.empty()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 81 | ALOGE("Found stale hardware composer layers when destroying " |
| 82 | "surface flinger layer %s", |
| 83 | mName.string()); |
| 84 | destroyAllHwcLayers(); |
| 85 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 86 | } |
| 87 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 88 | void BufferLayer::useSurfaceDamage() { |
| 89 | if (mFlinger->mForceFullDamage) { |
| 90 | surfaceDamageRegion = Region::INVALID_REGION; |
| 91 | } else { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 92 | surfaceDamageRegion = mConsumer->getSurfaceDamage(); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void BufferLayer::useEmptyDamage() { |
| 97 | surfaceDamageRegion.clear(); |
| 98 | } |
| 99 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 100 | bool BufferLayer::isProtected() const { |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 101 | const sp<GraphicBuffer>& buffer(mActiveBuffer); |
| 102 | return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool BufferLayer::isVisible() const { |
| 106 | return !(isHiddenByPolicy()) && getAlpha() > 0.0f && |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 107 | (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool BufferLayer::isFixedSize() const { |
| 111 | return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 112 | } |
| 113 | |
| 114 | status_t BufferLayer::setBuffers(uint32_t w, uint32_t h, PixelFormat format, uint32_t flags) { |
| 115 | uint32_t const maxSurfaceDims = |
| 116 | min(mFlinger->getMaxTextureSize(), mFlinger->getMaxViewportDims()); |
| 117 | |
| 118 | // never allow a surface larger than what our underlying GL implementation |
| 119 | // can handle. |
| 120 | if ((uint32_t(w) > maxSurfaceDims) || (uint32_t(h) > maxSurfaceDims)) { |
| 121 | ALOGE("dimensions too large %u x %u", uint32_t(w), uint32_t(h)); |
| 122 | return BAD_VALUE; |
| 123 | } |
| 124 | |
| 125 | mFormat = format; |
| 126 | |
| 127 | mPotentialCursor = (flags & ISurfaceComposerClient::eCursorWindow) ? true : false; |
| 128 | mProtectedByApp = (flags & ISurfaceComposerClient::eProtectedByApp) ? true : false; |
| 129 | mCurrentOpacity = getOpacityForFormat(format); |
| 130 | |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 131 | mConsumer->setDefaultBufferSize(w, h); |
| 132 | mConsumer->setDefaultBufferFormat(format); |
| 133 | mConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 134 | |
| 135 | return NO_ERROR; |
| 136 | } |
| 137 | |
| 138 | static constexpr mat4 inverseOrientation(uint32_t transform) { |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 139 | const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
| 140 | const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1); |
| 141 | const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 142 | mat4 tr; |
| 143 | |
| 144 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 145 | tr = tr * rot90; |
| 146 | } |
| 147 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 148 | tr = tr * flipH; |
| 149 | } |
| 150 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 151 | tr = tr * flipV; |
| 152 | } |
| 153 | return inverse(tr); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * onDraw will draw the current layer onto the presentable buffer |
| 158 | */ |
| 159 | void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip, |
| 160 | bool useIdentityTransform) const { |
| 161 | ATRACE_CALL(); |
| 162 | |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 163 | if (CC_UNLIKELY(mActiveBuffer == 0)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 164 | // the texture has not been created yet, this Layer has |
| 165 | // in fact never been drawn into. This happens frequently with |
| 166 | // SurfaceView because the WindowManager can't know when the client |
| 167 | // has drawn the first time. |
| 168 | |
| 169 | // If there is nothing under us, we paint the screen in black, otherwise |
| 170 | // we just skip this update. |
| 171 | |
| 172 | // figure out if there is something below us |
| 173 | Region under; |
| 174 | bool finished = false; |
| 175 | mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 176 | if (finished || layer == static_cast<BufferLayer const*>(this)) { |
| 177 | finished = true; |
| 178 | return; |
| 179 | } |
| 180 | under.orSelf(renderArea.getTransform().transform(layer->visibleRegion)); |
| 181 | }); |
| 182 | // if not everything below us is covered, we plug the holes! |
| 183 | Region holes(clip.subtract(under)); |
| 184 | if (!holes.isEmpty()) { |
| 185 | clearWithOpenGL(renderArea, 0, 0, 0, 1); |
| 186 | } |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | // Bind the current buffer to the GL texture, and wait for it to be |
| 191 | // ready for us to draw into. |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 192 | status_t err = mConsumer->bindTextureImage(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 193 | if (err != NO_ERROR) { |
| 194 | ALOGW("onDraw: bindTextureImage failed (err=%d)", err); |
| 195 | // Go ahead and draw the buffer anyway; no matter what we do the screen |
| 196 | // is probably going to have something visibly wrong. |
| 197 | } |
| 198 | |
| 199 | bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure()); |
| 200 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 201 | auto& engine(mFlinger->getRenderEngine()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 202 | |
| 203 | if (!blackOutLayer) { |
| 204 | // TODO: we could be more subtle with isFixedSize() |
| 205 | const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize(); |
| 206 | |
| 207 | // Query the texture matrix given our current filtering mode. |
| 208 | float textureMatrix[16]; |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 209 | mConsumer->setFilteringEnabled(useFiltering); |
| 210 | mConsumer->getTransformMatrix(textureMatrix); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 211 | |
| 212 | if (getTransformToDisplayInverse()) { |
| 213 | /* |
| 214 | * the code below applies the primary display's inverse transform to |
| 215 | * the texture transform |
| 216 | */ |
| 217 | uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
| 218 | mat4 tr = inverseOrientation(transform); |
| 219 | |
| 220 | /** |
| 221 | * TODO(b/36727915): This is basically a hack. |
| 222 | * |
| 223 | * Ensure that regardless of the parent transformation, |
| 224 | * this buffer is always transformed from native display |
| 225 | * orientation to display orientation. For example, in the case |
| 226 | * of a camera where the buffer remains in native orientation, |
| 227 | * we want the pixels to always be upright. |
| 228 | */ |
| 229 | sp<Layer> p = mDrawingParent.promote(); |
| 230 | if (p != nullptr) { |
| 231 | const auto parentTransform = p->getTransform(); |
| 232 | tr = tr * inverseOrientation(parentTransform.getOrientation()); |
| 233 | } |
| 234 | |
| 235 | // and finally apply it to the original texture matrix |
| 236 | const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); |
| 237 | memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); |
| 238 | } |
| 239 | |
| 240 | // Set things up for texturing. |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 241 | mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 242 | mTexture.setFiltering(useFiltering); |
| 243 | mTexture.setMatrix(textureMatrix); |
| 244 | |
| 245 | engine.setupLayerTexturing(mTexture); |
| 246 | } else { |
| 247 | engine.setupLayerBlackedOut(); |
| 248 | } |
| 249 | drawWithOpenGL(renderArea, useIdentityTransform); |
| 250 | engine.disableTexturing(); |
| 251 | } |
| 252 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 253 | void BufferLayer::onLayerDisplayed(const sp<Fence>& releaseFence) { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 254 | mConsumer->setReleaseFence(releaseFence); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 255 | } |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 256 | |
| 257 | void BufferLayer::abandon() { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 258 | mConsumer->abandon(); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | bool BufferLayer::shouldPresentNow(const DispSync& dispSync) const { |
| 262 | if (mSidebandStreamChanged || mAutoRefresh) { |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | Mutex::Autolock lock(mQueueItemLock); |
| 267 | if (mQueueItems.empty()) { |
| 268 | return false; |
| 269 | } |
| 270 | auto timestamp = mQueueItems[0].mTimestamp; |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 271 | nsecs_t expectedPresent = mConsumer->computeExpectedPresent(dispSync); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 272 | |
| 273 | // Ignore timestamps more than a second in the future |
| 274 | bool isPlausible = timestamp < (expectedPresent + s2ns(1)); |
| 275 | ALOGW_IF(!isPlausible, |
| 276 | "[%s] Timestamp %" PRId64 " seems implausible " |
| 277 | "relative to expectedPresent %" PRId64, |
| 278 | mName.string(), timestamp, expectedPresent); |
| 279 | |
| 280 | bool isDue = timestamp < expectedPresent; |
| 281 | return isDue || !isPlausible; |
| 282 | } |
| 283 | |
| 284 | void BufferLayer::setTransformHint(uint32_t orientation) const { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 285 | mConsumer->setTransformHint(orientation); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 286 | } |
| 287 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 288 | bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) { |
| 289 | if (mBufferLatched) { |
| 290 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 291 | mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 292 | } |
| 293 | mRefreshPending = false; |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 294 | return mQueuedFrames > 0 || mSidebandStreamChanged || mAutoRefresh; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 295 | } |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 296 | bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence, |
| 297 | const std::shared_ptr<FenceTime>& presentFence, |
| 298 | const CompositorTiming& compositorTiming) { |
| 299 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 300 | // composition. |
| 301 | if (!mFrameLatencyNeeded) return false; |
| 302 | |
| 303 | // Update mFrameEventHistory. |
| 304 | { |
| 305 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 306 | mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence, |
| 307 | compositorTiming); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Update mFrameTracker. |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 311 | nsecs_t desiredPresentTime = mConsumer->getTimestamp(); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 312 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 313 | |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 314 | std::shared_ptr<FenceTime> frameReadyFence = mConsumer->getCurrentFenceTime(); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 315 | if (frameReadyFence->isValid()) { |
| 316 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
| 317 | } else { |
| 318 | // There was no fence for this frame, so assume that it was ready |
| 319 | // to be presented at the desired present time. |
| 320 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
| 321 | } |
| 322 | |
| 323 | if (presentFence->isValid()) { |
| 324 | mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence)); |
| 325 | } else { |
| 326 | // The HWC doesn't support present fences, so use the refresh |
| 327 | // timestamp instead. |
| 328 | mFrameTracker.setActualPresentTime( |
| 329 | mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY)); |
| 330 | } |
| 331 | |
| 332 | mFrameTracker.advanceFrame(); |
| 333 | mFrameLatencyNeeded = false; |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | std::vector<OccupancyTracker::Segment> BufferLayer::getOccupancyHistory(bool forceFlush) { |
| 338 | std::vector<OccupancyTracker::Segment> history; |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 339 | status_t result = mConsumer->getOccupancyHistory(forceFlush, &history); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 340 | if (result != NO_ERROR) { |
| 341 | ALOGW("[%s] Failed to obtain occupancy history (%d)", mName.string(), result); |
| 342 | return {}; |
| 343 | } |
| 344 | return history; |
| 345 | } |
| 346 | |
| 347 | bool BufferLayer::getTransformToDisplayInverse() const { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 348 | return mConsumer->getTransformToDisplayInverse(); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 349 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 350 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 351 | void BufferLayer::releasePendingBuffer(nsecs_t dequeueReadyTime) { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 352 | if (!mConsumer->releasePendingBuffer()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 356 | auto releaseFenceTime = std::make_shared<FenceTime>(mConsumer->getPrevFinalReleaseFence()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 357 | mReleaseTimeline.updateSignalTimes(); |
| 358 | mReleaseTimeline.push(releaseFenceTime); |
| 359 | |
| 360 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 361 | if (mPreviousFrameNumber != 0) { |
| 362 | mFrameEventHistory.addRelease(mPreviousFrameNumber, dequeueReadyTime, |
| 363 | std::move(releaseFenceTime)); |
| 364 | } |
| 365 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 366 | |
| 367 | Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) { |
| 368 | ATRACE_CALL(); |
| 369 | |
| 370 | if (android_atomic_acquire_cas(true, false, &mSidebandStreamChanged) == 0) { |
| 371 | // mSidebandStreamChanged was true |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 372 | mSidebandStream = mConsumer->getSidebandStream(); |
David Sodman | 386c22e | 2017-11-09 16:34:46 -0800 | [diff] [blame] | 373 | // replicated in LayerBE until FE/BE is ready to be synchronized |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 374 | getBE().compositionInfo.hwc.sidebandStream = mSidebandStream; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 375 | if (getBE().compositionInfo.hwc.sidebandStream != nullptr) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 376 | setTransactionFlags(eTransactionNeeded); |
| 377 | mFlinger->setTransactionFlags(eTraversalNeeded); |
| 378 | } |
| 379 | recomputeVisibleRegions = true; |
| 380 | |
| 381 | const State& s(getDrawingState()); |
| 382 | return getTransform().transform(Region(Rect(s.active.w, s.active.h))); |
| 383 | } |
| 384 | |
| 385 | Region outDirtyRegion; |
| 386 | if (mQueuedFrames <= 0 && !mAutoRefresh) { |
| 387 | return outDirtyRegion; |
| 388 | } |
| 389 | |
| 390 | // if we've already called updateTexImage() without going through |
| 391 | // a composition step, we have to skip this layer at this point |
| 392 | // because we cannot call updateTeximage() without a corresponding |
| 393 | // compositionComplete() call. |
| 394 | // we'll trigger an update in onPreComposition(). |
| 395 | if (mRefreshPending) { |
| 396 | return outDirtyRegion; |
| 397 | } |
| 398 | |
| 399 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 400 | // try again later |
| 401 | if (!headFenceHasSignaled()) { |
| 402 | mFlinger->signalLayerUpdate(); |
| 403 | return outDirtyRegion; |
| 404 | } |
| 405 | |
| 406 | // Capture the old state of the layer for comparisons later |
| 407 | const State& s(getDrawingState()); |
| 408 | const bool oldOpacity = isOpaque(s); |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 409 | sp<GraphicBuffer> oldBuffer = mActiveBuffer; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 410 | |
| 411 | if (!allTransactionsSignaled()) { |
| 412 | mFlinger->signalLayerUpdate(); |
| 413 | return outDirtyRegion; |
| 414 | } |
| 415 | |
| 416 | // This boolean is used to make sure that SurfaceFlinger's shadow copy |
| 417 | // of the buffer queue isn't modified when the buffer queue is returning |
| 418 | // BufferItem's that weren't actually queued. This can happen in shared |
| 419 | // buffer mode. |
| 420 | bool queuedBuffer = false; |
| 421 | LayerRejecter r(mDrawingState, getCurrentState(), recomputeVisibleRegions, |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 422 | getProducerStickyTransform() != 0, mName.string(), mOverrideScalingMode, |
| 423 | mFreezeGeometryUpdates); |
| 424 | status_t updateResult = mConsumer->updateTexImage(&r, mFlinger->mPrimaryDispSync, &mAutoRefresh, |
| 425 | &queuedBuffer, mLastFrameNumberReceived); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 426 | if (updateResult == BufferQueue::PRESENT_LATER) { |
| 427 | // Producer doesn't want buffer to be displayed yet. Signal a |
| 428 | // layer update so we check again at the next opportunity. |
| 429 | mFlinger->signalLayerUpdate(); |
| 430 | return outDirtyRegion; |
Chia-I Wu | 0cb75ac | 2017-11-27 15:56:04 -0800 | [diff] [blame] | 431 | } else if (updateResult == BufferLayerConsumer::BUFFER_REJECTED) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 432 | // If the buffer has been rejected, remove it from the shadow queue |
| 433 | // and return early |
| 434 | if (queuedBuffer) { |
| 435 | Mutex::Autolock lock(mQueueItemLock); |
| 436 | mQueueItems.removeAt(0); |
| 437 | android_atomic_dec(&mQueuedFrames); |
| 438 | } |
| 439 | return outDirtyRegion; |
| 440 | } else if (updateResult != NO_ERROR || mUpdateTexImageFailed) { |
| 441 | // This can occur if something goes wrong when trying to create the |
| 442 | // EGLImage for this buffer. If this happens, the buffer has already |
| 443 | // been released, so we need to clean up the queue and bug out |
| 444 | // early. |
| 445 | if (queuedBuffer) { |
| 446 | Mutex::Autolock lock(mQueueItemLock); |
| 447 | mQueueItems.clear(); |
| 448 | android_atomic_and(0, &mQueuedFrames); |
| 449 | } |
| 450 | |
| 451 | // Once we have hit this state, the shadow queue may no longer |
| 452 | // correctly reflect the incoming BufferQueue's contents, so even if |
| 453 | // updateTexImage starts working, the only safe course of action is |
| 454 | // to continue to ignore updates. |
| 455 | mUpdateTexImageFailed = true; |
| 456 | |
| 457 | return outDirtyRegion; |
| 458 | } |
| 459 | |
| 460 | if (queuedBuffer) { |
| 461 | // Autolock scope |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 462 | auto currentFrameNumber = mConsumer->getFrameNumber(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 463 | |
| 464 | Mutex::Autolock lock(mQueueItemLock); |
| 465 | |
| 466 | // Remove any stale buffers that have been dropped during |
| 467 | // updateTexImage |
| 468 | while (mQueueItems[0].mFrameNumber != currentFrameNumber) { |
| 469 | mQueueItems.removeAt(0); |
| 470 | android_atomic_dec(&mQueuedFrames); |
| 471 | } |
| 472 | |
| 473 | mQueueItems.removeAt(0); |
| 474 | } |
| 475 | |
| 476 | // Decrement the queued-frames count. Signal another event if we |
| 477 | // have more frames pending. |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 478 | if ((queuedBuffer && android_atomic_dec(&mQueuedFrames) > 1) || mAutoRefresh) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 479 | mFlinger->signalLayerUpdate(); |
| 480 | } |
| 481 | |
| 482 | // update the active buffer |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 483 | mActiveBuffer = mConsumer->getCurrentBuffer(&mActiveBufferSlot); |
| 484 | getBE().compositionInfo.mBuffer = mActiveBuffer; |
| 485 | getBE().compositionInfo.mBufferSlot = mActiveBufferSlot; |
| 486 | |
| 487 | if (mActiveBuffer == nullptr) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 488 | // this can only happen if the very first buffer was rejected. |
| 489 | return outDirtyRegion; |
| 490 | } |
| 491 | |
| 492 | mBufferLatched = true; |
| 493 | mPreviousFrameNumber = mCurrentFrameNumber; |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 494 | mCurrentFrameNumber = mConsumer->getFrameNumber(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 495 | |
| 496 | { |
| 497 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 498 | mFrameEventHistory.addLatch(mCurrentFrameNumber, latchTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | mRefreshPending = true; |
| 502 | mFrameLatencyNeeded = true; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 503 | if (oldBuffer == nullptr) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 504 | // the first time we receive a buffer, we need to trigger a |
| 505 | // geometry invalidation. |
| 506 | recomputeVisibleRegions = true; |
| 507 | } |
| 508 | |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 509 | setDataSpace(mConsumer->getCurrentDataSpace()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 510 | |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 511 | Rect crop(mConsumer->getCurrentCrop()); |
| 512 | const uint32_t transform(mConsumer->getCurrentTransform()); |
| 513 | const uint32_t scalingMode(mConsumer->getCurrentScalingMode()); |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 514 | if ((crop != mCurrentCrop) || (transform != mCurrentTransform) || |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 515 | (scalingMode != mCurrentScalingMode)) { |
| 516 | mCurrentCrop = crop; |
| 517 | mCurrentTransform = transform; |
| 518 | mCurrentScalingMode = scalingMode; |
| 519 | recomputeVisibleRegions = true; |
| 520 | } |
| 521 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 522 | if (oldBuffer != nullptr) { |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 523 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 524 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
| 525 | if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 526 | recomputeVisibleRegions = true; |
| 527 | } |
| 528 | } |
| 529 | |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 530 | mCurrentOpacity = getOpacityForFormat(mActiveBuffer->format); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 531 | if (oldOpacity != isOpaque(s)) { |
| 532 | recomputeVisibleRegions = true; |
| 533 | } |
| 534 | |
| 535 | // Remove any sync points corresponding to the buffer which was just |
| 536 | // latched |
| 537 | { |
| 538 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 539 | auto point = mLocalSyncPoints.begin(); |
| 540 | while (point != mLocalSyncPoints.end()) { |
| 541 | if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) { |
| 542 | // This sync point must have been added since we started |
| 543 | // latching. Don't drop it yet. |
| 544 | ++point; |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { |
| 549 | point = mLocalSyncPoints.erase(point); |
| 550 | } else { |
| 551 | ++point; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // FIXME: postedRegion should be dirty & bounds |
| 557 | Region dirtyRegion(Rect(s.active.w, s.active.h)); |
| 558 | |
| 559 | // transform the dirty region to window-manager space |
| 560 | outDirtyRegion = (getTransform().transform(dirtyRegion)); |
| 561 | |
| 562 | return outDirtyRegion; |
| 563 | } |
| 564 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 565 | void BufferLayer::setDefaultBufferSize(uint32_t w, uint32_t h) { |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 566 | mConsumer->setDefaultBufferSize(w, h); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 567 | } |
| 568 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 569 | void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) { |
| 570 | // Apply this display's projection's viewport to the visible region |
| 571 | // before giving it to the HWC HAL. |
| 572 | const Transform& tr = displayDevice->getTransform(); |
| 573 | const auto& viewport = displayDevice->getViewport(); |
| 574 | Region visible = tr.transform(visibleRegion.intersect(viewport)); |
| 575 | auto hwcId = displayDevice->getHwcDisplayId(); |
David Sodman | 6f65f3e | 2017-11-03 14:28:09 -0700 | [diff] [blame] | 576 | auto& hwcInfo = getBE().mHwcLayers[hwcId]; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 577 | auto& hwcLayer = hwcInfo.layer; |
David Sodman | 5d89c1d | 2017-12-14 15:54:51 -0800 | [diff] [blame^] | 578 | auto error = (*hwcLayer)->setVisibleRegion(visible); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 579 | if (error != HWC2::Error::None) { |
| 580 | ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(), |
| 581 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 582 | visible.dump(LOG_TAG); |
| 583 | } |
| 584 | |
David Sodman | 5d89c1d | 2017-12-14 15:54:51 -0800 | [diff] [blame^] | 585 | error = (*hwcLayer)->setSurfaceDamage(surfaceDamageRegion); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 586 | if (error != HWC2::Error::None) { |
| 587 | ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(), |
| 588 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 589 | surfaceDamageRegion.dump(LOG_TAG); |
| 590 | } |
| 591 | |
| 592 | // Sideband layers |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 593 | if (getBE().compositionInfo.hwc.sidebandStream.get()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 594 | setCompositionType(hwcId, HWC2::Composition::Sideband); |
| 595 | ALOGV("[%s] Requesting Sideband composition", mName.string()); |
David Sodman | 5d89c1d | 2017-12-14 15:54:51 -0800 | [diff] [blame^] | 596 | error = (*hwcLayer)->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 597 | if (error != HWC2::Error::None) { |
| 598 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(), |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 599 | getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(), |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 600 | static_cast<int32_t>(error)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 601 | } |
| 602 | return; |
| 603 | } |
| 604 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 605 | // Device or Cursor layers |
| 606 | if (mPotentialCursor) { |
| 607 | ALOGV("[%s] Requesting Cursor composition", mName.string()); |
| 608 | setCompositionType(hwcId, HWC2::Composition::Cursor); |
| 609 | } else { |
| 610 | ALOGV("[%s] Requesting Device composition", mName.string()); |
| 611 | setCompositionType(hwcId, HWC2::Composition::Device); |
| 612 | } |
| 613 | |
Peiyong Lin | 13170c8 | 2018-01-22 18:55:51 -0800 | [diff] [blame] | 614 | ALOGV("setPerFrameData: dataspace = %d", mDrawingState.dataSpace); |
David Sodman | 5d89c1d | 2017-12-14 15:54:51 -0800 | [diff] [blame^] | 615 | error = (*hwcLayer)->setDataspace(mDrawingState.dataSpace); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 616 | if (error != HWC2::Error::None) { |
Peiyong Lin | 13170c8 | 2018-01-22 18:55:51 -0800 | [diff] [blame] | 617 | ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mDrawingState.dataSpace, |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 618 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 619 | } |
| 620 | |
Courtney Goeltzenleuchter | f9c98e5 | 2018-02-12 07:23:17 -0700 | [diff] [blame] | 621 | const HdrMetadata& metadata = mConsumer->getCurrentHdrMetadata(); |
David Sodman | 5d89c1d | 2017-12-14 15:54:51 -0800 | [diff] [blame^] | 622 | error = (*hwcLayer)->setHdrMetadata(metadata); |
Courtney Goeltzenleuchter | 301bb30 | 2018-03-12 11:12:42 -0600 | [diff] [blame] | 623 | if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) { |
Courtney Goeltzenleuchter | f9c98e5 | 2018-02-12 07:23:17 -0700 | [diff] [blame] | 624 | ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(), |
| 625 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 626 | } |
| 627 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 628 | uint32_t hwcSlot = 0; |
| 629 | sp<GraphicBuffer> hwcBuffer; |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 630 | getBE().mHwcLayers[hwcId].bufferCache.getHwcBuffer(mActiveBufferSlot, mActiveBuffer, &hwcSlot, |
| 631 | &hwcBuffer); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 632 | |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 633 | auto acquireFence = mConsumer->getCurrentFence(); |
David Sodman | 5d89c1d | 2017-12-14 15:54:51 -0800 | [diff] [blame^] | 634 | error = (*hwcLayer)->setBuffer(hwcSlot, hwcBuffer, acquireFence); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 635 | if (error != HWC2::Error::None) { |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 636 | ALOGE("[%s] Failed to set buffer %p: %s (%d)", mName.string(), |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 637 | getBE().compositionInfo.mBuffer->handle, to_string(error).c_str(), |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 638 | static_cast<int32_t>(error)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 642 | bool BufferLayer::isOpaque(const Layer::State& s) const { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 643 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
| 644 | // layer's opaque flag. |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 645 | if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 646 | return false; |
| 647 | } |
| 648 | |
| 649 | // if the layer has the opaque flag, then we're always opaque, |
| 650 | // otherwise we use the current buffer's format. |
| 651 | return ((s.flags & layer_state_t::eLayerOpaque) != 0) || mCurrentOpacity; |
| 652 | } |
| 653 | |
| 654 | void BufferLayer::onFirstRef() { |
| 655 | // Creates a custom BufferQueue for SurfaceFlingerConsumer to use |
| 656 | sp<IGraphicBufferProducer> producer; |
| 657 | sp<IGraphicBufferConsumer> consumer; |
| 658 | BufferQueue::createBufferQueue(&producer, &consumer, true); |
| 659 | mProducer = new MonitoredProducer(producer, mFlinger, this); |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 660 | mConsumer = new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName, this); |
Chia-I Wu | b28c674 | 2017-12-27 10:59:54 -0800 | [diff] [blame] | 661 | mConsumer->setConsumerUsageBits(getEffectiveUsage(0)); |
| 662 | mConsumer->setContentsChangedListener(this); |
| 663 | mConsumer->setName(mName); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 664 | |
| 665 | if (mFlinger->isLayerTripleBufferingDisabled()) { |
| 666 | mProducer->setMaxDequeuedBufferCount(2); |
| 667 | } |
| 668 | |
| 669 | const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); |
| 670 | updateTransformHint(hw); |
| 671 | } |
| 672 | |
| 673 | // --------------------------------------------------------------------------- |
| 674 | // Interface implementation for SurfaceFlingerConsumer::ContentsChangedListener |
| 675 | // --------------------------------------------------------------------------- |
| 676 | |
| 677 | void BufferLayer::onFrameAvailable(const BufferItem& item) { |
| 678 | // Add this buffer from our internal queue tracker |
| 679 | { // Autolock scope |
| 680 | Mutex::Autolock lock(mQueueItemLock); |
| 681 | mFlinger->mInterceptor.saveBufferUpdate(this, item.mGraphicBuffer->getWidth(), |
| 682 | item.mGraphicBuffer->getHeight(), |
| 683 | item.mFrameNumber); |
| 684 | // Reset the frame number tracker when we receive the first buffer after |
| 685 | // a frame number reset |
| 686 | if (item.mFrameNumber == 1) { |
| 687 | mLastFrameNumberReceived = 0; |
| 688 | } |
| 689 | |
| 690 | // Ensure that callbacks are handled in order |
| 691 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 692 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 693 | if (result != NO_ERROR) { |
| 694 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | mQueueItems.push_back(item); |
| 699 | android_atomic_inc(&mQueuedFrames); |
| 700 | |
| 701 | // Wake up any pending callbacks |
| 702 | mLastFrameNumberReceived = item.mFrameNumber; |
| 703 | mQueueItemCondition.broadcast(); |
| 704 | } |
| 705 | |
| 706 | mFlinger->signalLayerUpdate(); |
| 707 | } |
| 708 | |
| 709 | void BufferLayer::onFrameReplaced(const BufferItem& item) { |
| 710 | { // Autolock scope |
| 711 | Mutex::Autolock lock(mQueueItemLock); |
| 712 | |
| 713 | // Ensure that callbacks are handled in order |
| 714 | while (item.mFrameNumber != mLastFrameNumberReceived + 1) { |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 715 | status_t result = mQueueItemCondition.waitRelative(mQueueItemLock, ms2ns(500)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 716 | if (result != NO_ERROR) { |
| 717 | ALOGE("[%s] Timed out waiting on callback", mName.string()); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | if (mQueueItems.empty()) { |
| 722 | ALOGE("Can't replace a frame on an empty queue"); |
| 723 | return; |
| 724 | } |
| 725 | mQueueItems.editItemAt(mQueueItems.size() - 1) = item; |
| 726 | |
| 727 | // Wake up any pending callbacks |
| 728 | mLastFrameNumberReceived = item.mFrameNumber; |
| 729 | mQueueItemCondition.broadcast(); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | void BufferLayer::onSidebandStreamChanged() { |
| 734 | if (android_atomic_release_cas(false, true, &mSidebandStreamChanged) == 0) { |
| 735 | // mSidebandStreamChanged was false |
| 736 | mFlinger->signalLayerUpdate(); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | bool BufferLayer::needsFiltering(const RenderArea& renderArea) const { |
| 741 | return mNeedsFiltering || renderArea.needsFiltering(); |
| 742 | } |
| 743 | |
| 744 | // As documented in libhardware header, formats in the range |
| 745 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 746 | // are known to have no alpha channel |
| 747 | // TODO: move definition for device-specific range into |
| 748 | // hardware.h, instead of using hard-coded values here. |
| 749 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 750 | |
| 751 | bool BufferLayer::getOpacityForFormat(uint32_t format) { |
| 752 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 753 | return true; |
| 754 | } |
| 755 | switch (format) { |
| 756 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 757 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 758 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
| 759 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
| 760 | return false; |
| 761 | } |
| 762 | // in all other case, we have no blending (also for unknown formats) |
| 763 | return true; |
| 764 | } |
| 765 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 766 | void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 767 | const State& s(getDrawingState()); |
| 768 | |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 769 | computeGeometry(renderArea, getBE().mMesh, useIdentityTransform); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * NOTE: the way we compute the texture coordinates here produces |
| 773 | * different results than when we take the HWC path -- in the later case |
| 774 | * the "source crop" is rounded to texel boundaries. |
| 775 | * This can produce significantly different results when the texture |
| 776 | * is scaled by a large amount. |
| 777 | * |
| 778 | * The GL code below is more logical (imho), and the difference with |
| 779 | * HWC is due to a limitation of the HWC API to integers -- a question |
| 780 | * is suspend is whether we should ignore this problem or revert to |
| 781 | * GL composition when a buffer scaling is applied (maybe with some |
| 782 | * minimal value)? Or, we could make GL behave like HWC -- but this feel |
| 783 | * like more of a hack. |
| 784 | */ |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 785 | const Rect bounds{computeBounds()}; // Rounds from FloatRect |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 786 | |
| 787 | Transform t = getTransform(); |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 788 | Rect win = bounds; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 789 | if (!s.finalCrop.isEmpty()) { |
| 790 | win = t.transform(win); |
| 791 | if (!win.intersect(s.finalCrop, &win)) { |
| 792 | win.clear(); |
| 793 | } |
| 794 | win = t.inverse().transform(win); |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 795 | if (!win.intersect(bounds, &win)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 796 | win.clear(); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | float left = float(win.left) / float(s.active.w); |
| 801 | float top = float(win.top) / float(s.active.h); |
| 802 | float right = float(win.right) / float(s.active.w); |
| 803 | float bottom = float(win.bottom) / float(s.active.h); |
| 804 | |
| 805 | // TODO: we probably want to generate the texture coords with the mesh |
| 806 | // here we assume that we only have 4 vertices |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 807 | Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 808 | texCoords[0] = vec2(left, 1.0f - top); |
| 809 | texCoords[1] = vec2(left, 1.0f - bottom); |
| 810 | texCoords[2] = vec2(right, 1.0f - bottom); |
| 811 | texCoords[3] = vec2(right, 1.0f - top); |
| 812 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 813 | auto& engine(mFlinger->getRenderEngine()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 814 | engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */, |
| 815 | getColor()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 816 | engine.setSourceDataSpace(mCurrentState.dataSpace); |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 817 | |
Chia-I Wu | 8d2651e | 2018-01-24 12:18:49 -0800 | [diff] [blame] | 818 | if (mCurrentState.dataSpace == HAL_DATASPACE_BT2020_ITU_PQ && |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 819 | mConsumer->getCurrentApi() == NATIVE_WINDOW_API_MEDIA && |
| 820 | getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102) { |
| 821 | engine.setSourceY410BT2020(true); |
| 822 | } |
| 823 | |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 824 | engine.drawMesh(getBE().mMesh); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 825 | engine.disableBlending(); |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 826 | |
| 827 | engine.setSourceY410BT2020(false); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | uint32_t BufferLayer::getProducerStickyTransform() const { |
| 831 | int producerStickyTransform = 0; |
| 832 | int ret = mProducer->query(NATIVE_WINDOW_STICKY_TRANSFORM, &producerStickyTransform); |
| 833 | if (ret != OK) { |
| 834 | ALOGW("%s: Error %s (%d) while querying window sticky transform.", __FUNCTION__, |
| 835 | strerror(-ret), ret); |
| 836 | return 0; |
| 837 | } |
| 838 | return static_cast<uint32_t>(producerStickyTransform); |
| 839 | } |
| 840 | |
| 841 | bool BufferLayer::latchUnsignaledBuffers() { |
| 842 | static bool propertyLoaded = false; |
| 843 | static bool latch = false; |
| 844 | static std::mutex mutex; |
| 845 | std::lock_guard<std::mutex> lock(mutex); |
| 846 | if (!propertyLoaded) { |
| 847 | char value[PROPERTY_VALUE_MAX] = {}; |
| 848 | property_get("debug.sf.latch_unsignaled", value, "0"); |
| 849 | latch = atoi(value); |
| 850 | propertyLoaded = true; |
| 851 | } |
| 852 | return latch; |
| 853 | } |
| 854 | |
| 855 | uint64_t BufferLayer::getHeadFrameNumber() const { |
| 856 | Mutex::Autolock lock(mQueueItemLock); |
| 857 | if (!mQueueItems.empty()) { |
| 858 | return mQueueItems[0].mFrameNumber; |
| 859 | } else { |
| 860 | return mCurrentFrameNumber; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | bool BufferLayer::headFenceHasSignaled() const { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 865 | if (latchUnsignaledBuffers()) { |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | Mutex::Autolock lock(mQueueItemLock); |
| 870 | if (mQueueItems.empty()) { |
| 871 | return true; |
| 872 | } |
| 873 | if (mQueueItems[0].mIsDroppable) { |
| 874 | // Even though this buffer's fence may not have signaled yet, it could |
| 875 | // be replaced by another buffer before it has a chance to, which means |
| 876 | // that it's possible to get into a situation where a buffer is never |
| 877 | // able to be latched. To avoid this, grab this buffer anyway. |
| 878 | return true; |
| 879 | } |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 880 | return mQueueItems[0].mFenceTime->getSignalTime() != Fence::SIGNAL_TIME_PENDING; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | uint32_t BufferLayer::getEffectiveScalingMode() const { |
| 884 | if (mOverrideScalingMode >= 0) { |
| 885 | return mOverrideScalingMode; |
| 886 | } |
| 887 | return mCurrentScalingMode; |
| 888 | } |
| 889 | |
| 890 | // ---------------------------------------------------------------------------- |
| 891 | // transaction |
| 892 | // ---------------------------------------------------------------------------- |
| 893 | |
| 894 | void BufferLayer::notifyAvailableFrames() { |
| 895 | auto headFrameNumber = getHeadFrameNumber(); |
| 896 | bool headFenceSignaled = headFenceHasSignaled(); |
| 897 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 898 | for (auto& point : mLocalSyncPoints) { |
| 899 | if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) { |
| 900 | point->setFrameAvailable(); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | sp<IGraphicBufferProducer> BufferLayer::getProducer() const { |
| 906 | return mProducer; |
| 907 | } |
| 908 | |
| 909 | // --------------------------------------------------------------------------- |
| 910 | // h/w composer set-up |
| 911 | // --------------------------------------------------------------------------- |
| 912 | |
| 913 | bool BufferLayer::allTransactionsSignaled() { |
| 914 | auto headFrameNumber = getHeadFrameNumber(); |
| 915 | bool matchingFramesFound = false; |
| 916 | bool allTransactionsApplied = true; |
| 917 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 918 | |
| 919 | for (auto& point : mLocalSyncPoints) { |
| 920 | if (point->getFrameNumber() > headFrameNumber) { |
| 921 | break; |
| 922 | } |
| 923 | matchingFramesFound = true; |
| 924 | |
| 925 | if (!point->frameIsAvailable()) { |
| 926 | // We haven't notified the remote layer that the frame for |
| 927 | // this point is available yet. Notify it now, and then |
| 928 | // abort this attempt to latch. |
| 929 | point->setFrameAvailable(); |
| 930 | allTransactionsApplied = false; |
| 931 | break; |
| 932 | } |
| 933 | |
| 934 | allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied(); |
| 935 | } |
| 936 | return !matchingFramesFound || allTransactionsApplied; |
| 937 | } |
| 938 | |
| 939 | } // namespace android |
| 940 | |
| 941 | #if defined(__gl_h_) |
| 942 | #error "don't include gl/gl.h in this file" |
| 943 | #endif |
| 944 | |
| 945 | #if defined(__gl2_h_) |
| 946 | #error "don't include gl2/gl2.h in this file" |
| 947 | #endif |