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