Nathaniel Nifong | 20b177a | 2019-12-12 11:05:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "tools/debugger/DebugLayerManager.h" |
| 9 | |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/core/SkPicture.h" |
| 13 | #include "include/core/SkSurface.h" |
| 14 | #include "include/private/SkTHash.h" |
| 15 | #include "tools/debugger/DebugCanvas.h" |
| 16 | |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 | #include <tuple> |
| 20 | |
| 21 | void DebugLayerManager::setCommand(int nodeId, int frame, int command) { |
| 22 | auto* drawEvent = fDraws.find({frame, nodeId}); |
| 23 | if (!drawEvent) { |
| 24 | SkDebugf("Could not set command playhead for event {%d, %d}, it is not tracked by" |
| 25 | "DebugLayerManager.\n", frame, nodeId); |
| 26 | return; |
| 27 | } |
| 28 | const int count = drawEvent->debugCanvas->getSize(); |
| 29 | drawEvent->command = command < count ? command : count - 1; |
| 30 | // Invalidate stored images that depended on this combination of node and frame. |
| 31 | // actually this does all of the events for this nodeId, but close enough. |
| 32 | auto relevantFrames = listFramesForNode(nodeId); |
| 33 | for (const auto& frame : relevantFrames) { |
| 34 | fDraws[{frame, nodeId}].image = nullptr; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | void DebugLayerManager::storeSkPicture(int nodeId, int frame, sk_sp<SkPicture> picture, |
| 39 | SkIRect dirty) { |
| 40 | const LayerKey k = {frame, nodeId}; |
| 41 | |
| 42 | // Make debug canvas using bounds from SkPicture. This will be equal to whatever width and |
| 43 | // height were passed into SkPictureRecorder::beginRecording(w, h) which is the layer bounds. |
| 44 | const auto& layerBounds = picture->cullRect().roundOut(); |
| 45 | auto debugCanvas = std::make_unique<DebugCanvas>(layerBounds); |
| 46 | // Must be set or they end up undefined due to cosmic rays, bad luck, etc. |
| 47 | debugCanvas->setOverdrawViz(false); |
| 48 | debugCanvas->setDrawGpuOpBounds(false); |
| 49 | debugCanvas->setClipVizColor(SK_ColorTRANSPARENT); |
| 50 | // Setting this allows a layer to contain another layer. TODO(nifong): write a test for this. |
| 51 | debugCanvas->setLayerManagerAndFrame(this, frame); |
| 52 | // Only draw picture to the debug canvas once. |
| 53 | debugCanvas->drawPicture(picture); |
| 54 | int numCommands = debugCanvas->getSize(); |
| 55 | |
| 56 | DrawEvent event = { |
| 57 | frame == 0 || dirty==layerBounds, // fullRedraw |
| 58 | nullptr, // image |
| 59 | std::move(debugCanvas), // debugCanvas |
| 60 | numCommands-1, // command |
| 61 | {layerBounds.height(), layerBounds.width()}, // layerBounds |
| 62 | }; |
| 63 | |
| 64 | fDraws.set(k, std::move(event)); |
| 65 | keys.push_back(k); |
| 66 | } |
| 67 | |
Nathaniel Nifong | 94de6b4 | 2019-12-19 12:54:15 -0500 | [diff] [blame^] | 68 | void DebugLayerManager::drawLayerEventTo(SkCanvas* canvas, const int nodeId, const int frame) { |
| 69 | auto& evt = fDraws[{frame, nodeId}]; |
| 70 | evt.debugCanvas->drawTo(canvas, evt.command); |
| 71 | canvas->flush(); |
| 72 | } |
| 73 | |
Nathaniel Nifong | 20b177a | 2019-12-12 11:05:10 -0500 | [diff] [blame] | 74 | sk_sp<SkImage> DebugLayerManager::getLayerAsImage(const int nodeId, const int frame) { |
| 75 | // What is the last frame having an SkPicture for this layer? call it frame N |
| 76 | // have cached image of it? if so, return it. |
| 77 | // if not, draw it at frame N by the following method: |
| 78 | // The picture at frame N could have been a full redraw, or it could have been clipped to a |
| 79 | // dirty region. In order to know what the layer looked like on this frame, we must draw every |
| 80 | // picture starting with the last full redraw, up to the last one before the current frame, since |
| 81 | // any of those previous draws could be showing through. |
| 82 | |
| 83 | // list of frames this node was updated on. |
| 84 | auto relevantFrames = listFramesForNode(nodeId); |
| 85 | // find largest one not greater than `frame`. |
| 86 | uint32_t i = relevantFrames.size()-1; |
| 87 | while (relevantFrames[i] > frame) { i--; } |
| 88 | const int frameN = relevantFrames[i]; |
| 89 | // Fetch the draw event |
| 90 | auto& drawEvent = fDraws[{frameN, nodeId}]; |
| 91 | // if an image of this is cached, return it. |
| 92 | if (drawEvent.image) { |
| 93 | return drawEvent.image; |
| 94 | } |
| 95 | // when it's not cached, we'll have to render it in an offscreen surface. |
| 96 | // start at the last full redraw. (pick up counting backwards from above) |
| 97 | while (i>0 && !(fDraws[{relevantFrames[i], nodeId}].fullRedraw)) { i--; } |
| 98 | // The correct layer bounds can be obtained from any drawEvent on this layer. |
| 99 | // the color type and alpha type are chosen here to match wasm-skp-debugger/cpu.js which was |
| 100 | // chosen to match the capabilities of HTML canvas, which this ultimately has to be drawn into. |
| 101 | // TODO(nifong): introduce a method of letting the user choose the backend for this. |
| 102 | auto surface = SkSurface::MakeRaster(SkImageInfo::Make(drawEvent.layerBounds, |
| 103 | kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr)); |
| 104 | // draw everything from the last full redraw up to the current frame. |
| 105 | // other frames drawn are partial, meaning they were clipped to not completely cover the layer. |
| 106 | // count back up with i |
| 107 | auto* canvas = surface->getCanvas(); |
| 108 | for (; i<relevantFrames.size() && relevantFrames[i]<=frameN; i++) { |
Nathaniel Nifong | 94de6b4 | 2019-12-19 12:54:15 -0500 | [diff] [blame^] | 109 | drawLayerEventTo(canvas, nodeId, relevantFrames[i]); |
Nathaniel Nifong | 20b177a | 2019-12-12 11:05:10 -0500 | [diff] [blame] | 110 | } |
| 111 | drawEvent.image = surface->makeImageSnapshot(); |
| 112 | return drawEvent.image; |
| 113 | } |
| 114 | |
Nathaniel Nifong | 94de6b4 | 2019-12-19 12:54:15 -0500 | [diff] [blame^] | 115 | DebugLayerManager::DrawEventSummary DebugLayerManager::event(int nodeId, int frame) const { |
| 116 | auto* evt = fDraws.find({frame, nodeId}); |
| 117 | if (!evt) { return {}; } |
| 118 | return { |
| 119 | true, nodeId, evt->fullRedraw, evt->debugCanvas->getSize(), |
| 120 | evt->layerBounds.width(), evt->layerBounds.height() |
| 121 | }; |
| 122 | } |
| 123 | |
Nathaniel Nifong | 20b177a | 2019-12-12 11:05:10 -0500 | [diff] [blame] | 124 | std::vector<DebugLayerManager::DrawEventSummary> DebugLayerManager::summarizeEvents(int frame) const { |
| 125 | std::vector<DrawEventSummary> result; |
| 126 | for (const auto& node : listNodesForFrame(frame)) { |
Nathaniel Nifong | 94de6b4 | 2019-12-19 12:54:15 -0500 | [diff] [blame^] | 127 | auto summary = event(node, frame); |
| 128 | if (summary.found) { |
| 129 | result.push_back(summary); |
| 130 | } |
Nathaniel Nifong | 20b177a | 2019-12-12 11:05:10 -0500 | [diff] [blame] | 131 | } |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | std::vector<int> DebugLayerManager::listNodesForFrame(int frame) const { |
| 136 | std::vector<int> result; |
| 137 | for (const auto& key : keys) { |
| 138 | if (key.frame == frame) { |
| 139 | result.push_back(key.nodeId); |
| 140 | } |
| 141 | } |
| 142 | return result; |
| 143 | } |
| 144 | |
| 145 | std::vector<int> DebugLayerManager::listFramesForNode(int nodeId) const { |
| 146 | std::vector<int> result; |
| 147 | for (const auto& key : keys) { |
| 148 | if (key.nodeId == nodeId) { |
| 149 | result.push_back(key.frame); |
| 150 | } |
| 151 | } |
| 152 | return result; |
| 153 | } |
Nathaniel Nifong | 94de6b4 | 2019-12-19 12:54:15 -0500 | [diff] [blame^] | 154 | |
| 155 | DebugCanvas* DebugLayerManager::getEventDebugCanvas(int nodeId, int frame) { |
| 156 | auto& evt = fDraws[{frame, nodeId}]; |
| 157 | return evt.debugCanvas.get(); |
| 158 | } |
| 159 | |
| 160 | void DebugLayerManager::setOverdrawViz(bool overdrawViz) { |
| 161 | for (const auto& key : keys) { |
| 162 | auto& evt = fDraws[key]; |
| 163 | evt.debugCanvas->setOverdrawViz(overdrawViz); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void DebugLayerManager::setClipVizColor(SkColor clipVizColor) { |
| 168 | for (const auto& key : keys) { |
| 169 | auto& evt = fDraws[key]; |
| 170 | evt.debugCanvas->setClipVizColor(clipVizColor); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void DebugLayerManager::setDrawGpuOpBounds(bool drawGpuOpBounds) { |
| 175 | for (const auto& key : keys) { |
| 176 | auto& evt = fDraws[key]; |
| 177 | evt.debugCanvas->setDrawGpuOpBounds(drawGpuOpBounds); |
| 178 | } |
| 179 | } |