skia_paint_canvas.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_SKIA_PAINT_CANVAS_H_
  5. #define CC_PAINT_SKIA_PAINT_CANVAS_H_
  6. #include <memory>
  7. #include "base/compiler_specific.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/skia/include/core/SkBitmap.h"
  14. #include "third_party/skia/include/core/SkColor.h"
  15. #include "third_party/skia/include/core/SkColorSpace.h"
  16. #include "third_party/skia/include/core/SkData.h"
  17. #include "third_party/skia/include/core/SkRefCnt.h"
  18. #include "third_party/skia/include/core/SkScalar.h"
  19. #include "third_party/skia/include/core/SkTextBlob.h"
  20. class SkCanvas;
  21. class SkM44;
  22. class SkMatrix;
  23. class SkPath;
  24. class SkRRect;
  25. class SkSurfaceProps;
  26. enum class SkClipOp;
  27. struct SkImageInfo;
  28. struct SkIPoint;
  29. struct SkIRect;
  30. struct SkRect;
  31. namespace cc {
  32. class ImageProvider;
  33. class PaintFlags;
  34. // A PaintCanvas derived class that passes PaintCanvas APIs through to
  35. // an SkCanvas. This is more efficient than recording to a PaintRecord
  36. // and then playing back to an SkCanvas.
  37. class CC_PAINT_EXPORT SkiaPaintCanvas final : public PaintCanvas {
  38. public:
  39. struct CC_PAINT_EXPORT ContextFlushes {
  40. ContextFlushes();
  41. bool enable;
  42. int max_draws_before_flush;
  43. };
  44. explicit SkiaPaintCanvas(SkCanvas* canvas,
  45. ImageProvider* image_provider = nullptr,
  46. ContextFlushes context_flushes = ContextFlushes());
  47. explicit SkiaPaintCanvas(const SkBitmap& bitmap,
  48. ImageProvider* image_provider = nullptr);
  49. explicit SkiaPaintCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props);
  50. // If |target_color_space| is non-nullptr, then this will wrap |canvas| in a
  51. // SkColorSpaceXformCanvas.
  52. SkiaPaintCanvas(SkCanvas* canvas,
  53. sk_sp<SkColorSpace> target_color_space,
  54. ImageProvider* image_provider = nullptr,
  55. ContextFlushes context_flushes = ContextFlushes());
  56. SkiaPaintCanvas(const SkiaPaintCanvas&) = delete;
  57. ~SkiaPaintCanvas() override;
  58. SkiaPaintCanvas& operator=(const SkiaPaintCanvas&) = delete;
  59. void reset_image_provider() { image_provider_ = nullptr; }
  60. SkImageInfo imageInfo() const override;
  61. void* accessTopLayerPixels(SkImageInfo* info,
  62. size_t* rowBytes,
  63. SkIPoint* origin = nullptr) override;
  64. void flush() override;
  65. int save() override;
  66. int saveLayer(const SkRect* bounds, const PaintFlags* flags) override;
  67. int saveLayerAlpha(const SkRect* bounds, uint8_t alpha) override;
  68. void restore() override;
  69. int getSaveCount() const override;
  70. void restoreToCount(int save_count) override;
  71. void translate(SkScalar dx, SkScalar dy) override;
  72. void scale(SkScalar sx, SkScalar sy) override;
  73. void rotate(SkScalar degrees) override;
  74. void concat(const SkMatrix& matrix) override;
  75. void setMatrix(const SkMatrix& matrix) override;
  76. void concat(const SkM44& matrix) override;
  77. void setMatrix(const SkM44& matrix) override;
  78. void clipRect(const SkRect& rect, SkClipOp op, bool do_anti_alias) override;
  79. void clipRRect(const SkRRect& rrect,
  80. SkClipOp op,
  81. bool do_anti_alias) override;
  82. void clipPath(const SkPath& path,
  83. SkClipOp op,
  84. bool do_anti_alias,
  85. UsePaintCache) override;
  86. SkRect getLocalClipBounds() const override;
  87. bool getLocalClipBounds(SkRect* bounds) const override;
  88. SkIRect getDeviceClipBounds() const override;
  89. bool getDeviceClipBounds(SkIRect* bounds) const override;
  90. void drawColor(SkColor4f color, SkBlendMode mode) override;
  91. void clear(SkColor4f color) override;
  92. void drawLine(SkScalar x0,
  93. SkScalar y0,
  94. SkScalar x1,
  95. SkScalar y1,
  96. const PaintFlags& flags) override;
  97. void drawRect(const SkRect& rect, const PaintFlags& flags) override;
  98. void drawIRect(const SkIRect& rect, const PaintFlags& flags) override;
  99. void drawOval(const SkRect& oval, const PaintFlags& flags) override;
  100. void drawRRect(const SkRRect& rrect, const PaintFlags& flags) override;
  101. void drawDRRect(const SkRRect& outer,
  102. const SkRRect& inner,
  103. const PaintFlags& flags) override;
  104. void drawRoundRect(const SkRect& rect,
  105. SkScalar rx,
  106. SkScalar ry,
  107. const PaintFlags& flags) override;
  108. void drawPath(const SkPath& path,
  109. const PaintFlags& flags,
  110. UsePaintCache) override;
  111. void drawImage(const PaintImage& image,
  112. SkScalar left,
  113. SkScalar top,
  114. const SkSamplingOptions&,
  115. const PaintFlags* flags) override;
  116. void drawImageRect(const PaintImage& image,
  117. const SkRect& src,
  118. const SkRect& dst,
  119. const SkSamplingOptions&,
  120. const PaintFlags* flags,
  121. SkCanvas::SrcRectConstraint constraint) override;
  122. void drawSkottie(scoped_refptr<SkottieWrapper> skottie,
  123. const SkRect& dst,
  124. float t,
  125. SkottieFrameDataMap images,
  126. const SkottieColorMap& color_map,
  127. SkottieTextPropertyValueMap text_map) override;
  128. void drawTextBlob(sk_sp<SkTextBlob> blob,
  129. SkScalar x,
  130. SkScalar y,
  131. const PaintFlags& flags) override;
  132. void drawTextBlob(sk_sp<SkTextBlob> blob,
  133. SkScalar x,
  134. SkScalar y,
  135. NodeId node_id,
  136. const PaintFlags& flags) override;
  137. void drawPicture(sk_sp<const PaintRecord> record) override;
  138. bool isClipEmpty() const override;
  139. SkMatrix getTotalMatrix() const override;
  140. SkM44 getLocalToDevice() const override;
  141. bool NeedsFlush() const override;
  142. void Annotate(AnnotationType type,
  143. const SkRect& rect,
  144. sk_sp<SkData> data) override;
  145. void setNodeId(int) override;
  146. // Don't shadow non-virtual helper functions.
  147. using PaintCanvas::clipPath;
  148. using PaintCanvas::clipRect;
  149. using PaintCanvas::clipRRect;
  150. using PaintCanvas::drawColor;
  151. using PaintCanvas::drawImage;
  152. using PaintCanvas::drawPicture;
  153. // Same as the above drawPicture() except using the given custom data
  154. // raster callback.
  155. void drawPicture(
  156. sk_sp<const PaintRecord> record,
  157. PlaybackParams::CustomDataRasterCallback custom_raster_callback);
  158. private:
  159. void FlushAfterDrawIfNeeded();
  160. int GetMaxTextureSize() const;
  161. SkCanvas* canvas_;
  162. SkBitmap bitmap_;
  163. std::unique_ptr<SkCanvas> owned_;
  164. ImageProvider* image_provider_ = nullptr;
  165. const ContextFlushes context_flushes_;
  166. int num_of_ops_ = 0;
  167. };
  168. } // namespace cc
  169. #endif // CC_PAINT_SKIA_PAINT_CANVAS_H_