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
14class SkAndroidFrameworkUtils;
15
16/** \class SkPaintFilterCanvas
17
18 A utility proxy base class for implementing draw/paint filters.
19*/
20class SK_API SkPaintFilterCanvas : public SkCanvasVirtualEnforcer<SkNWayCanvas> {
21public:
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.
33 SkISize getBaseLayerSize() const override { return proxy()->getBaseLayerSize(); }
34 GrContext* getGrContext() override { return proxy()->getGrContext(); }
35 GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext() override {
36 return proxy()->internal_private_accessTopLayerRenderTargetContext();
37 }
38
39protected:
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 onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
64 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
65 const SkPaint*, SrcRectConstraint) override;
66 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
67 const SkPaint*) override;
68 void onDrawImageLattice(const SkImage*, const Lattice&, const SkRect&,
69 const SkPaint*) override;
70 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
71 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
72 const SkPoint texCoords[4], SkBlendMode,
73 const SkPaint& paint) override;
74 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
75 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
76
77 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
78 const SkPaint& paint) override;
79 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
80 int, SkBlendMode, const SkRect*, const SkPaint*) override;
81 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override;
82 void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
83
84 void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&,
85 SkBlendMode) override;
86 void onDrawEdgeAAImageSet(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
87 const SkPaint*, SrcRectConstraint) override;
88
89 // Forwarded to the wrapped canvas.
90 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
91 bool onPeekPixels(SkPixmap* pixmap) override;
92 bool onAccessTopLayerPixels(SkPixmap* pixmap) override;
93 SkImageInfo onImageInfo() const override;
94 bool onGetProps(SkSurfaceProps* props) const override;
95
96private:
97 class AutoPaintFilter;
98
99 SkCanvas* proxy() const { SkASSERT(fList.count() == 1); return fList[0]; }
100
101 SkPaintFilterCanvas* internal_private_asPaintFilterCanvas() const override {
102 return const_cast<SkPaintFilterCanvas*>(this);
103 }
104
105 friend class SkAndroidFrameworkUtils;
106};
107
108#endif
109