SkPaintFilterCanvas.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkPaintFilterCanvas_DEFINED
  8. #define SkPaintFilterCanvas_DEFINED
  9. #include "SkCanvasVirtualEnforcer.h"
  10. #include "SkNWayCanvas.h"
  11. #include "SkTLazy.h"
  12. /** \class SkPaintFilterCanvas
  13. A utility proxy base class for implementing draw/paint filters.
  14. */
  15. class SK_API SkPaintFilterCanvas : public SkCanvasVirtualEnforcer<SkNWayCanvas> {
  16. public:
  17. /**
  18. * The new SkPaintFilterCanvas is configured for forwarding to the
  19. * specified canvas. Also copies the target canvas matrix and clip bounds.
  20. */
  21. SkPaintFilterCanvas(SkCanvas* canvas);
  22. enum Type {
  23. kPaint_Type,
  24. kPoint_Type,
  25. kArc_Type,
  26. kBitmap_Type,
  27. kRect_Type,
  28. kRRect_Type,
  29. kDRRect_Type,
  30. kOval_Type,
  31. kPath_Type,
  32. kPicture_Type,
  33. kDrawable_Type,
  34. kText_Type,
  35. kTextBlob_Type,
  36. kVertices_Type,
  37. kPatch_Type,
  38. kTypeCount
  39. };
  40. // Forwarded to the wrapped canvas.
  41. SkISize getBaseLayerSize() const override { return proxy()->getBaseLayerSize(); }
  42. GrContext* getGrContext() override { return proxy()->getGrContext(); }
  43. GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext() override {
  44. return proxy()->internal_private_accessTopLayerRenderTargetContext();
  45. }
  46. protected:
  47. /**
  48. * Called with the paint that will be used to draw the specified type.
  49. * The implementation may modify the paint as they wish (using SkTCopyOnFirstWrite::writable).
  50. *
  51. * The result bool is used to determine whether the draw op is to be
  52. * executed (true) or skipped (false).
  53. *
  54. * Note: The base implementation calls onFilter() for top-level/explicit paints only.
  55. * To also filter encapsulated paints (e.g. SkPicture, SkTextBlob), clients may need to
  56. * override the relevant methods (i.e. drawPicture, drawTextBlob).
  57. */
  58. virtual bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const = 0;
  59. void onDrawPaint(const SkPaint&) override;
  60. void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
  61. void onDrawRect(const SkRect&, const SkPaint&) override;
  62. void onDrawRRect(const SkRRect&, const SkPaint&) override;
  63. void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
  64. void onDrawRegion(const SkRegion&, const SkPaint&) override;
  65. void onDrawOval(const SkRect&, const SkPaint&) override;
  66. void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
  67. void onDrawPath(const SkPath&, const SkPaint&) override;
  68. void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
  69. void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
  70. SrcRectConstraint) override;
  71. void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
  72. const SkPaint*) override;
  73. void onDrawBitmapLattice(const SkBitmap&, const Lattice&, const SkRect&,
  74. const SkPaint*) override;
  75. void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
  76. void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
  77. const SkPaint*, SrcRectConstraint) override;
  78. void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
  79. const SkPaint*) override;
  80. void onDrawImageLattice(const SkImage*, const Lattice&, const SkRect&,
  81. const SkPaint*) override;
  82. void onDrawImageSet(const SkCanvas::ImageSetEntry[], int count, SkFilterQuality,
  83. SkBlendMode) override;
  84. void onDrawVerticesObject(const SkVertices*, const SkVertices::Bone bones[], int boneCount,
  85. SkBlendMode, const SkPaint&) override;
  86. void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
  87. const SkPoint texCoords[4], SkBlendMode,
  88. const SkPaint& paint) override;
  89. void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
  90. void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
  91. void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
  92. const SkPaint&) override;
  93. void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
  94. const SkPaint&) override;
  95. void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
  96. SkScalar constY, const SkPaint&) override;
  97. void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
  98. const SkRect* cull, const SkPaint& paint) override;
  99. void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
  100. const SkPaint& paint) override;
  101. void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
  102. int, SkBlendMode, const SkRect*, const SkPaint*) override;
  103. void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override;
  104. void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
  105. // Forwarded to the wrapped canvas.
  106. sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
  107. bool onPeekPixels(SkPixmap* pixmap) override;
  108. bool onAccessTopLayerPixels(SkPixmap* pixmap) override;
  109. SkImageInfo onImageInfo() const override;
  110. bool onGetProps(SkSurfaceProps* props) const override;
  111. private:
  112. class AutoPaintFilter;
  113. SkCanvas* proxy() const { SkASSERT(fList.count() == 1); return fList[0]; }
  114. };
  115. #endif