• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * The new SkPaintFilterCanvas is configured for forwarding to the
22      * specified canvas.  Also copies the target canvas matrix and clip bounds.
23      */
24     SkPaintFilterCanvas(SkCanvas* canvas);
25 
26     enum Type {
27         kPaint_Type,
28         kPoint_Type,
29         kArc_Type,
30         kBitmap_Type,
31         kRect_Type,
32         kRRect_Type,
33         kDRRect_Type,
34         kOval_Type,
35         kPath_Type,
36         kPicture_Type,
37         kDrawable_Type,
38         kText_Type,
39         kTextBlob_Type,
40         kVertices_Type,
41         kPatch_Type,
42 
43         kTypeCount
44     };
45 
46     // Forwarded to the wrapped canvas.
getBaseLayerSize()47     SkISize getBaseLayerSize() const override { return proxy()->getBaseLayerSize(); }
getGrContext()48     GrContext*  getGrContext()       override { return proxy()->getGrContext();     }
49 
50 protected:
51     /**
52      *  Called with the paint that will be used to draw the specified type.
53      *  The implementation may modify the paint as they wish (using SkTCopyOnFirstWrite::writable).
54      *
55      *  The result bool is used to determine whether the draw op is to be
56      *  executed (true) or skipped (false).
57      *
58      *  Note: The base implementation calls onFilter() for top-level/explicit paints only.
59      *        To also filter encapsulated paints (e.g. SkPicture, SkTextBlob), clients may need to
60      *        override the relevant methods (i.e. drawPicture, drawTextBlob).
61      */
62     virtual bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const = 0;
63 
64     void onDrawPaint(const SkPaint&) override;
65     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
66     void onDrawRect(const SkRect&, const SkPaint&) override;
67     void onDrawRRect(const SkRRect&, const SkPaint&) override;
68     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
69     void onDrawOval(const SkRect&, const SkPaint&) override;
70     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
71     void onDrawPath(const SkPath&, const SkPaint&) override;
72     void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
73     void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
74                           SrcRectConstraint) override;
75     void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
76                           const SkPaint*) override;
77     void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
78     void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
79                          const SkPaint*, SrcRectConstraint) override;
80     void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
81                          const SkPaint*) override;
82     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
83     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
84                              const SkPoint texCoords[4], SkBlendMode,
85                              const SkPaint& paint) override;
86     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
87     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
88 
89     void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
90                     const SkPaint&) override;
91     void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
92                        const SkPaint&) override;
93     void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
94                         SkScalar constY, const SkPaint&) override;
95     void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
96                           const SkMatrix* matrix, 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 
102     // Forwarded to the wrapped canvas.
103     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
104     bool onPeekPixels(SkPixmap* pixmap) override;
105     bool onAccessTopLayerPixels(SkPixmap* pixmap) override;
106     SkImageInfo onImageInfo() const override;
107     bool onGetProps(SkSurfaceProps* props) const override;
108 
109 private:
110     class AutoPaintFilter;
111 
proxy()112     SkCanvas* proxy() const { SkASSERT(fList.count() == 1); return fList[0]; }
113 
114     typedef SkNWayCanvas INHERITED;
115 };
116 
117 #endif
118