SkRecords.h 12 KB

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