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 "SkNWayCanvas.h"
12 #include "SkTLazy.h"
13 
14 /** \class SkPaintFilterCanvas
15 
16     A utility proxy base class for implementing draw/paint filters.
17 */
18 class SK_API SkPaintFilterCanvas : public SkNWayCanvas {
19 public:
20     /**
21      * DEPRECATED: use the variant below.
22      */
23     SkPaintFilterCanvas(int width, int height);
24 
25     /**
26      * The new SkPaintFilterCanvas is configured for forwarding to the
27      * specified canvas.  Also copies the target canvas matrix and clip bounds.
28      */
29     SkPaintFilterCanvas(SkCanvas* canvas);
30 
31     enum Type {
32         kPaint_Type,
33         kPoint_Type,
34         kBitmap_Type,
35         kRect_Type,
36         kRRect_Type,
37         kDRRect_Type,
38         kOval_Type,
39         kPath_Type,
40         kPicture_Type,
41         kText_Type,
42         kTextBlob_Type,
43         kVertices_Type,
44         kPatch_Type,
45 
46         kTypeCount
47     };
48 
49 protected:
50     /**
51      *  Called with the paint that will be used to draw the specified type.
52      *  The implementation may modify the paint as they wish (using SkTCopyOnFirstWrite::writable).
53      *
54      *  The result bool is used to determine whether the draw op is to be
55      *  executed (true) or skipped (false).
56      *
57      *  Note: The base implementation calls onFilter() for top-level/explicit paints only.
58      *        To also filter encapsulated paints (e.g. SkPicture, SkTextBlob), clients may need to
59      *        override the relevant methods (i.e. drawPicture, drawTextBlob).
60      */
61     virtual bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const = 0;
62 
63     void onDrawPaint(const SkPaint&) override;
64     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
65     void onDrawRect(const SkRect&, const SkPaint&) override;
66     void onDrawRRect(const SkRRect&, const SkPaint&) override;
67     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
68     void onDrawOval(const SkRect&, const SkPaint&) override;
69     void onDrawPath(const SkPath&, const SkPaint&) override;
70     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
71     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
72                           SrcRectConstraint) override;
73     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
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 onDrawVertices(VertexMode vmode, int vertexCount,
81                               const SkPoint vertices[], const SkPoint texs[],
82                               const SkColor colors[], SkXfermode* xmode,
83                               const uint16_t indices[], int indexCount,
84                               const SkPaint&) override;
85     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
86                              const SkPoint texCoords[4], SkXfermode* xmode,
87                              const SkPaint& paint) override;
88     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
89 
90     void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
91                     const SkPaint&) override;
92     void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
93                        const SkPaint&) override;
94     void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
95                         SkScalar constY, const SkPaint&) override;
96     void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
97                           const SkMatrix* matrix, const SkPaint&) override;
98     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
99                         const SkPaint& paint) override;
100 
101 private:
102     class AutoPaintFilter;
103 
104     typedef SkNWayCanvas INHERITED;
105 };
106 
107 #endif
108