paint_op_buffer_serializer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_BUFFER_SERIALIZER_H_
  5. #define CC_PAINT_PAINT_OP_BUFFER_SERIALIZER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "cc/paint/paint_op_buffer.h"
  10. #include "third_party/skia/include/core/SkColor.h"
  11. #include "third_party/skia/include/private/chromium/SkChromeRemoteGlyphCache.h"
  12. #include "ui/gfx/geometry/rect_f.h"
  13. namespace cc {
  14. class CC_PAINT_EXPORT PaintOpBufferSerializer {
  15. public:
  16. using SerializeCallback =
  17. base::RepeatingCallback<size_t(const PaintOp*,
  18. const PaintOp::SerializeOptions&,
  19. const PaintFlags*,
  20. const SkM44&,
  21. const SkM44&)>;
  22. PaintOpBufferSerializer(SerializeCallback serialize_cb,
  23. const PaintOp::SerializeOptions& options);
  24. virtual ~PaintOpBufferSerializer();
  25. struct Preamble {
  26. // The full size of the content, to know whether edge texel clearing
  27. // is required or not. The full_raster_rect and playback_rect must
  28. // be contained in this size.
  29. gfx::Size content_size;
  30. // Rect in content space (1 unit = 1 pixel) of this tile.
  31. gfx::Rect full_raster_rect;
  32. // A subrect in content space (full_raster_rect must contain this) of
  33. // the partial content to play back.
  34. gfx::Rect playback_rect;
  35. // The translation and scale to do after
  36. gfx::Vector2dF post_translation;
  37. gfx::Vector2dF post_scale = gfx::Vector2dF(1.f, 1.f);
  38. // If requires_clear is true, then this will raster will be cleared to
  39. // transparent. If false, it assumes that the content will raster
  40. // opaquely up to content_size inset by 1 (with the last pixel being
  41. // potentially being partially transparent, if post scaled).
  42. bool requires_clear = true;
  43. // If clearing is needed, the color to clear to.
  44. SkColor4f background_color = SkColors::kTransparent;
  45. };
  46. // Serialize the buffer with a preamble. This function wraps the buffer in a
  47. // save/restore and includes any translations, scales, and clearing as
  48. // specified by the preamble. This should generally be used for top level
  49. // rastering of an entire tile.
  50. void Serialize(const PaintOpBuffer* buffer,
  51. const std::vector<size_t>* offsets,
  52. const Preamble& preamble);
  53. // Sereialize the buffer as |Serialize| with a preamble. This function also
  54. // destroys the PaintOps in |buffer| after serialization.
  55. void SerializeAndDestroy(PaintOpBuffer* buffer,
  56. const std::vector<size_t>* offsets,
  57. const Preamble& preamble);
  58. // Serialize the buffer without a preamble. This function serializes the whole
  59. // buffer without any extra ops added. No clearing is done. This should
  60. // generally be used for internal PaintOpBuffers that want to be sent as-is.
  61. void Serialize(const PaintOpBuffer* buffer);
  62. // Serialize the buffer with a scale and a playback rect. This should
  63. // generally be used for internal PaintOpBuffers in PaintShaders and
  64. // PaintFilters that need to guarantee the nested buffer is rasterized at the
  65. // specific scale to a separate image. This ensures that scale-dependent
  66. // analysis made during serialization is consistent with analysis done during
  67. // rasterization.
  68. void Serialize(const PaintOpBuffer* buffer,
  69. const gfx::Rect& playback_rect,
  70. const gfx::SizeF& post_scale);
  71. bool valid() const { return valid_; }
  72. private:
  73. void SerializePreamble(SkCanvas* canvas,
  74. const Preamble& preamble,
  75. const PlaybackParams& params);
  76. void SerializeBuffer(SkCanvas* canvas,
  77. const PaintOpBuffer* buffer,
  78. const std::vector<size_t>* offsets);
  79. void SerializeBufferAndDestroy(SkCanvas* canvas,
  80. PaintOpBuffer* buffer,
  81. const std::vector<size_t>* offsets);
  82. // Returns whether searilization of |op| succeeded and we need to serialize
  83. // the next PaintOp in the PaintOpBuffer.
  84. bool WillSerializeNextOp(const PaintOp* op,
  85. SkCanvas* canvas,
  86. PlaybackParams params,
  87. uint8_t alpha);
  88. bool SerializeOpWithFlags(SkCanvas* canvas,
  89. const PaintOpWithFlags* flags_op,
  90. const PlaybackParams& params,
  91. uint8_t alpha);
  92. bool SerializeOp(SkCanvas* canvas,
  93. const PaintOp* op,
  94. const PaintFlags* flags_to_serialize,
  95. const PlaybackParams& params);
  96. void Save(SkCanvas* canvas, const PlaybackParams& params);
  97. void RestoreToCount(SkCanvas* canvas,
  98. int count,
  99. const PlaybackParams& params);
  100. void ClearForOpaqueRaster(SkCanvas* canvas,
  101. const Preamble& preamble,
  102. const PlaybackParams& params);
  103. void PlaybackOnAnalysisCanvas(SkCanvas* canvas,
  104. const PaintOp* op,
  105. const PaintFlags* flags_to_serialize,
  106. const PlaybackParams& params);
  107. SerializeCallback serialize_cb_;
  108. PaintOp::SerializeOptions options_;
  109. bool valid_ = true;
  110. };
  111. // Serializes the ops in the memory available, fails on overflow.
  112. class CC_PAINT_EXPORT SimpleBufferSerializer : public PaintOpBufferSerializer {
  113. public:
  114. SimpleBufferSerializer(void* memory,
  115. size_t size,
  116. const PaintOp::SerializeOptions& options);
  117. ~SimpleBufferSerializer() override;
  118. size_t written() const { return written_; }
  119. private:
  120. size_t SerializeToMemory(const PaintOp* op,
  121. const PaintOp::SerializeOptions& options,
  122. const PaintFlags* flags_to_serialize,
  123. const SkM44& current_ctm,
  124. const SkM44& original_ctm);
  125. raw_ptr<void> memory_;
  126. const size_t total_;
  127. size_t written_ = 0u;
  128. };
  129. } // namespace cc
  130. #endif // CC_PAINT_PAINT_OP_BUFFER_SERIALIZER_H_