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 GrRecordingContext* recordingContext() override { return proxy()->recordingContext(); }
36 GrRenderTargetContext* internal_private_accessTopLayerRenderTargetContext() override {
37 return proxy()->internal_private_accessTopLayerRenderTargetContext();
38 }
39
40protected:
41 /**
42 * Called with the paint that will be used to draw the specified type.
43 * The implementation may modify the paint as they wish.
44 *
45 * The result bool is used to determine whether the draw op is to be
46 * executed (true) or skipped (false).
47 *
48 * Note: The base implementation calls onFilter() for top-level/explicit paints only.
49 * To also filter encapsulated paints (e.g. SkPicture, SkTextBlob), clients may need to
50 * override the relevant methods (i.e. drawPicture, drawTextBlob).
51 */
52 virtual bool onFilter(SkPaint& paint) const = 0;
53
54 void onDrawPaint(const SkPaint&) override;
55 void onDrawBehind(const SkPaint&) override;
56 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
57 void onDrawRect(const SkRect&, const SkPaint&) override;
58 void onDrawRRect(const SkRRect&, const SkPaint&) override;
59 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
60 void onDrawRegion(const SkRegion&, const SkPaint&) override;
61 void onDrawOval(const SkRect&, const SkPaint&) override;
62 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
63 void onDrawPath(const SkPath&, const SkPaint&) override;
64 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
65 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
66 const SkPaint*, SrcRectConstraint) override;
67 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
68 const SkPaint*) override;
69 void onDrawImageLattice(const SkImage*, const Lattice&, const SkRect&,
70 const SkPaint*) override;
71 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
72 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
73 const SkPoint texCoords[4], SkBlendMode,
74 const SkPaint& paint) override;
75 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
76 void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
77
78 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
79 const SkPaint& paint) override;
80 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
81 int, SkBlendMode, const SkRect*, const SkPaint*) override;
82 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override;
83 void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
84
85 void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&,
86 SkBlendMode) override;
87 void onDrawEdgeAAImageSet(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
88 const SkPaint*, SrcRectConstraint) override;
89
90 // Forwarded to the wrapped canvas.
91 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
92 bool onPeekPixels(SkPixmap* pixmap) override;
93 bool onAccessTopLayerPixels(SkPixmap* pixmap) override;
94 SkImageInfo onImageInfo() const override;
95 bool onGetProps(SkSurfaceProps* props) const override;
96
97private:
98 class AutoPaintFilter;
99
100 SkCanvas* proxy() const { SkASSERT(fList.count() == 1); return fList[0]; }
101
102 SkPaintFilterCanvas* internal_private_asPaintFilterCanvas() const override {
103 return const_cast<SkPaintFilterCanvas*>(this);
104 }
105
106 friend class SkAndroidFrameworkUtils;
107};
108
109#endif
110