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/SkAlphaType.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPicture.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkSurface.h"
18 #include "include/core/SkTypes.h"
19 #include "include/private/base/SkDebug.h"
20 #include "src/core/SkTHash.h"
21 #include "tools/debugger/DebugCanvas.h"
22
23 #include <cstdint>
24 #include <memory>
25 #include <unordered_map>
26 #include <utility>
27 #include <vector>
28
setCommand(int nodeId,int frame,int command)29 void DebugLayerManager::setCommand(int nodeId, int frame, int command) {
30 auto* drawEvent = fDraws.find({frame, nodeId});
31 if (!drawEvent) {
32 SkDebugf(
33 "Could not set command playhead for event {%d, %d}, it is not tracked by"
34 "DebugLayerManager.\n",
35 frame,
36 nodeId);
37 return;
38 }
39 const int count = drawEvent->debugCanvas->getSize();
40 drawEvent->command = command < count ? command : count - 1;
41 // Invalidate stored images that depended on this combination of node and frame.
42 // actually this does all of the events for this nodeId, but close enough.
43 auto relevantFrames = listFramesForNode(nodeId);
44 for (const auto& f : relevantFrames) {
45 fDraws[{f, nodeId}].image = nullptr;
46 }
47 }
48
storeSkPicture(int nodeId,int frame,const sk_sp<SkPicture> & picture,SkIRect dirty)49 void DebugLayerManager::storeSkPicture(int nodeId,
50 int frame,
51 const sk_sp<SkPicture>& picture,
52 SkIRect dirty) {
53 const LayerKey k = {frame, nodeId};
54
55 // Make debug canvas using bounds from SkPicture. This will be equal to whatever width and
56 // height were passed into SkPictureRecorder::beginRecording(w, h) which is the layer bounds.
57 const auto& layerBounds = picture->cullRect().roundOut();
58 auto debugCanvas = std::make_unique<DebugCanvas>(layerBounds);
59 // Must be set or they end up undefined due to cosmic rays, bad luck, etc.
60 debugCanvas->setOverdrawViz(false);
61 debugCanvas->setDrawGpuOpBounds(false);
62 debugCanvas->setClipVizColor(SK_ColorTRANSPARENT);
63 // Setting this allows a layer to contain another layer. TODO(nifong): write a test for this.
64 debugCanvas->setLayerManagerAndFrame(this, frame);
65 // Only draw picture to the debug canvas once.
66 debugCanvas->drawPicture(picture);
67 int numCommands = debugCanvas->getSize();
68
69 DrawEvent event = {
70 frame == 0 || dirty == layerBounds, // fullRedraw
71 nullptr, // image
72 std::move(debugCanvas), // debugCanvas
73 numCommands - 1, // command
74 {layerBounds.width(), layerBounds.height()}, // layerBounds
75 };
76
77 fDraws.set(k, std::move(event));
78 keys.push_back(k);
79 }
80
drawLayerEventTo(SkSurface * surface,const int nodeId,const int frame)81 void DebugLayerManager::drawLayerEventTo(SkSurface* surface, const int nodeId, const int frame) {
82 auto& evt = fDraws[{frame, nodeId}];
83 evt.debugCanvas->drawTo(surface->getCanvas(), evt.command);
84 }
85
getLayerAsImage(const int nodeId,const int frame)86 sk_sp<SkImage> DebugLayerManager::getLayerAsImage(const int nodeId, const int frame) {
87 // What is the last frame having an SkPicture for this layer? call it frame N
88 // have cached image of it? if so, return it.
89 // if not, draw it at frame N by the following method:
90 // The picture at frame N could have been a full redraw, or it could have been clipped to a
91 // dirty region. In order to know what the layer looked like on this frame, we must draw every
92 // picture starting with the last full redraw, up to the last one before the current frame, since
93 // any of those previous draws could be showing through.
94
95 // list of frames this node was updated on.
96 auto relevantFrames = listFramesForNode(nodeId);
97 // find largest one not greater than `frame`.
98 uint32_t i = relevantFrames.size()-1;
99 while (relevantFrames[i] > frame) { i--; }
100 const int frameN = relevantFrames[i];
101 // Fetch the draw event
102 auto& drawEvent = fDraws[{frameN, nodeId}];
103 // if an image of this is cached, return it.
104 if (drawEvent.image) {
105 return drawEvent.image;
106 }
107 // when it's not cached, we'll have to render it in an offscreen surface.
108 // start at the last full redraw. (pick up counting backwards from above)
109 while (i>0 && !(fDraws[{relevantFrames[i], nodeId}].fullRedraw)) { i--; }
110 // The correct layer bounds can be obtained from any drawEvent on this layer.
111 // the color type and alpha type are chosen here to match wasm-skp-debugger/cpu.js which was
112 // chosen to match the capabilities of HTML canvas, which this ultimately has to be drawn into.
113 // TODO(nifong): introduce a method of letting the user choose the backend for this.
114 auto surface = SkSurfaces::Raster(SkImageInfo::Make(
115 drawEvent.layerBounds, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
116 // draw everything from the last full redraw up to the current frame.
117 // other frames drawn are partial, meaning they were clipped to not completely cover the layer.
118 // count back up with i
119 for (; i<relevantFrames.size() && relevantFrames[i]<=frameN; i++) {
120 drawLayerEventTo(surface.get(), nodeId, relevantFrames[i]);
121 }
122 drawEvent.image = surface->makeImageSnapshot();
123 return drawEvent.image;
124 }
125
event(int nodeId,int frame) const126 DebugLayerManager::DrawEventSummary DebugLayerManager::event(int nodeId, int frame) const {
127 auto* evt = fDraws.find({frame, nodeId});
128 if (!evt) { return {}; }
129 return {
130 true, evt->debugCanvas->getSize(),
131 evt->layerBounds.width(), evt->layerBounds.height()
132 };
133 }
134
summarizeLayers(int frame) const135 std::vector<DebugLayerManager::LayerSummary> DebugLayerManager::summarizeLayers(int frame) const {
136 // Find the last update on or before `frame` for every node
137 // key: nodeId, one entry for every layer
138 // value: summary of the layer.
139 std::unordered_map<int, LayerSummary> summaryMap;
140 for (const auto& key : keys) {
141 auto* evt = fDraws.find(key);
142 if (!evt) { continue; }
143 // -1 as a default value for the last update serves as a way of indicating that this layer
144 // is present in the animation, but doesn't have an update less than or equal to `frame`
145 int lastUpdate = (key.frame <= frame ? key.frame : -1);
146
147 // do we have an entry for this layer yet? is it later than the one we're looking at?
148 auto found = summaryMap.find(key.nodeId);
149 if (found != summaryMap.end()) {
150 LayerSummary& item = summaryMap[key.nodeId];
151 if (lastUpdate > item.frameOfLastUpdate) {
152 item.frameOfLastUpdate = key.frame;
153 item.fullRedraw = evt->fullRedraw;
154 }
155 } else {
156 // record first entry for this layer
157 summaryMap.insert({key.nodeId, {
158 key.nodeId, lastUpdate, evt->fullRedraw,
159 evt->layerBounds.width(), evt->layerBounds.height()
160 }});
161 }
162 }
163 std::vector<LayerSummary> result;
164 for (auto it = summaryMap.begin(); it != summaryMap.end(); ++it) {
165 result.push_back(it->second);
166 }
167 return result;
168 }
169
listNodesForFrame(int frame) const170 std::vector<int> DebugLayerManager::listNodesForFrame(int frame) const {
171 std::vector<int> result;
172 for (const auto& key : keys) {
173 if (key.frame == frame) {
174 result.push_back(key.nodeId);
175 }
176 }
177 return result;
178 }
179
listFramesForNode(int nodeId) const180 std::vector<int> DebugLayerManager::listFramesForNode(int nodeId) const {
181 std::vector<int> result;
182 for (const auto& key : keys) {
183 if (key.nodeId == nodeId) {
184 result.push_back(key.frame);
185 }
186 }
187 return result;
188 }
189
getEventDebugCanvas(int nodeId,int frame)190 DebugCanvas* DebugLayerManager::getEventDebugCanvas(int nodeId, int frame) {
191 auto& evt = fDraws[{frame, nodeId}];
192 return evt.debugCanvas.get();
193 }
194
setOverdrawViz(bool overdrawViz)195 void DebugLayerManager::setOverdrawViz(bool overdrawViz) {
196 for (const auto& key : keys) {
197 auto& evt = fDraws[key];
198 evt.debugCanvas->setOverdrawViz(overdrawViz);
199 }
200 }
201
setClipVizColor(SkColor clipVizColor)202 void DebugLayerManager::setClipVizColor(SkColor clipVizColor) {
203 for (const auto& key : keys) {
204 auto& evt = fDraws[key];
205 evt.debugCanvas->setClipVizColor(clipVizColor);
206 }
207 }
208
setDrawGpuOpBounds(bool drawGpuOpBounds)209 void DebugLayerManager::setDrawGpuOpBounds(bool drawGpuOpBounds) {
210 for (const auto& key : keys) {
211 auto& evt = fDraws[key];
212 evt.debugCanvas->setDrawGpuOpBounds(drawGpuOpBounds);
213 }
214 }
215