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