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(SkSurface * surface,const int nodeId,const int frame)69 void DebugLayerManager::drawLayerEventTo(SkSurface* surface, const int nodeId, const int frame) {
70     auto& evt = fDraws[{frame, nodeId}];
71     evt.debugCanvas->drawTo(surface->getCanvas(), evt.command);
72     surface->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   for (; i<relevantFrames.size() && relevantFrames[i]<=frameN; i++) {
109     drawLayerEventTo(surface.get(), nodeId, relevantFrames[i]);
110   }
111   drawEvent.image = surface->makeImageSnapshot();
112   return drawEvent.image;
113 }
114 
event(int nodeId,int frame) const115 DebugLayerManager::DrawEventSummary DebugLayerManager::event(int nodeId, int frame) const {
116   auto* evt = fDraws.find({frame, nodeId});
117   if (!evt) { return {}; }
118   return {
119     true, evt->debugCanvas->getSize(),
120     evt->layerBounds.width(), evt->layerBounds.height()
121   };
122 }
123 
summarizeLayers(int frame) const124 std::vector<DebugLayerManager::LayerSummary> DebugLayerManager::summarizeLayers(int frame) const {
125   // Find the last update on or before `frame` for every node
126   // key: nodeId, one entry for every layer
127   // value: summary of the layer.
128   std::unordered_map<int, LayerSummary> summaryMap;
129   for (const auto& key : keys) {
130     auto* evt = fDraws.find(key);
131     if (!evt) { continue; }
132     // -1 as a default value for the last update serves as a way of indicating that this layer
133     // is present in the animation, but doesn't have an update less than or equal to `frame`
134     int lastUpdate = (key.frame <= frame ? key.frame : -1);
135 
136     // do we have an entry for this layer yet? is it later than the one we're looking at?
137     auto found = summaryMap.find(key.nodeId);
138     if (found != summaryMap.end()) {
139       LayerSummary& item = summaryMap[key.nodeId];
140       if (lastUpdate > item.frameOfLastUpdate) {
141         item.frameOfLastUpdate = key.frame;
142         item.fullRedraw = evt->fullRedraw;
143       }
144     } else {
145       // record first entry for this layer
146       summaryMap.insert({key.nodeId, {
147         key.nodeId, lastUpdate, evt->fullRedraw,
148         evt->layerBounds.width(), evt->layerBounds.height()
149       }});
150     }
151   }
152   std::vector<LayerSummary> result;
153   for (auto it = summaryMap.begin(); it != summaryMap.end(); ++it) {
154     result.push_back(it->second);
155   }
156   return result;
157 }
158 
listNodesForFrame(int frame) const159 std::vector<int> DebugLayerManager::listNodesForFrame(int frame) const {
160   std::vector<int> result;
161   for (const auto& key : keys) {
162     if (key.frame == frame) {
163       result.push_back(key.nodeId);
164     }
165   }
166   return result;
167 }
168 
listFramesForNode(int nodeId) const169 std::vector<int> DebugLayerManager::listFramesForNode(int nodeId) const {
170   std::vector<int> result;
171   for (const auto& key : keys) {
172     if (key.nodeId == nodeId) {
173       result.push_back(key.frame);
174     }
175   }
176   return result;
177 }
178 
getEventDebugCanvas(int nodeId,int frame)179 DebugCanvas* DebugLayerManager::getEventDebugCanvas(int nodeId, int frame) {
180   auto& evt = fDraws[{frame, nodeId}];
181   return evt.debugCanvas.get();
182 }
183 
setOverdrawViz(bool overdrawViz)184 void DebugLayerManager::setOverdrawViz(bool overdrawViz) {
185   for (const auto& key : keys) {
186     auto& evt = fDraws[key];
187     evt.debugCanvas->setOverdrawViz(overdrawViz);
188   }
189 }
190 
setClipVizColor(SkColor clipVizColor)191 void DebugLayerManager::setClipVizColor(SkColor clipVizColor) {
192   for (const auto& key : keys) {
193     auto& evt = fDraws[key];
194     evt.debugCanvas->setClipVizColor(clipVizColor);
195   }
196 }
197 
setDrawGpuOpBounds(bool drawGpuOpBounds)198 void DebugLayerManager::setDrawGpuOpBounds(bool drawGpuOpBounds) {
199   for (const auto& key : keys) {
200     auto& evt = fDraws[key];
201     evt.debugCanvas->setDrawGpuOpBounds(drawGpuOpBounds);
202   }
203 }
204