blob: 3e7306fb935f4d9976a42846fb59a2445c6d8e56 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkLayerDrawLooper_DEFINED
9#define SkLayerDrawLooper_DEFINED
10
11#include "SkDrawLooper.h"
reed@google.com0716c632011-04-12 18:32:06 +000012#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
14struct SkPoint;
15
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000016class SK_API SkLayerDrawLooper : public SkDrawLooper {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017public:
18 SkLayerDrawLooper();
19 virtual ~SkLayerDrawLooper();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000020
reed@google.com0716c632011-04-12 18:32:06 +000021 /**
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.
reed@google.com84260582011-11-21 16:42:10 +000025 * 0 means ignore the layer's paint... except that LayerInfo's fFlagsMask
26 * and fColorMode are always applied.
reed@google.com0716c632011-04-12 18:32:06 +000027 */
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000028 enum Bits {
reed@google.com0716c632011-04-12 18:32:06 +000029 kStyle_Bit = 1 << 0, //!< use this layer's Style/stroke settings
30 kTextSkewX_Bit = 1 << 1, //!< use this layer's textskewx
31 kPathEffect_Bit = 1 << 2, //!< use this layer's patheffect
32 kMaskFilter_Bit = 1 << 3, //!< use this layer's maskfilter
33 kShader_Bit = 1 << 4, //!< use this layer's shader
34 kColorFilter_Bit = 1 << 5, //!< use this layer's colorfilter
35 kXfermode_Bit = 1 << 6, //!< use this layer's xfermode
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000036
reed@google.com84260582011-11-21 16:42:10 +000037 /**
38 * Use the layer's paint entirely, with these exceptions:
39 * - We never override the draw's paint's text_encoding, since that is
40 * used to interpret the text/len parameters in draw[Pos]Text.
41 * - Flags and Color are always computed using the LayerInfo's
42 * fFlagsMask and fColorMode.
43 */
44 kEntirePaint_Bits = -1,
45
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000046 };
47 typedef int32_t BitFlags;
reed@google.com0716c632011-04-12 18:32:06 +000048
49 /**
50 * Info for how to apply the layer's paint and offset.
51 *
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000052 * fFlagsMask selects which flags in the layer's paint should be applied.
53 * result = (draw-flags & ~fFlagsMask) | (layer-flags & fFlagsMask)
54 * In the extreme:
55 * If fFlagsMask is 0, we ignore all of the layer's flags
56 * If fFlagsMask is -1, we use all of the layer's flags
57 *
reed@google.com0716c632011-04-12 18:32:06 +000058 * fColorMode controls how we compute the final color for the layer:
59 * The layer's paint's color is treated as the SRC
60 * The draw's paint's color is treated as the DST
61 * final-color = Mode(layers-color, draws-color);
62 * Any SkXfermode::Mode will work. Two common choices are:
63 * kSrc_Mode: to use the layer's color, ignoring the draw's
64 * kDst_Mode: to just keep the draw's color, ignoring the layer's
65 */
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000066 struct SK_API LayerInfo {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000067 uint32_t fFlagsMask; // SkPaint::Flags
reed@google.com0716c632011-04-12 18:32:06 +000068 BitFlags fPaintBits;
69 SkXfermode::Mode fColorMode;
70 SkVector fOffset;
71 bool fPostTranslate; //!< applies to fOffset
72
73 /**
74 * Initial the LayerInfo. Defaults to settings that will draw the
75 * layer with no changes: e.g.
76 * fPaintBits == 0
77 * fColorMode == kDst_Mode
78 * fOffset == (0, 0)
79 */
80 LayerInfo();
81 };
82
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000083 /**
84 * Call for each layer you want to add (from top to bottom).
85 * This returns a paint you can modify, but that ptr is only valid until
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000086 * the next call made to addLayer().
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 */
reed@google.com0716c632011-04-12 18:32:06 +000088 SkPaint* addLayer(const LayerInfo&);
89
90 /**
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000091 * This layer will draw with the original paint, ad the specified offset
reed@google.com0716c632011-04-12 18:32:06 +000092 */
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000093 void addLayer(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000095 /**
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000096 * This layer will with the original paint and no offset.
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 */
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000098 void addLayer() { this->addLayer(0, 0); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000099
100 // overrides from SkDrawLooper
reed@google.com4e2b3d32011-04-07 14:18:59 +0000101 virtual void init(SkCanvas*);
102 virtual bool next(SkCanvas*, SkPaint* paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
djsollen@google.comba28d032012-03-26 17:57:35 +0000104 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLayerDrawLooper)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105
106protected:
107 SkLayerDrawLooper(SkFlattenableReadBuffer&);
108
109 // overrides from SkFlattenable
110 virtual void flatten(SkFlattenableWriteBuffer& );
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111
112private:
113 struct Rec {
114 Rec* fNext;
115 SkPaint fPaint;
reed@google.com0716c632011-04-12 18:32:06 +0000116 LayerInfo fInfo;
117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 static Rec* Reverse(Rec*);
119 };
120 Rec* fRecs;
121 int fCount;
reed@google.com4e2b3d32011-04-07 14:18:59 +0000122
123 // state-machine during the init/next cycle
124 Rec* fCurrRec;
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000125
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000126 static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 class MyRegistrar : public SkFlattenable::Registrar {
129 public:
130 MyRegistrar();
131 };
132
133 typedef SkDrawLooper INHERITED;
134};
135
136
137#endif