| 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 |
|
| 8 |
#ifndef SkRecords_DEFINED |
| 9 |
#define SkRecords_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkBlender.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImage.h" |
| 16 |
#include "include/core/SkImageFilter.h" |
| 17 |
#include "include/core/SkM44.h" |
| 18 |
#include "include/core/SkMatrix.h" |
| 19 |
#include "include/core/SkMesh.h" |
| 20 |
#include "include/core/SkPaint.h" |
| 21 |
#include "include/core/SkPath.h" |
| 22 |
#include "include/core/SkPicture.h" |
| 23 |
#include "include/core/SkRRect.h" |
| 24 |
#include "include/core/SkRect.h" |
| 25 |
#include "include/core/SkRefCnt.h" |
| 26 |
#include "include/core/SkRegion.h" |
| 27 |
#include "include/core/SkSamplingOptions.h" |
| 28 |
#include "include/core/SkScalar.h" |
| 29 |
#include "include/core/SkShader.h" |
| 30 |
#include "include/core/SkString.h" |
| 31 |
#include "include/core/SkTextBlob.h" |
| 32 |
#include "include/core/SkVertices.h" |
| 33 |
#include "include/private/base/SkTemplates.h" |
| 34 |
#include "include/private/chromium/Slug.h" |
| 35 |
#include "src/core/SkDrawShadowInfo.h" |
| 36 |
|
| 37 |
#include <cstdint> |
| 38 |
|
| 39 |
enum class SkBlendMode; |
| 40 |
enum class SkClipOp; |
| 41 |
enum class SkTileMode; |
| 42 |
struct SkPoint; |
| 43 |
struct SkRSXform; |
| 44 |
|
| 45 |
namespace SkRecords { |
| 46 |
|
| 47 |
// A list of all the types of canvas calls we can record. |
| 48 |
// Each of these is reified into a struct below. |
| 49 |
// |
| 50 |
// (We're using the macro-of-macro trick here to do several different things with the same list.) |
| 51 |
// |
| 52 |
// We leave this SK_RECORD_TYPES macro defined for use by code that wants to operate on SkRecords |
| 53 |
// types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.) |
| 54 |
// |
| 55 |
// Order doesn't technically matter here, but the compiler can generally generate better code if |
| 56 |
// you keep them semantically grouped, especially the Draws. It's also nice to leave NoOp at 0. |
| 57 |
#define SK_RECORD_TYPES(M) \ |
| 58 |
M(NoOp) \ |
| 59 |
M(Restore) \ |
| 60 |
M(Save) \ |
| 61 |
M(SaveLayer) \ |
| 62 |
M(SaveBehind) \ |
| 63 |
M(SetMatrix) \ |
| 64 |
M(SetM44) \ |
| 65 |
M(Translate) \ |
| 66 |
M(Scale) \ |
| 67 |
M(Concat) \ |
| 68 |
M(Concat44) \ |
| 69 |
M(ClipPath) \ |
| 70 |
M(ClipRRect) \ |
| 71 |
M(ClipRect) \ |
| 72 |
M(ClipRegion) \ |
| 73 |
M(ClipShader) \ |
| 74 |
M(ResetClip) \ |
| 75 |
M(DrawArc) \ |
| 76 |
M(DrawDrawable) \ |
| 77 |
M(DrawImage) \ |
| 78 |
M(DrawImageLattice) \ |
| 79 |
M(DrawImageRect) \ |
| 80 |
M(DrawDRRect) \ |
| 81 |
M(DrawOval) \ |
| 82 |
M(DrawBehind) \ |
| 83 |
M(DrawPaint) \ |
| 84 |
M(DrawPath) \ |
| 85 |
M(DrawPatch) \ |
| 86 |
M(DrawPicture) \ |
| 87 |
M(DrawPoints) \ |
| 88 |
M(DrawRRect) \ |
| 89 |
M(DrawRect) \ |
| 90 |
M(DrawRegion) \ |
| 91 |
M(DrawTextBlob) \ |
| 92 |
M(DrawSlug) \ |
| 93 |
M(DrawAtlas) \ |
| 94 |
M(DrawVertices) \ |
| 95 |
M(DrawMesh) \ |
| 96 |
M(DrawShadowRec) \ |
| 97 |
M(DrawAnnotation) \ |
| 98 |
M(DrawEdgeAAQuad) \ |
| 99 |
M(DrawEdgeAAImageSet) |
| 100 |
|
| 101 |
|
| 102 |
// Defines SkRecords::Type, an enum of all record types. |
| 103 |
#define ENUM(T) T##_Type, |
| 104 |
enum Type { SK_RECORD_TYPES(ENUM) }; |
| 105 |
#undef ENUM |
| 106 |
|
| 107 |
#define ACT_AS_PTR(ptr) \ |
| 108 |
operator T*() const { return ptr; } \ |
| 109 |
T* operator->() const { return ptr; } \ |
| 110 |
T* data() const { return ptr; } |
| 111 |
|
| 112 |
// An Optional doesn't own the pointer's memory, but may need to destroy non-POD data. |
| 113 |
template <typename T> |
| 114 |
class Optional { |
| 115 |
public: |
| 116 |
Optional() : fPtr(nullptr) {} |
| 117 |
Optional(T* ptr) : fPtr(ptr) {} |
| 118 |
Optional(Optional&& o) : fPtr(o.fPtr) { |
| 119 |
o.fPtr = nullptr; |
| 120 |
} |
| 121 |
~Optional() { if (fPtr) fPtr->~T(); } |
| 122 |
|
| 123 |
ACT_AS_PTR(fPtr) |
| 124 |
private: |
| 125 |
T* fPtr; |
| 126 |
Optional(const Optional&) = delete; |
| 127 |
Optional& operator=(const Optional&) = delete; |
| 128 |
}; |
| 129 |
|
| 130 |
// PODArray doesn't own the pointer's memory, and we assume the data is POD. |
| 131 |
template <typename T> |
| 132 |
class PODArray { |
| 133 |
public: |
| 134 |
PODArray() {} |
| 135 |
PODArray(T* ptr) : fPtr(ptr) {} |
| 136 |
// Default copy and assign. |
| 137 |
|
| 138 |
ACT_AS_PTR(fPtr) |
| 139 |
private: |
| 140 |
T* fPtr; |
| 141 |
}; |
| 142 |
|
| 143 |
#undef ACT_AS_PTR |
| 144 |
|
| 145 |
// SkPath::getBounds() isn't thread safe unless we precache the bounds in a singlethreaded context. |
| 146 |
// SkPath::cheapComputeDirection() is similar. |
| 147 |
// Recording is a convenient time to cache these, or we can delay it to between record and playback. |
| 148 |
struct PreCachedPath : public SkPath { |
| 149 |
PreCachedPath() {} |
| 150 |
PreCachedPath(const SkPath& path); |
| 151 |
}; |
| 152 |
|
| 153 |
// Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we precache it. |
| 154 |
// This may not cover all SkMatrices used by the picture (e.g. some could be hiding in a shader). |
| 155 |
struct TypedMatrix : public SkMatrix { |
| 156 |
TypedMatrix() {} |
| 157 |
TypedMatrix(const SkMatrix& matrix); |
| 158 |
}; |
| 159 |
|
| 160 |
enum Tags { |
| 161 |
kDraw_Tag = 1, // May draw something (usually named DrawFoo). |
| 162 |
kHasImage_Tag = 2, // Contains an SkImage or SkBitmap. |
| 163 |
kHasText_Tag = 4, // Contains text. |
| 164 |
kHasPaint_Tag = 8, // May have an SkPaint field, at least optionally. |
| 165 |
kMultiDraw_Tag = 16, // Drawing operations that render multiple independent primitives. |
| 166 |
// These draws are capable of blending with themselves. |
| 167 |
|
| 168 |
kDrawWithPaint_Tag = kDraw_Tag | kHasPaint_Tag, |
| 169 |
}; |
| 170 |
|
| 171 |
// A macro to make it a little easier to define a struct that can be stored in SkRecord. |
| 172 |
#define RECORD(T, tags, ...) \ |
| 173 |
struct T { \ |
| 174 |
static const Type kType = T##_Type; \ |
| 175 |
static const int kTags = tags; \ |
| 176 |
__VA_ARGS__; \ |
| 177 |
}; |
| 178 |
|
| 179 |
#define RECORD_TRIVIAL(T, tags) \ |
| 180 |
struct T { \ |
| 181 |
static const Type kType = T##_Type; \ |
| 182 |
static const int kTags = tags; \ |
| 183 |
}; |
| 184 |
|
| 185 |
RECORD_TRIVIAL(NoOp, 0) |
| 186 |
RECORD(Restore, 0, |
| 187 |
TypedMatrix matrix) |
| 188 |
RECORD_TRIVIAL(Save, 0) |
| 189 |
|
| 190 |
RECORD(SaveLayer, kHasPaint_Tag, |
| 191 |
Optional<SkRect> bounds; |
| 192 |
Optional<SkPaint> paint; |
| 193 |
sk_sp<const SkImageFilter> backdrop; |
| 194 |
SkCanvas::SaveLayerFlags saveLayerFlags; |
| 195 |
SkScalar backdropScale; |
| 196 |
SkTileMode backdropTileMode; |
| 197 |
skia_private::AutoTArray<sk_sp<SkImageFilter>> filters) |
| 198 |
|
| 199 |
RECORD(SaveBehind, 0, |
| 200 |
Optional<SkRect> subset) |
| 201 |
|
| 202 |
RECORD(SetMatrix, 0, |
| 203 |
TypedMatrix matrix) |
| 204 |
RECORD(SetM44, 0, |
| 205 |
SkM44 matrix) |
| 206 |
RECORD(Concat, 0, |
| 207 |
TypedMatrix matrix) |
| 208 |
RECORD(Concat44, 0, |
| 209 |
SkM44 matrix) |
| 210 |
|
| 211 |
RECORD(Translate, 0, |
| 212 |
SkScalar dx; |
| 213 |
SkScalar dy) |
| 214 |
|
| 215 |
RECORD(Scale, 0, |
| 216 |
SkScalar sx; |
| 217 |
SkScalar sy) |
| 218 |
|
| 219 |
struct ClipOpAndAA { |
| 220 |
ClipOpAndAA() {} |
| 221 |
ClipOpAndAA(SkClipOp op, bool aa) : fOp(static_cast<unsigned>(op)), fAA(aa) {} |
| 222 |
|
| 223 |
SkClipOp op() const { return static_cast<SkClipOp>(fOp); } |
| 224 |
bool aa() const { return fAA != 0; } |
| 225 |
|
| 226 |
private: |
| 227 |
unsigned fOp : 31; // This really only needs to be 3, but there's no win today to do so. |
| 228 |
unsigned fAA : 1; // MSVC won't pack an enum with an bool, so we call this an unsigned. |
| 229 |
}; |
| 230 |
static_assert(sizeof(ClipOpAndAA) == 4, "ClipOpAndAASize"); |
| 231 |
|
| 232 |
RECORD(ClipPath, 0, |
| 233 |
PreCachedPath path; |
| 234 |
ClipOpAndAA opAA) |
| 235 |
RECORD(ClipRRect, 0, |
| 236 |
SkRRect rrect; |
| 237 |
ClipOpAndAA opAA) |
| 238 |
RECORD(ClipRect, 0, |
| 239 |
SkRect rect; |
| 240 |
ClipOpAndAA opAA) |
| 241 |
RECORD(ClipRegion, 0, |
| 242 |
SkRegion region; |
| 243 |
SkClipOp op) |
| 244 |
RECORD(ClipShader, 0, |
| 245 |
sk_sp<SkShader> shader; |
| 246 |
SkClipOp op) |
| 247 |
RECORD_TRIVIAL(ResetClip, 0) |
| 248 |
|
| 249 |
// While not strictly required, if you have an SkPaint, it's fastest to put it first. |
| 250 |
RECORD(DrawArc, kDraw_Tag|kHasPaint_Tag, |
| 251 |
SkPaint paint; |
| 252 |
SkRect oval; |
| 253 |
SkScalar startAngle; |
| 254 |
SkScalar sweepAngle; |
| 255 |
unsigned useCenter) |
| 256 |
RECORD(DrawDRRect, kDraw_Tag|kHasPaint_Tag, |
| 257 |
SkPaint paint; |
| 258 |
SkRRect outer; |
| 259 |
SkRRect inner) |
| 260 |
RECORD(DrawDrawable, kDraw_Tag, |
| 261 |
Optional<SkMatrix> matrix; |
| 262 |
SkRect worstCaseBounds; |
| 263 |
int32_t index) |
| 264 |
RECORD(DrawImage, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag, |
| 265 |
Optional<SkPaint> paint; |
| 266 |
sk_sp<const SkImage> image; |
| 267 |
SkScalar left; |
| 268 |
SkScalar top; |
| 269 |
SkSamplingOptions sampling) |
| 270 |
RECORD(DrawImageLattice, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag, |
| 271 |
Optional<SkPaint> paint; |
| 272 |
sk_sp<const SkImage> image; |
| 273 |
int xCount; |
| 274 |
PODArray<int> xDivs; |
| 275 |
int yCount; |
| 276 |
PODArray<int> yDivs; |
| 277 |
int flagCount; |
| 278 |
PODArray<SkCanvas::Lattice::RectType> flags; |
| 279 |
PODArray<SkColor> colors; |
| 280 |
SkIRect src; |
| 281 |
SkRect dst; |
| 282 |
SkFilterMode filter) |
| 283 |
RECORD(DrawImageRect, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag, |
| 284 |
Optional<SkPaint> paint; |
| 285 |
sk_sp<const SkImage> image; |
| 286 |
SkRect src; |
| 287 |
SkRect dst; |
| 288 |
SkSamplingOptions sampling; |
| 289 |
SkCanvas::SrcRectConstraint constraint) |
| 290 |
RECORD(DrawOval, kDraw_Tag|kHasPaint_Tag, |
| 291 |
SkPaint paint; |
| 292 |
SkRect oval) |
| 293 |
RECORD(DrawPaint, kDraw_Tag|kHasPaint_Tag, |
| 294 |
SkPaint paint) |
| 295 |
RECORD(DrawBehind, kDraw_Tag|kHasPaint_Tag, |
| 296 |
SkPaint paint) |
| 297 |
RECORD(DrawPath, kDraw_Tag|kHasPaint_Tag, |
| 298 |
SkPaint paint; |
| 299 |
PreCachedPath path) |
| 300 |
RECORD(DrawPicture, kDraw_Tag|kHasPaint_Tag, |
| 301 |
Optional<SkPaint> paint; |
| 302 |
sk_sp<const SkPicture> picture; |
| 303 |
TypedMatrix matrix) |
| 304 |
RECORD(DrawPoints, kDraw_Tag|kHasPaint_Tag|kMultiDraw_Tag, |
| 305 |
SkPaint paint; |
| 306 |
SkCanvas::PointMode mode; |
| 307 |
unsigned count; |
| 308 |
PODArray<SkPoint> pts) |
| 309 |
RECORD(DrawRRect, kDraw_Tag|kHasPaint_Tag, |
| 310 |
SkPaint paint; |
| 311 |
SkRRect rrect) |
| 312 |
RECORD(DrawRect, kDraw_Tag|kHasPaint_Tag, |
| 313 |
SkPaint paint; |
| 314 |
SkRect rect) |
| 315 |
RECORD(DrawRegion, kDraw_Tag|kHasPaint_Tag, |
| 316 |
SkPaint paint; |
| 317 |
SkRegion region) |
| 318 |
RECORD(DrawTextBlob, kDraw_Tag|kHasText_Tag|kHasPaint_Tag, |
| 319 |
SkPaint paint; |
| 320 |
sk_sp<const SkTextBlob> blob; |
| 321 |
SkScalar x; |
| 322 |
SkScalar y) |
| 323 |
RECORD(DrawSlug, kDraw_Tag|kHasText_Tag|kHasPaint_Tag, |
| 324 |
SkPaint paint; |
| 325 |
sk_sp<const sktext::gpu::Slug> slug) |
| 326 |
RECORD(DrawPatch, kDraw_Tag|kHasPaint_Tag, |
| 327 |
SkPaint paint; |
| 328 |
PODArray<SkPoint> cubics; |
| 329 |
PODArray<SkColor> colors; |
| 330 |
PODArray<SkPoint> texCoords; |
| 331 |
SkBlendMode bmode) |
| 332 |
RECORD(DrawAtlas, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag|kMultiDraw_Tag, |
| 333 |
Optional<SkPaint> paint; |
| 334 |
sk_sp<const SkImage> atlas; |
| 335 |
PODArray<SkRSXform> xforms; |
| 336 |
PODArray<SkRect> texs; |
| 337 |
PODArray<SkColor> colors; |
| 338 |
unsigned count; |
| 339 |
SkBlendMode mode; |
| 340 |
SkSamplingOptions sampling; |
| 341 |
Optional<SkRect> cull) |
| 342 |
RECORD(DrawVertices, kDraw_Tag|kHasPaint_Tag|kMultiDraw_Tag, |
| 343 |
SkPaint paint; |
| 344 |
sk_sp<SkVertices> vertices; |
| 345 |
SkBlendMode bmode) |
| 346 |
RECORD(DrawMesh, kDraw_Tag|kHasPaint_Tag|kMultiDraw_Tag, |
| 347 |
SkPaint paint; |
| 348 |
SkMesh mesh; |
| 349 |
sk_sp<SkBlender> blender) |
| 350 |
RECORD(DrawShadowRec, kDraw_Tag, |
| 351 |
PreCachedPath path; |
| 352 |
SkDrawShadowRec rec) |
| 353 |
RECORD(DrawAnnotation, 0, // TODO: kDraw_Tag, skbug.com/40036727 |
| 354 |
SkRect rect; |
| 355 |
SkString key; |
| 356 |
sk_sp<SkData> value) |
| 357 |
RECORD(DrawEdgeAAQuad, kDraw_Tag, |
| 358 |
SkRect rect; |
| 359 |
PODArray<SkPoint> clip; |
| 360 |
SkCanvas::QuadAAFlags aa; |
| 361 |
SkColor4f color; |
| 362 |
SkBlendMode mode) |
| 363 |
RECORD(DrawEdgeAAImageSet, kDraw_Tag|kHasImage_Tag|kHasPaint_Tag|kMultiDraw_Tag, |
| 364 |
Optional<SkPaint> paint; |
| 365 |
skia_private::AutoTArray<SkCanvas::ImageSetEntry> set; |
| 366 |
int count; |
| 367 |
PODArray<SkPoint> dstClips; |
| 368 |
PODArray<SkMatrix> preViewMatrices; |
| 369 |
SkSamplingOptions sampling; |
| 370 |
SkCanvas::SrcRectConstraint constraint) |
| 371 |
#undef RECORD |
| 372 |
|
| 373 |
} // namespace SkRecords |
| 374 |
|
| 375 |
#endif//SkRecords_DEFINED |