epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
tfarina@chromium.org | 6806fe8 | 2012-10-12 14:41:39 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkLayerDrawLooper_DEFINED |
| 9 | #define SkLayerDrawLooper_DEFINED |
| 10 | |
| 11 | #include "SkDrawLooper.h" |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 12 | #include "SkXfermode.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
| 14 | struct SkPoint; |
| 15 | |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 16 | class SK_API SkLayerDrawLooper : public SkDrawLooper { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 18 | SK_DECLARE_INST_COUNT(SkLayerDrawLooper) |
| 19 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | SkLayerDrawLooper(); |
| 21 | virtual ~SkLayerDrawLooper(); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 22 | |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Bits specifies which aspects of the layer's paint should replace the |
| 25 | * corresponding aspects on the draw's paint. |
| 26 | * kEntirePaint_Bits means use the layer's paint completely. |
reed@google.com | 8426058 | 2011-11-21 16:42:10 +0000 | [diff] [blame] | 27 | * 0 means ignore the layer's paint... except that LayerInfo's fFlagsMask |
| 28 | * and fColorMode are always applied. |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 29 | */ |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 30 | enum Bits { |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 31 | kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings |
| 32 | kTextSkewX_Bit = 1 << 1, //!< use this layer's textskewx |
| 33 | kPathEffect_Bit = 1 << 2, //!< use this layer's patheffect |
| 34 | kMaskFilter_Bit = 1 << 3, //!< use this layer's maskfilter |
| 35 | kShader_Bit = 1 << 4, //!< use this layer's shader |
| 36 | kColorFilter_Bit = 1 << 5, //!< use this layer's colorfilter |
| 37 | kXfermode_Bit = 1 << 6, //!< use this layer's xfermode |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 38 | |
reed@google.com | 8426058 | 2011-11-21 16:42:10 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Use the layer's paint entirely, with these exceptions: |
| 41 | * - We never override the draw's paint's text_encoding, since that is |
| 42 | * used to interpret the text/len parameters in draw[Pos]Text. |
| 43 | * - Flags and Color are always computed using the LayerInfo's |
| 44 | * fFlagsMask and fColorMode. |
| 45 | */ |
tomhudson@google.com | 1f90287 | 2012-06-01 13:15:47 +0000 | [diff] [blame] | 46 | kEntirePaint_Bits = -1 |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 47 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 48 | }; |
| 49 | typedef int32_t BitFlags; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Info for how to apply the layer's paint and offset. |
| 53 | * |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 54 | * fFlagsMask selects which flags in the layer's paint should be applied. |
| 55 | * result = (draw-flags & ~fFlagsMask) | (layer-flags & fFlagsMask) |
| 56 | * In the extreme: |
| 57 | * If fFlagsMask is 0, we ignore all of the layer's flags |
| 58 | * If fFlagsMask is -1, we use all of the layer's flags |
| 59 | * |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 60 | * fColorMode controls how we compute the final color for the layer: |
| 61 | * The layer's paint's color is treated as the SRC |
| 62 | * The draw's paint's color is treated as the DST |
| 63 | * final-color = Mode(layers-color, draws-color); |
| 64 | * Any SkXfermode::Mode will work. Two common choices are: |
| 65 | * kSrc_Mode: to use the layer's color, ignoring the draw's |
| 66 | * kDst_Mode: to just keep the draw's color, ignoring the layer's |
| 67 | */ |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 68 | struct SK_API LayerInfo { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 69 | uint32_t fFlagsMask; // SkPaint::Flags |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 70 | BitFlags fPaintBits; |
| 71 | SkXfermode::Mode fColorMode; |
| 72 | SkVector fOffset; |
| 73 | bool fPostTranslate; //!< applies to fOffset |
| 74 | |
| 75 | /** |
| 76 | * Initial the LayerInfo. Defaults to settings that will draw the |
| 77 | * layer with no changes: e.g. |
| 78 | * fPaintBits == 0 |
| 79 | * fColorMode == kDst_Mode |
| 80 | * fOffset == (0, 0) |
| 81 | */ |
| 82 | LayerInfo(); |
| 83 | }; |
| 84 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Call for each layer you want to add (from top to bottom). |
| 87 | * This returns a paint you can modify, but that ptr is only valid until |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 88 | * the next call made to addLayer(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | */ |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 90 | SkPaint* addLayer(const LayerInfo&); |
| 91 | |
| 92 | /** |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 93 | * This layer will draw with the original paint, at the specified offset |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 94 | */ |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 95 | void addLayer(SkScalar dx, SkScalar dy); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 96 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 97 | /** |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 98 | * This layer will with the original paint and no offset. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | */ |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 100 | void addLayer() { this->addLayer(0, 0); } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 101 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | // overrides from SkDrawLooper |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 103 | virtual void init(SkCanvas*); |
| 104 | virtual bool next(SkCanvas*, SkPaint* paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | |
robertphillips@google.com | 4991b8f | 2013-01-28 20:21:59 +0000 | [diff] [blame] | 106 | SK_DEVELOPER_TO_STRING() |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 107 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLayerDrawLooper) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 108 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | protected: |
| 110 | SkLayerDrawLooper(SkFlattenableReadBuffer&); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 111 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | private: |
| 114 | struct Rec { |
| 115 | Rec* fNext; |
| 116 | SkPaint fPaint; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 117 | LayerInfo fInfo; |
| 118 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | static Rec* Reverse(Rec*); |
| 120 | }; |
| 121 | Rec* fRecs; |
| 122 | int fCount; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 123 | |
| 124 | // state-machine during the init/next cycle |
| 125 | Rec* fCurrRec; |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 126 | |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 127 | static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 128 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | class MyRegistrar : public SkFlattenable::Registrar { |
| 130 | public: |
| 131 | MyRegistrar(); |
| 132 | }; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | typedef SkDrawLooper INHERITED; |
| 135 | }; |
| 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | #endif |