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