paint_op_reader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CC_PAINT_PAINT_OP_READER_H_
  5. #define CC_PAINT_PAINT_OP_READER_H_
  6. #include "base/memory/scoped_refptr.h"
  7. #include "cc/paint/paint_export.h"
  8. #include "cc/paint/paint_filter.h"
  9. #include "cc/paint/paint_op_writer.h"
  10. #include "cc/paint/transfer_cache_deserialize_helper.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. class SkColorSpace;
  13. namespace gpu {
  14. struct Mailbox;
  15. }
  16. namespace cc {
  17. class PaintShader;
  18. class SkottieWrapper;
  19. // PaintOpReader takes garbage |memory| and clobbers it with successive
  20. // read functions.
  21. class CC_PAINT_EXPORT PaintOpReader {
  22. public:
  23. // The DeserializeOptions passed to the reader must set all fields if it can
  24. // be used to for deserializing images, paint records or text blobs.
  25. PaintOpReader(const volatile void* memory,
  26. size_t size,
  27. const PaintOp::DeserializeOptions& options,
  28. bool enable_security_constraints = false)
  29. : memory_(static_cast<const volatile char*>(memory) +
  30. PaintOpWriter::HeaderBytes()),
  31. remaining_bytes_(
  32. base::bits::AlignDown(size, PaintOpWriter::Alignment())),
  33. options_(options),
  34. enable_security_constraints_(enable_security_constraints) {
  35. DCHECK_EQ(memory_,
  36. base::bits::AlignUp(memory_, PaintOpWriter::Alignment()));
  37. if (remaining_bytes_ < PaintOpWriter::HeaderBytes()) {
  38. valid_ = false;
  39. return;
  40. }
  41. remaining_bytes_ -= PaintOpWriter::HeaderBytes();
  42. }
  43. static void FixupMatrixPostSerialization(SkMatrix* matrix);
  44. static bool ReadAndValidateOpHeader(const volatile void* input,
  45. size_t input_size,
  46. uint8_t* type,
  47. uint32_t* skip);
  48. bool valid() const { return valid_; }
  49. size_t remaining_bytes() const { return remaining_bytes_; }
  50. void ReadData(size_t bytes, void* data);
  51. void ReadSize(size_t* size);
  52. void Read(SkScalar* data);
  53. void Read(uint8_t* data);
  54. void Read(uint32_t* data);
  55. void Read(uint64_t* data);
  56. void Read(int32_t* data);
  57. void Read(SkRect* rect);
  58. void Read(SkIRect* rect);
  59. void Read(SkRRect* rect);
  60. void Read(SkColor4f* color);
  61. void Read(SkPath* path);
  62. void Read(PaintFlags* flags);
  63. void Read(PaintImage* image);
  64. void Read(sk_sp<SkData>* data);
  65. void Read(sk_sp<GrSlug>* slug);
  66. void Read(sk_sp<PaintFilter>* filter);
  67. void Read(sk_sp<PaintShader>* shader);
  68. void Read(SkMatrix* matrix);
  69. void Read(SkM44* matrix);
  70. void Read(SkImageInfo* info);
  71. void Read(SkSamplingOptions* sampling);
  72. void Read(sk_sp<SkColorSpace>* color_space);
  73. void Read(SkYUVColorSpace* yuv_color_space);
  74. void Read(SkYUVAInfo::PlaneConfig* plane_config);
  75. void Read(SkYUVAInfo::Subsampling* subsampling);
  76. void Read(gpu::Mailbox* mailbox);
  77. void Read(scoped_refptr<SkottieWrapper>* skottie);
  78. void Read(SkClipOp* op) { ReadEnum<SkClipOp, SkClipOp::kMax_EnumValue>(op); }
  79. void Read(PaintCanvas::AnnotationType* type) {
  80. ReadEnum<PaintCanvas::AnnotationType,
  81. PaintCanvas::AnnotationType::LINK_TO_DESTINATION>(type);
  82. }
  83. void Read(SkCanvas::SrcRectConstraint* constraint) {
  84. ReadEnum<SkCanvas::SrcRectConstraint, SkCanvas::kFast_SrcRectConstraint>(
  85. constraint);
  86. }
  87. void Read(SkColorType* color_type) {
  88. ReadEnum<SkColorType, kLastEnum_SkColorType>(color_type);
  89. }
  90. void Read(PaintFlags::FilterQuality* quality) {
  91. ReadEnum<PaintFlags::FilterQuality, PaintFlags::FilterQuality::kLast>(
  92. quality);
  93. }
  94. void Read(SkBlendMode* blend_mode) {
  95. ReadEnum<SkBlendMode, SkBlendMode::kLastMode>(blend_mode);
  96. }
  97. void Read(SkTileMode* tile_mode) {
  98. ReadEnum<SkTileMode, SkTileMode::kLastTileMode>(tile_mode);
  99. }
  100. void Read(SkFilterMode* filter_mode) {
  101. ReadEnum<SkFilterMode, SkFilterMode::kLast>(filter_mode);
  102. }
  103. void Read(SkMipmapMode* mipmap_mode) {
  104. ReadEnum<SkMipmapMode, SkMipmapMode::kLast>(mipmap_mode);
  105. }
  106. void Read(bool* data) {
  107. uint8_t value = 0u;
  108. Read(&value);
  109. *data = !!value;
  110. }
  111. // Returns a pointer to the next block of memory of size |bytes|, and treats
  112. // this memory as read (advancing the reader). Returns nullptr if |bytes|
  113. // would exceed the available budfer.
  114. const volatile void* ExtractReadableMemory(size_t bytes);
  115. // Aligns the memory to the given alignment.
  116. void AlignMemory(size_t alignment);
  117. void AssertAlignment(size_t alignment) {
  118. #if DCHECK_IS_ON()
  119. uintptr_t memory = reinterpret_cast<uintptr_t>(memory_);
  120. DCHECK_EQ(base::bits::AlignUp(memory, alignment), memory);
  121. #endif
  122. }
  123. private:
  124. enum class DeserializationError {
  125. // Enum values must remain synchronized with PaintOpDeserializationError
  126. // in tools/metrics/histograms/enums.xml.
  127. kDrawLooperForbidden = 0,
  128. kEnumValueOutOfRange = 1,
  129. kForbiddenSerializedImageType = 2,
  130. kInsufficientRemainingBytes_AlignMemory = 3,
  131. kInsufficientRemainingBytes_ExtractReadableMemory = 4,
  132. kInsufficientRemainingBytes_Read_PaintRecord = 5,
  133. kInsufficientRemainingBytes_Read_PaintShader_ColorBytes = 6,
  134. kInsufficientRemainingBytes_Read_PaintShader_ColorSize = 7,
  135. kInsufficientRemainingBytes_Read_PaintShader_Positions = 8,
  136. kInsufficientRemainingBytes_Read_SkData = 9,
  137. kInsufficientRemainingBytes_Read_SkPath = 10,
  138. kInsufficientRemainingBytes_Read_SkRegion = 11,
  139. kInsufficientRemainingBytes_Read_GrSlug = 12,
  140. kInsufficientRemainingBytes_ReadData = 13,
  141. kInsufficientRemainingBytes_ReadFlattenable = 14,
  142. kInsufficientRemainingBytes_ReadMatrixConvolutionPaintFilter = 15,
  143. kInsufficientRemainingBytes_ReadSimple = 16,
  144. kInvalidPaintShader = 17,
  145. kInvalidPaintShaderPositionsSize = 18,
  146. kInvalidPaintShaderScalingBehavior = 19,
  147. kInvalidPaintShaderType = 20,
  148. kInvalidPlaneConfig = 21,
  149. kInvalidRasterScale = 22,
  150. kInvalidRecordShaderId = 23,
  151. kInvalidSerializedImageType = 24,
  152. kInvalidSkYUVColorSpace = 25,
  153. kInvalidSubsampling = 26,
  154. kInvalidTypeface = 27,
  155. kMissingPaintCachePathEntry = 28,
  156. kMissingPaintCacheTextBlobEntry = 29,
  157. kMissingSharedImageProvider = 30,
  158. kPaintFilterHasTooManyInputs = 31,
  159. kPaintOpBufferMakeFromMemoryFailure = 32,
  160. kPaintRecordForbidden = 33,
  161. kReadImageFailure = 34,
  162. kSharedImageOpenFailure = 35, // Obsolete
  163. kSkColorFilterUnflattenFailure = 36,
  164. kSkColorSpaceDeserializeFailure = 37,
  165. kSkDrawLooperUnflattenFailure = 38,
  166. kSkMaskFilterUnflattenFailure = 39,
  167. kSkPathEffectUnflattenFailure = 40,
  168. kSkPathReadFromMemoryFailure = 41,
  169. kSkRegionReadFromMemoryFailure = 42,
  170. kGrSlugDeserializeFailure = 43,
  171. kUnexpectedPaintShaderType = 44,
  172. kUnexpectedSerializedImageType = 45,
  173. kZeroMailbox = 46,
  174. kZeroRegionBytes = 47,
  175. kZeroSkPathBytes = 48,
  176. kSharedImageProviderUnknownMailbox = 49,
  177. kSharedImageProviderNoAccess = 50,
  178. kSharedImageProviderSkImageCreationFailed = 51,
  179. kZeroSkColorFilterBytes = 52,
  180. kInsufficientPixelData = 53,
  181. kMaxValue = kInsufficientPixelData
  182. };
  183. template <typename T>
  184. void ReadSimple(T* val);
  185. template <typename T>
  186. using Factory = sk_sp<T> (*)(const void* data,
  187. size_t size,
  188. const SkDeserialProcs* procs);
  189. template <typename T>
  190. void ReadFlattenable(sk_sp<T>* val,
  191. Factory<T> factory,
  192. DeserializationError error_on_factory_failure);
  193. template <typename Enum, Enum kMaxValue = Enum::kMaxValue>
  194. void ReadEnum(Enum* enum_value) {
  195. static_assert(static_cast<unsigned>(kMaxValue) <= 255,
  196. "Max value must fit in uint8_t");
  197. uint8_t value = 0u;
  198. Read(&value);
  199. if (value > static_cast<uint8_t>(kMaxValue)) {
  200. SetInvalid(DeserializationError::kEnumValueOutOfRange);
  201. return;
  202. }
  203. *enum_value = static_cast<Enum>(value);
  204. }
  205. void SetInvalid(DeserializationError error);
  206. // The main entry point is Read(sk_sp<PaintFilter>* filter) which calls one of
  207. // the following functions depending on read type.
  208. void ReadColorFilterPaintFilter(
  209. sk_sp<PaintFilter>* filter,
  210. const absl::optional<PaintFilter::CropRect>& crop_rect);
  211. void ReadBlurPaintFilter(
  212. sk_sp<PaintFilter>* filter,
  213. const absl::optional<PaintFilter::CropRect>& crop_rect);
  214. void ReadDropShadowPaintFilter(
  215. sk_sp<PaintFilter>* filter,
  216. const absl::optional<PaintFilter::CropRect>& crop_rect);
  217. void ReadMagnifierPaintFilter(
  218. sk_sp<PaintFilter>* filter,
  219. const absl::optional<PaintFilter::CropRect>& crop_rect);
  220. void ReadComposePaintFilter(
  221. sk_sp<PaintFilter>* filter,
  222. const absl::optional<PaintFilter::CropRect>& crop_rect);
  223. void ReadAlphaThresholdPaintFilter(
  224. sk_sp<PaintFilter>* filter,
  225. const absl::optional<PaintFilter::CropRect>& crop_rect);
  226. void ReadXfermodePaintFilter(
  227. sk_sp<PaintFilter>* filter,
  228. const absl::optional<PaintFilter::CropRect>& crop_rect);
  229. void ReadArithmeticPaintFilter(
  230. sk_sp<PaintFilter>* filter,
  231. const absl::optional<PaintFilter::CropRect>& crop_rect);
  232. void ReadMatrixConvolutionPaintFilter(
  233. sk_sp<PaintFilter>* filter,
  234. const absl::optional<PaintFilter::CropRect>& crop_rect);
  235. void ReadDisplacementMapEffectPaintFilter(
  236. sk_sp<PaintFilter>* filter,
  237. const absl::optional<PaintFilter::CropRect>& crop_rect);
  238. void ReadImagePaintFilter(
  239. sk_sp<PaintFilter>* filter,
  240. const absl::optional<PaintFilter::CropRect>& crop_rect);
  241. void ReadRecordPaintFilter(
  242. sk_sp<PaintFilter>* filter,
  243. const absl::optional<PaintFilter::CropRect>& crop_rect);
  244. void ReadMergePaintFilter(
  245. sk_sp<PaintFilter>* filter,
  246. const absl::optional<PaintFilter::CropRect>& crop_rect);
  247. void ReadMorphologyPaintFilter(
  248. sk_sp<PaintFilter>* filter,
  249. const absl::optional<PaintFilter::CropRect>& crop_rect);
  250. void ReadOffsetPaintFilter(
  251. sk_sp<PaintFilter>* filter,
  252. const absl::optional<PaintFilter::CropRect>& crop_rect);
  253. void ReadTilePaintFilter(
  254. sk_sp<PaintFilter>* filter,
  255. const absl::optional<PaintFilter::CropRect>& crop_rect);
  256. void ReadTurbulencePaintFilter(
  257. sk_sp<PaintFilter>* filter,
  258. const absl::optional<PaintFilter::CropRect>& crop_rect);
  259. void ReadShaderPaintFilter(
  260. sk_sp<PaintFilter>* filter,
  261. const absl::optional<PaintFilter::CropRect>& crop_rect);
  262. void ReadMatrixPaintFilter(
  263. sk_sp<PaintFilter>* filter,
  264. const absl::optional<PaintFilter::CropRect>& crop_rect);
  265. void ReadLightingDistantPaintFilter(
  266. sk_sp<PaintFilter>* filter,
  267. const absl::optional<PaintFilter::CropRect>& crop_rect);
  268. void ReadLightingPointPaintFilter(
  269. sk_sp<PaintFilter>* filter,
  270. const absl::optional<PaintFilter::CropRect>& crop_rect);
  271. void ReadLightingSpotPaintFilter(
  272. sk_sp<PaintFilter>* filter,
  273. const absl::optional<PaintFilter::CropRect>& crop_rect);
  274. void ReadStretchPaintFilter(
  275. sk_sp<PaintFilter>* filter,
  276. const absl::optional<PaintFilter::CropRect>& crop_rect);
  277. // Returns the size of the read record, 0 if error.
  278. size_t Read(sk_sp<PaintRecord>* record);
  279. void Read(SkRegion* region);
  280. uint8_t* CopyScratchSpace(size_t bytes);
  281. void DidRead(size_t bytes_read);
  282. const volatile char* memory_ = nullptr;
  283. size_t remaining_bytes_ = 0u;
  284. bool valid_ = true;
  285. const PaintOp::DeserializeOptions& options_;
  286. // Indicates that the data was serialized with the following constraints:
  287. // 1) PaintRecords and SkDrawLoopers are ignored.
  288. // 2) Images are decoded and only the bitmap is serialized.
  289. // If set to true, the above constraints are validated during deserialization
  290. // and the data types specified above are ignored.
  291. const bool enable_security_constraints_;
  292. };
  293. } // namespace cc
  294. #endif // CC_PAINT_PAINT_OP_READER_H_