| OLD | NEW |
| (Empty) |
| 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 "SkCanvas.h" | |
| 12 #include "SkDrawable.h" | |
| 13 #include "SkMatrix.h" | |
| 14 #include "SkPathPriv.h" | |
| 15 #include "SkPicture.h" | |
| 16 #include "SkRSXform.h" | |
| 17 #include "SkTextBlob.h" | |
| 18 | |
| 19 namespace SkRecords { | |
| 20 | |
| 21 // A list of all the types of canvas calls we can record. | |
| 22 // Each of these is reified into a struct below. | |
| 23 // | |
| 24 // (We're using the macro-of-macro trick here to do several different things wit
h the same list.) | |
| 25 // | |
| 26 // We leave this SK_RECORD_TYPES macro defined for use by code that wants to ope
rate on SkRecords | |
| 27 // types polymorphically. (See SkRecord::Record::{visit,mutate} for an example.
) | |
| 28 // | |
| 29 // Order doesn't technically matter here, but the compiler can generally generat
e better code if | |
| 30 // you keep them semantically grouped, especially the Draws. It's also nice to
leave NoOp at 0. | |
| 31 #define SK_RECORD_TYPES(M) \ | |
| 32 M(NoOp) \ | |
| 33 M(Restore) \ | |
| 34 M(Save) \ | |
| 35 M(SaveLayer) \ | |
| 36 M(SetMatrix) \ | |
| 37 M(ClipPath) \ | |
| 38 M(ClipRRect) \ | |
| 39 M(ClipRect) \ | |
| 40 M(ClipRegion) \ | |
| 41 M(DrawBitmap) \ | |
| 42 M(DrawBitmapNine) \ | |
| 43 M(DrawBitmapRect) \ | |
| 44 M(DrawBitmapRectFast) \ | |
| 45 M(DrawBitmapRectFixedSize) \ | |
| 46 M(DrawDrawable) \ | |
| 47 M(DrawImage) \ | |
| 48 M(DrawImageRect) \ | |
| 49 M(DrawImageNine) \ | |
| 50 M(DrawDRRect) \ | |
| 51 M(DrawOval) \ | |
| 52 M(DrawPaint) \ | |
| 53 M(DrawPath) \ | |
| 54 M(DrawPatch) \ | |
| 55 M(DrawPicture) \ | |
| 56 M(DrawPoints) \ | |
| 57 M(DrawPosText) \ | |
| 58 M(DrawPosTextH) \ | |
| 59 M(DrawText) \ | |
| 60 M(DrawTextOnPath) \ | |
| 61 M(DrawRRect) \ | |
| 62 M(DrawRect) \ | |
| 63 M(DrawSprite) \ | |
| 64 M(DrawTextBlob) \ | |
| 65 M(DrawAtlas) \ | |
| 66 M(DrawVertices) | |
| 67 | |
| 68 // Defines SkRecords::Type, an enum of all record types. | |
| 69 #define ENUM(T) T##_Type, | |
| 70 enum Type { SK_RECORD_TYPES(ENUM) }; | |
| 71 #undef ENUM | |
| 72 | |
| 73 // Macros to make it easier to define a record for a draw call with 0 args, 1 ar
gs, 2 args, etc. | |
| 74 // These should be clearer when you look at their use below. | |
| 75 #define RECORD0(T) \ | |
| 76 struct T { \ | |
| 77 static const Type kType = T##_Type; \ | |
| 78 }; | |
| 79 | |
| 80 // Instead of requring the exact type A here, we take any type Z which implicitl
y casts to A. | |
| 81 // This lets our wrappers like ImmutableBitmap work seamlessly. | |
| 82 | |
| 83 #define RECORD1(T, A, a) \ | |
| 84 struct T { \ | |
| 85 static const Type kType = T##_Type; \ | |
| 86 T() {} \ | |
| 87 template <typename Z> \ | |
| 88 T(const Z& a) : a(a) {} \ | |
| 89 A a; \ | |
| 90 }; | |
| 91 | |
| 92 #define RECORD2(T, A, a, B, b) \ | |
| 93 struct T { \ | |
| 94 static const Type kType = T##_Type; \ | |
| 95 T() {} \ | |
| 96 template <typename Z, typename Y> \ | |
| 97 T(const Z& a, const Y& b) : a(a), b(b) {} \ | |
| 98 A a; B b; \ | |
| 99 }; | |
| 100 | |
| 101 #define RECORD3(T, A, a, B, b, C, c) \ | |
| 102 struct T { \ | |
| 103 static const Type kType = T##_Type; \ | |
| 104 T() {} \ | |
| 105 template <typename Z, typename Y, typename X> \ | |
| 106 T(const Z& a, const Y& b, const X& c) : a(a), b(b), c(c) {} \ | |
| 107 A a; B b; C c; \ | |
| 108 }; | |
| 109 | |
| 110 #define RECORD4(T, A, a, B, b, C, c, D, d)
\ | |
| 111 struct T {
\ | |
| 112 static const Type kType = T##_Type;
\ | |
| 113 T() {}
\ | |
| 114 template <typename Z, typename Y, typename X, typename W>
\ | |
| 115 T(const Z& a, const Y& b, const X& c, const W& d) : a(a), b(b), c(c), d(d) {
} \ | |
| 116 A a; B b; C c; D d;
\ | |
| 117 }; | |
| 118 | |
| 119 #define RECORD5(T, A, a, B, b, C, c, D, d, E, e) \ | |
| 120 struct T { \ | |
| 121 static const Type kType = T##_Type; \ | |
| 122 T() {} \ | |
| 123 template <typename Z, typename Y, typename X, typename W, typename V> \ | |
| 124 T(const Z& a, const Y& b, const X& c, const W& d, const V& e) \ | |
| 125 : a(a), b(b), c(c), d(d), e(e) {} \ | |
| 126 A a; B b; C c; D d; E e; \ | |
| 127 }; | |
| 128 | |
| 129 #define RECORD8(T, A, a, B, b, C, c, D, d, E, e, F, f, G, g, H, h) \ | |
| 130 struct T { \ | |
| 131 static const Type kType = T##_Type; \ | |
| 132 T() {} \ | |
| 133 template <typename Z, typename Y, typename X, typename W, \ | |
| 134 typename V, typename U, typename S, typename R> \ | |
| 135 T(const Z& a, const Y& b, const X& c, const W& d, \ | |
| 136 const V& e, const U& f, const S& g, const R& h) \ | |
| 137 : a(a), b(b), c(c), d(d), e(e), f(f), g(g), h(h) {} \ | |
| 138 A a; B b; C c; D d; E e; F f; G g; H h; \ | |
| 139 }; | |
| 140 | |
| 141 #define ACT_AS_PTR(ptr) \ | |
| 142 operator T*() const { return ptr; } \ | |
| 143 T* operator->() const { return ptr; } | |
| 144 | |
| 145 template <typename T> | |
| 146 class RefBox : SkNoncopyable { | |
| 147 public: | |
| 148 RefBox() {} | |
| 149 RefBox(T* obj) : fObj(SkSafeRef(obj)) {} | |
| 150 ~RefBox() { SkSafeUnref(fObj); } | |
| 151 | |
| 152 ACT_AS_PTR(fObj); | |
| 153 | |
| 154 private: | |
| 155 T* fObj; | |
| 156 }; | |
| 157 | |
| 158 // An Optional doesn't own the pointer's memory, but may need to destroy non-POD
data. | |
| 159 template <typename T> | |
| 160 class Optional : SkNoncopyable { | |
| 161 public: | |
| 162 Optional() : fPtr(nullptr) {} | |
| 163 Optional(T* ptr) : fPtr(ptr) {} | |
| 164 ~Optional() { if (fPtr) fPtr->~T(); } | |
| 165 | |
| 166 ACT_AS_PTR(fPtr); | |
| 167 private: | |
| 168 T* fPtr; | |
| 169 }; | |
| 170 | |
| 171 // Like Optional, but ptr must not be NULL. | |
| 172 template <typename T> | |
| 173 class Adopted : SkNoncopyable { | |
| 174 public: | |
| 175 Adopted(T* ptr) : fPtr(ptr) { SkASSERT(fPtr); } | |
| 176 Adopted(Adopted* source) { | |
| 177 // Transfer ownership from source to this. | |
| 178 fPtr = source->fPtr; | |
| 179 source->fPtr = NULL; | |
| 180 } | |
| 181 ~Adopted() { if (fPtr) fPtr->~T(); } | |
| 182 | |
| 183 ACT_AS_PTR(fPtr); | |
| 184 private: | |
| 185 T* fPtr; | |
| 186 }; | |
| 187 | |
| 188 // PODArray doesn't own the pointer's memory, and we assume the data is POD. | |
| 189 template <typename T> | |
| 190 class PODArray { | |
| 191 public: | |
| 192 PODArray() {} | |
| 193 PODArray(T* ptr) : fPtr(ptr) {} | |
| 194 // Default copy and assign. | |
| 195 | |
| 196 ACT_AS_PTR(fPtr); | |
| 197 private: | |
| 198 T* fPtr; | |
| 199 }; | |
| 200 | |
| 201 #undef ACT_AS_PTR | |
| 202 | |
| 203 // Like SkBitmap, but deep copies pixels if they're not immutable. | |
| 204 // Using this, we guarantee the immutability of all bitmaps we record. | |
| 205 class ImmutableBitmap : SkNoncopyable { | |
| 206 public: | |
| 207 ImmutableBitmap() {} | |
| 208 explicit ImmutableBitmap(const SkBitmap& bitmap) { | |
| 209 if (bitmap.isImmutable()) { | |
| 210 fBitmap = bitmap; | |
| 211 } else { | |
| 212 bitmap.copyTo(&fBitmap); | |
| 213 } | |
| 214 fBitmap.setImmutable(); | |
| 215 } | |
| 216 | |
| 217 int width() const { return fBitmap.width(); } | |
| 218 int height() const { return fBitmap.height(); } | |
| 219 | |
| 220 // While the pixels are immutable, SkBitmap itself is not thread-safe, so re
turn a copy. | |
| 221 SkBitmap shallowCopy() const { return fBitmap; } | |
| 222 private: | |
| 223 SkBitmap fBitmap; | |
| 224 }; | |
| 225 | |
| 226 // SkPath::getBounds() isn't thread safe unless we precache the bounds in a sing
lethreaded context. | |
| 227 // SkPath::cheapComputeDirection() is similar. | |
| 228 // Recording is a convenient time to cache these, or we can delay it to between
record and playback. | |
| 229 struct PreCachedPath : public SkPath { | |
| 230 PreCachedPath() {} | |
| 231 explicit PreCachedPath(const SkPath& path) : SkPath(path) { | |
| 232 this->updateBoundsCache(); | |
| 233 #if 0 // Disabled to see if we ever really race on this. It costs time, chromi
um:496982. | |
| 234 SkPathPriv::FirstDirection junk; | |
| 235 (void)SkPathPriv::CheapComputeFirstDirection(*this, &junk); | |
| 236 #endif | |
| 237 } | |
| 238 }; | |
| 239 | |
| 240 // Like SkPath::getBounds(), SkMatrix::getType() isn't thread safe unless we pre
cache it. | |
| 241 // This may not cover all SkMatrices used by the picture (e.g. some could be hid
ing in a shader). | |
| 242 struct TypedMatrix : public SkMatrix { | |
| 243 TypedMatrix() {} | |
| 244 explicit TypedMatrix(const SkMatrix& matrix) : SkMatrix(matrix) { | |
| 245 (void)this->getType(); | |
| 246 } | |
| 247 }; | |
| 248 | |
| 249 RECORD0(NoOp); | |
| 250 | |
| 251 RECORD2(Restore, SkIRect, devBounds, TypedMatrix, matrix); | |
| 252 RECORD0(Save); | |
| 253 RECORD3(SaveLayer, Optional<SkRect>, bounds, Optional<SkPaint>, paint, SkCanvas:
:SaveFlags, flags); | |
| 254 | |
| 255 RECORD1(SetMatrix, TypedMatrix, matrix); | |
| 256 | |
| 257 struct RegionOpAndAA { | |
| 258 RegionOpAndAA() {} | |
| 259 RegionOpAndAA(SkRegion::Op op, bool aa) : op(op), aa(aa) {} | |
| 260 SkRegion::Op op : 31; // This really only needs to be 3, but there's no win
today to do so. | |
| 261 unsigned aa : 1; // MSVC won't pack an enum with an bool, so we call t
his an unsigned. | |
| 262 }; | |
| 263 SK_COMPILE_ASSERT(sizeof(RegionOpAndAA) == 4, RegionOpAndAASize); | |
| 264 | |
| 265 RECORD3(ClipPath, SkIRect, devBounds, PreCachedPath, path, RegionOpAndAA, opA
A); | |
| 266 RECORD3(ClipRRect, SkIRect, devBounds, SkRRect, rrect, RegionOpAndAA, opA
A); | |
| 267 RECORD3(ClipRect, SkIRect, devBounds, SkRect, rect, RegionOpAndAA, opA
A); | |
| 268 RECORD3(ClipRegion, SkIRect, devBounds, SkRegion, region, SkRegion::Op, o
p); | |
| 269 | |
| 270 // While not strictly required, if you have an SkPaint, it's fastest to put it f
irst. | |
| 271 RECORD4(DrawBitmap, Optional<SkPaint>, paint, | |
| 272 ImmutableBitmap, bitmap, | |
| 273 SkScalar, left, | |
| 274 SkScalar, top); | |
| 275 RECORD4(DrawBitmapNine, Optional<SkPaint>, paint, | |
| 276 ImmutableBitmap, bitmap, | |
| 277 SkIRect, center, | |
| 278 SkRect, dst); | |
| 279 RECORD4(DrawBitmapRect, Optional<SkPaint>, paint, | |
| 280 ImmutableBitmap, bitmap, | |
| 281 Optional<SkRect>, src, | |
| 282 SkRect, dst); | |
| 283 RECORD4(DrawBitmapRectFast, Optional<SkPaint>, paint, | |
| 284 ImmutableBitmap, bitmap, | |
| 285 Optional<SkRect>, src, | |
| 286 SkRect, dst); | |
| 287 RECORD5(DrawBitmapRectFixedSize, SkPaint, paint, | |
| 288 ImmutableBitmap, bitmap, | |
| 289 SkRect, src, | |
| 290 SkRect, dst, | |
| 291 SkCanvas::SrcRectConstraint, constraint); | |
| 292 RECORD3(DrawDRRect, SkPaint, paint, SkRRect, outer, SkRRect, inner); | |
| 293 RECORD3(DrawDrawable, Optional<SkMatrix>, matrix, SkRect, worstCaseBounds, int32
_t, index); | |
| 294 RECORD4(DrawImage, Optional<SkPaint>, paint, | |
| 295 RefBox<const SkImage>, image, | |
| 296 SkScalar, left, | |
| 297 SkScalar, top); | |
| 298 RECORD5(DrawImageRect, Optional<SkPaint>, paint, | |
| 299 RefBox<const SkImage>, image, | |
| 300 Optional<SkRect>, src, | |
| 301 SkRect, dst, | |
| 302 SkCanvas::SrcRectConstraint, constraint); | |
| 303 RECORD4(DrawImageNine, Optional<SkPaint>, paint, | |
| 304 RefBox<const SkImage>, image, | |
| 305 SkIRect, center, | |
| 306 SkRect, dst); | |
| 307 RECORD2(DrawOval, SkPaint, paint, SkRect, oval); | |
| 308 RECORD1(DrawPaint, SkPaint, paint); | |
| 309 RECORD2(DrawPath, SkPaint, paint, PreCachedPath, path); | |
| 310 RECORD3(DrawPicture, Optional<SkPaint>, paint, | |
| 311 RefBox<const SkPicture>, picture, | |
| 312 TypedMatrix, matrix); | |
| 313 RECORD4(DrawPoints, SkPaint, paint, SkCanvas::PointMode, mode, unsigned, count,
SkPoint*, pts); | |
| 314 RECORD4(DrawPosText, SkPaint, paint, | |
| 315 PODArray<char>, text, | |
| 316 size_t, byteLength, | |
| 317 PODArray<SkPoint>, pos); | |
| 318 RECORD5(DrawPosTextH, SkPaint, paint, | |
| 319 PODArray<char>, text, | |
| 320 unsigned, byteLength, | |
| 321 SkScalar, y, | |
| 322 PODArray<SkScalar>, xpos); | |
| 323 RECORD2(DrawRRect, SkPaint, paint, SkRRect, rrect); | |
| 324 RECORD2(DrawRect, SkPaint, paint, SkRect, rect); | |
| 325 RECORD4(DrawSprite, Optional<SkPaint>, paint, ImmutableBitmap, bitmap, int, left
, int, top); | |
| 326 RECORD5(DrawText, SkPaint, paint, | |
| 327 PODArray<char>, text, | |
| 328 size_t, byteLength, | |
| 329 SkScalar, x, | |
| 330 SkScalar, y); | |
| 331 RECORD4(DrawTextBlob, SkPaint, paint, | |
| 332 RefBox<const SkTextBlob>, blob, | |
| 333 SkScalar, x, | |
| 334 SkScalar, y); | |
| 335 RECORD5(DrawTextOnPath, SkPaint, paint, | |
| 336 PODArray<char>, text, | |
| 337 size_t, byteLength, | |
| 338 PreCachedPath, path, | |
| 339 TypedMatrix, matrix); | |
| 340 | |
| 341 RECORD5(DrawPatch, SkPaint, paint, | |
| 342 PODArray<SkPoint>, cubics, | |
| 343 PODArray<SkColor>, colors, | |
| 344 PODArray<SkPoint>, texCoords, | |
| 345 RefBox<SkXfermode>, xmode); | |
| 346 | |
| 347 RECORD8(DrawAtlas, Optional<SkPaint>, paint, | |
| 348 RefBox<const SkImage>, atlas, | |
| 349 PODArray<SkRSXform>, xforms, | |
| 350 PODArray<SkRect>, texs, | |
| 351 PODArray<SkColor>, colors, | |
| 352 int, count, | |
| 353 SkXfermode::Mode, mode, | |
| 354 Optional<SkRect>, cull); | |
| 355 | |
| 356 // This guy is so ugly we just write it manually. | |
| 357 struct DrawVertices { | |
| 358 static const Type kType = DrawVertices_Type; | |
| 359 | |
| 360 DrawVertices(const SkPaint& paint, | |
| 361 SkCanvas::VertexMode vmode, | |
| 362 int vertexCount, | |
| 363 SkPoint* vertices, | |
| 364 SkPoint* texs, | |
| 365 SkColor* colors, | |
| 366 SkXfermode* xmode, | |
| 367 uint16_t* indices, | |
| 368 int indexCount) | |
| 369 : paint(paint) | |
| 370 , vmode(vmode) | |
| 371 , vertexCount(vertexCount) | |
| 372 , vertices(vertices) | |
| 373 , texs(texs) | |
| 374 , colors(colors) | |
| 375 , xmode(SkSafeRef(xmode)) | |
| 376 , indices(indices) | |
| 377 , indexCount(indexCount) {} | |
| 378 | |
| 379 SkPaint paint; | |
| 380 SkCanvas::VertexMode vmode; | |
| 381 int vertexCount; | |
| 382 PODArray<SkPoint> vertices; | |
| 383 PODArray<SkPoint> texs; | |
| 384 PODArray<SkColor> colors; | |
| 385 SkAutoTUnref<SkXfermode> xmode; | |
| 386 PODArray<uint16_t> indices; | |
| 387 int indexCount; | |
| 388 }; | |
| 389 | |
| 390 #undef RECORD0 | |
| 391 #undef RECORD1 | |
| 392 #undef RECORD2 | |
| 393 #undef RECORD3 | |
| 394 #undef RECORD4 | |
| 395 #undef RECORD5 | |
| 396 #undef RECORD8 | |
| 397 | |
| 398 } // namespace SkRecords | |
| 399 | |
| 400 #endif//SkRecords_DEFINED | |
| OLD | NEW |