blob: 1e76ce21e5817a90854d136bdfb224e637854c01 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.org6806fe82012-10-12 14:41:39 +00007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkLayerDrawLooper_DEFINED
9#define SkLayerDrawLooper_DEFINED
10
11#include "SkDrawLooper.h"
commit-bot@chromium.org5da3f222013-05-14 20:11:23 +000012#include "SkPaint.h"
13#include "SkPoint.h"
reed@google.com0716c632011-04-12 18:32:06 +000014#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000016class SK_API SkLayerDrawLooper : public SkDrawLooper {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000018 SK_DECLARE_INST_COUNT(SkLayerDrawLooper)
19
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 SkLayerDrawLooper();
21 virtual ~SkLayerDrawLooper();
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000022
reed@google.com0716c632011-04-12 18:32:06 +000023 /**
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.com842292f2014-02-28 15:38:22 +000027 * 0 means ignore the layer's paint... except for fColorMode, which is
28 * always applied.
reed@google.com0716c632011-04-12 18:32:06 +000029 */
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000030 enum Bits {
reed@google.com0716c632011-04-12 18:32:06 +000031 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.comfbfcd562012-08-23 18:09:54 +000038
reed@google.com84260582011-11-21 16:42:10 +000039 /**
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.
reed@google.com842292f2014-02-28 15:38:22 +000043 * - Color is always computed using the LayerInfo's fColorMode.
reed@google.com84260582011-11-21 16:42:10 +000044 */
tomhudson@google.com1f902872012-06-01 13:15:47 +000045 kEntirePaint_Bits = -1
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000047 };
48 typedef int32_t BitFlags;
reed@google.com0716c632011-04-12 18:32:06 +000049
50 /**
51 * Info for how to apply the layer's paint and offset.
52 *
53 * fColorMode controls how we compute the final color for the layer:
54 * The layer's paint's color is treated as the SRC
55 * The draw's paint's color is treated as the DST
56 * final-color = Mode(layers-color, draws-color);
57 * Any SkXfermode::Mode will work. Two common choices are:
58 * kSrc_Mode: to use the layer's color, ignoring the draw's
59 * kDst_Mode: to just keep the draw's color, ignoring the layer's
60 */
bsalomon@google.com8c3ff172011-04-15 15:42:24 +000061 struct SK_API LayerInfo {
reed@google.com0716c632011-04-12 18:32:06 +000062 BitFlags fPaintBits;
63 SkXfermode::Mode fColorMode;
64 SkVector fOffset;
65 bool fPostTranslate; //!< applies to fOffset
66
67 /**
68 * Initial the LayerInfo. Defaults to settings that will draw the
69 * layer with no changes: e.g.
70 * fPaintBits == 0
71 * fColorMode == kDst_Mode
72 * fOffset == (0, 0)
73 */
74 LayerInfo();
75 };
76
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000077 /**
78 * Call for each layer you want to add (from top to bottom).
79 * This returns a paint you can modify, but that ptr is only valid until
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000080 * the next call made to addLayer().
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 */
reed@google.com0716c632011-04-12 18:32:06 +000082 SkPaint* addLayer(const LayerInfo&);
83
84 /**
robertphillips@google.com4991b8f2013-01-28 20:21:59 +000085 * This layer will draw with the original paint, at the specified offset
reed@google.com0716c632011-04-12 18:32:06 +000086 */
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000087 void addLayer(SkScalar dx, SkScalar dy);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +000089 /**
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000090 * This layer will with the original paint and no offset.
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 */
mike@reedtribe.orga8282ef2011-04-14 01:22:45 +000092 void addLayer() { this->addLayer(0, 0); }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000093
commit-bot@chromium.org8f838252013-05-22 12:35:50 +000094 /// Similar to addLayer, but adds a layer to the top.
95 SkPaint* addLayerOnTop(const LayerInfo&);
96
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +000097 virtual SkDrawLooper::Context* createContext(SkCanvas*, void* storage) const SK_OVERRIDE;
98
99 virtual size_t contextSize() const SK_OVERRIDE { return sizeof(LayerDrawLooperContext); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000101 SK_TO_STRING_OVERRIDE()
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000102
103 /// Implements Flattenable.
104 virtual Factory getFactory() const SK_OVERRIDE { return CreateProc; }
105 static SkFlattenable* CreateProc(SkReadBuffer& buffer);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000106
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000108 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110private:
111 struct Rec {
112 Rec* fNext;
113 SkPaint fPaint;
reed@google.com0716c632011-04-12 18:32:06 +0000114 LayerInfo fInfo;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 };
116 Rec* fRecs;
commit-bot@chromium.org8f838252013-05-22 12:35:50 +0000117 Rec* fTopRec;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 int fCount;
reed@google.com4e2b3d32011-04-07 14:18:59 +0000119
120 // state-machine during the init/next cycle
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000121 class LayerDrawLooperContext : public SkDrawLooper::Context {
122 public:
123 explicit LayerDrawLooperContext(const SkLayerDrawLooper* looper);
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000124
commit-bot@chromium.org79fbb402014-03-12 09:42:01 +0000125 protected:
126 virtual bool next(SkCanvas*, SkPaint* paint) SK_OVERRIDE;
127
128 private:
129 Rec* fCurrRec;
130
131 static void ApplyInfo(SkPaint* dst, const SkPaint& src, const LayerInfo&);
132 };
mike@reedtribe.org0e2810b2011-04-08 02:41:54 +0000133
reed@android.com8a1c16f2008-12-17 15:59:43 +0000134 class MyRegistrar : public SkFlattenable::Registrar {
135 public:
136 MyRegistrar();
137 };
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 typedef SkDrawLooper INHERITED;
commit-bot@chromium.org74ba2f62014-02-14 10:06:42 +0000140
141public:
142 class SK_API Builder {
143 public:
144 Builder();
145 ~Builder();
146
147 /**
148 * Call for each layer you want to add (from top to bottom).
149 * This returns a paint you can modify, but that ptr is only valid until
150 * the next call made to addLayer().
151 */
152 SkPaint* addLayer(const LayerInfo&);
153
154 /**
155 * This layer will draw with the original paint, at the specified offset
156 */
157 void addLayer(SkScalar dx, SkScalar dy);
158
159 /**
160 * This layer will with the original paint and no offset.
161 */
162 void addLayer() { this->addLayer(0, 0); }
163
164 /// Similar to addLayer, but adds a layer to the top.
165 SkPaint* addLayerOnTop(const LayerInfo&);
166
167 /**
168 * Pass list of layers on to newly built looper and return it. This will
169 * also reset the builder, so it can be used to build another looper.
170 */
171 SkLayerDrawLooper* detachLooper();
172
173 private:
174 Rec* fRecs;
175 Rec* fTopRec;
176 int fCount;
177 };
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178};
179
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180#endif