record_paint_canvas.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_RECORD_PAINT_CANVAS_H_
  5. #define CC_PAINT_RECORD_PAINT_CANVAS_H_
  6. #include "base/compiler_specific.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "build/build_config.h"
  9. #include "cc/paint/paint_canvas.h"
  10. #include "cc/paint/paint_flags.h"
  11. #include "cc/paint/paint_record.h"
  12. #include "cc/paint/skottie_color_map.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "third_party/skia/include/utils/SkNoDrawCanvas.h"
  15. namespace cc {
  16. class DisplayItemList;
  17. class PaintFlags;
  18. class CC_PAINT_EXPORT RecordPaintCanvas : public PaintCanvas {
  19. public:
  20. RecordPaintCanvas(DisplayItemList* list, const SkRect& bounds);
  21. RecordPaintCanvas(const RecordPaintCanvas&) = delete;
  22. ~RecordPaintCanvas() override;
  23. RecordPaintCanvas& operator=(const RecordPaintCanvas&) = delete;
  24. SkImageInfo imageInfo() const override;
  25. void* accessTopLayerPixels(SkImageInfo* info,
  26. size_t* rowBytes,
  27. SkIPoint* origin = nullptr) override;
  28. void flush() override;
  29. int save() override;
  30. int saveLayer(const SkRect* bounds, const PaintFlags* flags) override;
  31. int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) override;
  32. void restore() override;
  33. int getSaveCount() const override;
  34. void restoreToCount(int save_count) override;
  35. void translate(SkScalar dx, SkScalar dy) override;
  36. void scale(SkScalar sx, SkScalar sy) override;
  37. void rotate(SkScalar degrees) override;
  38. // TODO(crbug.com/1167153): The concat and setMatrix methods that take an
  39. // SkMatrix should be removed in favor of the SkM44 versions.
  40. void concat(const SkMatrix& matrix) override;
  41. void setMatrix(const SkMatrix& matrix) override;
  42. void concat(const SkM44& matrix) override;
  43. void setMatrix(const SkM44& matrix) override;
  44. void clipRect(const SkRect& rect, SkClipOp op, bool antialias) override;
  45. void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) override;
  46. void clipPath(const SkPath& path,
  47. SkClipOp op,
  48. bool antialias,
  49. UsePaintCache use_paint_cache) override;
  50. SkRect getLocalClipBounds() const override;
  51. bool getLocalClipBounds(SkRect* bounds) const override;
  52. SkIRect getDeviceClipBounds() const override;
  53. bool getDeviceClipBounds(SkIRect* bounds) const override;
  54. void drawColor(SkColor4f color, SkBlendMode mode) override;
  55. void clear(SkColor4f color) override;
  56. void drawLine(SkScalar x0,
  57. SkScalar y0,
  58. SkScalar x1,
  59. SkScalar y1,
  60. const PaintFlags& flags) override;
  61. void drawRect(const SkRect& rect, const PaintFlags& flags) override;
  62. void drawIRect(const SkIRect& rect, const PaintFlags& flags) override;
  63. void drawOval(const SkRect& oval, const PaintFlags& flags) override;
  64. void drawRRect(const SkRRect& rrect, const PaintFlags& flags) override;
  65. void drawDRRect(const SkRRect& outer,
  66. const SkRRect& inner,
  67. const PaintFlags& flags) override;
  68. void drawRoundRect(const SkRect& rect,
  69. SkScalar rx,
  70. SkScalar ry,
  71. const PaintFlags& flags) override;
  72. void drawPath(const SkPath& path,
  73. const PaintFlags& flags,
  74. UsePaintCache use_paint_cache) override;
  75. void drawImage(const PaintImage& image,
  76. SkScalar left,
  77. SkScalar top,
  78. const SkSamplingOptions&,
  79. const PaintFlags* flags) override;
  80. void drawImageRect(const PaintImage& image,
  81. const SkRect& src,
  82. const SkRect& dst,
  83. const SkSamplingOptions&,
  84. const PaintFlags* flags,
  85. SkCanvas::SrcRectConstraint constraint) override;
  86. void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
  87. const SkRect& dst,
  88. float t,
  89. SkottieFrameDataMap images,
  90. const SkottieColorMap& color_map,
  91. SkottieTextPropertyValueMap text_map) override;
  92. void drawTextBlob(sk_sp<SkTextBlob> blob,
  93. SkScalar x,
  94. SkScalar y,
  95. const PaintFlags& flags) override;
  96. void drawTextBlob(sk_sp<SkTextBlob> blob,
  97. SkScalar x,
  98. SkScalar y,
  99. NodeId node_id,
  100. const PaintFlags& flags) override;
  101. void drawPicture(sk_sp<const PaintRecord> record) override;
  102. bool isClipEmpty() const override;
  103. SkMatrix getTotalMatrix() const override;
  104. SkM44 getLocalToDevice() const override;
  105. void Annotate(AnnotationType type,
  106. const SkRect& rect,
  107. sk_sp<SkData> data) override;
  108. void recordCustomData(uint32_t id) override;
  109. void setNodeId(int) override;
  110. bool NeedsFlush() const override;
  111. // Don't shadow non-virtual helper functions.
  112. using PaintCanvas::clipRect;
  113. using PaintCanvas::clipRRect;
  114. using PaintCanvas::clipPath;
  115. using PaintCanvas::drawColor;
  116. using PaintCanvas::drawImage;
  117. using PaintCanvas::drawPicture;
  118. #if DCHECK_IS_ON()
  119. void EnterDisableFlushCheckScope() { ++disable_flush_check_scope_; }
  120. void LeaveDisableFlushCheckScope() { DCHECK(disable_flush_check_scope_--); }
  121. bool IsInDisableFlushCheckScope() { return disable_flush_check_scope_; }
  122. #endif
  123. class DisableFlushCheckScope {
  124. // Create an object of this type to temporarily allow draw commands to be
  125. // recorded while the recording is marked as needing to be flushed. This is
  126. // meant to be used to allow client code to issue the commands necessary to
  127. // reach a state where the recording can be safely flushed before beginning
  128. // to enforce a check that forbids recording additional draw commands after
  129. // a flush was requested.
  130. public:
  131. explicit DisableFlushCheckScope(RecordPaintCanvas* canvas) {
  132. #if DCHECK_IS_ON()
  133. // We require that NeedsFlush be false upon entering a top-level scope
  134. // to prevent consecutive scopes from evading evading flush checks
  135. // indefinitely.
  136. DCHECK(!canvas->NeedsFlush() || canvas->IsInDisableFlushCheckScope());
  137. canvas->EnterDisableFlushCheckScope();
  138. canvas_ = canvas;
  139. #endif
  140. }
  141. ~DisableFlushCheckScope() {
  142. #if DCHECK_IS_ON()
  143. canvas_->LeaveDisableFlushCheckScope();
  144. #endif
  145. }
  146. private:
  147. #if DCHECK_IS_ON()
  148. raw_ptr<RecordPaintCanvas> canvas_;
  149. #endif
  150. };
  151. private:
  152. template <typename T, typename... Args>
  153. size_t push(Args&&... args);
  154. const SkNoDrawCanvas* GetCanvas() const;
  155. SkNoDrawCanvas* GetCanvas();
  156. bool InitializedWithRecordingBounds() const;
  157. DisplayItemList* list_;
  158. // TODO(enne): Although RecordPaintCanvas is mostly a write-only interface
  159. // where paint commands are stored, occasionally users of PaintCanvas want
  160. // to ask stateful questions mid-stream of clip and transform state.
  161. // To avoid duplicating all this code (for now?), just forward to an SkCanvas
  162. // that's not backed by anything but can answer these questions.
  163. //
  164. // This is mutable so that const functions (e.g. quickReject) that may
  165. // lazy initialize the canvas can still be const.
  166. mutable absl::optional<SkNoDrawCanvas> canvas_;
  167. SkRect recording_bounds_;
  168. bool needs_flush_ = false;
  169. #if DCHECK_IS_ON()
  170. unsigned disable_flush_check_scope_ = 0;
  171. #endif
  172. };
  173. } // namespace cc
  174. #endif // CC_PAINT_RECORD_PAINT_CANVAS_H_