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