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