| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 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/MSKPPlayer.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkCanvasVirtualEnforcer.h" |
| 12 |
#include "include/core/SkPicture.h" |
| 13 |
#include "include/core/SkPictureRecorder.h" |
| 14 |
#include "include/core/SkSurface.h" |
| 15 |
#include "include/docs/SkMultiPictureDocument.h" |
| 16 |
#include "include/private/base/SkTArray.h" |
| 17 |
#include "include/utils/SkNoDrawCanvas.h" |
| 18 |
#include "src/base/SkTLazy.h" |
| 19 |
#include "src/core/SkCanvasPriv.h" |
| 20 |
#include "src/core/SkStringUtils.h" |
| 21 |
#include "tools/SkSharingProc.h" |
| 22 |
|
| 23 |
#if defined(SK_GANESH) |
| 24 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
using namespace skia_private; |
| 28 |
|
| 29 |
/////////////////////////////////////////////////////////////////////////////// |
| 30 |
|
| 31 |
// Base Cmd struct. |
| 32 |
struct MSKPPlayer::Cmd { |
| 33 |
virtual ~Cmd() = default; |
| 34 |
virtual bool isFullRedraw(SkCanvas*) const = 0; |
| 35 |
virtual void draw(SkCanvas* canvas, const LayerMap&, LayerStateMap*) const = 0; |
| 36 |
// If this command draws another layer, return it's ID. Otherwise, -1. |
| 37 |
virtual int layerID() const { return -1; } |
| 38 |
}; |
| 39 |
|
| 40 |
// Draws a SkPicture. |
| 41 |
struct MSKPPlayer::PicCmd : Cmd { |
| 42 |
sk_sp<SkPicture> fContent; |
| 43 |
SkIRect fClipRect = SkIRect::MakeEmpty(); // clip for picture (no clip if empty). |
| 44 |
|
| 45 |
bool isFullRedraw(SkCanvas* canvas) const override { |
| 46 |
if (fClipRect.isEmpty()) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
return fClipRect.contains(SkIRect::MakeSize(canvas->getBaseLayerSize())); |
| 50 |
} |
| 51 |
|
| 52 |
void draw(SkCanvas* canvas, const LayerMap&, LayerStateMap*) const override { |
| 53 |
if (!fClipRect.isEmpty()) { |
| 54 |
canvas->save(); |
| 55 |
canvas->clipIRect(fClipRect); |
| 56 |
} |
| 57 |
canvas->drawPicture(fContent.get()); |
| 58 |
if (!fClipRect.isEmpty()) { |
| 59 |
canvas->restore(); |
| 60 |
} |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
// Draws another layer. Stores the ID of the layer to draw and what command index on that |
| 65 |
// layer should be current when the layer is drawn. The layer contents are updated to the |
| 66 |
// stored command index before the layer is drawn. |
| 67 |
struct MSKPPlayer::DrawLayerCmd : Cmd { |
| 68 |
int fLayerId; |
| 69 |
size_t fLayerCmdCnt; |
| 70 |
SkRect fSrcRect; |
| 71 |
SkRect fDstRect; |
| 72 |
SkSamplingOptions fSampling; |
| 73 |
SkCanvas::SrcRectConstraint fConstraint; |
| 74 |
std::optional<SkPaint> fPaint; |
| 75 |
|
| 76 |
bool isFullRedraw(SkCanvas* canvas) const override { return false; } |
| 77 |
void draw(SkCanvas* canvas, const LayerMap&, LayerStateMap*) const override; |
| 78 |
int layerID() const override { return fLayerId; } |
| 79 |
}; |
| 80 |
|
| 81 |
void MSKPPlayer::DrawLayerCmd::draw(SkCanvas* canvas, |
| 82 |
const LayerMap& layerMap, |
| 83 |
LayerStateMap* layerStateMap) const { |
| 84 |
const LayerCmds& layer = layerMap.at(fLayerId); |
| 85 |
LayerState* layerState = &(*layerStateMap)[fLayerId]; |
| 86 |
if (!layerState->fSurface) { |
| 87 |
layerState->fCurrCmd = 0; |
| 88 |
layerState->fSurface = MSKPPlayer::MakeSurfaceForLayer(layer, canvas); |
| 89 |
if (!layerState->fSurface) { |
| 90 |
SkDebugf("Couldn't create surface for layer"); |
| 91 |
return; |
| 92 |
} |
| 93 |
} |
| 94 |
size_t cmd = layerState->fCurrCmd; |
| 95 |
if (cmd > fLayerCmdCnt) { |
| 96 |
// If the layer contains contents from later commands then replay from the beginning. |
| 97 |
cmd = 0; |
| 98 |
} |
| 99 |
SkCanvas* layerCanvas = layerState->fSurface->getCanvas(); |
| 100 |
// Check if there is a full redraw between cmd and fLayerCmdCnt and if so jump to it and ensure |
| 101 |
// we clear the canvas if starting from a full redraw. |
| 102 |
for (size_t checkCmd = fLayerCmdCnt - 1; checkCmd > cmd; --checkCmd) { |
| 103 |
if (layer.fCmds[checkCmd]->isFullRedraw(layerCanvas)) { |
| 104 |
cmd = checkCmd; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
for (; cmd < fLayerCmdCnt; ++cmd) { |
| 109 |
if (cmd == 0 || layer.fCmds[cmd]->isFullRedraw(layerCanvas)) { |
| 110 |
layerState->fSurface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 111 |
} |
| 112 |
layer.fCmds[cmd]->draw(layerCanvas, layerMap, layerStateMap); |
| 113 |
} |
| 114 |
layerState->fCurrCmd = fLayerCmdCnt; |
| 115 |
const SkPaint* paint = SkOptAddressOrNull(fPaint); |
| 116 |
canvas->drawImageRect(layerState->fSurface->makeImageSnapshot(), |
| 117 |
fSrcRect, |
| 118 |
fDstRect, |
| 119 |
fSampling, |
| 120 |
paint, |
| 121 |
fConstraint); |
| 122 |
} |
| 123 |
|
| 124 |
/////////////////////////////////////////////////////////////////////////////// |
| 125 |
|
| 126 |
class MSKPPlayer::CmdRecordCanvas : public SkCanvasVirtualEnforcer<SkCanvas> { |
| 127 |
public: |
| 128 |
CmdRecordCanvas(LayerCmds* dst, LayerMap* offscreenLayers, const SkIRect* clipRect = nullptr) |
| 129 |
: fDst(dst), fOffscreenLayers(offscreenLayers) { |
| 130 |
if (clipRect) { |
| 131 |
fClipRect = *clipRect; |
| 132 |
} |
| 133 |
fRecorder.beginRecording(SkRect::Make(dst->fDimensions)); |
| 134 |
} |
| 135 |
~CmdRecordCanvas() override { this->recordPicCmd(); } |
| 136 |
|
| 137 |
protected: |
| 138 |
void onDrawPaint(const SkPaint& paint) override { |
| 139 |
fRecorder.getRecordingCanvas()->drawPaint(paint); |
| 140 |
} |
| 141 |
|
| 142 |
void onDrawBehind(const SkPaint& paint) override { |
| 143 |
SkCanvasPriv::DrawBehind(fRecorder.getRecordingCanvas(), paint); |
| 144 |
} |
| 145 |
|
| 146 |
void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
| 147 |
fRecorder.getRecordingCanvas()->drawRect(rect, paint); |
| 148 |
} |
| 149 |
|
| 150 |
void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override { |
| 151 |
fRecorder.getRecordingCanvas()->drawRRect(rrect, paint); |
| 152 |
} |
| 153 |
|
| 154 |
void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) override { |
| 155 |
fRecorder.getRecordingCanvas()->drawDRRect(outer, inner, paint); |
| 156 |
} |
| 157 |
|
| 158 |
void onDrawOval(const SkRect& rect, const SkPaint& paint) override { |
| 159 |
fRecorder.getRecordingCanvas()->drawOval(rect, paint); |
| 160 |
} |
| 161 |
|
| 162 |
void onDrawArc(const SkRect& rect, |
| 163 |
SkScalar startAngle, |
| 164 |
SkScalar sweepAngle, |
| 165 |
bool useCenter, |
| 166 |
const SkPaint& paint) override { |
| 167 |
fRecorder.getRecordingCanvas()->drawArc(rect, startAngle, sweepAngle, useCenter, paint); |
| 168 |
} |
| 169 |
|
| 170 |
void onDrawPath(const SkPath& path, const SkPaint& paint) override { |
| 171 |
fRecorder.getRecordingCanvas()->drawPath(path, paint); |
| 172 |
} |
| 173 |
|
| 174 |
void onDrawRegion(const SkRegion& region, const SkPaint& paint) override { |
| 175 |
fRecorder.getRecordingCanvas()->drawRegion(region, paint); |
| 176 |
} |
| 177 |
|
| 178 |
void onDrawTextBlob(const SkTextBlob* blob, |
| 179 |
SkScalar x, |
| 180 |
SkScalar y, |
| 181 |
const SkPaint& paint) override { |
| 182 |
fRecorder.getRecordingCanvas()->drawTextBlob(blob, x, y, paint); |
| 183 |
} |
| 184 |
|
| 185 |
void onDrawPatch(const SkPoint cubics[12], |
| 186 |
const SkColor colors[4], |
| 187 |
const SkPoint texCoords[4], |
| 188 |
SkBlendMode mode, |
| 189 |
const SkPaint& paint) override { |
| 190 |
fRecorder.getRecordingCanvas()->drawPatch(cubics, colors, texCoords, mode, paint); |
| 191 |
} |
| 192 |
|
| 193 |
void onDrawPoints(SkCanvas::PointMode mode, |
| 194 |
size_t count, |
| 195 |
const SkPoint pts[], |
| 196 |
const SkPaint& paint) override { |
| 197 |
fRecorder.getRecordingCanvas()->drawPoints(mode, {pts, count}, paint); |
| 198 |
} |
| 199 |
|
| 200 |
void onDrawImage2(const SkImage* image, |
| 201 |
SkScalar dx, |
| 202 |
SkScalar dy, |
| 203 |
const SkSamplingOptions& sampling, |
| 204 |
const SkPaint* paint) override { |
| 205 |
fRecorder.getRecordingCanvas()->drawImage(image, dx, dy, sampling, paint); |
| 206 |
} |
| 207 |
|
| 208 |
void onDrawImageRect2(const SkImage* image, |
| 209 |
const SkRect& src, |
| 210 |
const SkRect& dst, |
| 211 |
const SkSamplingOptions& sampling, |
| 212 |
const SkPaint* paint, |
| 213 |
SrcRectConstraint constraint) override { |
| 214 |
if (fNextDrawImageFromLayerID != -1) { |
| 215 |
this->recordPicCmd(); |
| 216 |
auto drawLayer = std::make_unique<DrawLayerCmd>(); |
| 217 |
drawLayer->fLayerId = fNextDrawImageFromLayerID; |
| 218 |
drawLayer->fLayerCmdCnt = fOffscreenLayers->at(fNextDrawImageFromLayerID).fCmds.size(); |
| 219 |
drawLayer->fSrcRect = src; |
| 220 |
drawLayer->fDstRect = dst; |
| 221 |
drawLayer->fSampling = sampling; |
| 222 |
drawLayer->fConstraint = constraint; |
| 223 |
if (paint) { |
| 224 |
drawLayer->fPaint.emplace(*paint); |
| 225 |
} |
| 226 |
fDst->fCmds.push_back(std::move(drawLayer)); |
| 227 |
fNextDrawImageFromLayerID = -1; |
| 228 |
return; |
| 229 |
} |
| 230 |
fRecorder.getRecordingCanvas()->drawImageRect(image, src, dst, sampling, paint, constraint); |
| 231 |
} |
| 232 |
|
| 233 |
void onDrawImageLattice2(const SkImage* image, |
| 234 |
const Lattice& lattice, |
| 235 |
const SkRect& dst, |
| 236 |
SkFilterMode mode, |
| 237 |
const SkPaint* paint) override { |
| 238 |
fRecorder.getRecordingCanvas()->drawImageLattice(image, lattice, dst, mode, paint); |
| 239 |
} |
| 240 |
|
| 241 |
void onDrawAtlas2(const SkImage* image, |
| 242 |
const SkRSXform rsxForms[], |
| 243 |
const SkRect src[], |
| 244 |
const SkColor colors[], |
| 245 |
int count, |
| 246 |
SkBlendMode mode, |
| 247 |
const SkSamplingOptions& sampling, |
| 248 |
const SkRect* cull, |
| 249 |
const SkPaint* paint) override { |
| 250 |
fRecorder.getRecordingCanvas()->drawAtlas(image, |
| 251 |
{rsxForms, (size_t)count}, |
| 252 |
{src, (size_t)count}, |
| 253 |
{colors, colors ? (size_t)count : 0}, |
| 254 |
mode, |
| 255 |
sampling, |
| 256 |
cull, |
| 257 |
paint); |
| 258 |
} |
| 259 |
|
| 260 |
void onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], |
| 261 |
int count, |
| 262 |
const SkPoint dstClips[], |
| 263 |
const SkMatrix preViewMatrices[], |
| 264 |
const SkSamplingOptions& sampling, |
| 265 |
const SkPaint* paint, |
| 266 |
SrcRectConstraint constraint) override { |
| 267 |
fRecorder.getRecordingCanvas()->experimental_DrawEdgeAAImageSet(imageSet, |
| 268 |
count, |
| 269 |
dstClips, |
| 270 |
preViewMatrices, |
| 271 |
sampling, |
| 272 |
paint, |
| 273 |
constraint); |
| 274 |
} |
| 275 |
|
| 276 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 277 |
void onDrawEdgeAAQuad(const SkRect& rect, |
| 278 |
const SkPoint clip[4], |
| 279 |
SkCanvas::QuadAAFlags aaFlags, |
| 280 |
const SkColor4f& color, |
| 281 |
SkBlendMode mode) override {} |
| 282 |
#else |
| 283 |
void onDrawEdgeAAQuad(const SkRect& rect, |
| 284 |
const SkPoint clip[4], |
| 285 |
SkCanvas::QuadAAFlags aaFlags, |
| 286 |
const SkColor4f& color, |
| 287 |
SkBlendMode mode) override { |
| 288 |
fRecorder.getRecordingCanvas()->experimental_DrawEdgeAAQuad(rect, |
| 289 |
clip, |
| 290 |
aaFlags, |
| 291 |
color, |
| 292 |
mode); |
| 293 |
} |
| 294 |
#endif |
| 295 |
|
| 296 |
void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override { |
| 297 |
static constexpr char kOffscreenLayerDraw[] = "OffscreenLayerDraw"; |
| 298 |
static constexpr char kSurfaceID[] = "SurfaceID"; |
| 299 |
TArray<SkString> tokens; |
| 300 |
SkStrSplit(key, "|", kStrict_SkStrSplitMode, &tokens); |
| 301 |
if (tokens.size() == 2) { |
| 302 |
if (tokens[0].equals(kOffscreenLayerDraw)) { |
| 303 |
// Indicates that the next drawPicture command contains the SkPicture to render |
| 304 |
// to the layer identified by the ID. 'rect' indicates the dirty area to update |
| 305 |
// (and indicates the layer size if this command is introducing a new layer id). |
| 306 |
fNextDrawPictureToLayerID = std::stoi(tokens[1].c_str()); |
| 307 |
fNextDrawPictureToLayerClipRect = rect.roundOut(); |
| 308 |
if (fOffscreenLayers->find(fNextDrawPictureToLayerID) == fOffscreenLayers->end()) { |
| 309 |
SkASSERT(fNextDrawPictureToLayerClipRect.left() == 0 && |
| 310 |
fNextDrawPictureToLayerClipRect.top() == 0); |
| 311 |
(*fOffscreenLayers)[fNextDrawPictureToLayerID].fDimensions = |
| 312 |
fNextDrawPictureToLayerClipRect.size(); |
| 313 |
} |
| 314 |
// The next draw picture will notice that fNextDrawPictureToLayerID is set and |
| 315 |
// redirect the picture to the offscreen layer. |
| 316 |
return; |
| 317 |
} else if (tokens[0].equals(kSurfaceID)) { |
| 318 |
// Indicates that the following drawImageRect should draw an offscreen layer |
| 319 |
// to this layer. |
| 320 |
fNextDrawImageFromLayerID = std::stoi(tokens[1].c_str()); |
| 321 |
return; |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override { |
| 327 |
fRecorder.getRecordingCanvas()->private_draw_shadow_rec(path, rec); |
| 328 |
} |
| 329 |
|
| 330 |
void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override { |
| 331 |
fRecorder.getRecordingCanvas()->drawDrawable(drawable, matrix); |
| 332 |
} |
| 333 |
|
| 334 |
void onDrawPicture(const SkPicture* picture, |
| 335 |
const SkMatrix* matrix, |
| 336 |
const SkPaint* paint) override { |
| 337 |
if (fNextDrawPictureToLayerID != -1) { |
| 338 |
SkASSERT(!matrix); |
| 339 |
SkASSERT(!paint); |
| 340 |
LayerCmds* layer = &fOffscreenLayers->at(fNextDrawPictureToLayerID); |
| 341 |
CmdRecordCanvas sc(layer, fOffscreenLayers, &fNextDrawPictureToLayerClipRect); |
| 342 |
picture->playback(&sc); |
| 343 |
fNextDrawPictureToLayerID = -1; |
| 344 |
fNextDrawPictureToLayerClipRect = SkIRect::MakeEmpty(); |
| 345 |
return; |
| 346 |
} |
| 347 |
if (paint) { |
| 348 |
this->saveLayer(nullptr, paint); |
| 349 |
} |
| 350 |
if (matrix) { |
| 351 |
this->save(); |
| 352 |
this->concat(*matrix); |
| 353 |
} |
| 354 |
|
| 355 |
picture->playback(this); |
| 356 |
|
| 357 |
if (matrix) { |
| 358 |
this->restore(); |
| 359 |
} |
| 360 |
if (paint) { |
| 361 |
this->restore(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
private: |
| 366 |
void recordPicCmd() { |
| 367 |
auto cmd = std::make_unique<PicCmd>(); |
| 368 |
cmd->fContent = fRecorder.finishRecordingAsPicture(); |
| 369 |
cmd->fClipRect = fClipRect; |
| 370 |
if (cmd->fContent) { |
| 371 |
fDst->fCmds.push_back(std::move(cmd)); |
| 372 |
} |
| 373 |
// Set up to accumulate again. |
| 374 |
fRecorder.beginRecording(SkRect::Make(fDst->fDimensions)); |
| 375 |
} |
| 376 |
|
| 377 |
SkPictureRecorder fRecorder; // accumulates draws until we draw an offscreen into this layer. |
| 378 |
LayerCmds* fDst = nullptr; |
| 379 |
SkIRect fClipRect = SkIRect::MakeEmpty(); |
| 380 |
int fNextDrawPictureToLayerID = -1; |
| 381 |
SkIRect fNextDrawPictureToLayerClipRect = SkIRect::MakeEmpty(); |
| 382 |
int fNextDrawImageFromLayerID = -1; |
| 383 |
LayerMap* fOffscreenLayers = nullptr; |
| 384 |
}; |
| 385 |
|
| 386 |
/////////////////////////////////////////////////////////////////////////////// |
| 387 |
|
| 388 |
std::unique_ptr<MSKPPlayer> MSKPPlayer::Make(SkStreamSeekable* stream) { |
| 389 |
auto deserialContext = std::make_unique<SkSharingDeserialContext>(); |
| 390 |
SkDeserialProcs procs; |
| 391 |
procs.fImageProc = SkSharingDeserialContext::deserializeImage; |
| 392 |
procs.fImageCtx = deserialContext.get(); |
| 393 |
|
| 394 |
int pageCount = SkMultiPictureDocument::ReadPageCount(stream); |
| 395 |
if (!pageCount) { |
| 396 |
return nullptr; |
| 397 |
} |
| 398 |
std::vector<SkDocumentPage> pages(pageCount); |
| 399 |
if (!SkMultiPictureDocument::Read(stream, pages.data(), pageCount, &procs)) { |
| 400 |
return nullptr; |
| 401 |
} |
| 402 |
std::unique_ptr<MSKPPlayer> result(new MSKPPlayer); |
| 403 |
result->fRootLayers.reserve(pages.size()); |
| 404 |
for (const auto& page : pages) { |
| 405 |
SkISize dims = {SkScalarCeilToInt(page.fSize.width()), |
| 406 |
SkScalarCeilToInt(page.fSize.height())}; |
| 407 |
result->fRootLayers.emplace_back(); |
| 408 |
result->fRootLayers.back().fDimensions = dims; |
| 409 |
result->fMaxDimensions.fWidth = std::max(dims.width() , result->fMaxDimensions.width() ); |
| 410 |
result->fMaxDimensions.fHeight = std::max(dims.height(), result->fMaxDimensions.height()); |
| 411 |
CmdRecordCanvas sc(&result->fRootLayers.back(), &result->fOffscreenLayers); |
| 412 |
page.fPicture->playback(&sc); |
| 413 |
} |
| 414 |
return result; |
| 415 |
} |
| 416 |
|
| 417 |
MSKPPlayer::~MSKPPlayer() = default; |
| 418 |
|
| 419 |
SkISize MSKPPlayer::frameDimensions(int i) const { |
| 420 |
if (i < 0 || i >= this->numFrames()) { |
| 421 |
return {-1, -1}; |
| 422 |
} |
| 423 |
return fRootLayers[i].fDimensions; |
| 424 |
} |
| 425 |
|
| 426 |
bool MSKPPlayer::playFrame(SkCanvas* canvas, int i) { |
| 427 |
if (i < 0 || i >= this->numFrames()) { |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
// Find the first offscreen layer that has a valid surface. If it's recording context |
| 432 |
// differs from the passed canvas's then reset all the layers. Playback will |
| 433 |
// automatically allocate new surfaces for offscreen layers as they're encountered. |
| 434 |
for (const auto& ols : fOffscreenLayerStates) { |
| 435 |
const LayerState& state = ols.second; |
| 436 |
if (state.fSurface) { |
| 437 |
if (state.fSurface->recordingContext() != canvas->recordingContext()) { |
| 438 |
this->resetLayers(); |
| 439 |
} |
| 440 |
break; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
// Replay all the commands for this frame to the caller's canvas. |
| 445 |
const LayerCmds& layer = fRootLayers[i]; |
| 446 |
for (const auto& cmd : layer.fCmds) { |
| 447 |
cmd->draw(canvas, fOffscreenLayers, &fOffscreenLayerStates); |
| 448 |
} |
| 449 |
return true; |
| 450 |
} |
| 451 |
|
| 452 |
sk_sp<SkSurface> MSKPPlayer::MakeSurfaceForLayer(const LayerCmds& layer, SkCanvas* rootCanvas) { |
| 453 |
// Assume layer has same surface props and info as this (mskp doesn't currently record this |
| 454 |
// data). |
| 455 |
SkSurfaceProps props; |
| 456 |
rootCanvas->getProps(&props); |
| 457 |
return rootCanvas->makeSurface(rootCanvas->imageInfo().makeDimensions(layer.fDimensions), |
| 458 |
&props); |
| 459 |
} |
| 460 |
|
| 461 |
void MSKPPlayer::resetLayers() { fOffscreenLayerStates.clear(); } |
| 462 |
|
| 463 |
void MSKPPlayer::rewindLayers() { |
| 464 |
for (auto& [id, state] : fOffscreenLayerStates) { |
| 465 |
state.fCurrCmd = -1; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
void MSKPPlayer::allocateLayers(SkCanvas* canvas) { |
| 470 |
// Iterate over layers not states as states are lazily created in playback but here we want to |
| 471 |
// create any that don't already exist. |
| 472 |
for (auto& [id, layer] : fOffscreenLayers) { |
| 473 |
LayerState& state = fOffscreenLayerStates[id]; |
| 474 |
if (!state.fSurface || state.fSurface->recordingContext() != canvas->recordingContext()) { |
| 475 |
state.fCurrCmd = -1; |
| 476 |
state.fSurface = MakeSurfaceForLayer(fOffscreenLayers[id], canvas); |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
std::vector<int> MSKPPlayer::layerIDs(int frame) const { |
| 482 |
std::vector<int> result; |
| 483 |
if (frame < 0) { |
| 484 |
result.reserve(fOffscreenLayers.size()); |
| 485 |
for (auto& [id, _] : fOffscreenLayers) { |
| 486 |
result.push_back(id); |
| 487 |
} |
| 488 |
return result; |
| 489 |
} |
| 490 |
if (frame < static_cast<int>(fRootLayers.size())) { |
| 491 |
this->collectReferencedLayers(fRootLayers[frame], &result); |
| 492 |
} |
| 493 |
return result; |
| 494 |
} |
| 495 |
|
| 496 |
sk_sp<SkImage> MSKPPlayer::layerSnapshot(int layerID) const { |
| 497 |
auto iter = fOffscreenLayerStates.find(layerID); |
| 498 |
if (iter == fOffscreenLayerStates.end() || !iter->second.fSurface) { |
| 499 |
return nullptr; |
| 500 |
} |
| 501 |
return iter->second.fSurface->makeImageSnapshot(); |
| 502 |
} |
| 503 |
|
| 504 |
void MSKPPlayer::collectReferencedLayers(const LayerCmds& layer, std::vector<int>* out) const { |
| 505 |
for (const auto& cmd : layer.fCmds) { |
| 506 |
if (int id = cmd->layerID(); id >= 0) { |
| 507 |
// Linear, but we'd need to have a lot of layers to actually care. |
| 508 |
if (std::find(out->begin(), out->end(), id) == out->end()) { |
| 509 |
out->push_back(id); |
| 510 |
auto iter = fOffscreenLayers.find(id); |
| 511 |
SkASSERT(iter != fOffscreenLayers.end()); |
| 512 |
this->collectReferencedLayers(iter->second, out); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |