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