Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 17 | #include "pipeline/skia/SkiaPipeline.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 18 | |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 19 | #include <SkCanvas.h> |
| 20 | #include <SkColor.h> |
| 21 | #include <SkColorSpace.h> |
| 22 | #include <SkData.h> |
| 23 | #include <SkImage.h> |
Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 24 | #include <SkImageAndroid.h> |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 25 | #include <SkImageInfo.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 26 | #include <SkMatrix.h> |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 27 | #include <SkOverdrawCanvas.h> |
| 28 | #include <SkOverdrawColorFilter.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 29 | #include <SkPicture.h> |
| 30 | #include <SkPictureRecorder.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 31 | #include <SkRect.h> |
| 32 | #include <SkRefCnt.h> |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 33 | #include <SkSerialProcs.h> |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 34 | #include <SkStream.h> |
| 35 | #include <SkString.h> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 36 | #include <SkTypeface.h> |
| 37 | #include <android-base/properties.h> |
John Reck | 29b1ee0 | 2023-04-04 17:44:23 -0400 | [diff] [blame] | 38 | #include <gui/TraceUtils.h> |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 39 | #include <include/android/SkSurfaceAndroid.h> |
Kevin Lubick | a63d809 | 2024-04-12 17:43:17 +0000 | [diff] [blame] | 40 | #include <include/docs/SkMultiPictureDocument.h> |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 41 | #include <include/encode/SkPngEncoder.h> |
| 42 | #include <include/gpu/ganesh/SkSurfaceGanesh.h> |
Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 43 | #include <unistd.h> |
| 44 | |
| 45 | #include <sstream> |
| 46 | |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 47 | #include "LightingInfo.h" |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 48 | #include "VectorDrawable.h" |
John Reck | 29b1ee0 | 2023-04-04 17:44:23 -0400 | [diff] [blame] | 49 | #include "include/gpu/GpuTypes.h" // from Skia |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 50 | #include "thread/CommonPool.h" |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 51 | #include "tools/SkSharingProc.h" |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 52 | #include "utils/Color.h" |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 53 | #include "utils/String8.h" |
Jerome Gaillard | a02a12d | 2019-05-28 18:07:56 +0100 | [diff] [blame] | 54 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 55 | using namespace android::uirenderer::renderthread; |
| 56 | |
| 57 | namespace android { |
| 58 | namespace uirenderer { |
| 59 | namespace skiapipeline { |
| 60 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 61 | SkiaPipeline::SkiaPipeline(RenderThread& thread) : mRenderThread(thread) { |
Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 62 | setSurfaceColorProperties(mColorMode); |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 63 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 64 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 65 | SkiaPipeline::~SkiaPipeline() {} |
Stan Iliev | 232f362 | 2017-08-23 17:15:09 -0400 | [diff] [blame] | 66 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 67 | void SkiaPipeline::onDestroyHardwareResources() { |
Derek Sollenberger | 92a9eb9 | 2018-04-12 13:42:19 -0400 | [diff] [blame] | 68 | unpinImages(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 69 | mRenderThread.cacheManager().trimStaleResources(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 70 | } |
| 71 | |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 72 | void SkiaPipeline::renderLayers(const LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | LayerUpdateQueue* layerUpdateQueue, bool opaque, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 74 | const LightInfo& lightInfo) { |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 75 | LightingInfo::updateLighting(lightGeometry, lightInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 76 | ATRACE_NAME("draw layers"); |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 77 | renderLayersImpl(*layerUpdateQueue, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 78 | layerUpdateQueue->clear(); |
| 79 | } |
| 80 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 81 | bool SkiaPipeline::renderLayerImpl(RenderNode* layerNode, const Rect& layerDamage) { |
| 82 | SkASSERT(layerNode->getLayerSurface()); |
| 83 | SkiaDisplayList* displayList = layerNode->getDisplayList().asSkiaDl(); |
| 84 | if (!displayList || displayList->isEmpty()) { |
| 85 | ALOGE("%p drawLayers(%s) : missing drawable", layerNode, layerNode->getName()); |
| 86 | return false; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 87 | } |
Derek Sollenberger | f7df184 | 2017-09-05 11:15:58 -0400 | [diff] [blame] | 88 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 89 | SkCanvas* layerCanvas = layerNode->getLayerSurface()->getCanvas(); |
Jerome Gaillard | 09a38e4 | 2024-03-14 15:20:56 +0000 | [diff] [blame] | 90 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 91 | int saveCount = layerCanvas->save(); |
| 92 | SkASSERT(saveCount == 1); |
Jerome Gaillard | 09a38e4 | 2024-03-14 15:20:56 +0000 | [diff] [blame] | 93 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 94 | layerCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
Jerome Gaillard | 09a38e4 | 2024-03-14 15:20:56 +0000 | [diff] [blame] | 95 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 96 | // TODO: put localized light center calculation and storage to a drawable related code. |
| 97 | // It does not seem right to store something localized in a global state |
| 98 | // fix here and in recordLayers |
| 99 | const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw()); |
| 100 | Vector3 transformedLightCenter(savedLightCenter); |
| 101 | // map current light center into RenderNode's coordinate space |
| 102 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter); |
| 103 | LightingInfo::setLightCenterRaw(transformedLightCenter); |
| 104 | |
| 105 | const RenderProperties& properties = layerNode->properties(); |
| 106 | const SkRect bounds = SkRect::MakeWH(properties.getWidth(), properties.getHeight()); |
| 107 | if (properties.getClipToBounds() && layerCanvas->quickReject(bounds)) { |
| 108 | return false; |
John Reck | ce96630 | 2024-03-25 19:28:29 +0000 | [diff] [blame] | 109 | } |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 110 | |
| 111 | ATRACE_FORMAT("drawLayer [%s] %.1f x %.1f", layerNode->getName(), bounds.width(), |
| 112 | bounds.height()); |
| 113 | |
| 114 | layerNode->getSkiaLayer()->hasRenderedSinceRepaint = false; |
| 115 | layerCanvas->clear(SK_ColorTRANSPARENT); |
| 116 | |
| 117 | RenderNodeDrawable root(layerNode, layerCanvas, false); |
| 118 | root.forceDraw(layerCanvas); |
| 119 | layerCanvas->restoreToCount(saveCount); |
| 120 | |
| 121 | LightingInfo::setLightCenterRaw(savedLightCenter); |
| 122 | return true; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 123 | } |
| 124 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 125 | static void savePictureAsync(const sk_sp<SkData>& data, const std::string& filename) { |
| 126 | CommonPool::post([data, filename] { |
| 127 | if (0 == access(filename.c_str(), F_OK)) { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 131 | SkFILEWStream stream(filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 132 | if (stream.isValid()) { |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 133 | stream.write(data->data(), data->size()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 134 | stream.flush(); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 135 | ALOGD("SKP Captured Drawing Output (%zu bytes) for frame. %s", stream.bytesWritten(), |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 136 | filename.c_str()); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 137 | } |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 138 | }); |
| 139 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 140 | |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 141 | // Note multiple SkiaPipeline instances may be loaded if more than one app is visible. |
| 142 | // Each instance may observe the filename changing and try to record to a file of the same name. |
| 143 | // Only the first one will succeed. There is no scope available here where we could coordinate |
| 144 | // to cause this function to return true for only one of the instances. |
| 145 | bool SkiaPipeline::shouldStartNewFileCapture() { |
| 146 | // Don't start a new file based capture if one is currently ongoing. |
| 147 | if (mCaptureMode != CaptureMode::None) { return false; } |
| 148 | |
| 149 | // A new capture is started when the filename property changes. |
| 150 | // Read the filename property. |
| 151 | std::string prop = base::GetProperty(PROPERTY_CAPTURE_SKP_FILENAME, "0"); |
| 152 | // if the filename property changed to a valid value |
| 153 | if (prop[0] != '0' && mCapturedFile != prop) { |
| 154 | // remember this new filename |
| 155 | mCapturedFile = prop; |
| 156 | // and get a property indicating how many frames to capture. |
| 157 | mCaptureSequence = base::GetIntProperty(PROPERTY_CAPTURE_SKP_FRAMES, 1); |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 158 | if (mCaptureSequence <= 0) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 159 | return false; |
| 160 | } else if (mCaptureSequence == 1) { |
| 161 | mCaptureMode = CaptureMode::SingleFrameSKP; |
| 162 | } else { |
| 163 | mCaptureMode = CaptureMode::MultiFrameSKP; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 164 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | // performs the first-frame work of a multi frame SKP capture. Returns true if successful. |
| 171 | bool SkiaPipeline::setupMultiFrameCapture() { |
| 172 | ALOGD("Set up multi-frame capture, frames = %d", mCaptureSequence); |
| 173 | // We own this stream and need to hold it until close() finishes. |
| 174 | auto stream = std::make_unique<SkFILEWStream>(mCapturedFile.c_str()); |
| 175 | if (stream->isValid()) { |
| 176 | mOpenMultiPicStream = std::move(stream); |
| 177 | mSerialContext.reset(new SkSharingSerialContext()); |
| 178 | SkSerialProcs procs; |
| 179 | procs.fImageProc = SkSharingSerialContext::serializeImage; |
| 180 | procs.fImageCtx = mSerialContext.get(); |
Nathaniel Nifong | 429fff4 | 2020-01-30 14:38:21 -0500 | [diff] [blame] | 181 | procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){ |
| 182 | return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData); |
| 183 | }; |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 184 | // SkDocuments don't take owership of the streams they write. |
| 185 | // we need to keep it until after mMultiPic.close() |
| 186 | // procs is passed as a pointer, but just as a method of having an optional default. |
| 187 | // procs doesn't need to outlive this Make call. |
Kevin Lubick | a63d809 | 2024-04-12 17:43:17 +0000 | [diff] [blame] | 188 | mMultiPic = SkMultiPictureDocument::Make(mOpenMultiPicStream.get(), &procs, |
Nathaniel Nifong | 7c21677 | 2021-01-22 13:23:25 -0500 | [diff] [blame] | 189 | [sharingCtx = mSerialContext.get()](const SkPicture* pic) { |
| 190 | SkSharingSerialContext::collectNonTextureImagesFromPicture(pic, sharingCtx); |
| 191 | }); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 192 | return true; |
| 193 | } else { |
| 194 | ALOGE("Could not open \"%s\" for writing.", mCapturedFile.c_str()); |
| 195 | mCaptureSequence = 0; |
| 196 | mCaptureMode = CaptureMode::None; |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 201 | // recurse through the rendernode's children, add any nodes which are layers to the queue. |
| 202 | static void collectLayers(RenderNode* node, LayerUpdateQueue* layers) { |
John Reck | be67195 | 2021-01-13 22:39:32 -0500 | [diff] [blame] | 203 | SkiaDisplayList* dl = node->getDisplayList().asSkiaDl(); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 204 | if (dl) { |
| 205 | const auto& prop = node->properties(); |
| 206 | if (node->hasLayer()) { |
| 207 | layers->enqueueLayerWithDamage(node, Rect(prop.getWidth(), prop.getHeight())); |
| 208 | } |
| 209 | // The way to recurse through rendernodes is to call this with a lambda. |
| 210 | dl->updateChildren([&](RenderNode* child) { collectLayers(child, layers); }); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // record the provided layers to the provided canvas as self-contained skpictures. |
| 215 | static void recordLayers(const LayerUpdateQueue& layers, |
| 216 | SkCanvas* mskpCanvas) { |
| 217 | const Vector3 savedLightCenter(LightingInfo::getLightCenterRaw()); |
| 218 | // Record the commands to re-draw each dirty layer into an SkPicture |
| 219 | for (size_t i = 0; i < layers.entries().size(); i++) { |
| 220 | RenderNode* layerNode = layers.entries()[i].renderNode.get(); |
| 221 | const Rect& layerDamage = layers.entries()[i].damage; |
| 222 | const RenderProperties& properties = layerNode->properties(); |
| 223 | |
| 224 | // Temporarily map current light center into RenderNode's coordinate space |
| 225 | Vector3 transformedLightCenter(savedLightCenter); |
| 226 | layerNode->getSkiaLayer()->inverseTransformInWindow.mapPoint3d(transformedLightCenter); |
| 227 | LightingInfo::setLightCenterRaw(transformedLightCenter); |
| 228 | |
| 229 | SkPictureRecorder layerRec; |
| 230 | auto* recCanvas = layerRec.beginRecording(properties.getWidth(), |
| 231 | properties.getHeight()); |
| 232 | // This is not recorded but still causes clipping. |
| 233 | recCanvas->androidFramework_setDeviceClipRestriction(layerDamage.toSkIRect()); |
| 234 | RenderNodeDrawable root(layerNode, recCanvas, false); |
| 235 | root.forceDraw(recCanvas); |
| 236 | // Now write this picture into the SKP canvas with an annotation indicating what it is |
| 237 | mskpCanvas->drawAnnotation(layerDamage.toSkRect(), String8::format( |
| 238 | "OffscreenLayerDraw|%" PRId64, layerNode->uniqueId()).c_str(), nullptr); |
| 239 | mskpCanvas->drawPicture(layerRec.finishRecordingAsPicture()); |
| 240 | } |
| 241 | LightingInfo::setLightCenterRaw(savedLightCenter); |
| 242 | } |
| 243 | |
| 244 | SkCanvas* SkiaPipeline::tryCapture(SkSurface* surface, RenderNode* root, |
| 245 | const LayerUpdateQueue& dirtyLayers) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 246 | if (CC_LIKELY(!Properties::skpCaptureEnabled)) { |
| 247 | return surface->getCanvas(); // Bail out early when capture is not turned on. |
| 248 | } |
| 249 | // Note that shouldStartNewFileCapture tells us if this is the *first* frame of a capture. |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 250 | bool firstFrameOfAnim = false; |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 251 | if (shouldStartNewFileCapture() && mCaptureMode == CaptureMode::MultiFrameSKP) { |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 252 | // set a reminder to record every layer near the end of this method, after we have set up |
| 253 | // the nway canvas. |
| 254 | firstFrameOfAnim = true; |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 255 | if (!setupMultiFrameCapture()) { |
| 256 | return surface->getCanvas(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 257 | } |
| 258 | } |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 259 | |
| 260 | // Create a canvas pointer, fill it depending on what kind of capture is requested (if any) |
| 261 | SkCanvas* pictureCanvas = nullptr; |
| 262 | switch (mCaptureMode) { |
| 263 | case CaptureMode::CallbackAPI: |
| 264 | case CaptureMode::SingleFrameSKP: |
| 265 | mRecorder.reset(new SkPictureRecorder()); |
Nathaniel Nifong | 31fee3a | 2019-07-11 16:27:14 -0400 | [diff] [blame] | 266 | pictureCanvas = mRecorder->beginRecording(surface->width(), surface->height()); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 267 | break; |
| 268 | case CaptureMode::MultiFrameSKP: |
| 269 | // If a multi frame recording is active, initialize recording for a single frame of a |
| 270 | // multi frame file. |
| 271 | pictureCanvas = mMultiPic->beginPage(surface->width(), surface->height()); |
| 272 | break; |
| 273 | case CaptureMode::None: |
| 274 | // Returning here in the non-capture case means we can count on pictureCanvas being |
| 275 | // non-null below. |
| 276 | return surface->getCanvas(); |
| 277 | } |
| 278 | |
| 279 | // Setting up an nway canvas is common to any kind of capture. |
| 280 | mNwayCanvas = std::make_unique<SkNWayCanvas>(surface->width(), surface->height()); |
| 281 | mNwayCanvas->addCanvas(surface->getCanvas()); |
| 282 | mNwayCanvas->addCanvas(pictureCanvas); |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 283 | |
| 284 | if (firstFrameOfAnim) { |
| 285 | // On the first frame of any mskp capture we want to record any layers that are needed in |
| 286 | // frame but may have been rendered offscreen before recording began. |
| 287 | // We do not maintain a list of all layers, since it isn't needed outside this rare, |
| 288 | // recording use case. Traverse the tree to find them and put them in this LayerUpdateQueue. |
| 289 | LayerUpdateQueue luq; |
| 290 | collectLayers(root, &luq); |
| 291 | recordLayers(luq, mNwayCanvas.get()); |
| 292 | } else { |
| 293 | // on non-first frames, we record any normal layer draws (dirty regions) |
| 294 | recordLayers(dirtyLayers, mNwayCanvas.get()); |
| 295 | } |
| 296 | |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 297 | return mNwayCanvas.get(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void SkiaPipeline::endCapture(SkSurface* surface) { |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 301 | if (CC_LIKELY(mCaptureMode == CaptureMode::None)) { return; } |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 302 | mNwayCanvas.reset(); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 303 | ATRACE_CALL(); |
| 304 | if (mCaptureSequence > 0 && mCaptureMode == CaptureMode::MultiFrameSKP) { |
| 305 | mMultiPic->endPage(); |
| 306 | mCaptureSequence--; |
| 307 | if (mCaptureSequence == 0) { |
| 308 | mCaptureMode = CaptureMode::None; |
| 309 | // Pass mMultiPic and mOpenMultiPicStream to a background thread, which will handle |
| 310 | // the heavyweight serialization work and destroy them. mOpenMultiPicStream is released |
| 311 | // to a bare pointer because keeping it in a smart pointer makes the lambda |
| 312 | // non-copyable. The lambda is only called once, so this is safe. |
| 313 | SkFILEWStream* stream = mOpenMultiPicStream.release(); |
| 314 | CommonPool::post([doc = std::move(mMultiPic), stream]{ |
| 315 | ALOGD("Finalizing multi frame SKP"); |
| 316 | doc->close(); |
| 317 | delete stream; |
| 318 | ALOGD("Multi frame SKP complete."); |
| 319 | }); |
| 320 | } |
| 321 | } else { |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 322 | sk_sp<SkPicture> picture = mRecorder->finishRecordingAsPicture(); |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 323 | if (picture->approximateOpCount() > 0) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 324 | if (mPictureCapturedCallback) { |
| 325 | std::invoke(mPictureCapturedCallback, std::move(picture)); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 326 | } else { |
| 327 | // single frame skp to file |
Nathaniel Nifong | 429fff4 | 2020-01-30 14:38:21 -0500 | [diff] [blame] | 328 | SkSerialProcs procs; |
| 329 | procs.fTypefaceProc = [](SkTypeface* tf, void* ctx){ |
| 330 | return tf->serialize(SkTypeface::SerializeBehavior::kDoIncludeData); |
| 331 | }; |
Kevin Lubick | 14d969d | 2023-09-07 14:28:59 +0000 | [diff] [blame] | 332 | procs.fImageProc = [](SkImage* img, void* ctx) -> sk_sp<SkData> { |
| 333 | GrDirectContext* dCtx = static_cast<GrDirectContext*>(ctx); |
| 334 | return SkPngEncoder::Encode(dCtx, |
| 335 | img, |
| 336 | SkPngEncoder::Options{}); |
| 337 | }; |
| 338 | procs.fImageCtx = mRenderThread.getGrContext(); |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 339 | auto data = picture->serialize(&procs); |
Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 340 | savePictureAsync(data, mCapturedFile); |
| 341 | mCaptureSequence = 0; |
Derek Sollenberger | 5f9753d | 2020-04-01 15:59:02 -0400 | [diff] [blame] | 342 | mCaptureMode = CaptureMode::None; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 343 | } |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 344 | } |
| 345 | mRecorder.reset(); |
| 346 | } |
| 347 | } |
| 348 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 349 | void SkiaPipeline::renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 350 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 351 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 352 | const SkMatrix& preTransform) { |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 353 | bool previousSkpEnabled = Properties::skpCaptureEnabled; |
| 354 | if (mPictureCapturedCallback) { |
| 355 | Properties::skpCaptureEnabled = true; |
| 356 | } |
| 357 | |
Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 358 | // Initialize the canvas for the current frame, that might be a recording canvas if SKP |
| 359 | // capture is enabled. |
| 360 | SkCanvas* canvas = tryCapture(surface.get(), nodes[0].get(), layers); |
| 361 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 362 | // draw all layers up front |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 363 | renderLayersImpl(layers, opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 364 | |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 365 | renderFrameImpl(clip, nodes, opaque, contentDrawBounds, canvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 366 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 367 | endCapture(surface.get()); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 368 | |
| 369 | if (CC_UNLIKELY(Properties::debugOverdraw)) { |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 370 | renderOverdraw(clip, nodes, contentDrawBounds, surface, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 371 | } |
| 372 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 373 | Properties::skpCaptureEnabled = previousSkpEnabled; |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 374 | } |
| 375 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 376 | namespace { |
| 377 | static Rect nodeBounds(RenderNode& node) { |
| 378 | auto& props = node.properties(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 379 | return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 380 | } |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 381 | } // namespace |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 382 | |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 383 | void SkiaPipeline::renderFrameImpl(const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 384 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 385 | const Rect& contentDrawBounds, SkCanvas* canvas, |
| 386 | const SkMatrix& preTransform) { |
Stan Iliev | b66b8bb | 2016-12-15 18:17:42 -0500 | [diff] [blame] | 387 | SkAutoCanvasRestore saver(canvas, true); |
Nathaniel Nifong | cff969f | 2020-01-09 14:03:49 -0500 | [diff] [blame] | 388 | auto clipRestriction = preTransform.mapRect(clip).roundOut(); |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 389 | if (CC_UNLIKELY(isCapturingSkp())) { |
Nathaniel Nifong | 89a5dc5 | 2020-01-17 15:32:17 -0500 | [diff] [blame] | 390 | canvas->drawAnnotation(SkRect::Make(clipRestriction), "AndroidDeviceClipRestriction", |
| 391 | nullptr); |
| 392 | } else { |
| 393 | // clip drawing to dirty region only when not recording SKP files (which should contain all |
| 394 | // draw ops on every frame) |
| 395 | canvas->androidFramework_setDeviceClipRestriction(clipRestriction); |
| 396 | } |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 397 | canvas->concat(preTransform); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 398 | |
Nader Jawad | f5ce452 | 2023-03-27 13:02:41 -0700 | [diff] [blame] | 399 | if (!opaque) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 400 | canvas->clear(SK_ColorTRANSPARENT); |
| 401 | } |
| 402 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 403 | if (1 == nodes.size()) { |
| 404 | if (!nodes[0]->nothingToDraw()) { |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 405 | RenderNodeDrawable root(nodes[0].get(), canvas); |
| 406 | root.draw(canvas); |
| 407 | } |
| 408 | } else if (0 == nodes.size()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 409 | // nothing to draw |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 410 | } else { |
| 411 | // It there are multiple render nodes, they are laid out as follows: |
| 412 | // #0 - backdrop (content + caption) |
| 413 | // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop) |
| 414 | // #2 - additional overlay nodes |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 415 | // Usually the backdrop cannot be seen since it will be entirely covered by the content. |
| 416 | // While |
| 417 | // resizing however it might become partially visible. The following render loop will crop |
| 418 | // the |
| 419 | // backdrop against the content and draw the remaining part of it. It will then draw the |
| 420 | // content |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 421 | // cropped to the backdrop (since that indicates a shrinking of the window). |
| 422 | // |
| 423 | // Additional nodes will be drawn on top with no particular clipping semantics. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 424 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 425 | // Usually the contents bounds should be mContentDrawBounds - however - we will |
| 426 | // move it towards the fixed edge to give it a more stable appearance (for the moment). |
| 427 | // If there is no content bounds we ignore the layering as stated above and start with 2. |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 428 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 429 | // Backdrop bounds in render target space |
| 430 | const Rect backdrop = nodeBounds(*nodes[0]); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 431 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 432 | // Bounds that content will fill in render target space (note content node bounds may be |
| 433 | // bigger) |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 434 | Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight()); |
| 435 | content.translate(backdrop.left, backdrop.top); |
| 436 | if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) { |
| 437 | // Content doesn't entirely overlap backdrop, so fill around content (right/bottom) |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 438 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 439 | // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 440 | // also fill left/top. Currently, both 2up and freeform position content at the top/left |
| 441 | // of |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 442 | // the backdrop, so this isn't necessary. |
| 443 | RenderNodeDrawable backdropNode(nodes[0].get(), canvas); |
| 444 | if (content.right < backdrop.right) { |
| 445 | // draw backdrop to right side of content |
| 446 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 447 | canvas->clipRect(SkRect::MakeLTRB(content.right, backdrop.top, backdrop.right, |
| 448 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 449 | backdropNode.draw(canvas); |
| 450 | } |
| 451 | if (content.bottom < backdrop.bottom) { |
| 452 | // draw backdrop to bottom of content |
| 453 | // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill |
| 454 | SkAutoCanvasRestore acr(canvas, true); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 455 | canvas->clipRect(SkRect::MakeLTRB(content.left, content.bottom, content.right, |
| 456 | backdrop.bottom)); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 457 | backdropNode.draw(canvas); |
| 458 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 461 | RenderNodeDrawable contentNode(nodes[1].get(), canvas); |
| 462 | if (!backdrop.isEmpty()) { |
| 463 | // content node translation to catch up with backdrop |
| 464 | float dx = backdrop.left - contentDrawBounds.left; |
| 465 | float dy = backdrop.top - contentDrawBounds.top; |
| 466 | |
| 467 | SkAutoCanvasRestore acr(canvas, true); |
| 468 | canvas->translate(dx, dy); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 469 | const SkRect contentLocalClip = |
| 470 | SkRect::MakeXYWH(contentDrawBounds.left, contentDrawBounds.top, |
| 471 | backdrop.getWidth(), backdrop.getHeight()); |
Stan Iliev | 5277127 | 2016-11-17 09:54:38 -0500 | [diff] [blame] | 472 | canvas->clipRect(contentLocalClip); |
| 473 | contentNode.draw(canvas); |
| 474 | } else { |
| 475 | SkAutoCanvasRestore acr(canvas, true); |
| 476 | contentNode.draw(canvas); |
| 477 | } |
| 478 | |
| 479 | // remaining overlay nodes, simply defer |
| 480 | for (size_t index = 2; index < nodes.size(); index++) { |
| 481 | if (!nodes[index]->nothingToDraw()) { |
| 482 | SkAutoCanvasRestore acr(canvas, true); |
| 483 | RenderNodeDrawable overlayNode(nodes[index].get(), canvas); |
| 484 | overlayNode.draw(canvas); |
| 485 | } |
| 486 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 487 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 488 | } |
| 489 | |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 490 | void SkiaPipeline::setSurfaceColorProperties(ColorMode colorMode) { |
Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 491 | mColorMode = colorMode; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 492 | switch (colorMode) { |
| 493 | case ColorMode::Default: |
| 494 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
| 495 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 496 | break; |
| 497 | case ColorMode::WideColorGamut: |
| 498 | mSurfaceColorType = DeviceInfo::get()->getWideColorType(); |
| 499 | mSurfaceColorSpace = DeviceInfo::get()->getWideColorSpace(); |
| 500 | break; |
| 501 | case ColorMode::Hdr: |
Alec Mouri | 4a3035e | 2024-03-04 23:12:42 +0000 | [diff] [blame] | 502 | if (DeviceInfo::get()->isSupportRgba10101010ForHdr()) { |
| 503 | mSurfaceColorType = SkColorType::kRGBA_10x6_SkColorType; |
| 504 | mSurfaceColorSpace = SkColorSpace::MakeRGB( |
| 505 | GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3); |
| 506 | } else if (DeviceInfo::get()->isSupportFp16ForHdr()) { |
Alec Mouri | 22ab7f3 | 2023-09-06 02:11:56 +0000 | [diff] [blame] | 507 | mSurfaceColorType = SkColorType::kRGBA_F16_SkColorType; |
| 508 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 509 | } else { |
| 510 | mSurfaceColorType = SkColorType::kN32_SkColorType; |
| 511 | mSurfaceColorSpace = SkColorSpace::MakeRGB( |
| 512 | GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3); |
| 513 | } |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 514 | break; |
John Reck | 0b3f331 | 2023-01-31 16:21:28 -0500 | [diff] [blame] | 515 | case ColorMode::Hdr10: |
| 516 | mSurfaceColorType = SkColorType::kRGBA_1010102_SkColorType; |
| 517 | mSurfaceColorSpace = SkColorSpace::MakeRGB( |
| 518 | GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3); |
| 519 | break; |
Leon Scroggins III | cbdbb66 | 2021-11-30 13:59:00 -0500 | [diff] [blame] | 520 | case ColorMode::A8: |
| 521 | mSurfaceColorType = SkColorType::kAlpha_8_SkColorType; |
| 522 | mSurfaceColorSpace = nullptr; |
| 523 | break; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 527 | void SkiaPipeline::setTargetSdrHdrRatio(float ratio) { |
John Reck | 0b3f331 | 2023-01-31 16:21:28 -0500 | [diff] [blame] | 528 | if (mColorMode == ColorMode::Hdr || mColorMode == ColorMode::Hdr10) { |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 529 | mTargetSdrHdrRatio = ratio; |
Alec Mouri | 22ab7f3 | 2023-09-06 02:11:56 +0000 | [diff] [blame] | 530 | |
Alec Mouri | 4a3035e | 2024-03-04 23:12:42 +0000 | [diff] [blame] | 531 | if (mColorMode == ColorMode::Hdr && DeviceInfo::get()->isSupportFp16ForHdr() && |
| 532 | !DeviceInfo::get()->isSupportRgba10101010ForHdr()) { |
Alec Mouri | 22ab7f3 | 2023-09-06 02:11:56 +0000 | [diff] [blame] | 533 | mSurfaceColorSpace = SkColorSpace::MakeSRGB(); |
| 534 | } else { |
| 535 | mSurfaceColorSpace = SkColorSpace::MakeRGB( |
| 536 | GetExtendedTransferFunction(mTargetSdrHdrRatio), SkNamedGamut::kDisplayP3); |
| 537 | } |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 538 | } else { |
| 539 | mTargetSdrHdrRatio = 1.f; |
| 540 | } |
| 541 | } |
| 542 | |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 543 | // Overdraw debugging |
| 544 | |
| 545 | // These colors should be kept in sync with Caches::getOverdrawColor() with a few differences. |
Mike Reed | 331c4e1 | 2020-02-13 10:21:54 -0500 | [diff] [blame] | 546 | // This implementation requires transparent entries for "no overdraw" and "single draws". |
| 547 | static const SkColor kOverdrawColors[2][6] = { |
| 548 | { |
| 549 | 0x00000000, |
| 550 | 0x00000000, |
| 551 | 0x2f0000ff, |
| 552 | 0x2f00ff00, |
| 553 | 0x3fff0000, |
| 554 | 0x7fff0000, |
| 555 | }, |
| 556 | { |
| 557 | 0x00000000, |
| 558 | 0x00000000, |
| 559 | 0x2f0000ff, |
| 560 | 0x4fffff00, |
| 561 | 0x5fff89d7, |
| 562 | 0x7fff0000, |
| 563 | }, |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 564 | }; |
| 565 | |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 566 | void SkiaPipeline::renderOverdraw(const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 567 | const std::vector<sp<RenderNode>>& nodes, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 568 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 569 | const SkMatrix& preTransform) { |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 570 | // Set up the overdraw canvas. |
| 571 | SkImageInfo offscreenInfo = SkImageInfo::MakeA8(surface->width(), surface->height()); |
| 572 | sk_sp<SkSurface> offscreen = surface->makeSurface(offscreenInfo); |
Nathaniel Nifong | fe05b7c | 2019-09-17 12:52:52 -0400 | [diff] [blame] | 573 | LOG_ALWAYS_FATAL_IF(!offscreen, "Failed to create offscreen SkSurface for overdraw viz."); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 574 | SkOverdrawCanvas overdrawCanvas(offscreen->getCanvas()); |
| 575 | |
| 576 | // Fake a redraw to replay the draw commands. This will increment the alpha channel |
| 577 | // each time a pixel would have been drawn. |
| 578 | // Pass true for opaque so we skip the clear - the overdrawCanvas is already zero |
| 579 | // initialized. |
Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 580 | renderFrameImpl(clip, nodes, true, contentDrawBounds, &overdrawCanvas, preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 581 | sk_sp<SkImage> counts = offscreen->makeImageSnapshot(); |
| 582 | |
| 583 | // Draw overdraw colors to the canvas. The color filter will convert counts to colors. |
| 584 | SkPaint paint; |
Mike Reed | 331c4e1 | 2020-02-13 10:21:54 -0500 | [diff] [blame] | 585 | const SkColor* colors = kOverdrawColors[static_cast<int>(Properties::overdrawColorSet)]; |
| 586 | paint.setColorFilter(SkOverdrawColorFilter::MakeWithSkColors(colors)); |
Mike Reed | 7994a31 | 2021-01-28 18:06:26 -0500 | [diff] [blame] | 587 | surface->getCanvas()->drawImage(counts.get(), 0.0f, 0.0f, SkSamplingOptions(), &paint); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 588 | } |
| 589 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 590 | } /* namespace skiapipeline */ |
| 591 | } /* namespace uirenderer */ |
| 592 | } /* namespace android */ |