blob: c903d7bf8b3bdbe3408e86ba574a23edc81ec202 [file] [log] [blame]
Nathaniel Nifong20b177a2019-12-12 11:05:10 -05001/*
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
21void DebugLayerManager::setCommand(int nodeId, int frame, int command) {
22 auto* drawEvent = fDraws.find({frame, nodeId});
23 if (!drawEvent) {
24 SkDebugf("Could not set command playhead for event {%d, %d}, it is not tracked by"
25 "DebugLayerManager.\n", frame, nodeId);
26 return;
27 }
28 const int count = drawEvent->debugCanvas->getSize();
29 drawEvent->command = command < count ? command : count - 1;
30 // Invalidate stored images that depended on this combination of node and frame.
31 // actually this does all of the events for this nodeId, but close enough.
32 auto relevantFrames = listFramesForNode(nodeId);
33 for (const auto& frame : relevantFrames) {
34 fDraws[{frame, nodeId}].image = nullptr;
35 }
36}
37
38void DebugLayerManager::storeSkPicture(int nodeId, int frame, sk_sp<SkPicture> picture,
39 SkIRect dirty) {
40 const LayerKey k = {frame, nodeId};
41
42 // Make debug canvas using bounds from SkPicture. This will be equal to whatever width and
43 // height were passed into SkPictureRecorder::beginRecording(w, h) which is the layer bounds.
44 const auto& layerBounds = picture->cullRect().roundOut();
45 auto debugCanvas = std::make_unique<DebugCanvas>(layerBounds);
46 // Must be set or they end up undefined due to cosmic rays, bad luck, etc.
47 debugCanvas->setOverdrawViz(false);
48 debugCanvas->setDrawGpuOpBounds(false);
49 debugCanvas->setClipVizColor(SK_ColorTRANSPARENT);
50 // Setting this allows a layer to contain another layer. TODO(nifong): write a test for this.
51 debugCanvas->setLayerManagerAndFrame(this, frame);
52 // Only draw picture to the debug canvas once.
53 debugCanvas->drawPicture(picture);
54 int numCommands = debugCanvas->getSize();
55
56 DrawEvent event = {
57 frame == 0 || dirty==layerBounds, // fullRedraw
58 nullptr, // image
59 std::move(debugCanvas), // debugCanvas
60 numCommands-1, // command
61 {layerBounds.height(), layerBounds.width()}, // layerBounds
62 };
63
64 fDraws.set(k, std::move(event));
65 keys.push_back(k);
66}
67
68sk_sp<SkImage> DebugLayerManager::getLayerAsImage(const int nodeId, const int frame) {
69 // What is the last frame having an SkPicture for this layer? call it frame N
70 // have cached image of it? if so, return it.
71 // if not, draw it at frame N by the following method:
72 // The picture at frame N could have been a full redraw, or it could have been clipped to a
73 // dirty region. In order to know what the layer looked like on this frame, we must draw every
74 // picture starting with the last full redraw, up to the last one before the current frame, since
75 // any of those previous draws could be showing through.
76
77 // list of frames this node was updated on.
78 auto relevantFrames = listFramesForNode(nodeId);
79 // find largest one not greater than `frame`.
80 uint32_t i = relevantFrames.size()-1;
81 while (relevantFrames[i] > frame) { i--; }
82 const int frameN = relevantFrames[i];
83 // Fetch the draw event
84 auto& drawEvent = fDraws[{frameN, nodeId}];
85 // if an image of this is cached, return it.
86 if (drawEvent.image) {
87 return drawEvent.image;
88 }
89 // when it's not cached, we'll have to render it in an offscreen surface.
90 // start at the last full redraw. (pick up counting backwards from above)
91 while (i>0 && !(fDraws[{relevantFrames[i], nodeId}].fullRedraw)) { i--; }
92 // The correct layer bounds can be obtained from any drawEvent on this layer.
93 // the color type and alpha type are chosen here to match wasm-skp-debugger/cpu.js which was
94 // chosen to match the capabilities of HTML canvas, which this ultimately has to be drawn into.
95 // TODO(nifong): introduce a method of letting the user choose the backend for this.
96 auto surface = SkSurface::MakeRaster(SkImageInfo::Make(drawEvent.layerBounds,
97 kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
98 // draw everything from the last full redraw up to the current frame.
99 // other frames drawn are partial, meaning they were clipped to not completely cover the layer.
100 // count back up with i
101 auto* canvas = surface->getCanvas();
102 for (; i<relevantFrames.size() && relevantFrames[i]<=frameN; i++) {
103 auto& evt = fDraws[{relevantFrames[i], nodeId}];
104 evt.debugCanvas->drawTo(canvas, evt.command);
105 canvas->flush();
106 }
107 drawEvent.image = surface->makeImageSnapshot();
108 return drawEvent.image;
109}
110
111std::vector<DebugLayerManager::DrawEventSummary> DebugLayerManager::summarizeEvents(int frame) const {
112 std::vector<DrawEventSummary> result;
113 for (const auto& node : listNodesForFrame(frame)) {
114 auto* evt = fDraws.find({frame, node});
115 if (!evt) { continue; }
116 result.push_back({
117 node, evt->fullRedraw, evt->debugCanvas->getSize(),
118 evt->layerBounds.width(), evt->layerBounds.height()
119 });
120 }
121 return result;
122}
123
124std::vector<int> DebugLayerManager::listNodesForFrame(int frame) const {
125 std::vector<int> result;
126 for (const auto& key : keys) {
127 if (key.frame == frame) {
128 result.push_back(key.nodeId);
129 }
130 }
131 return result;
132}
133
134std::vector<int> DebugLayerManager::listFramesForNode(int nodeId) const {
135 std::vector<int> result;
136 for (const auto& key : keys) {
137 if (key.nodeId == nodeId) {
138 result.push_back(key.frame);
139 }
140 }
141 return result;
142}