SkPictureFlat.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 SkPictureFlat_DEFINED
  8. #define SkPictureFlat_DEFINED
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPicture.h"
  12. #include "include/private/SkChecksum.h"
  13. #include "src/core/SkPtrRecorder.h"
  14. #include "src/core/SkReadBuffer.h"
  15. #include "src/core/SkTDynamicHash.h"
  16. #include "src/core/SkWriteBuffer.h"
  17. /*
  18. * Note: While adding new DrawTypes, it is necessary to add to the end of this list
  19. * and update LAST_DRAWTYPE_ENUM to avoid having the code read older skps wrong.
  20. * (which can cause segfaults)
  21. *
  22. * Reordering can be done during version updates.
  23. */
  24. enum DrawType {
  25. UNUSED,
  26. CLIP_PATH,
  27. CLIP_REGION,
  28. CLIP_RECT,
  29. CLIP_RRECT,
  30. CONCAT,
  31. DRAW_BITMAP_RETIRED_2016_REMOVED_2018,
  32. DRAW_BITMAP_MATRIX_RETIRED_2016_REMOVED_2018,
  33. DRAW_BITMAP_NINE_RETIRED_2016_REMOVED_2018,
  34. DRAW_BITMAP_RECT_RETIRED_2016_REMOVED_2018,
  35. DRAW_CLEAR,
  36. DRAW_DATA,
  37. DRAW_OVAL,
  38. DRAW_PAINT,
  39. DRAW_PATH,
  40. DRAW_PICTURE,
  41. DRAW_POINTS,
  42. DRAW_POS_TEXT_REMOVED_1_2019,
  43. DRAW_POS_TEXT_TOP_BOTTOM_REMOVED_1_2019,
  44. DRAW_POS_TEXT_H_REMOVED_1_2019,
  45. DRAW_POS_TEXT_H_TOP_BOTTOM_REMOVED_1_2019,
  46. DRAW_RECT,
  47. DRAW_RRECT,
  48. DRAW_SPRITE_RETIRED_2015_REMOVED_2018,
  49. DRAW_TEXT_REMOVED_1_2019,
  50. DRAW_TEXT_ON_PATH_RETIRED_08_2018_REMOVED_10_2018,
  51. DRAW_TEXT_TOP_BOTTOM_REMOVED_1_2019,
  52. DRAW_VERTICES_RETIRED_03_2017_REMOVED_01_2018,
  53. RESTORE,
  54. ROTATE,
  55. SAVE,
  56. SAVE_LAYER_SAVEFLAGS_DEPRECATED,
  57. SCALE,
  58. SET_MATRIX,
  59. SKEW,
  60. TRANSLATE,
  61. NOOP,
  62. BEGIN_COMMENT_GROUP_obsolete,
  63. COMMENT_obsolete,
  64. END_COMMENT_GROUP_obsolete,
  65. // new ops -- feel free to re-alphabetize on next version bump
  66. DRAW_DRRECT,
  67. PUSH_CULL, // deprecated, M41 was last Chromium version to write this to an .skp
  68. POP_CULL, // deprecated, M41 was last Chromium version to write this to an .skp
  69. DRAW_PATCH, // could not add in aphabetical order
  70. DRAW_PICTURE_MATRIX_PAINT,
  71. DRAW_TEXT_BLOB,
  72. DRAW_IMAGE,
  73. DRAW_IMAGE_RECT_STRICT_obsolete,
  74. DRAW_ATLAS,
  75. DRAW_IMAGE_NINE,
  76. DRAW_IMAGE_RECT,
  77. SAVE_LAYER_SAVELAYERFLAGS_DEPRECATED_JAN_2016_REMOVED_01_2018,
  78. SAVE_LAYER_SAVELAYERREC,
  79. DRAW_ANNOTATION,
  80. DRAW_DRAWABLE,
  81. DRAW_DRAWABLE_MATRIX,
  82. DRAW_TEXT_RSXFORM_DEPRECATED_DEC_2018,
  83. TRANSLATE_Z, // deprecated (M60)
  84. DRAW_SHADOW_REC,
  85. DRAW_IMAGE_LATTICE,
  86. DRAW_ARC,
  87. DRAW_REGION,
  88. DRAW_VERTICES_OBJECT,
  89. FLUSH,
  90. DRAW_EDGEAA_IMAGE_SET,
  91. SAVE_BEHIND,
  92. DRAW_EDGEAA_QUAD,
  93. DRAW_BEHIND_PAINT,
  94. LAST_DRAWTYPE_ENUM = DRAW_BEHIND_PAINT,
  95. };
  96. enum DrawVertexFlags {
  97. DRAW_VERTICES_HAS_TEXS = 0x01,
  98. DRAW_VERTICES_HAS_COLORS = 0x02,
  99. DRAW_VERTICES_HAS_INDICES = 0x04,
  100. DRAW_VERTICES_HAS_XFER = 0x08,
  101. };
  102. enum DrawAtlasFlags {
  103. DRAW_ATLAS_HAS_COLORS = 1 << 0,
  104. DRAW_ATLAS_HAS_CULL = 1 << 1,
  105. };
  106. enum DrawTextRSXformFlags {
  107. DRAW_TEXT_RSXFORM_HAS_CULL = 1 << 0,
  108. };
  109. enum SaveLayerRecFlatFlags {
  110. SAVELAYERREC_HAS_BOUNDS = 1 << 0,
  111. SAVELAYERREC_HAS_PAINT = 1 << 1,
  112. SAVELAYERREC_HAS_BACKDROP = 1 << 2,
  113. SAVELAYERREC_HAS_FLAGS = 1 << 3,
  114. SAVELAYERREC_HAS_CLIPMASK = 1 << 4,
  115. SAVELAYERREC_HAS_CLIPMATRIX = 1 << 5,
  116. };
  117. enum SaveBehindFlatFlags {
  118. SAVEBEHIND_HAS_SUBSET = 1 << 0,
  119. };
  120. ///////////////////////////////////////////////////////////////////////////////
  121. // clipparams are packed in 5 bits
  122. // doAA:1 | clipOp:4
  123. static inline uint32_t ClipParams_pack(SkClipOp op, bool doAA) {
  124. unsigned doAABit = doAA ? 1 : 0;
  125. return (doAABit << 4) | static_cast<int>(op);
  126. }
  127. template <typename T> T asValidEnum(SkReadBuffer* buffer, uint32_t candidate) {
  128. if (buffer->validate(candidate <= static_cast<uint32_t>(T::kMax_EnumValue))) {
  129. return static_cast<T>(candidate);
  130. }
  131. return T::kMax_EnumValue;
  132. }
  133. static inline SkClipOp ClipParams_unpackRegionOp(SkReadBuffer* buffer, uint32_t packed) {
  134. return asValidEnum<SkClipOp>(buffer, packed & 0xF);
  135. }
  136. static inline bool ClipParams_unpackDoAA(uint32_t packed) {
  137. return SkToBool((packed >> 4) & 1);
  138. }
  139. ///////////////////////////////////////////////////////////////////////////////
  140. class SkTypefacePlayback {
  141. public:
  142. SkTypefacePlayback() : fCount(0), fArray(nullptr) {}
  143. ~SkTypefacePlayback() = default;
  144. void setCount(size_t count);
  145. size_t count() const { return fCount; }
  146. sk_sp<SkTypeface>& operator[](size_t index) {
  147. SkASSERT(index < fCount);
  148. return fArray[index];
  149. }
  150. void setupBuffer(SkReadBuffer& buffer) const {
  151. buffer.setTypefaceArray(fArray.get(), fCount);
  152. }
  153. protected:
  154. size_t fCount;
  155. std::unique_ptr<sk_sp<SkTypeface>[]> fArray;
  156. };
  157. class SkFactoryPlayback {
  158. public:
  159. SkFactoryPlayback(int count) : fCount(count) { fArray = new SkFlattenable::Factory[count]; }
  160. ~SkFactoryPlayback() { delete[] fArray; }
  161. SkFlattenable::Factory* base() const { return fArray; }
  162. void setupBuffer(SkReadBuffer& buffer) const {
  163. buffer.setFactoryPlayback(fArray, fCount);
  164. }
  165. private:
  166. int fCount;
  167. SkFlattenable::Factory* fArray;
  168. };
  169. #endif