fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 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" |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame^] | 12 | #include "SkTLazy.h" |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 13 | |
| 14 | /** \class SkPaintFilterCanvas |
| 15 | |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame^] | 16 | A utility proxy base class for implementing draw/paint filters. |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 17 | */ |
| 18 | class SK_API SkPaintFilterCanvas : public SkNWayCanvas { |
| 19 | public: |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 20 | /** |
| 21 | * DEPRECATED: use the variant below. |
| 22 | */ |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 23 | SkPaintFilterCanvas(int width, int height); |
| 24 | |
fmalita | f433bb2 | 2015-08-17 08:05:13 -0700 | [diff] [blame] | 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 | |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 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. |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 52 | * |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame^] | 53 | * Upon return, if filteredPaint is initialized it will replace the original paint |
| 54 | * for the current draw. Note that that implementation is responsible for |
| 55 | * initializing *filteredPaint (e.g. via set(*paint)). |
| 56 | * |
| 57 | * The result bool is used to determine whether the draw op is to be |
| 58 | * executed (true) or skipped (false). When the draw is skipped, filteredPaint is |
| 59 | * ignored. |
| 60 | * |
| 61 | * Note: The base implementation calls onFilter() for top-level/explicit paints only. |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 62 | * To also filter encapsulated paints (e.g. SkPicture, SkTextBlob), clients may need to |
| 63 | * override the relevant methods (i.e. drawPicture, drawTextBlob). |
| 64 | */ |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame^] | 65 | virtual bool onFilter(const SkPaint* paint, Type type, SkTLazy<SkPaint>* filteredPaint) const { |
| 66 | if (paint) { |
| 67 | this->onFilterPaint(filteredPaint->set(*paint), type); |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | // DEPRECATED - do not use |
| 73 | virtual void onFilterPaint(SkPaint*, Type) const { } |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 74 | |
| 75 | void onDrawPaint(const SkPaint&) override; |
| 76 | void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; |
| 77 | void onDrawRect(const SkRect&, const SkPaint&) override; |
| 78 | void onDrawRRect(const SkRRect&, const SkPaint&) override; |
| 79 | void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; |
| 80 | void onDrawOval(const SkRect&, const SkPaint&) override; |
| 81 | void onDrawPath(const SkPath&, const SkPaint&) override; |
| 82 | void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override; |
| 83 | void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 84 | SrcRectConstraint) override; |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame^] | 85 | void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst, |
| 86 | const SkPaint*) override; |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 87 | void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; |
| 88 | void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 89 | const SkPaint*, SrcRectConstraint) override; |
fmalita | bad23dc | 2016-01-11 13:58:29 -0800 | [diff] [blame^] | 90 | void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, |
| 91 | const SkPaint*) override; |
fmalita | 65cdb57 | 2015-03-26 07:24:48 -0700 | [diff] [blame] | 92 | void onDrawVertices(VertexMode vmode, int vertexCount, |
| 93 | const SkPoint vertices[], const SkPoint texs[], |
| 94 | const SkColor colors[], SkXfermode* xmode, |
| 95 | const uint16_t indices[], int indexCount, |
| 96 | const SkPaint&) override; |
| 97 | void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 98 | const SkPoint texCoords[4], SkXfermode* xmode, |
| 99 | const SkPaint& paint) override; |
| 100 | void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; |
| 101 | |
| 102 | void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 103 | const SkPaint&) override; |
| 104 | void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 105 | const SkPaint&) override; |
| 106 | void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 107 | SkScalar constY, const SkPaint&) override; |
| 108 | void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 109 | const SkMatrix* matrix, const SkPaint&) override; |
| 110 | void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 111 | const SkPaint& paint) override; |
| 112 | |
| 113 | private: |
| 114 | class AutoPaintFilter; |
| 115 | |
| 116 | typedef SkNWayCanvas INHERITED; |
| 117 | }; |
| 118 | |
| 119 | #endif |