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