Flutter Engine
The Flutter Engine
Classes | Public Member Functions | List of all members
DebugLayerManager Class Reference

#include <DebugLayerManager.h>

Classes

struct  DrawEventSummary
 
struct  LayerKey
 
struct  LayerSummary
 

Public Member Functions

 DebugLayerManager ()
 
void storeSkPicture (int nodeId, int frame, const sk_sp< SkPicture > &picture, SkIRect dirty)
 
void setCommand (int nodeId, int frame, int command)
 
void drawLayerEventTo (SkSurface *, const int nodeId, const int frame)
 
sk_sp< SkImagegetLayerAsImage (const int nodeId, const int frame)
 
DrawEventSummary event (int nodeId, int frame) const
 
std::vector< LayerSummarysummarizeLayers (int frame) const
 
std::vector< intlistNodesForFrame (int frame) const
 
std::vector< intlistFramesForNode (int nodeId) const
 
void toJSON (SkJSONWriter &, UrlDataManager &, SkCanvas *, int nodeId, int frame)
 
DebugCanvasgetEventDebugCanvas (int nodeid, int frame)
 
void setOverdrawViz (bool overdrawViz)
 
void setClipVizColor (SkColor clipVizColor)
 
void setDrawGpuOpBounds (bool drawGpuOpBounds)
 
const std::vector< DebugLayerManager::LayerKey > & getKeys () const
 

Detailed Description

Definition at line 45 of file DebugLayerManager.h.

Constructor & Destructor Documentation

◆ DebugLayerManager()

DebugLayerManager::DebugLayerManager ( )
inline

Definition at line 47 of file DebugLayerManager.h.

47{}

Member Function Documentation

◆ drawLayerEventTo()

void DebugLayerManager::drawLayerEventTo ( SkSurface surface,
const int  nodeId,
const int  frame 
)

Definition at line 81 of file DebugLayerManager.cpp.

81 {
82 auto& evt = fDraws[{frame, nodeId}];
83 evt.debugCanvas->drawTo(surface->getCanvas(), evt.command);
84}
VkSurfaceKHR surface
Definition: main.cc:49
double frame
Definition: examples.cpp:31

◆ event()

DebugLayerManager::DrawEventSummary DebugLayerManager::event ( int  nodeId,
int  frame 
) const

Definition at line 126 of file DebugLayerManager.cpp.

126 {
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}

◆ getEventDebugCanvas()

DebugCanvas * DebugLayerManager::getEventDebugCanvas ( int  nodeid,
int  frame 
)

Definition at line 190 of file DebugLayerManager.cpp.

190 {
191 auto& evt = fDraws[{frame, nodeId}];
192 return evt.debugCanvas.get();
193}

◆ getKeys()

const std::vector< DebugLayerManager::LayerKey > & DebugLayerManager::getKeys ( ) const
inline

Definition at line 135 of file DebugLayerManager.h.

135{ return keys; }

◆ getLayerAsImage()

sk_sp< SkImage > DebugLayerManager::getLayerAsImage ( const int  nodeId,
const int  frame 
)

Definition at line 86 of file DebugLayerManager.cpp.

86 {
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}
kUnpremul_SkAlphaType
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
void drawLayerEventTo(SkSurface *, const int nodeId, const int frame)
std::vector< int > listFramesForNode(int nodeId) const
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)

◆ listFramesForNode()

std::vector< int > DebugLayerManager::listFramesForNode ( int  nodeId) const

Definition at line 180 of file DebugLayerManager.cpp.

180 {
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}
GAsyncResult * result

◆ listNodesForFrame()

std::vector< int > DebugLayerManager::listNodesForFrame ( int  frame) const

Definition at line 170 of file DebugLayerManager.cpp.

170 {
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}

◆ setClipVizColor()

void DebugLayerManager::setClipVizColor ( SkColor  clipVizColor)

Definition at line 202 of file DebugLayerManager.cpp.

202 {
203 for (const auto& key : keys) {
204 auto& evt = fDraws[key];
205 evt.debugCanvas->setClipVizColor(clipVizColor);
206 }
207}

◆ setCommand()

void DebugLayerManager::setCommand ( int  nodeId,
int  frame,
int  command 
)

Definition at line 29 of file DebugLayerManager.cpp.

29 {
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}
int count
Definition: FontMgrTest.cpp:50
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
list command
Definition: valgrind.py:24

◆ setDrawGpuOpBounds()

void DebugLayerManager::setDrawGpuOpBounds ( bool  drawGpuOpBounds)

Definition at line 209 of file DebugLayerManager.cpp.

209 {
210 for (const auto& key : keys) {
211 auto& evt = fDraws[key];
212 evt.debugCanvas->setDrawGpuOpBounds(drawGpuOpBounds);
213 }
214}

◆ setOverdrawViz()

void DebugLayerManager::setOverdrawViz ( bool  overdrawViz)

Definition at line 195 of file DebugLayerManager.cpp.

195 {
196 for (const auto& key : keys) {
197 auto& evt = fDraws[key];
198 evt.debugCanvas->setOverdrawViz(overdrawViz);
199 }
200}

◆ storeSkPicture()

void DebugLayerManager::storeSkPicture ( int  nodeId,
int  frame,
const sk_sp< SkPicture > &  picture,
SkIRect  dirty 
)

Definition at line 49 of file DebugLayerManager.cpp.

52 {
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}
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
DrawEventSummary event(int nodeId, int frame) const
virtual SkRect cullRect() const =0
sk_sp< const SkPicture > picture
Definition: SkRecords.h:299
constexpr int32_t width() const
Definition: SkRect.h:158
void roundOut(SkIRect *dst) const
Definition: SkRect.h:1241

◆ summarizeLayers()

std::vector< DebugLayerManager::LayerSummary > DebugLayerManager::summarizeLayers ( int  frame) const

Definition at line 135 of file DebugLayerManager.cpp.

135 {
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}

◆ toJSON()

void DebugLayerManager::toJSON ( SkJSONWriter ,
UrlDataManager ,
SkCanvas ,
int  nodeId,
int  frame 
)

The documentation for this class was generated from the following files: