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