SkPictureRecord.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2011 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 SkPictureRecord_DEFINED
  8. #define SkPictureRecord_DEFINED
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkCanvasVirtualEnforcer.h"
  11. #include "include/core/SkFlattenable.h"
  12. #include "include/core/SkPicture.h"
  13. #include "include/core/SkVertices.h"
  14. #include "include/private/SkTArray.h"
  15. #include "include/private/SkTDArray.h"
  16. #include "include/private/SkTHash.h"
  17. #include "include/private/SkTo.h"
  18. #include "src/core/SkPictureData.h"
  19. #include "src/core/SkWriter32.h"
  20. // These macros help with packing and unpacking a single byte value and
  21. // a 3 byte value into/out of a uint32_t
  22. #define MASK_24 0x00FFFFFF
  23. #define UNPACK_8_24(combined, small, large) \
  24. small = (combined >> 24) & 0xFF; \
  25. large = combined & MASK_24
  26. #define PACK_8_24(small, large) ((small << 24) | large)
  27. class SkPictureRecord : public SkCanvasVirtualEnforcer<SkCanvas> {
  28. public:
  29. SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags);
  30. const SkTArray<sk_sp<const SkPicture>>& getPictures() const {
  31. return fPictures;
  32. }
  33. const SkTArray<sk_sp<SkDrawable>>& getDrawables() const {
  34. return fDrawables;
  35. }
  36. const SkTArray<sk_sp<const SkTextBlob>>& getTextBlobs() const {
  37. return fTextBlobs;
  38. }
  39. const SkTArray<sk_sp<const SkVertices>>& getVertices() const {
  40. return fVertices;
  41. }
  42. const SkTArray<sk_sp<const SkImage>>& getImages() const {
  43. return fImages;
  44. }
  45. sk_sp<SkData> opData() const {
  46. this->validate(fWriter.bytesWritten(), 0);
  47. if (fWriter.bytesWritten() == 0) {
  48. return SkData::MakeEmpty();
  49. }
  50. return fWriter.snapshotAsData();
  51. }
  52. void setFlags(uint32_t recordFlags) {
  53. fRecordFlags = recordFlags;
  54. }
  55. const SkWriter32& writeStream() const {
  56. return fWriter;
  57. }
  58. void beginRecording();
  59. void endRecording();
  60. protected:
  61. void addNoOp();
  62. private:
  63. void handleOptimization(int opt);
  64. size_t recordRestoreOffsetPlaceholder(SkClipOp);
  65. void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset);
  66. SkTDArray<int32_t> fRestoreOffsetStack;
  67. SkTDArray<uint32_t> fCullOffsetStack;
  68. /*
  69. * Write the 'drawType' operation and chunk size to the skp. 'size'
  70. * can potentially be increased if the chunk size needs its own storage
  71. * location (i.e., it overflows 24 bits).
  72. * Returns the start offset of the chunk. This is the location at which
  73. * the opcode & size are stored.
  74. * TODO: since we are handing the size into here we could call reserve
  75. * and then return a pointer to the memory storage. This could decrease
  76. * allocation overhead but could lead to more wasted space (the tail
  77. * end of blocks could go unused). Possibly add a second addDraw that
  78. * operates in this manner.
  79. */
  80. size_t addDraw(DrawType drawType, size_t* size) {
  81. size_t offset = fWriter.bytesWritten();
  82. this->predrawNotify();
  83. SkASSERT(0 != *size);
  84. SkASSERT(((uint8_t) drawType) == drawType);
  85. if (0 != (*size & ~MASK_24) || *size == MASK_24) {
  86. fWriter.writeInt(PACK_8_24(drawType, MASK_24));
  87. *size += 1;
  88. fWriter.writeInt(SkToU32(*size));
  89. } else {
  90. fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size)));
  91. }
  92. return offset;
  93. }
  94. void addInt(int value) {
  95. fWriter.writeInt(value);
  96. }
  97. void addScalar(SkScalar scalar) {
  98. fWriter.writeScalar(scalar);
  99. }
  100. void addImage(const SkImage*);
  101. void addMatrix(const SkMatrix& matrix);
  102. void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); }
  103. void addPaintPtr(const SkPaint* paint);
  104. void addPatch(const SkPoint cubics[12]);
  105. void addPath(const SkPath& path);
  106. void addPicture(const SkPicture* picture);
  107. void addDrawable(SkDrawable* picture);
  108. void addPoint(const SkPoint& point);
  109. void addPoints(const SkPoint pts[], int count);
  110. void addRect(const SkRect& rect);
  111. void addRectPtr(const SkRect* rect);
  112. void addIRect(const SkIRect& rect);
  113. void addIRectPtr(const SkIRect* rect);
  114. void addRRect(const SkRRect&);
  115. void addRegion(const SkRegion& region);
  116. void addText(const void* text, size_t byteLength);
  117. void addTextBlob(const SkTextBlob* blob);
  118. void addVertices(const SkVertices*);
  119. int find(const SkBitmap& bitmap);
  120. protected:
  121. void validate(size_t initialOffset, size_t size) const {
  122. SkASSERT(fWriter.bytesWritten() == initialOffset + size);
  123. }
  124. sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
  125. bool onPeekPixels(SkPixmap*) override { return false; }
  126. void onFlush() override;
  127. void willSave() override;
  128. SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
  129. bool onDoSaveBehind(const SkRect*) override;
  130. void willRestore() override;
  131. void didConcat(const SkMatrix&) override;
  132. void didSetMatrix(const SkMatrix&) override;
  133. void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
  134. void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
  135. const SkPaint& paint) override;
  136. void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
  137. const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override;
  138. void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
  139. SkBlendMode, const SkRect*, const SkPaint*) override;
  140. void onDrawPaint(const SkPaint&) override;
  141. void onDrawBehind(const SkPaint&) override;
  142. void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
  143. void onDrawRect(const SkRect&, const SkPaint&) override;
  144. void onDrawRegion(const SkRegion&, const SkPaint&) override;
  145. void onDrawOval(const SkRect&, const SkPaint&) override;
  146. void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
  147. void onDrawRRect(const SkRRect&, const SkPaint&) override;
  148. void onDrawPath(const SkPath&, const SkPaint&) override;
  149. void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
  150. void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
  151. const SkPaint*, SrcRectConstraint) override;
  152. void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
  153. const SkPaint*) override;
  154. void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst,
  155. const SkPaint*) override;
  156. void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
  157. void onDrawVerticesObject(const SkVertices*, const SkVertices::Bone bones[], int boneCount,
  158. SkBlendMode, const SkPaint&) override;
  159. void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
  160. void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
  161. void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
  162. void onClipRegion(const SkRegion&, SkClipOp) override;
  163. void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
  164. void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
  165. void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
  166. void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, SkColor,
  167. SkBlendMode) override;
  168. void onDrawEdgeAAImageSet(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
  169. const SkPaint*, SrcRectConstraint) override;
  170. int addPathToHeap(const SkPath& path); // does not write to ops stream
  171. // These entry points allow the writing of matrices, clips, saves &
  172. // restores to be deferred (e.g., if the MC state is being collapsed and
  173. // only written out as needed).
  174. void recordConcat(const SkMatrix& matrix);
  175. void recordTranslate(const SkMatrix& matrix);
  176. void recordScale(const SkMatrix& matrix);
  177. size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA);
  178. size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA);
  179. size_t recordClipPath(int pathID, SkClipOp op, bool doAA);
  180. size_t recordClipRegion(const SkRegion& region, SkClipOp op);
  181. void recordSave();
  182. void recordSaveLayer(const SaveLayerRec&);
  183. void recordRestore(bool fillInSkips = true);
  184. // SHOULD NEVER BE CALLED
  185. void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override {
  186. SK_ABORT("not reached");
  187. }
  188. void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
  189. SrcRectConstraint) override {
  190. SK_ABORT("not reached");
  191. }
  192. void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
  193. const SkPaint*) override {
  194. SK_ABORT("not reached");
  195. }
  196. void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst,
  197. const SkPaint*) override {
  198. SK_ABORT("not reached");
  199. }
  200. private:
  201. SkTArray<SkPaint> fPaints;
  202. struct PathHash {
  203. uint32_t operator()(const SkPath& p) { return p.getGenerationID(); }
  204. };
  205. SkTHashMap<SkPath, int, PathHash> fPaths;
  206. SkWriter32 fWriter;
  207. SkTArray<sk_sp<const SkImage>> fImages;
  208. SkTArray<sk_sp<const SkPicture>> fPictures;
  209. SkTArray<sk_sp<SkDrawable>> fDrawables;
  210. SkTArray<sk_sp<const SkTextBlob>> fTextBlobs;
  211. SkTArray<sk_sp<const SkVertices>> fVertices;
  212. uint32_t fRecordFlags;
  213. int fInitialSaveCount;
  214. friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor
  215. typedef SkCanvasVirtualEnforcer<SkCanvas> INHERITED;
  216. };
  217. #endif