paint_op_writer.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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_WRITER_H_
  5. #define CC_PAINT_PAINT_OP_WRITER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "cc/paint/paint_canvas.h"
  8. #include "cc/paint/paint_export.h"
  9. #include "cc/paint/paint_filter.h"
  10. #include "cc/paint/paint_op_buffer_serializer.h"
  11. #include "third_party/skia/include/core/SkImageInfo.h"
  12. #include "third_party/skia/include/core/SkYUVAInfo.h"
  13. struct SkRect;
  14. struct SkIRect;
  15. class SkRRect;
  16. namespace gpu {
  17. struct Mailbox;
  18. }
  19. namespace cc {
  20. class DecodedDrawImage;
  21. class DrawImage;
  22. class PaintShader;
  23. class CC_PAINT_EXPORT PaintOpWriter {
  24. public:
  25. // The SerializeOptions passed to the writer must set the required fields
  26. // if it can be used for serializing images, paint records or text blobs.
  27. PaintOpWriter(void* memory,
  28. size_t size,
  29. const PaintOp::SerializeOptions& options,
  30. bool enable_security_constraints = false);
  31. ~PaintOpWriter();
  32. static size_t constexpr HeaderBytes() { return 4u; }
  33. static size_t constexpr Alignment() { return 4u; }
  34. static size_t GetFlattenableSize(const SkFlattenable* flattenable);
  35. static size_t GetImageSize(const PaintImage& image);
  36. static size_t GetRecordSize(const PaintRecord* record);
  37. // Write a sequence of arbitrary bytes.
  38. void WriteData(size_t bytes, const void* input);
  39. size_t size() const { return valid_ ? size_ - remaining_bytes_ : 0u; }
  40. uint64_t* WriteSize(size_t size);
  41. void Write(SkScalar data);
  42. void Write(SkMatrix data);
  43. void Write(const SkM44& data);
  44. void Write(uint8_t data);
  45. void Write(uint32_t data);
  46. void Write(uint64_t data);
  47. void Write(int32_t data);
  48. void Write(const SkRect& rect);
  49. void Write(const SkIRect& rect);
  50. void Write(const SkRRect& rect);
  51. void Write(const SkColor4f& color);
  52. void Write(const SkPath& path, UsePaintCache);
  53. void Write(const sk_sp<SkData>& data);
  54. void Write(const SkColorSpace* data);
  55. void Write(const SkSamplingOptions&);
  56. void Write(const sk_sp<GrSlug>& slug);
  57. void Write(SkYUVColorSpace yuv_color_space);
  58. void Write(SkYUVAInfo::PlaneConfig plane_config);
  59. void Write(SkYUVAInfo::Subsampling subsampling);
  60. void Write(const gpu::Mailbox& mailbox);
  61. // Shaders and filters need to know the current transform in order to lock in
  62. // the scale factor they will be evaluated at after deserialization. This is
  63. // critical to ensure that nested PaintRecords are analyzed and rasterized
  64. // identically when text is involved.
  65. void Write(const PaintFlags& flags, const SkM44& current_ctm);
  66. void Write(const PaintShader* shader,
  67. PaintFlags::FilterQuality quality,
  68. const SkM44& current_ctm);
  69. void Write(const PaintFilter* filter, const SkM44& current_ctm);
  70. void Write(SkClipOp op) { WriteEnum(op); }
  71. void Write(PaintCanvas::AnnotationType type) { WriteEnum(type); }
  72. void Write(SkCanvas::SrcRectConstraint constraint) { WriteEnum(constraint); }
  73. void Write(SkColorType color_type) { WriteEnum(color_type); }
  74. void Write(PaintFlags::FilterQuality filter_quality) {
  75. WriteEnum(filter_quality);
  76. }
  77. void Write(SkBlendMode blend_mode) { WriteEnum(blend_mode); }
  78. void Write(SkTileMode tile_mode) { WriteEnum(tile_mode); }
  79. void Write(SkFilterMode filter_mode) { WriteEnum(filter_mode); }
  80. void Write(SkMipmapMode mipmap_mode) { WriteEnum(mipmap_mode); }
  81. void Write(bool data) { Write(static_cast<uint8_t>(data)); }
  82. // Aligns the memory to the given alignment.
  83. void AlignMemory(size_t alignment);
  84. void AssertAlignment(size_t alignment) {
  85. #if DCHECK_IS_ON()
  86. uintptr_t memory = reinterpret_cast<uintptr_t>(memory_.get());
  87. DCHECK_EQ(base::bits::AlignUp(memory, alignment), memory);
  88. #endif
  89. }
  90. // sk_sp is implicitly convertible to uint8_t (likely via implicit bool
  91. // conversion). In order to avoid accidentally calling that overload instead
  92. // of a specific function (such as would be the case if one forgets to call
  93. // .get() on it), the following template asserts if it's instantiated.
  94. template <typename T>
  95. void Write(const sk_sp<T>&) {
  96. // Note that this is essentially static_assert(false, ...) but it needs to
  97. // be dependent on T in order to only trigger if instantiated.
  98. static_assert(sizeof(T) == 0,
  99. "Attempted to call a non-existent sk_sp override.");
  100. }
  101. template <typename T>
  102. void Write(const T*) {
  103. static_assert(sizeof(T) == 0,
  104. "Attempted to call a non-existent T* override.");
  105. }
  106. // Serializes the given |draw_image|.
  107. // |scale_adjustment| is set to the scale applied to the serialized image.
  108. // |quality| is set to the quality that should be used when rasterizing this
  109. // image.
  110. void Write(const DrawImage& draw_image, SkSize* scale_adjustment);
  111. // Serializes the given |skottie| vector graphic.
  112. void Write(scoped_refptr<SkottieWrapper> skottie);
  113. private:
  114. template <typename T>
  115. void WriteSimple(const T& val);
  116. void WriteFlattenable(const SkFlattenable* val);
  117. template <typename Enum>
  118. void WriteEnum(Enum value) {
  119. Write(base::checked_cast<uint8_t>(value));
  120. }
  121. // The main entry point is Write(const PaintFilter* filter) which casts the
  122. // filter and calls one of the following functions.
  123. void Write(const ColorFilterPaintFilter& filter, const SkM44& current_ctm);
  124. void Write(const BlurPaintFilter& filter, const SkM44& current_ctm);
  125. void Write(const DropShadowPaintFilter& filter, const SkM44& current_ctm);
  126. void Write(const MagnifierPaintFilter& filter, const SkM44& current_ctm);
  127. void Write(const ComposePaintFilter& filter, const SkM44& current_ctm);
  128. void Write(const AlphaThresholdPaintFilter& filter, const SkM44& current_ctm);
  129. void Write(const XfermodePaintFilter& filter, const SkM44& current_ctm);
  130. void Write(const ArithmeticPaintFilter& filter, const SkM44& current_ctm);
  131. void Write(const MatrixConvolutionPaintFilter& filter,
  132. const SkM44& current_ctm);
  133. void Write(const DisplacementMapEffectPaintFilter& filter,
  134. const SkM44& current_ctm);
  135. void Write(const ImagePaintFilter& filter, const SkM44& current_ctm);
  136. void Write(const RecordPaintFilter& filter, const SkM44& current_ctm);
  137. void Write(const MergePaintFilter& filter, const SkM44& current_ctm);
  138. void Write(const MorphologyPaintFilter& filter, const SkM44& current_ctm);
  139. void Write(const OffsetPaintFilter& filter, const SkM44& current_ctm);
  140. void Write(const TilePaintFilter& filter, const SkM44& current_ctm);
  141. void Write(const TurbulencePaintFilter& filter, const SkM44& current_ctm);
  142. void Write(const ShaderPaintFilter& filter, const SkM44& current_ctm);
  143. void Write(const MatrixPaintFilter& filter, const SkM44& current_ctm);
  144. void Write(const LightingDistantPaintFilter& filter,
  145. const SkM44& current_ctm);
  146. void Write(const LightingPointPaintFilter& filter, const SkM44& current_ctm);
  147. void Write(const LightingSpotPaintFilter& filter, const SkM44& current_ctm);
  148. void Write(const StretchPaintFilter& filter, const SkM44& current_ctm);
  149. void Write(const PaintRecord* record,
  150. const gfx::Rect& playback_rect,
  151. const gfx::SizeF& post_scale);
  152. void Write(const SkRegion& region);
  153. void WriteImage(const DecodedDrawImage& decoded_draw_image);
  154. void WriteImage(uint32_t transfer_cache_entry_id, bool needs_mips);
  155. void WriteImage(const gpu::Mailbox& mailbox);
  156. void DidWrite(size_t bytes_written);
  157. void EnsureBytes(size_t required_bytes);
  158. sk_sp<PaintShader> TransformShaderIfNecessary(
  159. const PaintShader* original,
  160. PaintFlags::FilterQuality quality,
  161. const SkM44& current_ctm,
  162. uint32_t* paint_image_transfer_cache_entry_id,
  163. gfx::SizeF* paint_record_post_scale,
  164. bool* paint_image_needs_mips,
  165. gpu::Mailbox* mailbox_out);
  166. raw_ptr<char> memory_ = nullptr;
  167. size_t size_ = 0u;
  168. size_t remaining_bytes_ = 0u;
  169. const PaintOp::SerializeOptions& options_;
  170. bool valid_ = true;
  171. // Indicates that the following security constraints must be applied during
  172. // serialization:
  173. // 1) PaintRecords and SkDrawLoopers must be ignored.
  174. // 2) Codec backed images must be decoded and only the bitmap should be
  175. // serialized.
  176. const bool enable_security_constraints_;
  177. };
  178. } // namespace cc
  179. #endif // CC_PAINT_PAINT_OP_WRITER_H_