epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 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: |
| 18 | SkLayerDrawLooper(); |
| 19 | virtual ~SkLayerDrawLooper(); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 20 | |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Bits specifies which aspects of the layer's paint should replace the |
| 23 | * corresponding aspects on the draw's paint. |
| 24 | * kEntirePaint_Bits means use the layer's paint completely. |
| 25 | * 0 means ignore the layer's paint. |
| 26 | */ |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 27 | enum Bits { |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 28 | kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings |
| 29 | kTextSkewX_Bit = 1 << 1, //!< use this layer's textskewx |
| 30 | kPathEffect_Bit = 1 << 2, //!< use this layer's patheffect |
| 31 | kMaskFilter_Bit = 1 << 3, //!< use this layer's maskfilter |
| 32 | kShader_Bit = 1 << 4, //!< use this layer's shader |
| 33 | kColorFilter_Bit = 1 << 5, //!< use this layer's colorfilter |
| 34 | kXfermode_Bit = 1 << 6, //!< use this layer's xfermode |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 35 | |
| 36 | kEntirePaint_Bits = -1, //!< use this layer's paint entirely |
| 37 | }; |
| 38 | typedef int32_t BitFlags; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Info for how to apply the layer's paint and offset. |
| 42 | * |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 43 | * fFlagsMask selects which flags in the layer's paint should be applied. |
| 44 | * result = (draw-flags & ~fFlagsMask) | (layer-flags & fFlagsMask) |
| 45 | * In the extreme: |
| 46 | * If fFlagsMask is 0, we ignore all of the layer's flags |
| 47 | * If fFlagsMask is -1, we use all of the layer's flags |
| 48 | * |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 49 | * fColorMode controls how we compute the final color for the layer: |
| 50 | * The layer's paint's color is treated as the SRC |
| 51 | * The draw's paint's color is treated as the DST |
| 52 | * final-color = Mode(layers-color, draws-color); |
| 53 | * Any SkXfermode::Mode will work. Two common choices are: |
| 54 | * kSrc_Mode: to use the layer's color, ignoring the draw's |
| 55 | * kDst_Mode: to just keep the draw's color, ignoring the layer's |
| 56 | */ |
bsalomon@google.com | 8c3ff17 | 2011-04-15 15:42:24 +0000 | [diff] [blame] | 57 | struct SK_API LayerInfo { |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 58 | uint32_t fFlagsMask; // SkPaint::Flags |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 59 | BitFlags fPaintBits; |
| 60 | SkXfermode::Mode fColorMode; |
| 61 | SkVector fOffset; |
| 62 | bool fPostTranslate; //!< applies to fOffset |
| 63 | |
| 64 | /** |
| 65 | * Initial the LayerInfo. Defaults to settings that will draw the |
| 66 | * layer with no changes: e.g. |
| 67 | * fPaintBits == 0 |
| 68 | * fColorMode == kDst_Mode |
| 69 | * fOffset == (0, 0) |
| 70 | */ |
| 71 | LayerInfo(); |
| 72 | }; |
| 73 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 74 | /** |
| 75 | * Call for each layer you want to add (from top to bottom). |
| 76 | * 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] | 77 | * the next call made to addLayer(). |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | */ |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 79 | SkPaint* addLayer(const LayerInfo&); |
| 80 | |
| 81 | /** |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 82 | * This layer will draw with the original paint, ad the specified offset |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 83 | */ |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 84 | void addLayer(SkScalar dx, SkScalar dy); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 86 | /** |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 87 | * This layer will with the original paint and no offset. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | */ |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 89 | void addLayer() { this->addLayer(0, 0); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | |
| 91 | // overrides from SkDrawLooper |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 92 | virtual void init(SkCanvas*); |
| 93 | virtual bool next(SkCanvas*, SkPaint* paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
| 95 | // must be public for Registrar :( |
| 96 | static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) { |
| 97 | return SkNEW_ARGS(SkLayerDrawLooper, (buffer)); |
| 98 | } |
| 99 | |
| 100 | protected: |
| 101 | SkLayerDrawLooper(SkFlattenableReadBuffer&); |
| 102 | |
| 103 | // overrides from SkFlattenable |
| 104 | virtual void flatten(SkFlattenableWriteBuffer& ); |
| 105 | virtual Factory getFactory() { return CreateProc; } |
| 106 | |
| 107 | private: |
| 108 | struct Rec { |
| 109 | Rec* fNext; |
| 110 | SkPaint fPaint; |
reed@google.com | 0716c63 | 2011-04-12 18:32:06 +0000 | [diff] [blame] | 111 | LayerInfo fInfo; |
| 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | static Rec* Reverse(Rec*); |
| 114 | }; |
| 115 | Rec* fRecs; |
| 116 | int fCount; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 117 | |
| 118 | // state-machine during the init/next cycle |
| 119 | Rec* fCurrRec; |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 120 | |
mike@reedtribe.org | a8282ef | 2011-04-14 01:22:45 +0000 | [diff] [blame] | 121 | static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&); |
mike@reedtribe.org | 0e2810b | 2011-04-08 02:41:54 +0000 | [diff] [blame] | 122 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | class MyRegistrar : public SkFlattenable::Registrar { |
| 124 | public: |
| 125 | MyRegistrar(); |
| 126 | }; |
| 127 | |
| 128 | typedef SkDrawLooper INHERITED; |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | #endif |