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" |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 26 | |
| 27 | #include "RenderEngine/RenderEngine.h" |
| 28 | |
| 29 | #include <gui/BufferItem.h> |
| 30 | #include <gui/BufferQueue.h> |
| 31 | #include <gui/LayerDebugInfo.h> |
| 32 | #include <gui/Surface.h> |
| 33 | |
| 34 | #include <ui/DebugUtils.h> |
| 35 | |
| 36 | #include <utils/Errors.h> |
| 37 | #include <utils/Log.h> |
| 38 | #include <utils/NativeHandle.h> |
| 39 | #include <utils/StopWatch.h> |
| 40 | #include <utils/Trace.h> |
| 41 | |
| 42 | #include <cutils/compiler.h> |
| 43 | #include <cutils/native_handle.h> |
| 44 | #include <cutils/properties.h> |
| 45 | |
| 46 | #include <math.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <mutex> |
| 49 | |
| 50 | namespace android { |
| 51 | |
| 52 | BufferLayer::BufferLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name, |
| 53 | uint32_t w, uint32_t h, uint32_t flags) |
| 54 | : Layer(flinger, client, name, w, h, flags), |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 55 | mTextureName(mFlinger->getNewTexture()), |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 56 | mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE), |
| 57 | mBufferLatched(false), |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 58 | mRefreshPending(false) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 59 | ALOGV("Creating Layer %s", name.string()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 60 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 61 | mTexture.init(Texture::TEXTURE_EXTERNAL, mTextureName); |
| 62 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 63 | mPremultipliedAlpha = !(flags & ISurfaceComposerClient::eNonPremultiplied); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 64 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 65 | mPotentialCursor = flags & ISurfaceComposerClient::eCursorWindow; |
| 66 | mProtectedByApp = flags & ISurfaceComposerClient::eProtectedByApp; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 67 | |
| 68 | // drawing state & current state are identical |
| 69 | mDrawingState = mCurrentState; |
| 70 | } |
| 71 | |
| 72 | BufferLayer::~BufferLayer() { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 73 | mFlinger->deleteTextureAsync(mTextureName); |
| 74 | |
David Sodman | 6f65f3e | 2017-11-03 14:28:09 -0700 | [diff] [blame] | 75 | if (!getBE().mHwcLayers.empty()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 76 | ALOGE("Found stale hardware composer layers when destroying " |
| 77 | "surface flinger layer %s", |
| 78 | mName.string()); |
| 79 | destroyAllHwcLayers(); |
| 80 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 81 | } |
| 82 | |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 83 | void BufferLayer::useSurfaceDamage() { |
| 84 | if (mFlinger->mForceFullDamage) { |
| 85 | surfaceDamageRegion = Region::INVALID_REGION; |
| 86 | } else { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 87 | surfaceDamageRegion = getDrawingSurfaceDamage(); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | void BufferLayer::useEmptyDamage() { |
| 92 | surfaceDamageRegion.clear(); |
| 93 | } |
| 94 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 95 | bool BufferLayer::isOpaque(const Layer::State& s) const { |
| 96 | // if we don't have a buffer or sidebandStream yet, we're translucent regardless of the |
| 97 | // layer's opaque flag. |
| 98 | if ((getBE().compositionInfo.hwc.sidebandStream == nullptr) && (mActiveBuffer == nullptr)) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // if the layer has the opaque flag, then we're always opaque, |
| 103 | // otherwise we use the current buffer's format. |
| 104 | return ((s.flags & layer_state_t::eLayerOpaque) != 0) || getOpacityForFormat(getPixelFormat()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool BufferLayer::isVisible() const { |
| 108 | return !(isHiddenByPolicy()) && getAlpha() > 0.0f && |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 109 | (mActiveBuffer != nullptr || getBE().compositionInfo.hwc.sidebandStream != nullptr); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool BufferLayer::isFixedSize() const { |
| 113 | return getEffectiveScalingMode() != NATIVE_WINDOW_SCALING_MODE_FREEZE; |
| 114 | } |
| 115 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 116 | static constexpr mat4 inverseOrientation(uint32_t transform) { |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 117 | const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1); |
| 118 | const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1); |
| 119 | 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] | 120 | mat4 tr; |
| 121 | |
| 122 | if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) { |
| 123 | tr = tr * rot90; |
| 124 | } |
| 125 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) { |
| 126 | tr = tr * flipH; |
| 127 | } |
| 128 | if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) { |
| 129 | tr = tr * flipV; |
| 130 | } |
| 131 | return inverse(tr); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * onDraw will draw the current layer onto the presentable buffer |
| 136 | */ |
| 137 | void BufferLayer::onDraw(const RenderArea& renderArea, const Region& clip, |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 138 | bool useIdentityTransform) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 139 | ATRACE_CALL(); |
| 140 | |
David Sodman | ca10ed2 | 2018-04-16 14:10:25 -0700 | [diff] [blame] | 141 | CompositionInfo& compositionInfo = getBE().compositionInfo; |
| 142 | |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 143 | if (CC_UNLIKELY(mActiveBuffer == 0)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 144 | // the texture has not been created yet, this Layer has |
| 145 | // in fact never been drawn into. This happens frequently with |
| 146 | // SurfaceView because the WindowManager can't know when the client |
| 147 | // has drawn the first time. |
| 148 | |
| 149 | // If there is nothing under us, we paint the screen in black, otherwise |
| 150 | // we just skip this update. |
| 151 | |
| 152 | // figure out if there is something below us |
| 153 | Region under; |
| 154 | bool finished = false; |
| 155 | mFlinger->mDrawingState.traverseInZOrder([&](Layer* layer) { |
| 156 | if (finished || layer == static_cast<BufferLayer const*>(this)) { |
| 157 | finished = true; |
| 158 | return; |
| 159 | } |
| 160 | under.orSelf(renderArea.getTransform().transform(layer->visibleRegion)); |
| 161 | }); |
| 162 | // if not everything below us is covered, we plug the holes! |
| 163 | Region holes(clip.subtract(under)); |
| 164 | if (!holes.isEmpty()) { |
| 165 | clearWithOpenGL(renderArea, 0, 0, 0, 1); |
| 166 | } |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // Bind the current buffer to the GL texture, and wait for it to be |
| 171 | // ready for us to draw into. |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 172 | status_t err = bindTextureImage(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 173 | if (err != NO_ERROR) { |
| 174 | ALOGW("onDraw: bindTextureImage failed (err=%d)", err); |
| 175 | // Go ahead and draw the buffer anyway; no matter what we do the screen |
| 176 | // is probably going to have something visibly wrong. |
| 177 | } |
| 178 | |
| 179 | bool blackOutLayer = isProtected() || (isSecure() && !renderArea.isSecure()); |
| 180 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 181 | auto& engine(mFlinger->getRenderEngine()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 182 | |
| 183 | if (!blackOutLayer) { |
| 184 | // TODO: we could be more subtle with isFixedSize() |
| 185 | const bool useFiltering = getFiltering() || needsFiltering(renderArea) || isFixedSize(); |
| 186 | |
| 187 | // Query the texture matrix given our current filtering mode. |
| 188 | float textureMatrix[16]; |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 189 | setFilteringEnabled(useFiltering); |
| 190 | getDrawingTransformMatrix(textureMatrix); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 191 | |
| 192 | if (getTransformToDisplayInverse()) { |
| 193 | /* |
| 194 | * the code below applies the primary display's inverse transform to |
| 195 | * the texture transform |
| 196 | */ |
| 197 | uint32_t transform = DisplayDevice::getPrimaryDisplayOrientationTransform(); |
| 198 | mat4 tr = inverseOrientation(transform); |
| 199 | |
| 200 | /** |
| 201 | * TODO(b/36727915): This is basically a hack. |
| 202 | * |
| 203 | * Ensure that regardless of the parent transformation, |
| 204 | * this buffer is always transformed from native display |
| 205 | * orientation to display orientation. For example, in the case |
| 206 | * of a camera where the buffer remains in native orientation, |
| 207 | * we want the pixels to always be upright. |
| 208 | */ |
| 209 | sp<Layer> p = mDrawingParent.promote(); |
| 210 | if (p != nullptr) { |
| 211 | const auto parentTransform = p->getTransform(); |
| 212 | tr = tr * inverseOrientation(parentTransform.getOrientation()); |
| 213 | } |
| 214 | |
| 215 | // and finally apply it to the original texture matrix |
| 216 | const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr); |
| 217 | memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix)); |
| 218 | } |
| 219 | |
| 220 | // Set things up for texturing. |
David Sodman | 0cf8f8d | 2017-12-20 18:19:45 -0800 | [diff] [blame] | 221 | mTexture.setDimensions(mActiveBuffer->getWidth(), mActiveBuffer->getHeight()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 222 | mTexture.setFiltering(useFiltering); |
| 223 | mTexture.setMatrix(textureMatrix); |
David Sodman | ca10ed2 | 2018-04-16 14:10:25 -0700 | [diff] [blame] | 224 | compositionInfo.re.texture = mTexture; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 225 | |
| 226 | engine.setupLayerTexturing(mTexture); |
| 227 | } else { |
| 228 | engine.setupLayerBlackedOut(); |
| 229 | } |
| 230 | drawWithOpenGL(renderArea, useIdentityTransform); |
| 231 | engine.disableTexturing(); |
| 232 | } |
| 233 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 234 | void BufferLayer::drawNow(const RenderArea& renderArea, bool useIdentityTransform) { |
David Sodman | ca10ed2 | 2018-04-16 14:10:25 -0700 | [diff] [blame] | 235 | CompositionInfo& compositionInfo = getBE().compositionInfo; |
| 236 | auto& engine(mFlinger->getRenderEngine()); |
| 237 | |
| 238 | draw(renderArea, useIdentityTransform); |
| 239 | |
| 240 | engine.setupLayerTexturing(compositionInfo.re.texture); |
| 241 | engine.setupLayerBlending(compositionInfo.re.preMultipliedAlpha, compositionInfo.re.opaque, |
| 242 | false, compositionInfo.re.color); |
| 243 | engine.setSourceDataSpace(compositionInfo.hwc.dataspace); |
| 244 | engine.setSourceY410BT2020(compositionInfo.re.Y410BT2020); |
| 245 | engine.drawMesh(getBE().getMesh()); |
| 246 | engine.disableBlending(); |
| 247 | engine.disableTexturing(); |
| 248 | engine.setSourceY410BT2020(false); |
| 249 | } |
| 250 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 251 | bool BufferLayer::isHdrY410() const { |
| 252 | // pixel format is HDR Y410 masquerading as RGBA_1010102 |
| 253 | return (mCurrentDataSpace == ui::Dataspace::BT2020_ITU_PQ && |
| 254 | getDrawingApi() == NATIVE_WINDOW_API_MEDIA && |
| 255 | getBE().compositionInfo.mBuffer->getPixelFormat() == HAL_PIXEL_FORMAT_RGBA_1010102); |
David Sodman | eb085e0 | 2017-10-05 18:49:04 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 258 | void BufferLayer::setPerFrameData(const sp<const DisplayDevice>& display) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 259 | // Apply this display's projection's viewport to the visible region |
| 260 | // before giving it to the HWC HAL. |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 261 | const ui::Transform& tr = display->getTransform(); |
Dominik Laskowski | eecd659 | 2018-05-29 10:25:41 -0700 | [diff] [blame] | 262 | const auto& viewport = display->getViewport(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 263 | Region visible = tr.transform(visibleRegion.intersect(viewport)); |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 264 | const auto displayId = display->getId(); |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 265 | if (!hasHwcLayer(displayId)) { |
| 266 | ALOGE("[%s] failed to setPerFrameData: no HWC layer found (%d)", |
| 267 | mName.string(), displayId); |
| 268 | return; |
| 269 | } |
| 270 | auto& hwcInfo = getBE().mHwcLayers[displayId]; |
| 271 | auto& hwcLayer = hwcInfo.layer; |
| 272 | auto error = hwcLayer->setVisibleRegion(visible); |
| 273 | if (error != HWC2::Error::None) { |
| 274 | ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(), |
| 275 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 276 | visible.dump(LOG_TAG); |
| 277 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 278 | |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 279 | error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); |
| 280 | if (error != HWC2::Error::None) { |
| 281 | ALOGE("[%s] Failed to set surface damage: %s (%d)", mName.string(), |
| 282 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 283 | surfaceDamageRegion.dump(LOG_TAG); |
| 284 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 285 | |
| 286 | // Sideband layers |
David Sodman | 0cc6918 | 2017-11-17 12:12:07 -0800 | [diff] [blame] | 287 | if (getBE().compositionInfo.hwc.sidebandStream.get()) { |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 288 | setCompositionType(displayId, HWC2::Composition::Sideband); |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 289 | ALOGV("[%s] Requesting Sideband composition", mName.string()); |
| 290 | error = hwcLayer->setSidebandStream(getBE().compositionInfo.hwc.sidebandStream->handle()); |
| 291 | if (error != HWC2::Error::None) { |
| 292 | ALOGE("[%s] Failed to set sideband stream %p: %s (%d)", mName.string(), |
| 293 | getBE().compositionInfo.hwc.sidebandStream->handle(), to_string(error).c_str(), |
| 294 | static_cast<int32_t>(error)); |
| 295 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 299 | // Device or Cursor layers |
| 300 | if (mPotentialCursor) { |
| 301 | ALOGV("[%s] Requesting Cursor composition", mName.string()); |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 302 | setCompositionType(displayId, HWC2::Composition::Cursor); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 303 | } else { |
| 304 | ALOGV("[%s] Requesting Device composition", mName.string()); |
Dominik Laskowski | 7e04546 | 2018-05-30 13:02:02 -0700 | [diff] [blame] | 305 | setCompositionType(displayId, HWC2::Composition::Device); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 308 | ALOGV("setPerFrameData: dataspace = %d", mCurrentDataSpace); |
| 309 | error = hwcLayer->setDataspace(mCurrentDataSpace); |
| 310 | if (error != HWC2::Error::None) { |
| 311 | ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace, |
| 312 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 313 | } |
| 314 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 315 | const HdrMetadata& metadata = getDrawingHdrMetadata(); |
Lloyd Pique | 074e812 | 2018-07-26 12:57:23 -0700 | [diff] [blame] | 316 | error = hwcLayer->setPerFrameMetadata(display->getSupportedPerFrameMetadata(), metadata); |
| 317 | if (error != HWC2::Error::None && error != HWC2::Error::Unsupported) { |
| 318 | ALOGE("[%s] Failed to set hdrMetadata: %s (%d)", mName.string(), |
| 319 | to_string(error).c_str(), static_cast<int32_t>(error)); |
| 320 | } |
| 321 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 322 | setHwcLayerBuffer(display); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 325 | bool BufferLayer::onPreComposition(nsecs_t refreshStartTime) { |
| 326 | if (mBufferLatched) { |
| 327 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 328 | mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 329 | } |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 330 | mRefreshPending = false; |
| 331 | return hasReadyFrame(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 334 | bool BufferLayer::onPostComposition(const std::shared_ptr<FenceTime>& glDoneFence, |
| 335 | const std::shared_ptr<FenceTime>& presentFence, |
| 336 | const CompositorTiming& compositorTiming) { |
| 337 | // mFrameLatencyNeeded is true when a new frame was latched for the |
| 338 | // composition. |
| 339 | if (!mFrameLatencyNeeded) return false; |
| 340 | |
| 341 | // Update mFrameEventHistory. |
Dan Stoza | 436ccf3 | 2018-06-21 12:10:12 -0700 | [diff] [blame] | 342 | { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 343 | Mutex::Autolock lock(mFrameEventHistoryMutex); |
| 344 | mFrameEventHistory.addPostComposition(mCurrentFrameNumber, glDoneFence, presentFence, |
| 345 | compositorTiming); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 348 | // Update mFrameTracker. |
| 349 | nsecs_t desiredPresentTime = getDesiredPresentTime(); |
| 350 | mFrameTracker.setDesiredPresentTime(desiredPresentTime); |
| 351 | |
| 352 | const std::string layerName(getName().c_str()); |
| 353 | mTimeStats.setDesiredTime(layerName, mCurrentFrameNumber, desiredPresentTime); |
| 354 | |
| 355 | std::shared_ptr<FenceTime> frameReadyFence = getCurrentFenceTime(); |
| 356 | if (frameReadyFence->isValid()) { |
| 357 | mFrameTracker.setFrameReadyFence(std::move(frameReadyFence)); |
| 358 | } else { |
| 359 | // There was no fence for this frame, so assume that it was ready |
| 360 | // to be presented at the desired present time. |
| 361 | mFrameTracker.setFrameReadyTime(desiredPresentTime); |
Dominik Laskowski | 45de9bd | 2018-06-11 17:44:10 -0700 | [diff] [blame] | 362 | } |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 363 | |
| 364 | if (presentFence->isValid()) { |
| 365 | mTimeStats.setPresentFence(layerName, mCurrentFrameNumber, presentFence); |
| 366 | mFrameTracker.setActualPresentFence(std::shared_ptr<FenceTime>(presentFence)); |
| 367 | } else if (mFlinger->getHwComposer().isConnected(HWC_DISPLAY_PRIMARY)) { |
| 368 | // The HWC doesn't support present fences, so use the refresh |
| 369 | // timestamp instead. |
| 370 | const nsecs_t actualPresentTime = |
| 371 | mFlinger->getHwComposer().getRefreshTimestamp(HWC_DISPLAY_PRIMARY); |
| 372 | mTimeStats.setPresentTime(layerName, mCurrentFrameNumber, actualPresentTime); |
| 373 | mFrameTracker.setActualPresentTime(actualPresentTime); |
| 374 | } |
| 375 | |
| 376 | mFrameTracker.advanceFrame(); |
| 377 | mFrameLatencyNeeded = false; |
| 378 | return true; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 381 | Region BufferLayer::latchBuffer(bool& recomputeVisibleRegions, nsecs_t latchTime) { |
| 382 | ATRACE_CALL(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 383 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 384 | std::optional<Region> sidebandStreamDirtyRegion = latchSidebandStream(recomputeVisibleRegions); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 385 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 386 | if (sidebandStreamDirtyRegion) { |
| 387 | return *sidebandStreamDirtyRegion; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 390 | Region dirtyRegion; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 391 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 392 | if (!hasReadyFrame()) { |
| 393 | return dirtyRegion; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 394 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 395 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 396 | // if we've already called updateTexImage() without going through |
| 397 | // a composition step, we have to skip this layer at this point |
| 398 | // because we cannot call updateTeximage() without a corresponding |
| 399 | // compositionComplete() call. |
| 400 | // we'll trigger an update in onPreComposition(). |
| 401 | if (mRefreshPending) { |
| 402 | return dirtyRegion; |
| 403 | } |
| 404 | |
| 405 | // If the head buffer's acquire fence hasn't signaled yet, return and |
| 406 | // try again later |
| 407 | if (!fenceHasSignaled()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 408 | mFlinger->signalLayerUpdate(); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 409 | return dirtyRegion; |
| 410 | } |
| 411 | |
| 412 | // Capture the old state of the layer for comparisons later |
| 413 | const State& s(getDrawingState()); |
| 414 | const bool oldOpacity = isOpaque(s); |
| 415 | sp<GraphicBuffer> oldBuffer = mActiveBuffer; |
| 416 | |
| 417 | if (!allTransactionsSignaled()) { |
| 418 | mFlinger->signalLayerUpdate(); |
| 419 | return dirtyRegion; |
| 420 | } |
| 421 | |
| 422 | status_t err = updateTexImage(recomputeVisibleRegions, latchTime); |
| 423 | if (err != NO_ERROR) { |
| 424 | return dirtyRegion; |
| 425 | } |
| 426 | |
| 427 | err = updateActiveBuffer(); |
| 428 | if (err != NO_ERROR) { |
| 429 | return dirtyRegion; |
| 430 | } |
| 431 | |
| 432 | mBufferLatched = true; |
| 433 | |
| 434 | err = updateFrameNumber(latchTime); |
| 435 | if (err != NO_ERROR) { |
| 436 | return dirtyRegion; |
| 437 | } |
| 438 | |
| 439 | mRefreshPending = true; |
| 440 | mFrameLatencyNeeded = true; |
| 441 | if (oldBuffer == nullptr) { |
| 442 | // the first time we receive a buffer, we need to trigger a |
| 443 | // geometry invalidation. |
| 444 | recomputeVisibleRegions = true; |
| 445 | } |
| 446 | |
| 447 | ui::Dataspace dataSpace = getDrawingDataSpace(); |
| 448 | // treat modern dataspaces as legacy dataspaces whenever possible, until |
| 449 | // we can trust the buffer producers |
| 450 | switch (dataSpace) { |
| 451 | case ui::Dataspace::V0_SRGB: |
| 452 | dataSpace = ui::Dataspace::SRGB; |
| 453 | break; |
| 454 | case ui::Dataspace::V0_SRGB_LINEAR: |
| 455 | dataSpace = ui::Dataspace::SRGB_LINEAR; |
| 456 | break; |
| 457 | case ui::Dataspace::V0_JFIF: |
| 458 | dataSpace = ui::Dataspace::JFIF; |
| 459 | break; |
| 460 | case ui::Dataspace::V0_BT601_625: |
| 461 | dataSpace = ui::Dataspace::BT601_625; |
| 462 | break; |
| 463 | case ui::Dataspace::V0_BT601_525: |
| 464 | dataSpace = ui::Dataspace::BT601_525; |
| 465 | break; |
| 466 | case ui::Dataspace::V0_BT709: |
| 467 | dataSpace = ui::Dataspace::BT709; |
| 468 | break; |
| 469 | default: |
| 470 | break; |
| 471 | } |
| 472 | mCurrentDataSpace = dataSpace; |
| 473 | |
| 474 | Rect crop(getDrawingCrop()); |
| 475 | const uint32_t transform(getDrawingTransform()); |
| 476 | const uint32_t scalingMode(getDrawingScalingMode()); |
| 477 | if ((crop != mCurrentCrop) || (transform != mCurrentTransform) || |
| 478 | (scalingMode != mCurrentScalingMode)) { |
| 479 | mCurrentCrop = crop; |
| 480 | mCurrentTransform = transform; |
| 481 | mCurrentScalingMode = scalingMode; |
| 482 | recomputeVisibleRegions = true; |
| 483 | } |
| 484 | |
| 485 | if (oldBuffer != nullptr) { |
| 486 | uint32_t bufWidth = mActiveBuffer->getWidth(); |
| 487 | uint32_t bufHeight = mActiveBuffer->getHeight(); |
| 488 | if (bufWidth != uint32_t(oldBuffer->width) || bufHeight != uint32_t(oldBuffer->height)) { |
| 489 | recomputeVisibleRegions = true; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (oldOpacity != isOpaque(s)) { |
| 494 | recomputeVisibleRegions = true; |
| 495 | } |
| 496 | |
| 497 | // Remove any sync points corresponding to the buffer which was just |
| 498 | // latched |
| 499 | { |
| 500 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 501 | auto point = mLocalSyncPoints.begin(); |
| 502 | while (point != mLocalSyncPoints.end()) { |
| 503 | if (!(*point)->frameIsAvailable() || !(*point)->transactionIsApplied()) { |
| 504 | // This sync point must have been added since we started |
| 505 | // latching. Don't drop it yet. |
| 506 | ++point; |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | if ((*point)->getFrameNumber() <= mCurrentFrameNumber) { |
| 511 | point = mLocalSyncPoints.erase(point); |
| 512 | } else { |
| 513 | ++point; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // FIXME: postedRegion should be dirty & bounds |
| 519 | // transform the dirty region to window-manager space |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 520 | return getTransform().transform(Region(Rect(getActiveWidth(s), getActiveHeight(s)))); |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // transaction |
| 524 | void BufferLayer::notifyAvailableFrames() { |
| 525 | auto headFrameNumber = getHeadFrameNumber(); |
| 526 | bool headFenceSignaled = fenceHasSignaled(); |
| 527 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 528 | for (auto& point : mLocalSyncPoints) { |
| 529 | if (headFrameNumber >= point->getFrameNumber() && headFenceSignaled) { |
| 530 | point->setFrameAvailable(); |
| 531 | } |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 535 | bool BufferLayer::hasReadyFrame() const { |
| 536 | return hasDrawingBuffer() || getSidebandStreamChanged() || getAutoRefresh(); |
| 537 | } |
| 538 | |
| 539 | uint32_t BufferLayer::getEffectiveScalingMode() const { |
| 540 | if (mOverrideScalingMode >= 0) { |
| 541 | return mOverrideScalingMode; |
| 542 | } |
| 543 | |
| 544 | return mCurrentScalingMode; |
| 545 | } |
| 546 | |
| 547 | bool BufferLayer::isProtected() const { |
| 548 | const sp<GraphicBuffer>& buffer(mActiveBuffer); |
| 549 | return (buffer != 0) && (buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 550 | } |
| 551 | |
| 552 | bool BufferLayer::latchUnsignaledBuffers() { |
| 553 | static bool propertyLoaded = false; |
| 554 | static bool latch = false; |
| 555 | static std::mutex mutex; |
| 556 | std::lock_guard<std::mutex> lock(mutex); |
| 557 | if (!propertyLoaded) { |
| 558 | char value[PROPERTY_VALUE_MAX] = {}; |
| 559 | property_get("debug.sf.latch_unsignaled", value, "0"); |
| 560 | latch = atoi(value); |
| 561 | propertyLoaded = true; |
| 562 | } |
| 563 | return latch; |
| 564 | } |
| 565 | |
| 566 | // h/w composer set-up |
| 567 | bool BufferLayer::allTransactionsSignaled() { |
| 568 | auto headFrameNumber = getHeadFrameNumber(); |
| 569 | bool matchingFramesFound = false; |
| 570 | bool allTransactionsApplied = true; |
| 571 | Mutex::Autolock lock(mLocalSyncPointMutex); |
| 572 | |
| 573 | for (auto& point : mLocalSyncPoints) { |
| 574 | if (point->getFrameNumber() > headFrameNumber) { |
| 575 | break; |
| 576 | } |
| 577 | matchingFramesFound = true; |
| 578 | |
| 579 | if (!point->frameIsAvailable()) { |
| 580 | // We haven't notified the remote layer that the frame for |
| 581 | // this point is available yet. Notify it now, and then |
| 582 | // abort this attempt to latch. |
| 583 | point->setFrameAvailable(); |
| 584 | allTransactionsApplied = false; |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | allTransactionsApplied = allTransactionsApplied && point->transactionIsApplied(); |
| 589 | } |
| 590 | return !matchingFramesFound || allTransactionsApplied; |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | // As documented in libhardware header, formats in the range |
| 594 | // 0x100 - 0x1FF are specific to the HAL implementation, and |
| 595 | // are known to have no alpha channel |
| 596 | // TODO: move definition for device-specific range into |
| 597 | // hardware.h, instead of using hard-coded values here. |
| 598 | #define HARDWARE_IS_DEVICE_FORMAT(f) ((f) >= 0x100 && (f) <= 0x1FF) |
| 599 | |
| 600 | bool BufferLayer::getOpacityForFormat(uint32_t format) { |
| 601 | if (HARDWARE_IS_DEVICE_FORMAT(format)) { |
| 602 | return true; |
| 603 | } |
| 604 | switch (format) { |
| 605 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 606 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 607 | case HAL_PIXEL_FORMAT_RGBA_FP16: |
| 608 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
| 609 | return false; |
| 610 | } |
| 611 | // in all other case, we have no blending (also for unknown formats) |
| 612 | return true; |
| 613 | } |
| 614 | |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 615 | bool BufferLayer::needsFiltering(const RenderArea& renderArea) const { |
| 616 | return mNeedsFiltering || renderArea.needsFiltering(); |
Chia-I Wu | 692e083 | 2018-06-05 15:46:58 -0700 | [diff] [blame] | 617 | } |
| 618 | |
David Sodman | 41fdfc9 | 2017-11-06 16:09:56 -0800 | [diff] [blame] | 619 | void BufferLayer::drawWithOpenGL(const RenderArea& renderArea, bool useIdentityTransform) const { |
Dan Stoza | 84d619e | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 620 | ATRACE_CALL(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 621 | const State& s(getDrawingState()); |
| 622 | |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 623 | computeGeometry(renderArea, getBE().mMesh, useIdentityTransform); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * NOTE: the way we compute the texture coordinates here produces |
| 627 | * different results than when we take the HWC path -- in the later case |
| 628 | * the "source crop" is rounded to texel boundaries. |
| 629 | * This can produce significantly different results when the texture |
| 630 | * is scaled by a large amount. |
| 631 | * |
| 632 | * The GL code below is more logical (imho), and the difference with |
| 633 | * HWC is due to a limitation of the HWC API to integers -- a question |
| 634 | * is suspend is whether we should ignore this problem or revert to |
| 635 | * GL composition when a buffer scaling is applied (maybe with some |
| 636 | * minimal value)? Or, we could make GL behave like HWC -- but this feel |
| 637 | * like more of a hack. |
| 638 | */ |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 639 | const Rect bounds{computeBounds()}; // Rounds from FloatRect |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 640 | |
Peiyong Lin | efefaac | 2018-08-17 12:27:51 -0700 | [diff] [blame] | 641 | ui::Transform t = getTransform(); |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 642 | Rect win = bounds; |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 643 | Rect finalCrop = getFinalCrop(s); |
| 644 | if (!finalCrop.isEmpty()) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 645 | win = t.transform(win); |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 646 | if (!win.intersect(finalCrop, &win)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 647 | win.clear(); |
| 648 | } |
| 649 | win = t.inverse().transform(win); |
Dan Stoza | 80d6116 | 2017-12-20 15:57:52 -0800 | [diff] [blame] | 650 | if (!win.intersect(bounds, &win)) { |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 651 | win.clear(); |
| 652 | } |
| 653 | } |
| 654 | |
Marissa Wall | 61c5862 | 2018-07-18 10:12:20 -0700 | [diff] [blame] | 655 | float left = float(win.left) / float(getActiveWidth(s)); |
| 656 | float top = float(win.top) / float(getActiveHeight(s)); |
| 657 | float right = float(win.right) / float(getActiveWidth(s)); |
| 658 | float bottom = float(win.bottom) / float(getActiveHeight(s)); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 659 | |
| 660 | // TODO: we probably want to generate the texture coords with the mesh |
| 661 | // here we assume that we only have 4 vertices |
David Sodman | 9eeae69 | 2017-11-02 10:53:32 -0700 | [diff] [blame] | 662 | Mesh::VertexArray<vec2> texCoords(getBE().mMesh.getTexCoordArray<vec2>()); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 663 | texCoords[0] = vec2(left, 1.0f - top); |
| 664 | texCoords[1] = vec2(left, 1.0f - bottom); |
| 665 | texCoords[2] = vec2(right, 1.0f - bottom); |
| 666 | texCoords[3] = vec2(right, 1.0f - top); |
| 667 | |
bohu | 2156613 | 2018-03-27 14:36:34 -0700 | [diff] [blame] | 668 | auto& engine(mFlinger->getRenderEngine()); |
| 669 | engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(s), false /* disableTexture */, |
| 670 | getColor()); |
Chia-I Wu | 01591c9 | 2018-05-22 12:03:00 -0700 | [diff] [blame] | 671 | engine.setSourceDataSpace(mCurrentDataSpace); |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 672 | |
Chia-I Wu | 692e083 | 2018-06-05 15:46:58 -0700 | [diff] [blame] | 673 | if (isHdrY410()) { |
bohu | 2156613 | 2018-03-27 14:36:34 -0700 | [diff] [blame] | 674 | engine.setSourceY410BT2020(true); |
Chia-I Wu | 5c6e463 | 2018-01-11 08:54:38 -0800 | [diff] [blame] | 675 | } |
bohu | 2156613 | 2018-03-27 14:36:34 -0700 | [diff] [blame] | 676 | |
| 677 | engine.drawMesh(getBE().mMesh); |
| 678 | engine.disableBlending(); |
| 679 | |
| 680 | engine.setSourceY410BT2020(false); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 681 | } |
| 682 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 683 | uint64_t BufferLayer::getHeadFrameNumber() const { |
Marissa Wall | fd66862 | 2018-05-10 10:21:13 -0700 | [diff] [blame] | 684 | if (hasDrawingBuffer()) { |
| 685 | return getFrameNumber(); |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 686 | } else { |
| 687 | return mCurrentFrameNumber; |
| 688 | } |
| 689 | } |
| 690 | |
David Sodman | 0c69cad | 2017-08-21 12:12:51 -0700 | [diff] [blame] | 691 | } // namespace android |
| 692 | |
| 693 | #if defined(__gl_h_) |
| 694 | #error "don't include gl/gl.h in this file" |
| 695 | #endif |
| 696 | |
| 697 | #if defined(__gl2_h_) |
| 698 | #error "don't include gl2/gl2.h in this file" |
| 699 | #endif |