Flutter Engine
The Flutter Engine
DebugLayerManager.cpp
Go to the documentation of this file.
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
9
16#include "include/core/SkRect.h"
20#include "src/core/SkTHash.h"
22
23#include <cstdint>
24#include <memory>
25#include <unordered_map>
26#include <utility>
27#include <vector>
28
29void DebugLayerManager::setCommand(int nodeId, int frame, int command) {
30 auto* drawEvent = fDraws.find({frame, nodeId});
31 if (!drawEvent) {
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
50 int frame,
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
81void 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
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.
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
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
135std::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
170std::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
180std::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
191 auto& evt = fDraws[{frame, nodeId}];
192 return evt.debugCanvas.get();
193}
194
195void DebugLayerManager::setOverdrawViz(bool overdrawViz) {
196 for (const auto& key : keys) {
197 auto& evt = fDraws[key];
198 evt.debugCanvas->setOverdrawViz(overdrawViz);
199 }
200}
201
203 for (const auto& key : keys) {
204 auto& evt = fDraws[key];
205 evt.debugCanvas->setClipVizColor(clipVizColor);
206 }
207}
208
209void DebugLayerManager::setDrawGpuOpBounds(bool drawGpuOpBounds) {
210 for (const auto& key : keys) {
211 auto& evt = fDraws[key];
212 evt.debugCanvas->setDrawGpuOpBounds(drawGpuOpBounds);
213 }
214}
int count
Definition: FontMgrTest.cpp:50
kUnpremul_SkAlphaType
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
DrawEventSummary event(int nodeId, int frame) const
std::vector< int > listNodesForFrame(int frame) const
void setCommand(int nodeId, int frame, int command)
DebugCanvas * getEventDebugCanvas(int nodeid, int frame)
void drawLayerEventTo(SkSurface *, const int nodeId, const int frame)
void setClipVizColor(SkColor clipVizColor)
void setOverdrawViz(bool overdrawViz)
void storeSkPicture(int nodeId, int frame, const sk_sp< SkPicture > &picture, SkIRect dirty)
void setDrawGpuOpBounds(bool drawGpuOpBounds)
std::vector< int > listFramesForNode(int nodeId) const
sk_sp< SkImage > getLayerAsImage(const int nodeId, const int frame)
std::vector< LayerSummary > summarizeLayers(int frame) const
virtual SkRect cullRect() const =0
VkSurfaceKHR surface
Definition: main.cc:49
double frame
Definition: examples.cpp:31
GAsyncResult * result
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
list command
Definition: valgrind.py:24
Definition: SkRect.h:32
constexpr int32_t width() const
Definition: SkRect.h:158
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
void roundOut(SkIRect *dst) const
Definition: SkRect.h:1241