OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkDrawGradient.h" | 10 #include "SkDrawGradient.h" |
11 #include "SkAnimateMaker.h" | 11 #include "SkAnimateMaker.h" |
12 #include "SkAnimatorScript.h" | 12 #include "SkAnimatorScript.h" |
13 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
| 14 #include "SkUnitMapper.h" |
| 15 |
| 16 static SkScalar SkUnitToScalar(U16CPU x) { |
| 17 return x / 65535.0f; |
| 18 } |
| 19 |
| 20 static U16CPU SkScalarToUnit(SkScalar x) { |
| 21 SkScalar pin = SkScalarPin(x, 0, SK_Scalar1); |
| 22 return (int) (pin * 65535.0f); |
| 23 } |
| 24 |
| 25 class SkDrawGradientUnitMapper : public SkUnitMapper { |
| 26 public: |
| 27 SkDrawGradientUnitMapper(SkAnimateMaker* maker, const char* script) : fMaker
(maker), fScript(script) { |
| 28 } |
| 29 |
| 30 SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 31 |
| 32 protected: |
| 33 virtual uint16_t mapUnit16(uint16_t x) { |
| 34 fUnit = SkUnitToScalar(x); |
| 35 SkScriptValue value; |
| 36 SkAnimatorScript engine(*fMaker, NULL, SkType_Float); |
| 37 engine.propertyCallBack(GetUnitValue, &fUnit); |
| 38 if (engine.evaluate(fScript, &value, SkType_Float)) |
| 39 x = SkScalarToUnit(value.fOperand.fScalar); |
| 40 return x; |
| 41 } |
| 42 |
| 43 static bool GetUnitValue(const char* token, size_t len, void* unitPtr, SkScr
iptValue* value) { |
| 44 if (SK_LITERAL_STR_EQUAL("unit", token, len)) { |
| 45 value->fOperand.fScalar = *(SkScalar*) unitPtr; |
| 46 value->fType = SkType_Float; |
| 47 return true; |
| 48 } |
| 49 return false; |
| 50 } |
| 51 |
| 52 SkAnimateMaker* fMaker; |
| 53 const char* fScript; |
| 54 SkScalar fUnit; |
| 55 }; |
| 56 |
14 | 57 |
15 #if SK_USE_CONDENSED_INFO == 0 | 58 #if SK_USE_CONDENSED_INFO == 0 |
16 | 59 |
17 const SkMemberInfo SkDrawGradient::fInfo[] = { | 60 const SkMemberInfo SkDrawGradient::fInfo[] = { |
18 SK_MEMBER_INHERITED, | 61 SK_MEMBER_INHERITED, |
19 SK_MEMBER_ARRAY(offsets, Float), | 62 SK_MEMBER_ARRAY(offsets, Float), |
20 SK_MEMBER(unitMapper, String) | 63 SK_MEMBER(unitMapper, String) |
21 }; | 64 }; |
22 | 65 |
23 #endif | 66 #endif |
24 | 67 |
25 DEFINE_GET_MEMBER(SkDrawGradient); | 68 DEFINE_GET_MEMBER(SkDrawGradient); |
26 | 69 |
27 SkDrawGradient::SkDrawGradient() { | 70 SkDrawGradient::SkDrawGradient() : fUnitMapper(NULL) { |
28 } | 71 } |
29 | 72 |
30 SkDrawGradient::~SkDrawGradient() { | 73 SkDrawGradient::~SkDrawGradient() { |
31 for (int index = 0; index < fDrawColors.count(); index++) | 74 for (int index = 0; index < fDrawColors.count(); index++) |
32 delete fDrawColors[index]; | 75 delete fDrawColors[index]; |
| 76 delete fUnitMapper; |
33 } | 77 } |
34 | 78 |
35 bool SkDrawGradient::addChild(SkAnimateMaker& , SkDisplayable* child) { | 79 bool SkDrawGradient::addChild(SkAnimateMaker& , SkDisplayable* child) { |
36 SkASSERT(child); | 80 SkASSERT(child); |
37 if (child->isColor()) { | 81 if (child->isColor()) { |
38 SkDrawColor* color = (SkDrawColor*) child; | 82 SkDrawColor* color = (SkDrawColor*) child; |
39 *fDrawColors.append() = color; | 83 *fDrawColors.append() = color; |
40 return true; | 84 return true; |
41 } | 85 } |
42 return false; | 86 return false; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 if (offsets[i] <= offsets[i-1]) { | 131 if (offsets[i] <= offsets[i-1]) { |
88 maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMust
Increase); | 132 maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMust
Increase); |
89 return; | 133 return; |
90 } | 134 } |
91 if (offsets[i] > SK_Scalar1) { | 135 if (offsets[i] > SK_Scalar1) { |
92 maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMust
BeNoMoreThanOne); | 136 maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMust
BeNoMoreThanOne); |
93 return; | 137 return; |
94 } | 138 } |
95 } | 139 } |
96 } | 140 } |
| 141 if (unitMapper.size() > 0) |
| 142 fUnitMapper = new SkDrawGradientUnitMapper(&maker, unitMapper.c_str()); |
97 INHERITED::onEndElement(maker); | 143 INHERITED::onEndElement(maker); |
98 } | 144 } |
99 | 145 |
100 #if SK_USE_CONDENSED_INFO == 0 | 146 #if SK_USE_CONDENSED_INFO == 0 |
101 | 147 |
102 const SkMemberInfo SkDrawLinearGradient::fInfo[] = { | 148 const SkMemberInfo SkDrawLinearGradient::fInfo[] = { |
103 SK_MEMBER_INHERITED, | 149 SK_MEMBER_INHERITED, |
104 SK_MEMBER_ARRAY(points, Float), | 150 SK_MEMBER_ARRAY(points, Float), |
105 }; | 151 }; |
106 | 152 |
(...skipping 16 matching lines...) Expand all Loading... |
123 dumpBase(maker); | 169 dumpBase(maker); |
124 dumpRest(maker); | 170 dumpRest(maker); |
125 } | 171 } |
126 #endif | 172 #endif |
127 | 173 |
128 SkShader* SkDrawLinearGradient::getShader() { | 174 SkShader* SkDrawLinearGradient::getShader() { |
129 if (addPrelude() == 0 || points.count() != 4) | 175 if (addPrelude() == 0 || points.count() != 4) |
130 return NULL; | 176 return NULL; |
131 SkShader* shader = SkGradientShader::CreateLinear((SkPoint*)points.begin(), | 177 SkShader* shader = SkGradientShader::CreateLinear((SkPoint*)points.begin(), |
132 fColors.begin(), offsets.begin(), fColors.count(), (SkShader::TileMode)
tileMode, | 178 fColors.begin(), offsets.begin(), fColors.count(), (SkShader::TileMode)
tileMode, |
133 0, getMatrix()); | 179 fUnitMapper, 0, getMatrix()); |
134 SkAutoTDelete<SkShader> autoDel(shader); | 180 SkAutoTDelete<SkShader> autoDel(shader); |
135 (void)autoDel.detach(); | 181 (void)autoDel.detach(); |
136 return shader; | 182 return shader; |
137 } | 183 } |
138 | 184 |
139 | 185 |
140 #if SK_USE_CONDENSED_INFO == 0 | 186 #if SK_USE_CONDENSED_INFO == 0 |
141 | 187 |
142 const SkMemberInfo SkDrawRadialGradient::fInfo[] = { | 188 const SkMemberInfo SkDrawRadialGradient::fInfo[] = { |
143 SK_MEMBER_INHERITED, | 189 SK_MEMBER_INHERITED, |
(...skipping 14 matching lines...) Expand all Loading... |
158 dumpBase(maker); | 204 dumpBase(maker); |
159 dumpRest(maker); | 205 dumpRest(maker); |
160 } | 206 } |
161 #endif | 207 #endif |
162 | 208 |
163 SkShader* SkDrawRadialGradient::getShader() { | 209 SkShader* SkDrawRadialGradient::getShader() { |
164 if (addPrelude() == 0) | 210 if (addPrelude() == 0) |
165 return NULL; | 211 return NULL; |
166 SkShader* shader = SkGradientShader::CreateRadial(center, | 212 SkShader* shader = SkGradientShader::CreateRadial(center, |
167 radius, fColors.begin(), offsets.begin(), fColors.count(), (SkShader::Ti
leMode) tileMode, | 213 radius, fColors.begin(), offsets.begin(), fColors.count(), (SkShader::Ti
leMode) tileMode, |
168 0, getMatrix()); | 214 fUnitMapper, 0, getMatrix()); |
169 SkAutoTDelete<SkShader> autoDel(shader); | 215 SkAutoTDelete<SkShader> autoDel(shader); |
170 (void)autoDel.detach(); | 216 (void)autoDel.detach(); |
171 return shader; | 217 return shader; |
172 } | 218 } |
OLD | NEW |