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