| 1 |
/* |
| 2 |
* Copyright 2014 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 |
#include "src/core/SkPicturePlayback.h" |
| 8 |
|
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkClipOp.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkData.h" |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkImageFilter.h" |
| 16 |
#include "include/core/SkMatrix.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkPoint.h" |
| 19 |
#include "include/core/SkRRect.h" |
| 20 |
#include "include/core/SkRSXform.h" |
| 21 |
#include "include/core/SkRect.h" |
| 22 |
#include "include/core/SkRefCnt.h" |
| 23 |
#include "include/core/SkRegion.h" |
| 24 |
#include "include/core/SkSamplingOptions.h" |
| 25 |
#include "include/core/SkScalar.h" |
| 26 |
#include "include/core/SkString.h" |
| 27 |
#include "include/core/SkTileMode.h" |
| 28 |
#include "include/core/SkTypes.h" |
| 29 |
#include "include/private/base/SkAlign.h" |
| 30 |
#include "include/private/base/SkTArray.h" |
| 31 |
#include "include/private/base/SkTemplates.h" |
| 32 |
#include "include/private/base/SkTo.h" |
| 33 |
#include "src/base/SkSafeMath.h" |
| 34 |
#include "src/core/SkCanvasPriv.h" |
| 35 |
#include "src/core/SkDrawShadowInfo.h" |
| 36 |
#include "src/core/SkPictureData.h" |
| 37 |
#include "src/core/SkPictureFlat.h" |
| 38 |
#include "src/core/SkPicturePriv.h" |
| 39 |
#include "src/core/SkReadBuffer.h" |
| 40 |
#include "src/core/SkVerticesPriv.h" |
| 41 |
#include "src/utils/SkPatchUtils.h" |
| 42 |
|
| 43 |
class SkDrawable; |
| 44 |
class SkPath; |
| 45 |
class SkTextBlob; |
| 46 |
class SkVertices; |
| 47 |
|
| 48 |
namespace sktext { namespace gpu { class Slug; } } |
| 49 |
|
| 50 |
using namespace skia_private; |
| 51 |
|
| 52 |
static const SkRect* get_rect_ptr(SkReadBuffer* reader, SkRect* storage) { |
| 53 |
if (reader->readBool()) { |
| 54 |
reader->readRect(storage); |
| 55 |
return storage; |
| 56 |
} else { |
| 57 |
return nullptr; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void SkPicturePlayback::draw(SkCanvas* canvas, |
| 62 |
SkPicture::AbortCallback* callback, |
| 63 |
SkReadBuffer* buffer) { |
| 64 |
AutoResetOpID aroi(this); |
| 65 |
SkASSERT(0 == fCurOffset); |
| 66 |
|
| 67 |
// We do not need to set SkDeserialProcs because we are just reading ints. |
| 68 |
// e.g. images and typefaces are stored in an array and referred to by index. |
| 69 |
SkReadBuffer reader(fPictureData->opData()->bytes(), |
| 70 |
fPictureData->opData()->size()); |
| 71 |
reader.setVersion(fPictureData->info().getVersion()); |
| 72 |
|
| 73 |
// Record this, so we can concat w/ it if we encounter a setMatrix() |
| 74 |
SkM44 initialMatrix = canvas->getLocalToDevice(); |
| 75 |
|
| 76 |
SkAutoCanvasRestore acr(canvas, false); |
| 77 |
|
| 78 |
while (!reader.eof() && reader.isValid()) { |
| 79 |
if (callback && callback->abort()) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
fCurOffset = reader.offset(); |
| 84 |
|
| 85 |
uint32_t bits = reader.readInt(); |
| 86 |
uint32_t op = bits >> 24, |
| 87 |
size = bits & 0xffffff; |
| 88 |
if (size == 0xffffff) { |
| 89 |
size = reader.readInt(); |
| 90 |
} |
| 91 |
|
| 92 |
if (!reader.validate(size > 0 && op > UNUSED && op <= LAST_DRAWTYPE_ENUM)) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
this->handleOp(&reader, (DrawType)op, size, canvas, initialMatrix); |
| 97 |
} |
| 98 |
|
| 99 |
// need to propagate invalid state to the parent reader |
| 100 |
if (buffer) { |
| 101 |
buffer->validate(reader.isValid()); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static void validate_offsetToRestore(SkReadBuffer* reader, size_t offsetToRestore) { |
| 106 |
if (offsetToRestore) { |
| 107 |
reader->validate(SkIsAlign4(offsetToRestore) && offsetToRestore >= reader->offset()); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static bool do_clip_op(SkReadBuffer* reader, SkCanvas* canvas, SkRegion::Op op, |
| 112 |
SkClipOp* clipOpToUse) { |
| 113 |
switch(op) { |
| 114 |
case SkRegion::kDifference_Op: |
| 115 |
case SkRegion::kIntersect_Op: |
| 116 |
// Fully supported, identity mapping between SkClipOp and Region::Op |
| 117 |
*clipOpToUse = static_cast<SkClipOp>(op); |
| 118 |
return true; |
| 119 |
case SkRegion::kReplace_Op: |
| 120 |
// Emulate the replace by resetting first and following it up with an intersect |
| 121 |
SkASSERT(reader->isVersionLT(SkPicturePriv::kNoExpandingClipOps)); |
| 122 |
SkCanvasPriv::ResetClip(canvas); |
| 123 |
*clipOpToUse = SkClipOp::kIntersect; |
| 124 |
return true; |
| 125 |
default: |
| 126 |
// An expanding clip op, which if encountered on an old SKP, we just silently ignore |
| 127 |
SkASSERT(reader->isVersionLT(SkPicturePriv::kNoExpandingClipOps)); |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void SkPicturePlayback::handleOp(SkReadBuffer* reader, |
| 133 |
DrawType op, |
| 134 |
uint32_t size, |
| 135 |
SkCanvas* canvas, |
| 136 |
const SkM44& initialMatrix) { |
| 137 |
#define BREAK_ON_READ_ERROR(r) if (!r->isValid()) break |
| 138 |
|
| 139 |
switch (op) { |
| 140 |
case NOOP: { |
| 141 |
SkASSERT(size >= 4); |
| 142 |
reader->skip(size - 4); |
| 143 |
} break; |
| 144 |
case FLUSH: |
| 145 |
break; |
| 146 |
case CLIP_PATH: { |
| 147 |
const SkPath& path = fPictureData->getPath(reader); |
| 148 |
uint32_t packed = reader->readInt(); |
| 149 |
SkRegion::Op rgnOp = ClipParams_unpackRegionOp(reader, packed); |
| 150 |
bool doAA = ClipParams_unpackDoAA(packed); |
| 151 |
size_t offsetToRestore = reader->readInt(); |
| 152 |
validate_offsetToRestore(reader, offsetToRestore); |
| 153 |
BREAK_ON_READ_ERROR(reader); |
| 154 |
|
| 155 |
SkClipOp clipOp; |
| 156 |
if (do_clip_op(reader, canvas, rgnOp, &clipOp)) { |
| 157 |
canvas->clipPath(path, clipOp, doAA); |
| 158 |
} |
| 159 |
if (canvas->isClipEmpty() && offsetToRestore) { |
| 160 |
reader->skip(offsetToRestore - reader->offset()); |
| 161 |
} |
| 162 |
} break; |
| 163 |
case CLIP_REGION: { |
| 164 |
SkRegion region; |
| 165 |
reader->readRegion(®ion); |
| 166 |
uint32_t packed = reader->readInt(); |
| 167 |
SkRegion::Op rgnOp = ClipParams_unpackRegionOp(reader, packed); |
| 168 |
size_t offsetToRestore = reader->readInt(); |
| 169 |
validate_offsetToRestore(reader, offsetToRestore); |
| 170 |
BREAK_ON_READ_ERROR(reader); |
| 171 |
|
| 172 |
SkClipOp clipOp; |
| 173 |
if (do_clip_op(reader, canvas, rgnOp, &clipOp)) { |
| 174 |
canvas->clipRegion(region, clipOp); |
| 175 |
} |
| 176 |
if (canvas->isClipEmpty() && offsetToRestore) { |
| 177 |
reader->skip(offsetToRestore - reader->offset()); |
| 178 |
} |
| 179 |
} break; |
| 180 |
case CLIP_RECT: { |
| 181 |
SkRect rect; |
| 182 |
reader->readRect(&rect); |
| 183 |
uint32_t packed = reader->readInt(); |
| 184 |
SkRegion::Op rgnOp = ClipParams_unpackRegionOp(reader, packed); |
| 185 |
bool doAA = ClipParams_unpackDoAA(packed); |
| 186 |
size_t offsetToRestore = reader->readInt(); |
| 187 |
validate_offsetToRestore(reader, offsetToRestore); |
| 188 |
BREAK_ON_READ_ERROR(reader); |
| 189 |
|
| 190 |
SkClipOp clipOp; |
| 191 |
if (do_clip_op(reader, canvas, rgnOp, &clipOp)) { |
| 192 |
canvas->clipRect(rect, clipOp, doAA); |
| 193 |
} |
| 194 |
if (canvas->isClipEmpty() && offsetToRestore) { |
| 195 |
reader->skip(offsetToRestore - reader->offset()); |
| 196 |
} |
| 197 |
} break; |
| 198 |
case CLIP_RRECT: { |
| 199 |
SkRRect rrect; |
| 200 |
reader->readRRect(&rrect); |
| 201 |
uint32_t packed = reader->readInt(); |
| 202 |
SkRegion::Op rgnOp = ClipParams_unpackRegionOp(reader, packed); |
| 203 |
bool doAA = ClipParams_unpackDoAA(packed); |
| 204 |
size_t offsetToRestore = reader->readInt(); |
| 205 |
validate_offsetToRestore(reader, offsetToRestore); |
| 206 |
BREAK_ON_READ_ERROR(reader); |
| 207 |
|
| 208 |
SkClipOp clipOp; |
| 209 |
if (do_clip_op(reader, canvas, rgnOp, &clipOp)) { |
| 210 |
canvas->clipRRect(rrect, clipOp, doAA); |
| 211 |
} |
| 212 |
if (canvas->isClipEmpty() && offsetToRestore) { |
| 213 |
reader->skip(offsetToRestore - reader->offset()); |
| 214 |
} |
| 215 |
} break; |
| 216 |
case CLIP_SHADER_IN_PAINT: { |
| 217 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 218 |
// clipShader() was never used in conjunction with deprecated, expanding clip ops, so |
| 219 |
// it requires the op to just be intersect or difference. |
| 220 |
SkClipOp clipOp = reader->checkRange(SkClipOp::kDifference, SkClipOp::kIntersect); |
| 221 |
BREAK_ON_READ_ERROR(reader); |
| 222 |
|
| 223 |
canvas->clipShader(paint.refShader(), clipOp); |
| 224 |
} break; |
| 225 |
case RESET_CLIP: |
| 226 |
// For Android, an emulated "replace" clip op appears as a manual reset followed by |
| 227 |
// an intersect operation (equivalent to the above handling of replace ops encountered |
| 228 |
// in old serialized pictures). |
| 229 |
SkCanvasPriv::ResetClip(canvas); |
| 230 |
break; |
| 231 |
case PUSH_CULL: break; // Deprecated, safe to ignore both push and pop. |
| 232 |
case POP_CULL: break; |
| 233 |
case CONCAT: { |
| 234 |
SkMatrix matrix; |
| 235 |
reader->readMatrix(&matrix); |
| 236 |
BREAK_ON_READ_ERROR(reader); |
| 237 |
|
| 238 |
canvas->concat(matrix); |
| 239 |
break; |
| 240 |
} |
| 241 |
case CONCAT44: { |
| 242 |
const SkScalar* colMaj = reader->skipT<SkScalar>(16); |
| 243 |
BREAK_ON_READ_ERROR(reader); |
| 244 |
canvas->concat(SkM44::ColMajor(colMaj)); |
| 245 |
break; |
| 246 |
} |
| 247 |
case DRAW_ANNOTATION: { |
| 248 |
SkRect rect; |
| 249 |
reader->readRect(&rect); |
| 250 |
SkString key; |
| 251 |
reader->readString(&key); |
| 252 |
sk_sp<SkData> data = reader->readByteArrayAsData(); |
| 253 |
BREAK_ON_READ_ERROR(reader); |
| 254 |
SkASSERT(data); |
| 255 |
|
| 256 |
canvas->drawAnnotation(rect, key.c_str(), data.get()); |
| 257 |
} break; |
| 258 |
case DRAW_ARC: { |
| 259 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 260 |
SkRect rect; |
| 261 |
reader->readRect(&rect); |
| 262 |
SkScalar startAngle = reader->readScalar(); |
| 263 |
SkScalar sweepAngle = reader->readScalar(); |
| 264 |
int useCenter = reader->readInt(); |
| 265 |
BREAK_ON_READ_ERROR(reader); |
| 266 |
|
| 267 |
canvas->drawArc(rect, startAngle, sweepAngle, SkToBool(useCenter), paint); |
| 268 |
} break; |
| 269 |
case DRAW_ATLAS: { |
| 270 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 271 |
const SkImage* atlas = fPictureData->getImage(reader); |
| 272 |
const uint32_t flags = reader->readUInt(); |
| 273 |
const size_t count = reader->readUInt(); |
| 274 |
const SkRSXform* xform = (const SkRSXform*)reader->skip(count, sizeof(SkRSXform)); |
| 275 |
const SkRect* tex = (const SkRect*)reader->skip(count, sizeof(SkRect)); |
| 276 |
const SkColor* colors = nullptr; |
| 277 |
SkBlendMode mode = SkBlendMode::kDst; |
| 278 |
if (flags & DRAW_ATLAS_HAS_COLORS) { |
| 279 |
colors = (const SkColor*)reader->skip(count, sizeof(SkColor)); |
| 280 |
mode = reader->read32LE(SkBlendMode::kLastMode); |
| 281 |
BREAK_ON_READ_ERROR(reader); |
| 282 |
} |
| 283 |
const SkRect* cull = nullptr; |
| 284 |
if (flags & DRAW_ATLAS_HAS_CULL) { |
| 285 |
cull = (const SkRect*)reader->skip(sizeof(SkRect)); |
| 286 |
} |
| 287 |
BREAK_ON_READ_ERROR(reader); |
| 288 |
|
| 289 |
SkSamplingOptions sampling; |
| 290 |
if (flags & DRAW_ATLAS_HAS_SAMPLING) { |
| 291 |
sampling = reader->readSampling(); |
| 292 |
BREAK_ON_READ_ERROR(reader); |
| 293 |
} |
| 294 |
canvas->drawAtlas(atlas, |
| 295 |
{xform, count}, |
| 296 |
{tex, count}, |
| 297 |
{colors, colors ? count : 0}, |
| 298 |
mode, sampling, cull, paint); |
| 299 |
} break; |
| 300 |
case DRAW_CLEAR: { |
| 301 |
auto c = reader->readInt(); |
| 302 |
BREAK_ON_READ_ERROR(reader); |
| 303 |
|
| 304 |
canvas->clear(c); |
| 305 |
} break; |
| 306 |
case DRAW_DATA: { |
| 307 |
// This opcode is now dead, just need to skip it for backwards compatibility |
| 308 |
size_t length = reader->readInt(); |
| 309 |
(void)reader->skip(length); |
| 310 |
// skip handles padding the read out to a multiple of 4 |
| 311 |
} break; |
| 312 |
case DRAW_DRAWABLE: { |
| 313 |
auto* d = fPictureData->getDrawable(reader); |
| 314 |
BREAK_ON_READ_ERROR(reader); |
| 315 |
|
| 316 |
canvas->drawDrawable(d); |
| 317 |
} break; |
| 318 |
case DRAW_DRAWABLE_MATRIX: { |
| 319 |
SkMatrix matrix; |
| 320 |
reader->readMatrix(&matrix); |
| 321 |
SkDrawable* drawable = fPictureData->getDrawable(reader); |
| 322 |
BREAK_ON_READ_ERROR(reader); |
| 323 |
|
| 324 |
canvas->drawDrawable(drawable, &matrix); |
| 325 |
} break; |
| 326 |
case DRAW_DRRECT: { |
| 327 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 328 |
SkRRect outer, inner; |
| 329 |
reader->readRRect(&outer); |
| 330 |
reader->readRRect(&inner); |
| 331 |
BREAK_ON_READ_ERROR(reader); |
| 332 |
|
| 333 |
canvas->drawDRRect(outer, inner, paint); |
| 334 |
} break; |
| 335 |
case DRAW_EDGEAA_QUAD: { |
| 336 |
SkRect rect; |
| 337 |
reader->readRect(&rect); |
| 338 |
SkCanvas::QuadAAFlags aaFlags = static_cast<SkCanvas::QuadAAFlags>(reader->read32()); |
| 339 |
SkColor4f color; |
| 340 |
reader->readColor4f(&color); |
| 341 |
SkBlendMode blend = reader->read32LE(SkBlendMode::kLastMode); |
| 342 |
BREAK_ON_READ_ERROR(reader); |
| 343 |
bool hasClip = reader->readInt(); |
| 344 |
const SkPoint* clip = nullptr; |
| 345 |
if (hasClip) { |
| 346 |
clip = (const SkPoint*) reader->skip(4, sizeof(SkPoint)); |
| 347 |
} |
| 348 |
BREAK_ON_READ_ERROR(reader); |
| 349 |
canvas->experimental_DrawEdgeAAQuad(rect, clip, aaFlags, color, blend); |
| 350 |
} break; |
| 351 |
case DRAW_EDGEAA_IMAGE_SET: |
| 352 |
case DRAW_EDGEAA_IMAGE_SET2: { |
| 353 |
static const size_t kEntryReadSize = |
| 354 |
4 * sizeof(uint32_t) + 2 * sizeof(SkRect) + sizeof(SkScalar); |
| 355 |
static const size_t kMatrixSize = 9 * sizeof(SkScalar); // != sizeof(SkMatrix) |
| 356 |
|
| 357 |
int cnt = reader->readInt(); |
| 358 |
if (!reader->validate(cnt >= 0)) { |
| 359 |
break; |
| 360 |
} |
| 361 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 362 |
|
| 363 |
SkSamplingOptions sampling; |
| 364 |
if (op == DRAW_EDGEAA_IMAGE_SET2) { |
| 365 |
sampling = reader->readSampling(); |
| 366 |
} else { |
| 367 |
sampling = SkSamplingOptions(SkFilterMode::kNearest); |
| 368 |
} |
| 369 |
|
| 370 |
SkCanvas::SrcRectConstraint constraint = |
| 371 |
reader->checkRange(SkCanvas::kStrict_SrcRectConstraint, |
| 372 |
SkCanvas::kFast_SrcRectConstraint); |
| 373 |
|
| 374 |
if (!reader->validate(SkSafeMath::Mul(cnt, kEntryReadSize) <= reader->available())) { |
| 375 |
break; |
| 376 |
} |
| 377 |
|
| 378 |
// Track minimum necessary clip points and matrices that must be provided to satisfy |
| 379 |
// the entries. |
| 380 |
int expectedClipPointCount = 0; |
| 381 |
int maxMatrixIndex = -1; |
| 382 |
AutoTArray<SkCanvas::ImageSetEntry> set(cnt); |
| 383 |
for (int i = 0; i < cnt && reader->isValid(); ++i) { |
| 384 |
set[i].fImage = sk_ref_sp(fPictureData->getImage(reader)); |
| 385 |
reader->readRect(&set[i].fSrcRect); |
| 386 |
reader->readRect(&set[i].fDstRect); |
| 387 |
set[i].fMatrixIndex = reader->readInt(); |
| 388 |
set[i].fAlpha = reader->readScalar(); |
| 389 |
set[i].fAAFlags = reader->readUInt(); |
| 390 |
set[i].fHasClip = reader->readInt(); |
| 391 |
|
| 392 |
expectedClipPointCount += set[i].fHasClip ? 4 : 0; // 4 points per clip quad |
| 393 |
if (set[i].fMatrixIndex > maxMatrixIndex) { |
| 394 |
maxMatrixIndex = set[i].fMatrixIndex; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
int dstClipPointCount = reader->readInt(); |
| 399 |
const SkPoint* dstClips = nullptr; |
| 400 |
if (!reader->validate(dstClipPointCount >= 0) || |
| 401 |
!reader->validate(expectedClipPointCount == dstClipPointCount)) { |
| 402 |
// A bad dstClipCount (either negative, or not enough to satisfy entries). |
| 403 |
// Use exact comparison; the serialized SKP should only have included the exact |
| 404 |
// amount used by the recorded draw, even if the original array was larger. |
| 405 |
break; |
| 406 |
} else if (dstClipPointCount > 0) { |
| 407 |
dstClips = (const SkPoint*) reader->skip(dstClipPointCount, sizeof(SkPoint)); |
| 408 |
if (dstClips == nullptr) { |
| 409 |
// Not enough bytes remaining so the reader has been invalidated |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
int matrixCount = reader->readInt(); |
| 414 |
if (!reader->validate(matrixCount >= 0) || |
| 415 |
!reader->validate(maxMatrixIndex == (matrixCount - 1)) || |
| 416 |
!reader->validate( |
| 417 |
SkSafeMath::Mul(matrixCount, kMatrixSize) <= reader->available())) { |
| 418 |
// Entries access out-of-bound matrix indices, given provided matrices or |
| 419 |
// there aren't enough bytes to provide that many matrices |
| 420 |
break; |
| 421 |
} |
| 422 |
TArray<SkMatrix> matrices(matrixCount); |
| 423 |
for (int i = 0; i < matrixCount && reader->isValid(); ++i) { |
| 424 |
reader->readMatrix(&matrices.push_back()); |
| 425 |
} |
| 426 |
BREAK_ON_READ_ERROR(reader); |
| 427 |
|
| 428 |
canvas->experimental_DrawEdgeAAImageSet(set.get(), cnt, dstClips, matrices.begin(), |
| 429 |
sampling, paint, constraint); |
| 430 |
} break; |
| 431 |
case DRAW_IMAGE: { |
| 432 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 433 |
const SkImage* image = fPictureData->getImage(reader); |
| 434 |
SkPoint loc; |
| 435 |
reader->readPoint(&loc); |
| 436 |
BREAK_ON_READ_ERROR(reader); |
| 437 |
|
| 438 |
canvas->drawImage(image, loc.fX, loc.fY, |
| 439 |
SkSamplingOptions(SkFilterMode::kNearest), |
| 440 |
paint); |
| 441 |
} break; |
| 442 |
case DRAW_IMAGE2: { |
| 443 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 444 |
const SkImage* image = fPictureData->getImage(reader); |
| 445 |
SkPoint loc; |
| 446 |
reader->readPoint(&loc); |
| 447 |
SkSamplingOptions sampling = reader->readSampling(); |
| 448 |
BREAK_ON_READ_ERROR(reader); |
| 449 |
|
| 450 |
canvas->drawImage(image, loc.fX, loc.fY, sampling, paint); |
| 451 |
} break; |
| 452 |
case DRAW_IMAGE_LATTICE: { |
| 453 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 454 |
const SkImage* image = fPictureData->getImage(reader); |
| 455 |
SkCanvas::Lattice lattice; |
| 456 |
(void)SkCanvasPriv::ReadLattice(*reader, &lattice); |
| 457 |
const SkRect* dst = reader->skipT<SkRect>(); |
| 458 |
BREAK_ON_READ_ERROR(reader); |
| 459 |
|
| 460 |
canvas->drawImageLattice(image, lattice, *dst, SkFilterMode::kNearest, paint); |
| 461 |
} break; |
| 462 |
case DRAW_IMAGE_LATTICE2: { |
| 463 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 464 |
const SkImage* image = fPictureData->getImage(reader); |
| 465 |
SkCanvas::Lattice lattice; |
| 466 |
(void)SkCanvasPriv::ReadLattice(*reader, &lattice); |
| 467 |
const SkRect* dst = reader->skipT<SkRect>(); |
| 468 |
SkFilterMode filter = reader->read32LE(SkFilterMode::kLinear); |
| 469 |
BREAK_ON_READ_ERROR(reader); |
| 470 |
|
| 471 |
canvas->drawImageLattice(image, lattice, *dst, filter, paint); |
| 472 |
} break; |
| 473 |
case DRAW_IMAGE_NINE: { |
| 474 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 475 |
const SkImage* image = fPictureData->getImage(reader); |
| 476 |
SkIRect center; |
| 477 |
reader->readIRect(¢er); |
| 478 |
SkRect dst; |
| 479 |
reader->readRect(&dst); |
| 480 |
BREAK_ON_READ_ERROR(reader); |
| 481 |
|
| 482 |
canvas->drawImageNine(image, center, dst, SkFilterMode::kNearest, paint); |
| 483 |
} break; |
| 484 |
case DRAW_IMAGE_RECT: { |
| 485 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 486 |
const SkImage* image = fPictureData->getImage(reader); |
| 487 |
SkRect storage; |
| 488 |
const SkRect* src = get_rect_ptr(reader, &storage); // may be null |
| 489 |
SkRect dst; |
| 490 |
reader->readRect(&dst); // required |
| 491 |
// DRAW_IMAGE_RECT_STRICT assumes this constraint, and doesn't store it |
| 492 |
SkCanvas::SrcRectConstraint constraint = SkCanvas::kStrict_SrcRectConstraint; |
| 493 |
if (DRAW_IMAGE_RECT == op) { |
| 494 |
// newer op-code stores the constraint explicitly |
| 495 |
constraint = reader->checkRange(SkCanvas::kStrict_SrcRectConstraint, |
| 496 |
SkCanvas::kFast_SrcRectConstraint); |
| 497 |
} |
| 498 |
BREAK_ON_READ_ERROR(reader); |
| 499 |
|
| 500 |
auto sampling = SkSamplingOptions(SkFilterMode::kNearest); |
| 501 |
if (src) { |
| 502 |
canvas->drawImageRect(image, *src, dst, sampling, paint, constraint); |
| 503 |
} else { |
| 504 |
canvas->drawImageRect(image, dst, sampling, paint); |
| 505 |
} |
| 506 |
} break; |
| 507 |
case DRAW_IMAGE_RECT2: { |
| 508 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 509 |
const SkImage* image = fPictureData->getImage(reader); |
| 510 |
SkRect src = reader->readRect(); |
| 511 |
SkRect dst = reader->readRect(); |
| 512 |
SkSamplingOptions sampling = reader->readSampling(); |
| 513 |
auto constraint = reader->read32LE(SkCanvas::kFast_SrcRectConstraint); |
| 514 |
BREAK_ON_READ_ERROR(reader); |
| 515 |
|
| 516 |
canvas->drawImageRect(image, src, dst, sampling, paint, constraint); |
| 517 |
} break; |
| 518 |
case DRAW_OVAL: { |
| 519 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 520 |
SkRect rect; |
| 521 |
reader->readRect(&rect); |
| 522 |
BREAK_ON_READ_ERROR(reader); |
| 523 |
|
| 524 |
canvas->drawOval(rect, paint); |
| 525 |
} break; |
| 526 |
case DRAW_PAINT: { |
| 527 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 528 |
BREAK_ON_READ_ERROR(reader); |
| 529 |
|
| 530 |
canvas->drawPaint(paint); |
| 531 |
} break; |
| 532 |
case DRAW_BEHIND_PAINT: { |
| 533 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 534 |
BREAK_ON_READ_ERROR(reader); |
| 535 |
|
| 536 |
SkCanvasPriv::DrawBehind(canvas, paint); |
| 537 |
} break; |
| 538 |
case DRAW_PATCH: { |
| 539 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 540 |
|
| 541 |
const SkPoint* cubics = (const SkPoint*)reader->skip(SkPatchUtils::kNumCtrlPts, |
| 542 |
sizeof(SkPoint)); |
| 543 |
uint32_t flag = reader->readInt(); |
| 544 |
const SkColor* colors = nullptr; |
| 545 |
if (flag & DRAW_VERTICES_HAS_COLORS) { |
| 546 |
colors = (const SkColor*)reader->skip(SkPatchUtils::kNumCorners, sizeof(SkColor)); |
| 547 |
} |
| 548 |
const SkPoint* texCoords = nullptr; |
| 549 |
if (flag & DRAW_VERTICES_HAS_TEXS) { |
| 550 |
texCoords = (const SkPoint*)reader->skip(SkPatchUtils::kNumCorners, |
| 551 |
sizeof(SkPoint)); |
| 552 |
} |
| 553 |
SkBlendMode bmode = SkBlendMode::kModulate; |
| 554 |
if (flag & DRAW_VERTICES_HAS_XFER) { |
| 555 |
unsigned mode = reader->readInt(); |
| 556 |
if (mode <= (unsigned)SkBlendMode::kLastMode) { |
| 557 |
bmode = (SkBlendMode)mode; |
| 558 |
} |
| 559 |
} |
| 560 |
BREAK_ON_READ_ERROR(reader); |
| 561 |
|
| 562 |
canvas->drawPatch(cubics, colors, texCoords, bmode, paint); |
| 563 |
} break; |
| 564 |
case DRAW_PATH: { |
| 565 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 566 |
const auto& path = fPictureData->getPath(reader); |
| 567 |
BREAK_ON_READ_ERROR(reader); |
| 568 |
|
| 569 |
canvas->drawPath(path, paint); |
| 570 |
} break; |
| 571 |
case DRAW_PICTURE: { |
| 572 |
const auto* pic = fPictureData->getPicture(reader); |
| 573 |
BREAK_ON_READ_ERROR(reader); |
| 574 |
|
| 575 |
canvas->drawPicture(pic); |
| 576 |
} break; |
| 577 |
case DRAW_PICTURE_MATRIX_PAINT: { |
| 578 |
const SkPaint* paint = fPictureData->optionalPaint(reader); |
| 579 |
SkMatrix matrix; |
| 580 |
reader->readMatrix(&matrix); |
| 581 |
const SkPicture* pic = fPictureData->getPicture(reader); |
| 582 |
BREAK_ON_READ_ERROR(reader); |
| 583 |
|
| 584 |
canvas->drawPicture(pic, &matrix, paint); |
| 585 |
} break; |
| 586 |
case DRAW_POINTS: { |
| 587 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 588 |
SkCanvas::PointMode mode = reader->checkRange(SkCanvas::kPoints_PointMode, |
| 589 |
SkCanvas::kPolygon_PointMode); |
| 590 |
size_t count = reader->readInt(); |
| 591 |
const SkPoint* pts = (const SkPoint*)reader->skip(count, sizeof(SkPoint)); |
| 592 |
BREAK_ON_READ_ERROR(reader); |
| 593 |
|
| 594 |
canvas->drawPoints(mode, {pts, count}, paint); |
| 595 |
} break; |
| 596 |
case DRAW_RECT: { |
| 597 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 598 |
SkRect rect; |
| 599 |
reader->readRect(&rect); |
| 600 |
BREAK_ON_READ_ERROR(reader); |
| 601 |
|
| 602 |
canvas->drawRect(rect, paint); |
| 603 |
} break; |
| 604 |
case DRAW_REGION: { |
| 605 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 606 |
SkRegion region; |
| 607 |
reader->readRegion(®ion); |
| 608 |
BREAK_ON_READ_ERROR(reader); |
| 609 |
|
| 610 |
canvas->drawRegion(region, paint); |
| 611 |
} break; |
| 612 |
case DRAW_RRECT: { |
| 613 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 614 |
SkRRect rrect; |
| 615 |
reader->readRRect(&rrect); |
| 616 |
BREAK_ON_READ_ERROR(reader); |
| 617 |
|
| 618 |
canvas->drawRRect(rrect, paint); |
| 619 |
} break; |
| 620 |
case DRAW_SHADOW_REC: { |
| 621 |
const auto& path = fPictureData->getPath(reader); |
| 622 |
SkDrawShadowRec rec; |
| 623 |
reader->readPoint3(&rec.fZPlaneParams); |
| 624 |
reader->readPoint3(&rec.fLightPos); |
| 625 |
rec.fLightRadius = reader->readScalar(); |
| 626 |
rec.fAmbientColor = reader->read32(); |
| 627 |
rec.fSpotColor = reader->read32(); |
| 628 |
rec.fFlags = reader->read32(); |
| 629 |
BREAK_ON_READ_ERROR(reader); |
| 630 |
|
| 631 |
canvas->private_draw_shadow_rec(path, rec); |
| 632 |
} break; |
| 633 |
case DRAW_TEXT_BLOB: { |
| 634 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 635 |
const SkTextBlob* blob = fPictureData->getTextBlob(reader); |
| 636 |
SkScalar x = reader->readScalar(); |
| 637 |
SkScalar y = reader->readScalar(); |
| 638 |
BREAK_ON_READ_ERROR(reader); |
| 639 |
|
| 640 |
canvas->drawTextBlob(blob, x, y, paint); |
| 641 |
} break; |
| 642 |
case DRAW_SLUG: { |
| 643 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 644 |
const sktext::gpu::Slug* slug = fPictureData->getSlug(reader); |
| 645 |
BREAK_ON_READ_ERROR(reader); |
| 646 |
|
| 647 |
canvas->drawSlug(slug, paint); |
| 648 |
} break; |
| 649 |
case DRAW_VERTICES_OBJECT: { |
| 650 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 651 |
const SkVertices* vertices = fPictureData->getVertices(reader); |
| 652 |
const int boneCount = reader->readInt(); |
| 653 |
(void)reader->skip(boneCount, sizeof(SkVertices_DeprecatedBone)); |
| 654 |
SkBlendMode bmode = reader->read32LE(SkBlendMode::kLastMode); |
| 655 |
BREAK_ON_READ_ERROR(reader); |
| 656 |
|
| 657 |
if (vertices) { // TODO: read error if vertices == null? |
| 658 |
canvas->drawVertices(vertices, bmode, paint); |
| 659 |
} |
| 660 |
} break; |
| 661 |
case RESTORE: |
| 662 |
canvas->restore(); |
| 663 |
break; |
| 664 |
case ROTATE: { |
| 665 |
auto deg = reader->readScalar(); |
| 666 |
canvas->rotate(deg); |
| 667 |
} break; |
| 668 |
case SAVE: |
| 669 |
canvas->save(); |
| 670 |
break; |
| 671 |
case SAVE_BEHIND: { |
| 672 |
uint32_t flags = reader->readInt(); |
| 673 |
const SkRect* subset = nullptr; |
| 674 |
SkRect storage; |
| 675 |
if (flags & SAVEBEHIND_HAS_SUBSET) { |
| 676 |
reader->readRect(&storage); |
| 677 |
subset = &storage; |
| 678 |
} |
| 679 |
SkCanvasPriv::SaveBehind(canvas, subset); |
| 680 |
} break; |
| 681 |
case SAVE_LAYER_SAVELAYERREC: { |
| 682 |
SkCanvas::SaveLayerRec rec(nullptr, nullptr, nullptr, 0); |
| 683 |
const uint32_t flatFlags = reader->readInt(); |
| 684 |
SkRect bounds; |
| 685 |
skia_private::AutoSTArray<2, sk_sp<SkImageFilter>> filters; |
| 686 |
if (flatFlags & SAVELAYERREC_HAS_BOUNDS) { |
| 687 |
reader->readRect(&bounds); |
| 688 |
rec.fBounds = &bounds; |
| 689 |
} |
| 690 |
if (flatFlags & SAVELAYERREC_HAS_PAINT) { |
| 691 |
rec.fPaint = &fPictureData->requiredPaint(reader); |
| 692 |
} |
| 693 |
if (flatFlags & SAVELAYERREC_HAS_BACKDROP) { |
| 694 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 695 |
rec.fBackdrop = paint.getImageFilter(); |
| 696 |
} |
| 697 |
if (flatFlags & SAVELAYERREC_HAS_FLAGS) { |
| 698 |
rec.fSaveLayerFlags = reader->readInt(); |
| 699 |
} |
| 700 |
if (flatFlags & SAVELAYERREC_HAS_CLIPMASK_OBSOLETE) { |
| 701 |
(void)fPictureData->getImage(reader); |
| 702 |
} |
| 703 |
if (flatFlags & SAVELAYERREC_HAS_CLIPMATRIX_OBSOLETE) { |
| 704 |
SkMatrix clipMatrix_ignored; |
| 705 |
reader->readMatrix(&clipMatrix_ignored); |
| 706 |
} |
| 707 |
if (!reader->isVersionLT(SkPicturePriv::Version::kBackdropScaleFactor) && |
| 708 |
(flatFlags & SAVELAYERREC_HAS_BACKDROP_SCALE)) { |
| 709 |
SkCanvasPriv::SetBackdropScaleFactor(&rec, reader->readScalar()); |
| 710 |
} |
| 711 |
if (!reader->isVersionLT(SkPicturePriv::Version::kMultipleFiltersOnSaveLayer) && |
| 712 |
(flatFlags & SAVELAYERREC_HAS_MULTIPLE_FILTERS)) { |
| 713 |
int filterCount = reader->readUInt(); |
| 714 |
reader->validate(filterCount > 0 && filterCount <= SkCanvas::kMaxFiltersPerLayer); |
| 715 |
BREAK_ON_READ_ERROR(reader); |
| 716 |
filters.reset(filterCount); |
| 717 |
for (int i = 0; i < filterCount; ++i) { |
| 718 |
const SkPaint& paint = fPictureData->requiredPaint(reader); |
| 719 |
filters[i] = paint.refImageFilter(); |
| 720 |
} |
| 721 |
rec.fFilters = filters; |
| 722 |
} |
| 723 |
if (!reader->isVersionLT(SkPicturePriv::Version::kSaveLayerBackdropTileMode) && |
| 724 |
(flatFlags & SAVELAYERREC_HAS_BACKDROP_TILEMODE)) { |
| 725 |
rec.fBackdropTileMode = reader->read32LE(SkTileMode::kLastTileMode); |
| 726 |
} |
| 727 |
BREAK_ON_READ_ERROR(reader); |
| 728 |
|
| 729 |
canvas->saveLayer(rec); |
| 730 |
} break; |
| 731 |
case SCALE: { |
| 732 |
SkScalar sx = reader->readScalar(); |
| 733 |
SkScalar sy = reader->readScalar(); |
| 734 |
canvas->scale(sx, sy); |
| 735 |
} break; |
| 736 |
case SET_M44: { |
| 737 |
SkM44 m; |
| 738 |
reader->read(&m); |
| 739 |
canvas->setMatrix(initialMatrix * m); |
| 740 |
} break; |
| 741 |
case SET_MATRIX: { |
| 742 |
SkMatrix matrix; |
| 743 |
reader->readMatrix(&matrix); |
| 744 |
canvas->setMatrix(initialMatrix * SkM44(matrix)); |
| 745 |
} break; |
| 746 |
case SKEW: { |
| 747 |
SkScalar sx = reader->readScalar(); |
| 748 |
SkScalar sy = reader->readScalar(); |
| 749 |
canvas->skew(sx, sy); |
| 750 |
} break; |
| 751 |
case TRANSLATE: { |
| 752 |
SkScalar dx = reader->readScalar(); |
| 753 |
SkScalar dy = reader->readScalar(); |
| 754 |
canvas->translate(dx, dy); |
| 755 |
} break; |
| 756 |
default: |
| 757 |
reader->validate(false); // unknown op |
| 758 |
break; |
| 759 |
} |
| 760 |
|
| 761 |
#undef BREAK_ON_READ_ERROR |
| 762 |
} |