blob: 3440186e170dedfdca4a39a1236d5c89738777f5 [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.
25 * 0 means ignore the layer's paint.
26 */
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000027 enum Bits {
reed@google.com0716c632011-04-12 18:32:06 +000028 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.org0e2810b2011-04-08 02:41:54 +000035
36 kEntirePaint_Bits = -1, //!< use this layer's paint entirely
37 };
38 typedef int32_t BitFlags;
reed@google.com0716c632011-04-12 18:32:06 +000039
40 /**
41 * Info for how to apply the layer's paint and offset.
42 *
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000043 * 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.com0716c632011-04-12 18:32:06 +000049 * 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.com8c3ff172011-04-15 15:42:24 +000057 struct SK_API LayerInfo {
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000058 uint32_t fFlagsMask; // SkPaint::Flags
reed@google.com0716c632011-04-12 18:32:06 +000059 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.org0e2810b2011-04-08 02:41:54 +000074 /**
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.orga8282ef2011-04-14 01:22:45 +000077 * the next call made to addLayer().
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 */
reed@google.com0716c632011-04-12 18:32:06 +000079 SkPaint* addLayer(const LayerInfo&);
80
81 /**
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000082 * This layer will draw with the original paint, ad the specified offset
reed@google.com0716c632011-04-12 18:32:06 +000083 */
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000084 void addLayer(SkScalar dx, SkScalar dy);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000086 /**
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000087 * This layer will with the original paint and no offset.
reed@android.com8a1c16f2008-12-17 15:59:43 +000088 */
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000089 void addLayer() { this->addLayer(0, 0); }
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
91 // overrides from SkDrawLooper
reed@google.com4e2b3d32011-04-07 14:18:59 +000092 virtual void init(SkCanvas*);
93 virtual bool next(SkCanvas*, SkPaint* paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
95 // must be public for Registrar :(
96 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
97 return SkNEW_ARGS(SkLayerDrawLooper, (buffer));
98 }
99
100protected:
101 SkLayerDrawLooper(SkFlattenableReadBuffer&);
102
103 // overrides from SkFlattenable
104 virtual void flatten(SkFlattenableWriteBuffer& );
105 virtual Factory getFactory() { return CreateProc; }
106
107private:
108 struct Rec {
109 Rec* fNext;
110 SkPaint fPaint;
reed@google.com0716c632011-04-12 18:32:06 +0000111 LayerInfo fInfo;
112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 static Rec* Reverse(Rec*);
114 };
115 Rec* fRecs;
116 int fCount;
reed@google.com4e2b3d32011-04-07 14:18:59 +0000117
118 // state-machine during the init/next cycle
119 Rec* fCurrRec;
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000120
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +0000121 static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000122
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 class MyRegistrar : public SkFlattenable::Registrar {
124 public:
125 MyRegistrar();
126 };
127
128 typedef SkDrawLooper INHERITED;
129};
130
131
132#endif