John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "RenderEngine" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
rnlee | c6a7364 | 2021-06-04 14:16:42 -0700 | [diff] [blame] | 21 | #include "SkiaRenderEngine.h" |
| 22 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 23 | #include <GrBackendSemaphore.h> |
| 24 | #include <GrContextOptions.h> |
Kevin Lubick | 4011eb9 | 2023-09-12 14:18:19 +0000 | [diff] [blame] | 25 | #include <GrTypes.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 26 | #include <SkBlendMode.h> |
| 27 | #include <SkCanvas.h> |
| 28 | #include <SkColor.h> |
| 29 | #include <SkColorFilter.h> |
| 30 | #include <SkColorMatrix.h> |
| 31 | #include <SkColorSpace.h> |
| 32 | #include <SkData.h> |
| 33 | #include <SkGraphics.h> |
| 34 | #include <SkImage.h> |
| 35 | #include <SkImageFilters.h> |
| 36 | #include <SkImageInfo.h> |
| 37 | #include <SkM44.h> |
| 38 | #include <SkMatrix.h> |
| 39 | #include <SkPaint.h> |
| 40 | #include <SkPath.h> |
| 41 | #include <SkPoint.h> |
| 42 | #include <SkPoint3.h> |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 43 | #include <SkRRect.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 44 | #include <SkRect.h> |
| 45 | #include <SkRefCnt.h> |
| 46 | #include <SkRegion.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 47 | #include <SkRuntimeEffect.h> |
| 48 | #include <SkSamplingOptions.h> |
| 49 | #include <SkScalar.h> |
| 50 | #include <SkShader.h> |
| 51 | #include <SkShadowUtils.h> |
| 52 | #include <SkString.h> |
| 53 | #include <SkSurface.h> |
| 54 | #include <SkTileMode.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 55 | #include <android-base/stringprintf.h> |
Leon Scroggins III | df8166e | 2024-01-25 15:50:56 -0500 | [diff] [blame] | 56 | #include <common/FlagManager.h> |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 57 | #include <gui/FenceMonitor.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 58 | #include <gui/TraceUtils.h> |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 59 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 60 | #include <pthread.h> |
| 61 | #include <src/core/SkTraceEventCommon.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 62 | #include <sync/sync.h> |
| 63 | #include <ui/BlurRegion.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 64 | #include <ui/DebugUtils.h> |
| 65 | #include <ui/GraphicBuffer.h> |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 66 | #include <ui/HdrRenderTypeUtils.h> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 67 | #include <utils/Trace.h> |
| 68 | |
| 69 | #include <cmath> |
| 70 | #include <cstdint> |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 71 | #include <deque> |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 72 | #include <memory> |
| 73 | #include <numeric> |
| 74 | |
| 75 | #include "Cache.h" |
| 76 | #include "ColorSpaces.h" |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 77 | #include "compat/SkiaGpuContext.h" |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 78 | #include "filters/BlurFilter.h" |
| 79 | #include "filters/GaussianBlurFilter.h" |
| 80 | #include "filters/KawaseBlurFilter.h" |
| 81 | #include "filters/LinearEffect.h" |
| 82 | #include "log/log_main.h" |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 83 | #include "skia/compat/SkiaBackendTexture.h" |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 84 | #include "skia/debug/SkiaCapture.h" |
| 85 | #include "skia/debug/SkiaMemoryReporter.h" |
| 86 | #include "skia/filters/StretchShaderFactory.h" |
| 87 | #include "system/graphics-base-v1.0.h" |
| 88 | |
| 89 | namespace { |
| 90 | |
| 91 | // Debugging settings |
| 92 | static const bool kPrintLayerSettings = false; |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 93 | static const bool kGaneshFlushAfterEveryLayer = kPrintLayerSettings; |
John Reck | 5b02fc4 | 2023-06-14 14:41:10 -0400 | [diff] [blame] | 94 | static constexpr bool kEnableLayerBrightening = true; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | // Utility functions related to SkRect |
| 99 | |
| 100 | namespace { |
| 101 | |
| 102 | static inline SkRect getSkRect(const android::FloatRect& rect) { |
| 103 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); |
| 104 | } |
| 105 | |
| 106 | static inline SkRect getSkRect(const android::Rect& rect) { |
| 107 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Verifies that common, simple bounds + clip combinations can be converted into |
| 112 | * a single RRect draw call returning true if possible. If true the radii parameter |
| 113 | * will be filled with the correct radii values that combined with bounds param will |
| 114 | * produce the insected roundRect. If false, the returned state of the radii param is undefined. |
| 115 | */ |
| 116 | static bool intersectionIsRoundRect(const SkRect& bounds, const SkRect& crop, |
| 117 | const SkRect& insetCrop, const android::vec2& cornerRadius, |
| 118 | SkVector radii[4]) { |
| 119 | const bool leftEqual = bounds.fLeft == crop.fLeft; |
| 120 | const bool topEqual = bounds.fTop == crop.fTop; |
| 121 | const bool rightEqual = bounds.fRight == crop.fRight; |
| 122 | const bool bottomEqual = bounds.fBottom == crop.fBottom; |
| 123 | |
| 124 | // In the event that the corners of the bounds only partially align with the crop we |
| 125 | // need to ensure that the resulting shape can still be represented as a round rect. |
| 126 | // In particular the round rect implementation will scale the value of all corner radii |
| 127 | // if the sum of the radius along any edge is greater than the length of that edge. |
| 128 | // See https://www.w3.org/TR/css-backgrounds-3/#corner-overlap |
| 129 | const bool requiredWidth = bounds.width() > (cornerRadius.x * 2); |
| 130 | const bool requiredHeight = bounds.height() > (cornerRadius.y * 2); |
| 131 | if (!requiredWidth || !requiredHeight) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | // Check each cropped corner to ensure that it exactly matches the crop or its corner is |
| 136 | // contained within the cropped shape and does not need rounded. |
| 137 | // compute the UpperLeft corner radius |
| 138 | if (leftEqual && topEqual) { |
| 139 | radii[0].set(cornerRadius.x, cornerRadius.y); |
| 140 | } else if ((leftEqual && bounds.fTop >= insetCrop.fTop) || |
| 141 | (topEqual && bounds.fLeft >= insetCrop.fLeft)) { |
| 142 | radii[0].set(0, 0); |
| 143 | } else { |
| 144 | return false; |
| 145 | } |
| 146 | // compute the UpperRight corner radius |
| 147 | if (rightEqual && topEqual) { |
| 148 | radii[1].set(cornerRadius.x, cornerRadius.y); |
| 149 | } else if ((rightEqual && bounds.fTop >= insetCrop.fTop) || |
| 150 | (topEqual && bounds.fRight <= insetCrop.fRight)) { |
| 151 | radii[1].set(0, 0); |
| 152 | } else { |
| 153 | return false; |
| 154 | } |
| 155 | // compute the BottomRight corner radius |
| 156 | if (rightEqual && bottomEqual) { |
| 157 | radii[2].set(cornerRadius.x, cornerRadius.y); |
| 158 | } else if ((rightEqual && bounds.fBottom <= insetCrop.fBottom) || |
| 159 | (bottomEqual && bounds.fRight <= insetCrop.fRight)) { |
| 160 | radii[2].set(0, 0); |
| 161 | } else { |
| 162 | return false; |
| 163 | } |
| 164 | // compute the BottomLeft corner radius |
| 165 | if (leftEqual && bottomEqual) { |
| 166 | radii[3].set(cornerRadius.x, cornerRadius.y); |
| 167 | } else if ((leftEqual && bounds.fBottom <= insetCrop.fBottom) || |
| 168 | (bottomEqual && bounds.fLeft >= insetCrop.fLeft)) { |
| 169 | radii[3].set(0, 0); |
| 170 | } else { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | static inline std::pair<SkRRect, SkRRect> getBoundsAndClip(const android::FloatRect& boundsRect, |
| 178 | const android::FloatRect& cropRect, |
| 179 | const android::vec2& cornerRadius) { |
| 180 | const SkRect bounds = getSkRect(boundsRect); |
| 181 | const SkRect crop = getSkRect(cropRect); |
| 182 | |
| 183 | SkRRect clip; |
| 184 | if (cornerRadius.x > 0 && cornerRadius.y > 0) { |
| 185 | // it the crop and the bounds are equivalent or there is no crop then we don't need a clip |
| 186 | if (bounds == crop || crop.isEmpty()) { |
| 187 | return {SkRRect::MakeRectXY(bounds, cornerRadius.x, cornerRadius.y), clip}; |
| 188 | } |
| 189 | |
| 190 | // This makes an effort to speed up common, simple bounds + clip combinations by |
| 191 | // converting them to a single RRect draw. It is possible there are other cases |
| 192 | // that can be converted. |
| 193 | if (crop.contains(bounds)) { |
| 194 | const auto insetCrop = crop.makeInset(cornerRadius.x, cornerRadius.y); |
| 195 | if (insetCrop.contains(bounds)) { |
| 196 | return {SkRRect::MakeRect(bounds), clip}; // clip is empty - no rounding required |
| 197 | } |
| 198 | |
| 199 | SkVector radii[4]; |
| 200 | if (intersectionIsRoundRect(bounds, crop, insetCrop, cornerRadius, radii)) { |
| 201 | SkRRect intersectionBounds; |
| 202 | intersectionBounds.setRectRadii(bounds, radii); |
| 203 | return {intersectionBounds, clip}; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // we didn't hit any of our fast paths so set the clip to the cropRect |
| 208 | clip.setRectXY(crop, cornerRadius.x, cornerRadius.y); |
| 209 | } |
| 210 | |
| 211 | // if we hit this point then we either don't have rounded corners or we are going to rely |
| 212 | // on the clip to round the corners for us |
| 213 | return {SkRRect::MakeRect(bounds), clip}; |
| 214 | } |
| 215 | |
| 216 | static inline bool layerHasBlur(const android::renderengine::LayerSettings& layer, |
| 217 | bool colorTransformModifiesAlpha) { |
| 218 | if (layer.backgroundBlurRadius > 0 || layer.blurRegions.size()) { |
| 219 | // return false if the content is opaque and would therefore occlude the blur |
| 220 | const bool opaqueContent = !layer.source.buffer.buffer || layer.source.buffer.isOpaque; |
| 221 | const bool opaqueAlpha = layer.alpha == 1.0f && !colorTransformModifiesAlpha; |
| 222 | return layer.skipContentDraw || !(opaqueContent && opaqueAlpha); |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | static inline SkColor getSkColor(const android::vec4& color) { |
| 228 | return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255); |
| 229 | } |
| 230 | |
| 231 | static inline SkM44 getSkM44(const android::mat4& matrix) { |
| 232 | return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], |
| 233 | matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1], |
| 234 | matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2], |
| 235 | matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]); |
| 236 | } |
| 237 | |
| 238 | static inline SkPoint3 getSkPoint3(const android::vec3& vector) { |
| 239 | return SkPoint3::Make(vector.x, vector.y, vector.z); |
| 240 | } |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 241 | } // namespace |
rnlee | c6a7364 | 2021-06-04 14:16:42 -0700 | [diff] [blame] | 242 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 243 | namespace android { |
| 244 | namespace renderengine { |
rnlee | c6a7364 | 2021-06-04 14:16:42 -0700 | [diff] [blame] | 245 | namespace skia { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 246 | |
| 247 | using base::StringAppendF; |
| 248 | |
Bruno BELANYI | b9b5b70 | 2023-10-13 13:25:11 +0000 | [diff] [blame] | 249 | std::future<void> SkiaRenderEngine::primeCache(bool shouldPrimeUltraHDR) { |
| 250 | Cache::primeShaderCache(this, shouldPrimeUltraHDR); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 251 | return {}; |
| 252 | } |
| 253 | |
| 254 | sk_sp<SkData> SkiaRenderEngine::SkSLCacheMonitor::load(const SkData& key) { |
| 255 | // This "cache" does not actually cache anything. It just allows us to |
| 256 | // monitor Skia's internal cache. So this method always returns null. |
| 257 | return nullptr; |
| 258 | } |
| 259 | |
| 260 | void SkiaRenderEngine::SkSLCacheMonitor::store(const SkData& key, const SkData& data, |
| 261 | const SkString& description) { |
| 262 | mShadersCachedSinceLastCall++; |
| 263 | mTotalShadersCompiled++; |
| 264 | ATRACE_FORMAT("SF cache: %i shaders", mTotalShadersCompiled); |
| 265 | } |
| 266 | |
| 267 | int SkiaRenderEngine::reportShadersCompiled() { |
| 268 | return mSkSLCacheMonitor.totalShadersCompiled(); |
| 269 | } |
Leon Scroggins III | a37ca99 | 2022-02-02 18:08:20 -0500 | [diff] [blame] | 270 | |
| 271 | void SkiaRenderEngine::setEnableTracing(bool tracingEnabled) { |
| 272 | SkAndroidFrameworkTraceUtil::setEnableTracing(tracingEnabled); |
rnlee | c6a7364 | 2021-06-04 14:16:42 -0700 | [diff] [blame] | 273 | } |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 274 | |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 275 | SkiaRenderEngine::SkiaRenderEngine(Threaded threaded, PixelFormat pixelFormat, |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 276 | bool supportsBackgroundBlur) |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 277 | : RenderEngine(threaded), mDefaultPixelFormat(pixelFormat) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 278 | if (supportsBackgroundBlur) { |
| 279 | ALOGD("Background Blurs Enabled"); |
| 280 | mBlurFilter = new KawaseBlurFilter(); |
| 281 | } |
| 282 | mCapture = std::make_unique<SkiaCapture>(); |
| 283 | } |
| 284 | |
| 285 | SkiaRenderEngine::~SkiaRenderEngine() { } |
| 286 | |
| 287 | // To be called from backend dtors. |
| 288 | void SkiaRenderEngine::finishRenderingAndAbandonContext() { |
| 289 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 290 | |
| 291 | if (mBlurFilter) { |
| 292 | delete mBlurFilter; |
| 293 | } |
| 294 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 295 | if (mContext) { |
| 296 | mContext->finishRenderingAndAbandonContext(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 299 | if (mProtectedContext) { |
| 300 | mProtectedContext->finishRenderingAndAbandonContext(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | void SkiaRenderEngine::useProtectedContext(bool useProtectedContext) { |
| 305 | if (useProtectedContext == mInProtectedContext || |
| 306 | (useProtectedContext && !supportsProtectedContent())) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // release any scratch resources before switching into a new mode |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 311 | if (getActiveContext()) { |
| 312 | getActiveContext()->purgeUnlockedScratchResources(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // Backend-specific way to switch to protected context |
| 316 | if (useProtectedContextImpl( |
| 317 | useProtectedContext ? GrProtected::kYes : GrProtected::kNo)) { |
| 318 | mInProtectedContext = useProtectedContext; |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 319 | // given that we are sharing the same thread between two contexts we need to |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 320 | // make sure that the thread state is reset when switching between the two. |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 321 | if (getActiveContext()) { |
| 322 | getActiveContext()->resetContextIfApplicable(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 327 | SkiaGpuContext* SkiaRenderEngine::getActiveContext() { |
| 328 | return mInProtectedContext ? mProtectedContext.get() : mContext.get(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static float toDegrees(uint32_t transform) { |
| 332 | switch (transform) { |
| 333 | case ui::Transform::ROT_90: |
| 334 | return 90.0; |
| 335 | case ui::Transform::ROT_180: |
| 336 | return 180.0; |
| 337 | case ui::Transform::ROT_270: |
| 338 | return 270.0; |
| 339 | default: |
| 340 | return 0.0; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static SkColorMatrix toSkColorMatrix(const android::mat4& matrix) { |
| 345 | return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1], |
| 346 | matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2], |
| 347 | matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3], |
| 348 | matrix[3][3], 0); |
| 349 | } |
| 350 | |
| 351 | static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) { |
| 352 | int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK; |
| 353 | int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK; |
| 354 | |
| 355 | // Treat unsupported dataspaces as srgb |
| 356 | if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR && |
| 357 | destTransfer != HAL_DATASPACE_TRANSFER_HLG && |
| 358 | destTransfer != HAL_DATASPACE_TRANSFER_ST2084) { |
| 359 | destTransfer = HAL_DATASPACE_TRANSFER_SRGB; |
| 360 | } |
| 361 | |
| 362 | if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR && |
| 363 | sourceTransfer != HAL_DATASPACE_TRANSFER_HLG && |
| 364 | sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) { |
| 365 | sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB; |
| 366 | } |
| 367 | |
| 368 | const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR; |
| 369 | const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB; |
| 370 | const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR; |
| 371 | const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB; |
| 372 | |
| 373 | return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) && |
| 374 | sourceTransfer != destTransfer; |
| 375 | } |
| 376 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 377 | void SkiaRenderEngine::ensureContextsCreated() { |
| 378 | if (mContext) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 382 | std::tie(mContext, mProtectedContext) = createContexts(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void SkiaRenderEngine::mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, |
| 386 | bool isRenderable) { |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 387 | // Only run this if RE is running on its own thread. This |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 388 | // way the access to GL/VK operations is guaranteed to be happening on the |
Ian Elliott | 1f0911e | 2022-09-09 16:31:47 -0600 | [diff] [blame] | 389 | // same thread. |
Leon Scroggins III | 696bf93 | 2024-01-24 15:21:05 -0500 | [diff] [blame] | 390 | if (!isThreaded()) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 391 | return; |
| 392 | } |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 393 | // We don't attempt to map a buffer if the buffer contains protected content. In GL this is |
| 394 | // important because GPU resources for protected buffers are much more limited. (In Vk we |
Robert Phillips | 59f7173 | 2023-08-23 15:18:27 -0400 | [diff] [blame] | 395 | // simply match the existing behavior for protected buffers.) We also never cache any |
| 396 | // buffers while in a protected context. |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 397 | const bool isProtectedBuffer = buffer->getUsage() & GRALLOC_USAGE_PROTECTED; |
Alec Mouri | 07196db | 2023-09-12 23:33:49 +0000 | [diff] [blame] | 398 | // Don't attempt to map buffers if we're not gpu sampleable. Callers shouldn't send a buffer |
| 399 | // over to RenderEngine. |
| 400 | const bool isGpuSampleable = buffer->getUsage() & GRALLOC_USAGE_HW_TEXTURE; |
| 401 | if (isProtectedBuffer || isProtected() || !isGpuSampleable) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | ATRACE_CALL(); |
| 405 | |
| 406 | // If we were to support caching protected buffers then we will need to switch the |
| 407 | // currently bound context if we are not already using the protected context (and subsequently |
| 408 | // switch back after the buffer is cached). However, for non-protected content we can bind |
| 409 | // the texture in either GL context because they are initialized with the same share_context |
| 410 | // which allows the texture state to be shared between them. |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 411 | auto context = getActiveContext(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 412 | auto& cache = mTextureCache; |
| 413 | |
| 414 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 415 | mGraphicBufferExternalRefs[buffer->getId()]++; |
| 416 | |
| 417 | if (const auto& iter = cache.find(buffer->getId()); iter == cache.end()) { |
Leon Scroggins III | df8166e | 2024-01-25 15:50:56 -0500 | [diff] [blame] | 418 | if (FlagManager::getInstance().renderable_buffer_usage()) { |
| 419 | isRenderable = buffer->getUsage() & GRALLOC_USAGE_HW_RENDER; |
| 420 | } |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 421 | std::unique_ptr<SkiaBackendTexture> backendTexture = |
| 422 | context->makeBackendTexture(buffer->toAHardwareBuffer(), isRenderable); |
| 423 | auto imageTextureRef = |
| 424 | std::make_shared<AutoBackendTexture::LocalRef>(std::move(backendTexture), |
| 425 | mTextureCleanupMgr); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 426 | cache.insert({buffer->getId(), imageTextureRef}); |
| 427 | } |
| 428 | } |
| 429 | |
Alec Mouri | 92f89fa | 2023-02-24 00:05:06 +0000 | [diff] [blame] | 430 | void SkiaRenderEngine::unmapExternalTextureBuffer(sp<GraphicBuffer>&& buffer) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 431 | ATRACE_CALL(); |
| 432 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 433 | if (const auto& iter = mGraphicBufferExternalRefs.find(buffer->getId()); |
| 434 | iter != mGraphicBufferExternalRefs.end()) { |
| 435 | if (iter->second == 0) { |
| 436 | ALOGW("Attempted to unmap GraphicBuffer <id: %" PRId64 |
| 437 | "> from RenderEngine texture, but the " |
| 438 | "ref count was already zero!", |
| 439 | buffer->getId()); |
| 440 | mGraphicBufferExternalRefs.erase(buffer->getId()); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | iter->second--; |
| 445 | |
| 446 | // Swap contexts if needed prior to deleting this buffer |
| 447 | // See Issue 1 of |
| 448 | // https://www.khronos.org/registry/EGL/extensions/EXT/EGL_EXT_protected_content.txt: even |
| 449 | // when a protected context and an unprotected context are part of the same share group, |
| 450 | // protected surfaces may not be accessed by an unprotected context, implying that protected |
| 451 | // surfaces may only be freed when a protected context is active. |
| 452 | const bool inProtected = mInProtectedContext; |
| 453 | useProtectedContext(buffer->getUsage() & GRALLOC_USAGE_PROTECTED); |
| 454 | |
| 455 | if (iter->second == 0) { |
| 456 | mTextureCache.erase(buffer->getId()); |
| 457 | mGraphicBufferExternalRefs.erase(buffer->getId()); |
| 458 | } |
| 459 | |
| 460 | // Swap back to the previous context so that cached values of isProtected in SurfaceFlinger |
| 461 | // are up-to-date. |
| 462 | if (inProtected != mInProtectedContext) { |
| 463 | useProtectedContext(inProtected); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 468 | std::shared_ptr<AutoBackendTexture::LocalRef> SkiaRenderEngine::getOrCreateBackendTexture( |
| 469 | const sp<GraphicBuffer>& buffer, bool isOutputBuffer) { |
Robert Phillips | 59f7173 | 2023-08-23 15:18:27 -0400 | [diff] [blame] | 470 | // Do not lookup the buffer in the cache for protected contexts |
| 471 | if (!isProtected()) { |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 472 | if (const auto& it = mTextureCache.find(buffer->getId()); it != mTextureCache.end()) { |
| 473 | return it->second; |
| 474 | } |
| 475 | } |
Nolan Scobie | ca05028 | 2024-03-15 13:27:06 -0400 | [diff] [blame] | 476 | std::unique_ptr<SkiaBackendTexture> backendTexture = |
| 477 | getActiveContext()->makeBackendTexture(buffer->toAHardwareBuffer(), isOutputBuffer); |
| 478 | return std::make_shared<AutoBackendTexture::LocalRef>(std::move(backendTexture), |
| 479 | mTextureCleanupMgr); |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 482 | bool SkiaRenderEngine::canSkipPostRenderCleanup() const { |
| 483 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 484 | return mTextureCleanupMgr.isEmpty(); |
| 485 | } |
| 486 | |
| 487 | void SkiaRenderEngine::cleanupPostRender() { |
| 488 | ATRACE_CALL(); |
| 489 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 490 | mTextureCleanupMgr.cleanup(); |
| 491 | } |
| 492 | |
| 493 | sk_sp<SkShader> SkiaRenderEngine::createRuntimeEffectShader( |
| 494 | const RuntimeEffectShaderParameters& parameters) { |
| 495 | // The given surface will be stretched by HWUI via matrix transformation |
| 496 | // which gets similar results for most surfaces |
| 497 | // Determine later on if we need to leverage the stertch shader within |
| 498 | // surface flinger |
| 499 | const auto& stretchEffect = parameters.layer.stretchEffect; |
| 500 | auto shader = parameters.shader; |
| 501 | if (stretchEffect.hasEffect()) { |
| 502 | const auto targetBuffer = parameters.layer.source.buffer.buffer; |
| 503 | const auto graphicBuffer = targetBuffer ? targetBuffer->getBuffer() : nullptr; |
| 504 | if (graphicBuffer && parameters.shader) { |
| 505 | shader = mStretchShaderFactory.createSkShader(shader, stretchEffect); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (parameters.requiresLinearEffect) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 510 | auto effect = |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 511 | shaders::LinearEffect{.inputDataspace = parameters.layer.sourceDataspace, |
| 512 | .outputDataspace = parameters.outputDataSpace, |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame] | 513 | .undoPremultipliedAlpha = parameters.undoPremultipliedAlpha, |
| 514 | .fakeOutputDataspace = parameters.fakeOutputDataspace}; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 515 | |
| 516 | auto effectIter = mRuntimeEffects.find(effect); |
| 517 | sk_sp<SkRuntimeEffect> runtimeEffect = nullptr; |
| 518 | if (effectIter == mRuntimeEffects.end()) { |
| 519 | runtimeEffect = buildRuntimeEffect(effect); |
| 520 | mRuntimeEffects.insert({effect, runtimeEffect}); |
| 521 | } else { |
| 522 | runtimeEffect = effectIter->second; |
| 523 | } |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 524 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 525 | mat4 colorTransform = parameters.layer.colorTransform; |
| 526 | |
| 527 | colorTransform *= |
| 528 | mat4::scale(vec4(parameters.layerDimmingRatio, parameters.layerDimmingRatio, |
| 529 | parameters.layerDimmingRatio, 1.f)); |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 530 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 531 | const auto targetBuffer = parameters.layer.source.buffer.buffer; |
| 532 | const auto graphicBuffer = targetBuffer ? targetBuffer->getBuffer() : nullptr; |
| 533 | const auto hardwareBuffer = graphicBuffer ? graphicBuffer->toAHardwareBuffer() : nullptr; |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 534 | return createLinearEffectShader(parameters.shader, effect, runtimeEffect, |
| 535 | std::move(colorTransform), parameters.display.maxLuminance, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 536 | parameters.display.currentLuminanceNits, |
| 537 | parameters.layer.source.buffer.maxLuminanceNits, |
| 538 | hardwareBuffer, parameters.display.renderIntent); |
| 539 | } |
| 540 | return parameters.shader; |
| 541 | } |
| 542 | |
| 543 | void SkiaRenderEngine::initCanvas(SkCanvas* canvas, const DisplaySettings& display) { |
| 544 | if (CC_UNLIKELY(mCapture->isCaptureRunning())) { |
| 545 | // Record display settings when capture is running. |
| 546 | std::stringstream displaySettings; |
| 547 | PrintTo(display, &displaySettings); |
| 548 | // Store the DisplaySettings in additional information. |
| 549 | canvas->drawAnnotation(SkRect::MakeEmpty(), "DisplaySettings", |
| 550 | SkData::MakeWithCString(displaySettings.str().c_str())); |
| 551 | } |
| 552 | |
| 553 | // Before doing any drawing, let's make sure that we'll start at the origin of the display. |
| 554 | // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual |
| 555 | // displays might have different scaling when compared to the physical screen. |
| 556 | |
| 557 | canvas->clipRect(getSkRect(display.physicalDisplay)); |
| 558 | canvas->translate(display.physicalDisplay.left, display.physicalDisplay.top); |
| 559 | |
| 560 | const auto clipWidth = display.clip.width(); |
| 561 | const auto clipHeight = display.clip.height(); |
| 562 | auto rotatedClipWidth = clipWidth; |
| 563 | auto rotatedClipHeight = clipHeight; |
| 564 | // Scale is contingent on the rotation result. |
| 565 | if (display.orientation & ui::Transform::ROT_90) { |
| 566 | std::swap(rotatedClipWidth, rotatedClipHeight); |
| 567 | } |
| 568 | const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) / |
| 569 | static_cast<SkScalar>(rotatedClipWidth); |
| 570 | const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) / |
| 571 | static_cast<SkScalar>(rotatedClipHeight); |
| 572 | canvas->scale(scaleX, scaleY); |
| 573 | |
| 574 | // Canvas rotation is done by centering the clip window at the origin, rotating, translating |
| 575 | // back so that the top left corner of the clip is at (0, 0). |
| 576 | canvas->translate(rotatedClipWidth / 2, rotatedClipHeight / 2); |
| 577 | canvas->rotate(toDegrees(display.orientation)); |
| 578 | canvas->translate(-clipWidth / 2, -clipHeight / 2); |
| 579 | canvas->translate(-display.clip.left, -display.clip.top); |
| 580 | } |
| 581 | |
| 582 | class AutoSaveRestore { |
| 583 | public: |
| 584 | AutoSaveRestore(SkCanvas* canvas) : mCanvas(canvas) { mSaveCount = canvas->save(); } |
| 585 | ~AutoSaveRestore() { restore(); } |
| 586 | void replace(SkCanvas* canvas) { |
| 587 | mCanvas = canvas; |
| 588 | mSaveCount = canvas->save(); |
| 589 | } |
| 590 | void restore() { |
| 591 | if (mCanvas) { |
| 592 | mCanvas->restoreToCount(mSaveCount); |
| 593 | mCanvas = nullptr; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | private: |
| 598 | SkCanvas* mCanvas; |
| 599 | int mSaveCount; |
| 600 | }; |
| 601 | |
| 602 | static SkRRect getBlurRRect(const BlurRegion& region) { |
| 603 | const auto rect = SkRect::MakeLTRB(region.left, region.top, region.right, region.bottom); |
| 604 | const SkVector radii[4] = {SkVector::Make(region.cornerRadiusTL, region.cornerRadiusTL), |
| 605 | SkVector::Make(region.cornerRadiusTR, region.cornerRadiusTR), |
| 606 | SkVector::Make(region.cornerRadiusBR, region.cornerRadiusBR), |
| 607 | SkVector::Make(region.cornerRadiusBL, region.cornerRadiusBL)}; |
| 608 | SkRRect roundedRect; |
| 609 | roundedRect.setRectRadii(rect, radii); |
| 610 | return roundedRect; |
| 611 | } |
| 612 | |
| 613 | // Arbitrary default margin which should be close enough to zero. |
| 614 | constexpr float kDefaultMargin = 0.0001f; |
| 615 | static bool equalsWithinMargin(float expected, float value, float margin = kDefaultMargin) { |
| 616 | LOG_ALWAYS_FATAL_IF(margin < 0.f, "Margin is negative!"); |
| 617 | return std::abs(expected - value) < margin; |
| 618 | } |
| 619 | |
| 620 | namespace { |
| 621 | template <typename T> |
| 622 | void logSettings(const T& t) { |
| 623 | std::stringstream stream; |
| 624 | PrintTo(t, &stream); |
| 625 | auto string = stream.str(); |
| 626 | size_t pos = 0; |
| 627 | // Perfetto ignores \n, so split up manually into separate ALOGD statements. |
| 628 | const size_t size = string.size(); |
| 629 | while (pos < size) { |
| 630 | const size_t end = std::min(string.find("\n", pos), size); |
| 631 | ALOGD("%s", string.substr(pos, end - pos).c_str()); |
| 632 | pos = end + 1; |
| 633 | } |
| 634 | } |
| 635 | } // namespace |
| 636 | |
| 637 | // Helper class intended to be used on the stack to ensure that texture cleanup |
| 638 | // is deferred until after this class goes out of scope. |
| 639 | class DeferTextureCleanup final { |
| 640 | public: |
| 641 | DeferTextureCleanup(AutoBackendTexture::CleanupManager& mgr) : mMgr(mgr) { |
| 642 | mMgr.setDeferredStatus(true); |
| 643 | } |
| 644 | ~DeferTextureCleanup() { mMgr.setDeferredStatus(false); } |
| 645 | |
| 646 | private: |
| 647 | DISALLOW_COPY_AND_ASSIGN(DeferTextureCleanup); |
| 648 | AutoBackendTexture::CleanupManager& mMgr; |
| 649 | }; |
| 650 | |
| 651 | void SkiaRenderEngine::drawLayersInternal( |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 652 | const std::shared_ptr<std::promise<FenceResult>>&& resultPromise, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 653 | const DisplaySettings& display, const std::vector<LayerSettings>& layers, |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame] | 654 | const std::shared_ptr<ExternalTexture>& buffer, base::unique_fd&& bufferFence) { |
Leon Scroggins III | 5a655b8 | 2022-09-07 13:17:09 -0400 | [diff] [blame] | 655 | ATRACE_FORMAT("%s for %s", __func__, display.namePlusId.c_str()); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 656 | |
| 657 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 658 | |
| 659 | if (buffer == nullptr) { |
| 660 | ALOGE("No output buffer provided. Aborting GPU composition."); |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 661 | resultPromise->set_value(base::unexpected(BAD_VALUE)); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | |
| 665 | validateOutputBufferUsage(buffer->getBuffer()); |
| 666 | |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 667 | auto context = getActiveContext(); |
Nolan Scobie | 17ffe90 | 2024-03-25 11:07:30 -0400 | [diff] [blame] | 668 | LOG_ALWAYS_FATAL_IF(context->isAbandonedOrDeviceLost(), |
| 669 | "Context is abandoned/device lost at start of %s", __func__); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 670 | |
| 671 | // any AutoBackendTexture deletions will now be deferred until cleanupPostRender is called |
| 672 | DeferTextureCleanup dtc(mTextureCleanupMgr); |
| 673 | |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 674 | auto surfaceTextureRef = getOrCreateBackendTexture(buffer->getBuffer(), true); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 675 | |
| 676 | // wait on the buffer to be ready to use prior to using it |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 677 | waitFence(context, bufferFence); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 678 | |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 679 | sk_sp<SkSurface> dstSurface = surfaceTextureRef->getOrCreateSurface(display.outputDataspace); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 680 | |
| 681 | SkCanvas* dstCanvas = mCapture->tryCapture(dstSurface.get()); |
| 682 | if (dstCanvas == nullptr) { |
| 683 | ALOGE("Cannot acquire canvas from Skia."); |
Patrick Williams | 2e9748f | 2022-08-09 22:48:18 +0000 | [diff] [blame] | 684 | resultPromise->set_value(base::unexpected(BAD_VALUE)); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 685 | return; |
| 686 | } |
| 687 | |
| 688 | // setup color filter if necessary |
| 689 | sk_sp<SkColorFilter> displayColorTransform; |
| 690 | if (display.colorTransform != mat4() && !display.deviceHandlesColorTransform) { |
| 691 | displayColorTransform = SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform)); |
| 692 | } |
| 693 | const bool ctModifiesAlpha = |
| 694 | displayColorTransform && !displayColorTransform->isAlphaUnchanged(); |
| 695 | |
| 696 | // Find the max layer white point to determine the max luminance of the scene... |
| 697 | const float maxLayerWhitePoint = std::transform_reduce( |
| 698 | layers.cbegin(), layers.cend(), 0.f, |
| 699 | [](float left, float right) { return std::max(left, right); }, |
| 700 | [&](const auto& l) { return l.whitePointNits; }); |
| 701 | |
| 702 | // ...and compute the dimming ratio if dimming is requested |
| 703 | const float displayDimmingRatio = display.targetLuminanceNits > 0.f && |
John Reck | 5b02fc4 | 2023-06-14 14:41:10 -0400 | [diff] [blame] | 704 | maxLayerWhitePoint > 0.f && |
| 705 | (kEnableLayerBrightening || display.targetLuminanceNits > maxLayerWhitePoint) |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 706 | ? maxLayerWhitePoint / display.targetLuminanceNits |
| 707 | : 1.f; |
| 708 | |
| 709 | // Find if any layers have requested blur, we'll use that info to decide when to render to an |
| 710 | // offscreen buffer and when to render to the native buffer. |
| 711 | sk_sp<SkSurface> activeSurface(dstSurface); |
| 712 | SkCanvas* canvas = dstCanvas; |
| 713 | SkiaCapture::OffscreenState offscreenCaptureState; |
| 714 | const LayerSettings* blurCompositionLayer = nullptr; |
Leon Scroggins III | f31b4bc | 2023-08-25 10:40:27 -0400 | [diff] [blame] | 715 | if (mBlurFilter) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 716 | bool requiresCompositionLayer = false; |
| 717 | for (const auto& layer : layers) { |
| 718 | // if the layer doesn't have blur or it is not visible then continue |
| 719 | if (!layerHasBlur(layer, ctModifiesAlpha)) { |
| 720 | continue; |
| 721 | } |
| 722 | if (layer.backgroundBlurRadius > 0 && |
| 723 | layer.backgroundBlurRadius < mBlurFilter->getMaxCrossFadeRadius()) { |
| 724 | requiresCompositionLayer = true; |
| 725 | } |
| 726 | for (auto region : layer.blurRegions) { |
| 727 | if (region.blurRadius < mBlurFilter->getMaxCrossFadeRadius()) { |
| 728 | requiresCompositionLayer = true; |
| 729 | } |
| 730 | } |
| 731 | if (requiresCompositionLayer) { |
| 732 | activeSurface = dstSurface->makeSurface(dstSurface->imageInfo()); |
| 733 | canvas = mCapture->tryOffscreenCapture(activeSurface.get(), &offscreenCaptureState); |
| 734 | blurCompositionLayer = &layer; |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | AutoSaveRestore surfaceAutoSaveRestore(canvas); |
| 741 | // Clear the entire canvas with a transparent black to prevent ghost images. |
| 742 | canvas->clear(SK_ColorTRANSPARENT); |
| 743 | initCanvas(canvas, display); |
| 744 | |
| 745 | if (kPrintLayerSettings) { |
| 746 | logSettings(display); |
| 747 | } |
| 748 | for (const auto& layer : layers) { |
| 749 | ATRACE_FORMAT("DrawLayer: %s", layer.name.c_str()); |
| 750 | |
| 751 | if (kPrintLayerSettings) { |
| 752 | logSettings(layer); |
| 753 | } |
| 754 | |
| 755 | sk_sp<SkImage> blurInput; |
| 756 | if (blurCompositionLayer == &layer) { |
| 757 | LOG_ALWAYS_FATAL_IF(activeSurface == dstSurface); |
| 758 | LOG_ALWAYS_FATAL_IF(canvas == dstCanvas); |
| 759 | |
| 760 | // save a snapshot of the activeSurface to use as input to the blur shaders |
| 761 | blurInput = activeSurface->makeImageSnapshot(); |
| 762 | |
Leon Scroggins III | 48a60cc | 2024-01-31 12:41:33 -0500 | [diff] [blame] | 763 | // blit the offscreen framebuffer into the destination AHB. This ensures that |
| 764 | // even if the blurred image does not cover the screen (for example, during |
| 765 | // a rotation animation, or if blur regions are used), the entire screen is |
| 766 | // initialized. |
| 767 | if (layer.blurRegions.size() || FlagManager::getInstance().restore_blur_step()) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 768 | SkPaint paint; |
| 769 | paint.setBlendMode(SkBlendMode::kSrc); |
| 770 | if (CC_UNLIKELY(mCapture->isCaptureRunning())) { |
| 771 | uint64_t id = mCapture->endOffscreenCapture(&offscreenCaptureState); |
| 772 | dstCanvas->drawAnnotation(SkRect::Make(dstCanvas->imageInfo().dimensions()), |
| 773 | String8::format("SurfaceID|%" PRId64, id).c_str(), |
| 774 | nullptr); |
| 775 | dstCanvas->drawImage(blurInput, 0, 0, SkSamplingOptions(), &paint); |
| 776 | } else { |
| 777 | activeSurface->draw(dstCanvas, 0, 0, SkSamplingOptions(), &paint); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // assign dstCanvas to canvas and ensure that the canvas state is up to date |
| 782 | canvas = dstCanvas; |
| 783 | surfaceAutoSaveRestore.replace(canvas); |
| 784 | initCanvas(canvas, display); |
| 785 | |
| 786 | LOG_ALWAYS_FATAL_IF(activeSurface->getCanvas()->getSaveCount() != |
| 787 | dstSurface->getCanvas()->getSaveCount()); |
| 788 | LOG_ALWAYS_FATAL_IF(activeSurface->getCanvas()->getTotalMatrix() != |
| 789 | dstSurface->getCanvas()->getTotalMatrix()); |
| 790 | |
| 791 | // assign dstSurface to activeSurface |
| 792 | activeSurface = dstSurface; |
| 793 | } |
| 794 | |
| 795 | SkAutoCanvasRestore layerAutoSaveRestore(canvas, true); |
| 796 | if (CC_UNLIKELY(mCapture->isCaptureRunning())) { |
| 797 | // Record the name of the layer if the capture is running. |
| 798 | std::stringstream layerSettings; |
| 799 | PrintTo(layer, &layerSettings); |
| 800 | // Store the LayerSettings in additional information. |
| 801 | canvas->drawAnnotation(SkRect::MakeEmpty(), layer.name.c_str(), |
| 802 | SkData::MakeWithCString(layerSettings.str().c_str())); |
| 803 | } |
| 804 | // Layers have a local transform that should be applied to them |
| 805 | canvas->concat(getSkM44(layer.geometry.positionTransform).asM33()); |
| 806 | |
| 807 | const auto [bounds, roundRectClip] = |
| 808 | getBoundsAndClip(layer.geometry.boundaries, layer.geometry.roundedCornersCrop, |
| 809 | layer.geometry.roundedCornersRadius); |
Leon Scroggins III | f31b4bc | 2023-08-25 10:40:27 -0400 | [diff] [blame] | 810 | if (mBlurFilter && layerHasBlur(layer, ctModifiesAlpha)) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 811 | std::unordered_map<uint32_t, sk_sp<SkImage>> cachedBlurs; |
| 812 | |
| 813 | // if multiple layers have blur, then we need to take a snapshot now because |
| 814 | // only the lowest layer will have blurImage populated earlier |
| 815 | if (!blurInput) { |
| 816 | blurInput = activeSurface->makeImageSnapshot(); |
| 817 | } |
Leon Scroggins III | a09cddc | 2023-07-25 09:34:11 -0400 | [diff] [blame] | 818 | |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 819 | // rect to be blurred in the coordinate space of blurInput |
Leon Scroggins III | a09cddc | 2023-07-25 09:34:11 -0400 | [diff] [blame] | 820 | SkRect blurRect = canvas->getTotalMatrix().mapRect(bounds.rect()); |
| 821 | |
| 822 | // Some layers may be much bigger than the screen. If we used |
| 823 | // `blurRect` directly, this would allocate a large buffer with no |
| 824 | // benefit. Apply the clip, which already takes the display size |
| 825 | // into account. The clipped size will then be used to calculate the |
| 826 | // size of the buffer we will create for blurring. |
| 827 | if (!blurRect.intersect(SkRect::Make(canvas->getDeviceClipBounds()))) { |
| 828 | // This should not happen, but if it did, we would use the full |
| 829 | // sized layer, which should still be fine. |
| 830 | ALOGW("blur bounds does not intersect display clip!"); |
| 831 | } |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 832 | |
| 833 | // if the clip needs to be applied then apply it now and make sure |
| 834 | // it is restored before we attempt to draw any shadows. |
| 835 | SkAutoCanvasRestore acr(canvas, true); |
| 836 | if (!roundRectClip.isEmpty()) { |
| 837 | canvas->clipRRect(roundRectClip, true); |
| 838 | } |
| 839 | |
| 840 | // TODO(b/182216890): Filter out empty layers earlier |
| 841 | if (blurRect.width() > 0 && blurRect.height() > 0) { |
| 842 | if (layer.backgroundBlurRadius > 0) { |
| 843 | ATRACE_NAME("BackgroundBlur"); |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 844 | auto blurredImage = mBlurFilter->generate(context, layer.backgroundBlurRadius, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 845 | blurInput, blurRect); |
| 846 | |
| 847 | cachedBlurs[layer.backgroundBlurRadius] = blurredImage; |
| 848 | |
| 849 | mBlurFilter->drawBlurRegion(canvas, bounds, layer.backgroundBlurRadius, 1.0f, |
| 850 | blurRect, blurredImage, blurInput); |
| 851 | } |
| 852 | |
| 853 | canvas->concat(getSkM44(layer.blurRegionTransform).asM33()); |
| 854 | for (auto region : layer.blurRegions) { |
| 855 | if (cachedBlurs[region.blurRadius] == nullptr) { |
| 856 | ATRACE_NAME("BlurRegion"); |
| 857 | cachedBlurs[region.blurRadius] = |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 858 | mBlurFilter->generate(context, region.blurRadius, blurInput, |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 859 | blurRect); |
| 860 | } |
| 861 | |
| 862 | mBlurFilter->drawBlurRegion(canvas, getBlurRRect(region), region.blurRadius, |
| 863 | region.alpha, blurRect, |
| 864 | cachedBlurs[region.blurRadius], blurInput); |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | if (layer.shadow.length > 0) { |
| 870 | // This would require a new parameter/flag to SkShadowUtils::DrawShadow |
| 871 | LOG_ALWAYS_FATAL_IF(layer.disableBlending, "Cannot disableBlending with a shadow"); |
| 872 | |
| 873 | SkRRect shadowBounds, shadowClip; |
| 874 | if (layer.geometry.boundaries == layer.shadow.boundaries) { |
| 875 | shadowBounds = bounds; |
| 876 | shadowClip = roundRectClip; |
| 877 | } else { |
| 878 | std::tie(shadowBounds, shadowClip) = |
| 879 | getBoundsAndClip(layer.shadow.boundaries, layer.geometry.roundedCornersCrop, |
| 880 | layer.geometry.roundedCornersRadius); |
| 881 | } |
| 882 | |
| 883 | // Technically, if bounds is a rect and roundRectClip is not empty, |
| 884 | // it means that the bounds and roundedCornersCrop were different |
| 885 | // enough that we should intersect them to find the proper shadow. |
| 886 | // In practice, this often happens when the two rectangles appear to |
| 887 | // not match due to rounding errors. Draw the rounded version, which |
| 888 | // looks more like the intent. |
| 889 | const auto& rrect = |
| 890 | shadowBounds.isRect() && !shadowClip.isEmpty() ? shadowClip : shadowBounds; |
| 891 | drawShadow(canvas, rrect, layer.shadow); |
| 892 | } |
| 893 | |
| 894 | const float layerDimmingRatio = layer.whitePointNits <= 0.f |
| 895 | ? displayDimmingRatio |
| 896 | : (layer.whitePointNits / maxLayerWhitePoint) * displayDimmingRatio; |
| 897 | |
| 898 | const bool dimInLinearSpace = display.dimmingStage != |
| 899 | aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF; |
| 900 | |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 901 | const bool isExtendedHdr = (layer.sourceDataspace & ui::Dataspace::RANGE_MASK) == |
| 902 | static_cast<int32_t>(ui::Dataspace::RANGE_EXTENDED) && |
| 903 | (display.outputDataspace & ui::Dataspace::TRANSFER_MASK) == |
| 904 | static_cast<int32_t>(ui::Dataspace::TRANSFER_SRGB); |
| 905 | |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame] | 906 | const bool useFakeOutputDataspaceForRuntimeEffect = !dimInLinearSpace && isExtendedHdr; |
| 907 | |
| 908 | const ui::Dataspace fakeDataspace = useFakeOutputDataspaceForRuntimeEffect |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 909 | ? static_cast<ui::Dataspace>( |
| 910 | (display.outputDataspace & ui::Dataspace::STANDARD_MASK) | |
| 911 | ui::Dataspace::TRANSFER_GAMMA2_2 | |
| 912 | (display.outputDataspace & ui::Dataspace::RANGE_MASK)) |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame] | 913 | : ui::Dataspace::UNKNOWN; |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 914 | |
| 915 | // If the input dataspace is range extended, the output dataspace transfer is sRGB |
| 916 | // and dimmingStage is GAMMA_OETF, dim in linear space instead, and |
| 917 | // set the output dataspace's transfer to be GAMMA2_2. |
| 918 | // This allows DPU side to use oetf_gamma_2p2 for extended HDR layer |
| 919 | // to avoid tone shift. |
| 920 | // The reason of tone shift here is because HDR layers manage white point |
| 921 | // luminance in linear space, which color pipelines request GAMMA_OETF break |
| 922 | // without a gamma 2.2 fixup. |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 923 | const bool requiresLinearEffect = layer.colorTransform != mat4() || |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 924 | (needsToneMapping(layer.sourceDataspace, display.outputDataspace)) || |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 925 | (dimInLinearSpace && !equalsWithinMargin(1.f, layerDimmingRatio)) || |
| 926 | (!dimInLinearSpace && isExtendedHdr); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 927 | |
| 928 | // quick abort from drawing the remaining portion of the layer |
| 929 | if (layer.skipContentDraw || |
| 930 | (layer.alpha == 0 && !requiresLinearEffect && !layer.disableBlending && |
| 931 | (!displayColorTransform || displayColorTransform->isAlphaUnchanged()))) { |
| 932 | continue; |
| 933 | } |
| 934 | |
Alec Mouri | 47bcb07 | 2023-08-15 02:02:49 +0000 | [diff] [blame] | 935 | const ui::Dataspace layerDataspace = layer.sourceDataspace; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 936 | |
| 937 | SkPaint paint; |
| 938 | if (layer.source.buffer.buffer) { |
| 939 | ATRACE_NAME("DrawImage"); |
| 940 | validateInputBufferUsage(layer.source.buffer.buffer->getBuffer()); |
| 941 | const auto& item = layer.source.buffer; |
Ian Elliott | 8506e36 | 2023-03-08 12:12:09 -0700 | [diff] [blame] | 942 | auto imageTextureRef = getOrCreateBackendTexture(item.buffer->getBuffer(), false); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 943 | |
| 944 | // if the layer's buffer has a fence, then we must must respect the fence prior to using |
| 945 | // the buffer. |
| 946 | if (layer.source.buffer.fence != nullptr) { |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 947 | waitFence(context, layer.source.buffer.fence->get()); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // isOpaque means we need to ignore the alpha in the image, |
| 951 | // replacing it with the alpha specified by the LayerSettings. See |
| 952 | // https://developer.android.com/reference/android/view/SurfaceControl.Builder#setOpaque(boolean) |
| 953 | // The proper way to do this is to use an SkColorType that ignores |
| 954 | // alpha, like kRGB_888x_SkColorType, and that is used if the |
| 955 | // incoming image is kRGBA_8888_SkColorType. However, the incoming |
| 956 | // image may be kRGBA_F16_SkColorType, for which there is no RGBX |
| 957 | // SkColorType, or kRGBA_1010102_SkColorType, for which we have |
| 958 | // kRGB_101010x_SkColorType, but it is not yet supported as a source |
| 959 | // on the GPU. (Adding both is tracked in skbug.com/12048.) In the |
| 960 | // meantime, we'll use a workaround that works unless we need to do |
| 961 | // any color conversion. The workaround requires that we pretend the |
| 962 | // image is already premultiplied, so that we do not premultiply it |
| 963 | // before applying SkBlendMode::kPlus. |
| 964 | const bool useIsOpaqueWorkaround = item.isOpaque && |
| 965 | (imageTextureRef->colorType() == kRGBA_1010102_SkColorType || |
| 966 | imageTextureRef->colorType() == kRGBA_F16_SkColorType); |
| 967 | const auto alphaType = useIsOpaqueWorkaround ? kPremul_SkAlphaType |
| 968 | : item.isOpaque ? kOpaque_SkAlphaType |
| 969 | : item.usePremultipliedAlpha ? kPremul_SkAlphaType |
| 970 | : kUnpremul_SkAlphaType; |
Nolan Scobie | 17e2551 | 2024-03-13 18:02:48 -0400 | [diff] [blame] | 971 | sk_sp<SkImage> image = imageTextureRef->makeImage(layerDataspace, alphaType); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 972 | |
| 973 | auto texMatrix = getSkM44(item.textureTransform).asM33(); |
| 974 | // textureTansform was intended to be passed directly into a shader, so when |
| 975 | // building the total matrix with the textureTransform we need to first |
| 976 | // normalize it, then apply the textureTransform, then scale back up. |
| 977 | texMatrix.preScale(1.0f / bounds.width(), 1.0f / bounds.height()); |
| 978 | texMatrix.postScale(image->width(), image->height()); |
| 979 | |
| 980 | SkMatrix matrix; |
| 981 | if (!texMatrix.invert(&matrix)) { |
| 982 | matrix = texMatrix; |
| 983 | } |
| 984 | // The shader does not respect the translation, so we add it to the texture |
| 985 | // transform for the SkImage. This will make sure that the correct layer contents |
| 986 | // are drawn in the correct part of the screen. |
| 987 | matrix.postTranslate(bounds.rect().fLeft, bounds.rect().fTop); |
| 988 | |
| 989 | sk_sp<SkShader> shader; |
| 990 | |
| 991 | if (layer.source.buffer.useTextureFiltering) { |
| 992 | shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, |
| 993 | SkSamplingOptions( |
| 994 | {SkFilterMode::kLinear, SkMipmapMode::kNone}), |
| 995 | &matrix); |
| 996 | } else { |
| 997 | shader = image->makeShader(SkSamplingOptions(), matrix); |
| 998 | } |
| 999 | |
| 1000 | if (useIsOpaqueWorkaround) { |
| 1001 | shader = SkShaders::Blend(SkBlendMode::kPlus, shader, |
| 1002 | SkShaders::Color(SkColors::kBlack, |
| 1003 | toSkColorSpace(layerDataspace))); |
| 1004 | } |
| 1005 | |
| 1006 | paint.setShader(createRuntimeEffectShader( |
| 1007 | RuntimeEffectShaderParameters{.shader = shader, |
| 1008 | .layer = layer, |
| 1009 | .display = display, |
| 1010 | .undoPremultipliedAlpha = !item.isOpaque && |
| 1011 | item.usePremultipliedAlpha, |
| 1012 | .requiresLinearEffect = requiresLinearEffect, |
| 1013 | .layerDimmingRatio = dimInLinearSpace |
| 1014 | ? layerDimmingRatio |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 1015 | : 1.f, |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame] | 1016 | .outputDataSpace = display.outputDataspace, |
| 1017 | .fakeOutputDataspace = fakeDataspace})); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1018 | |
| 1019 | // Turn on dithering when dimming beyond this (arbitrary) threshold... |
linkai | 296566e | 2024-01-22 20:58:15 +0800 | [diff] [blame] | 1020 | static constexpr float kDimmingThreshold = 0.9f; |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1021 | // ...or we're rendering an HDR layer down to an 8-bit target |
| 1022 | // Most HDR standards require at least 10-bits of color depth for source content, so we |
| 1023 | // can just extract the transfer function rather than dig into precise gralloc layout. |
| 1024 | // Furthermore, we can assume that the only 8-bit target we support is RGBA8888. |
Sally Qi | f6918d4 | 2023-08-07 15:28:30 -0700 | [diff] [blame] | 1025 | const bool requiresDownsample = |
| 1026 | getHdrRenderType(layer.sourceDataspace, |
| 1027 | std::optional<ui::PixelFormat>(static_cast<ui::PixelFormat>( |
| 1028 | buffer->getPixelFormat()))) != HdrRenderType::SDR && |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1029 | buffer->getPixelFormat() == PIXEL_FORMAT_RGBA_8888; |
| 1030 | if (layerDimmingRatio <= kDimmingThreshold || requiresDownsample) { |
| 1031 | paint.setDither(true); |
| 1032 | } |
| 1033 | paint.setAlphaf(layer.alpha); |
| 1034 | |
| 1035 | if (imageTextureRef->colorType() == kAlpha_8_SkColorType) { |
| 1036 | LOG_ALWAYS_FATAL_IF(layer.disableBlending, "Cannot disableBlending with A8"); |
| 1037 | |
| 1038 | // SysUI creates the alpha layer as a coverage layer, which is |
| 1039 | // appropriate for the DPU. Use a color matrix to convert it to |
| 1040 | // a mask. |
| 1041 | // TODO (b/219525258): Handle input as a mask. |
| 1042 | // |
| 1043 | // The color matrix will convert A8 pixels with no alpha to |
| 1044 | // black, as described by this vector. If the display handles |
| 1045 | // the color transform, we need to invert it to find the color |
| 1046 | // that will result in black after the DPU applies the transform. |
| 1047 | SkV4 black{0.0f, 0.0f, 0.0f, 1.0f}; // r, g, b, a |
| 1048 | if (display.colorTransform != mat4() && display.deviceHandlesColorTransform) { |
| 1049 | SkM44 colorSpaceMatrix = getSkM44(display.colorTransform); |
| 1050 | if (colorSpaceMatrix.invert(&colorSpaceMatrix)) { |
| 1051 | black = colorSpaceMatrix * black; |
| 1052 | } else { |
| 1053 | // We'll just have to use 0,0,0 as black, which should |
| 1054 | // be close to correct. |
| 1055 | ALOGI("Could not invert colorTransform!"); |
| 1056 | } |
| 1057 | } |
| 1058 | SkColorMatrix colorMatrix(0, 0, 0, 0, black[0], |
| 1059 | 0, 0, 0, 0, black[1], |
| 1060 | 0, 0, 0, 0, black[2], |
| 1061 | 0, 0, 0, -1, 1); |
| 1062 | if (display.colorTransform != mat4() && !display.deviceHandlesColorTransform) { |
| 1063 | // On the other hand, if the device doesn't handle it, we |
| 1064 | // have to apply it ourselves. |
| 1065 | colorMatrix.postConcat(toSkColorMatrix(display.colorTransform)); |
| 1066 | } |
| 1067 | paint.setColorFilter(SkColorFilters::Matrix(colorMatrix)); |
| 1068 | } |
| 1069 | } else { |
| 1070 | ATRACE_NAME("DrawColor"); |
| 1071 | const auto color = layer.source.solidColor; |
| 1072 | sk_sp<SkShader> shader = SkShaders::Color(SkColor4f{.fR = color.r, |
| 1073 | .fG = color.g, |
| 1074 | .fB = color.b, |
| 1075 | .fA = layer.alpha}, |
| 1076 | toSkColorSpace(layerDataspace)); |
| 1077 | paint.setShader(createRuntimeEffectShader( |
| 1078 | RuntimeEffectShaderParameters{.shader = shader, |
| 1079 | .layer = layer, |
| 1080 | .display = display, |
| 1081 | .undoPremultipliedAlpha = false, |
| 1082 | .requiresLinearEffect = requiresLinearEffect, |
Sally Qi | 628ef6e | 2023-03-30 14:49:03 -0700 | [diff] [blame] | 1083 | .layerDimmingRatio = layerDimmingRatio, |
Alec Mouri | e0bb6f4 | 2023-08-02 22:41:52 +0000 | [diff] [blame] | 1084 | .outputDataSpace = display.outputDataspace, |
| 1085 | .fakeOutputDataspace = fakeDataspace})); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | if (layer.disableBlending) { |
| 1089 | paint.setBlendMode(SkBlendMode::kSrc); |
| 1090 | } |
| 1091 | |
| 1092 | // An A8 buffer will already have the proper color filter attached to |
| 1093 | // its paint, including the displayColorTransform as needed. |
| 1094 | if (!paint.getColorFilter()) { |
| 1095 | if (!dimInLinearSpace && !equalsWithinMargin(1.0, layerDimmingRatio)) { |
| 1096 | // If we don't dim in linear space, then when we gamma correct the dimming ratio we |
| 1097 | // can assume a gamma 2.2 transfer function. |
| 1098 | static constexpr float kInverseGamma22 = 1.f / 2.2f; |
| 1099 | const auto gammaCorrectedDimmingRatio = |
| 1100 | std::pow(layerDimmingRatio, kInverseGamma22); |
| 1101 | auto dimmingMatrix = |
| 1102 | mat4::scale(vec4(gammaCorrectedDimmingRatio, gammaCorrectedDimmingRatio, |
| 1103 | gammaCorrectedDimmingRatio, 1.f)); |
| 1104 | |
| 1105 | const auto colorFilter = |
| 1106 | SkColorFilters::Matrix(toSkColorMatrix(std::move(dimmingMatrix))); |
| 1107 | paint.setColorFilter(displayColorTransform |
| 1108 | ? displayColorTransform->makeComposed(colorFilter) |
| 1109 | : colorFilter); |
| 1110 | } else { |
| 1111 | paint.setColorFilter(displayColorTransform); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (!roundRectClip.isEmpty()) { |
| 1116 | canvas->clipRRect(roundRectClip, true); |
| 1117 | } |
| 1118 | |
| 1119 | if (!bounds.isRect()) { |
| 1120 | paint.setAntiAlias(true); |
| 1121 | canvas->drawRRect(bounds, paint); |
| 1122 | } else { |
| 1123 | canvas->drawRect(bounds.rect(), paint); |
| 1124 | } |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 1125 | if (kGaneshFlushAfterEveryLayer) { |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1126 | ATRACE_NAME("flush surface"); |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 1127 | // No-op in Graphite. If "flushing" Skia's drawing commands after each layer is desired |
| 1128 | // in Graphite, then a graphite::Recording would need to be snapped and tracked for each |
| 1129 | // layer, which is likely possible but adds non-trivial complexity (in both bookkeeping |
| 1130 | // and refactoring). |
Kevin Lubick | 208e11a | 2023-05-31 19:34:46 +0000 | [diff] [blame] | 1131 | skgpu::ganesh::Flush(activeSurface); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1134 | |
| 1135 | surfaceAutoSaveRestore.restore(); |
| 1136 | mCapture->endCapture(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1137 | |
Nolan Scobie | 1e06f2d | 2024-03-21 14:56:38 -0400 | [diff] [blame] | 1138 | LOG_ALWAYS_FATAL_IF(activeSurface != dstSurface); |
| 1139 | auto drawFence = sp<Fence>::make(flushAndSubmit(context, dstSurface)); |
Alec Mouri | 0e7d8fd | 2023-05-03 23:58:43 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (ATRACE_ENABLED()) { |
| 1142 | static gui::FenceMonitor sMonitor("RE Completion"); |
| 1143 | sMonitor.queueFence(drawFence); |
| 1144 | } |
| 1145 | resultPromise->set_value(std::move(drawFence)); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | size_t SkiaRenderEngine::getMaxTextureSize() const { |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 1149 | return mContext->getMaxTextureSize(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | size_t SkiaRenderEngine::getMaxViewportDims() const { |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 1153 | return mContext->getMaxRenderTargetSize(); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | void SkiaRenderEngine::drawShadow(SkCanvas* canvas, |
| 1157 | const SkRRect& casterRRect, |
| 1158 | const ShadowSettings& settings) { |
| 1159 | ATRACE_CALL(); |
| 1160 | const float casterZ = settings.length / 2.0f; |
| 1161 | const auto flags = |
| 1162 | settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag; |
| 1163 | |
| 1164 | SkShadowUtils::DrawShadow(canvas, SkPath::RRect(casterRRect), SkPoint3::Make(0, 0, casterZ), |
| 1165 | getSkPoint3(settings.lightPos), settings.lightRadius, |
| 1166 | getSkColor(settings.ambientColor), getSkColor(settings.spotColor), |
| 1167 | flags); |
| 1168 | } |
| 1169 | |
| 1170 | void SkiaRenderEngine::onActiveDisplaySizeChanged(ui::Size size) { |
| 1171 | // This cache multiplier was selected based on review of cache sizes relative |
| 1172 | // to the screen resolution. Looking at the worst case memory needed by blur (~1.5x), |
| 1173 | // shadows (~1x), and general data structures (e.g. vertex buffers) we selected this as a |
| 1174 | // conservative default based on that analysis. |
| 1175 | const float SURFACE_SIZE_MULTIPLIER = 3.5f * bytesPerPixel(mDefaultPixelFormat); |
| 1176 | const int maxResourceBytes = size.width * size.height * SURFACE_SIZE_MULTIPLIER; |
| 1177 | |
| 1178 | // start by resizing the current context |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 1179 | getActiveContext()->setResourceCacheLimit(maxResourceBytes); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1180 | |
| 1181 | // if it is possible to switch contexts then we will resize the other context |
| 1182 | const bool originalProtectedState = mInProtectedContext; |
| 1183 | useProtectedContext(!mInProtectedContext); |
| 1184 | if (mInProtectedContext != originalProtectedState) { |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 1185 | getActiveContext()->setResourceCacheLimit(maxResourceBytes); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1186 | // reset back to the initial context that was active when this method was called |
| 1187 | useProtectedContext(originalProtectedState); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | void SkiaRenderEngine::dump(std::string& result) { |
| 1192 | // Dump for the specific backend (GLES or Vk) |
| 1193 | appendBackendSpecificInfoToDump(result); |
| 1194 | |
| 1195 | // Info about protected content |
| 1196 | StringAppendF(&result, "RenderEngine supports protected context: %d\n", |
| 1197 | supportsProtectedContent()); |
| 1198 | StringAppendF(&result, "RenderEngine is in protected context: %d\n", mInProtectedContext); |
| 1199 | StringAppendF(&result, "RenderEngine shaders cached since last dump/primeCache: %d\n", |
| 1200 | mSkSLCacheMonitor.shadersCachedSinceLastCall()); |
| 1201 | |
| 1202 | std::vector<ResourcePair> cpuResourceMap = { |
| 1203 | {"skia/sk_resource_cache/bitmap_", "Bitmaps"}, |
| 1204 | {"skia/sk_resource_cache/rrect-blur_", "Masks"}, |
| 1205 | {"skia/sk_resource_cache/rects-blur_", "Masks"}, |
| 1206 | {"skia/sk_resource_cache/tessellated", "Shadows"}, |
| 1207 | {"skia", "Other"}, |
| 1208 | }; |
| 1209 | SkiaMemoryReporter cpuReporter(cpuResourceMap, false); |
| 1210 | SkGraphics::DumpMemoryStatistics(&cpuReporter); |
| 1211 | StringAppendF(&result, "Skia CPU Caches: "); |
| 1212 | cpuReporter.logTotals(result); |
| 1213 | cpuReporter.logOutput(result); |
| 1214 | |
| 1215 | { |
| 1216 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 1217 | |
| 1218 | std::vector<ResourcePair> gpuResourceMap = { |
| 1219 | {"texture_renderbuffer", "Texture/RenderBuffer"}, |
| 1220 | {"texture", "Texture"}, |
| 1221 | {"gr_text_blob_cache", "Text"}, |
| 1222 | {"skia", "Other"}, |
| 1223 | }; |
| 1224 | SkiaMemoryReporter gpuReporter(gpuResourceMap, true); |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 1225 | mContext->dumpMemoryStatistics(&gpuReporter); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1226 | StringAppendF(&result, "Skia's GPU Caches: "); |
| 1227 | gpuReporter.logTotals(result); |
| 1228 | gpuReporter.logOutput(result); |
| 1229 | StringAppendF(&result, "Skia's Wrapped Objects:\n"); |
| 1230 | gpuReporter.logOutput(result, true); |
| 1231 | |
| 1232 | StringAppendF(&result, "RenderEngine tracked buffers: %zu\n", |
| 1233 | mGraphicBufferExternalRefs.size()); |
| 1234 | StringAppendF(&result, "Dumping buffer ids...\n"); |
| 1235 | for (const auto& [id, refCounts] : mGraphicBufferExternalRefs) { |
| 1236 | StringAppendF(&result, "- 0x%" PRIx64 " - %d refs \n", id, refCounts); |
| 1237 | } |
| 1238 | StringAppendF(&result, "RenderEngine AHB/BackendTexture cache size: %zu\n", |
| 1239 | mTextureCache.size()); |
| 1240 | StringAppendF(&result, "Dumping buffer ids...\n"); |
| 1241 | // TODO(178539829): It would be nice to know which layer these are coming from and what |
| 1242 | // the texture sizes are. |
| 1243 | for (const auto& [id, unused] : mTextureCache) { |
| 1244 | StringAppendF(&result, "- 0x%" PRIx64 "\n", id); |
| 1245 | } |
| 1246 | StringAppendF(&result, "\n"); |
| 1247 | |
| 1248 | SkiaMemoryReporter gpuProtectedReporter(gpuResourceMap, true); |
Nolan Scobie | fc125ec | 2024-03-11 20:08:27 -0400 | [diff] [blame] | 1249 | if (mProtectedContext) { |
| 1250 | mProtectedContext->dumpMemoryStatistics(&gpuProtectedReporter); |
Lingfeng Yang | 00c1ff6 | 2022-06-02 09:19:28 -0700 | [diff] [blame] | 1251 | } |
| 1252 | StringAppendF(&result, "Skia's GPU Protected Caches: "); |
| 1253 | gpuProtectedReporter.logTotals(result); |
| 1254 | gpuProtectedReporter.logOutput(result); |
| 1255 | StringAppendF(&result, "Skia's Protected Wrapped Objects:\n"); |
| 1256 | gpuProtectedReporter.logOutput(result, true); |
| 1257 | |
| 1258 | StringAppendF(&result, "\n"); |
| 1259 | StringAppendF(&result, "RenderEngine runtime effects: %zu\n", mRuntimeEffects.size()); |
| 1260 | for (const auto& [linearEffect, unused] : mRuntimeEffects) { |
| 1261 | StringAppendF(&result, "- inputDataspace: %s\n", |
| 1262 | dataspaceDetails( |
| 1263 | static_cast<android_dataspace>(linearEffect.inputDataspace)) |
| 1264 | .c_str()); |
| 1265 | StringAppendF(&result, "- outputDataspace: %s\n", |
| 1266 | dataspaceDetails( |
| 1267 | static_cast<android_dataspace>(linearEffect.outputDataspace)) |
| 1268 | .c_str()); |
| 1269 | StringAppendF(&result, "undoPremultipliedAlpha: %s\n", |
| 1270 | linearEffect.undoPremultipliedAlpha ? "true" : "false"); |
| 1271 | } |
| 1272 | } |
| 1273 | StringAppendF(&result, "\n"); |
| 1274 | } |
| 1275 | |
rnlee | c6a7364 | 2021-06-04 14:16:42 -0700 | [diff] [blame] | 1276 | } // namespace skia |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1277 | } // namespace renderengine |
rnlee | c6a7364 | 2021-06-04 14:16:42 -0700 | [diff] [blame] | 1278 | } // namespace android |