Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkDrawGradient.h" |
michael@0 | 11 | #include "SkAnimateMaker.h" |
michael@0 | 12 | #include "SkAnimatorScript.h" |
michael@0 | 13 | #include "SkGradientShader.h" |
michael@0 | 14 | #include "SkUnitMapper.h" |
michael@0 | 15 | |
michael@0 | 16 | static SkScalar SkUnitToScalar(U16CPU x) { |
michael@0 | 17 | return x / 65535.0f; |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | static U16CPU SkScalarToUnit(SkScalar x) { |
michael@0 | 21 | SkScalar pin = SkScalarPin(x, 0, SK_Scalar1); |
michael@0 | 22 | return (int) (pin * 65535.0f); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | class SkDrawGradientUnitMapper : public SkUnitMapper { |
michael@0 | 26 | public: |
michael@0 | 27 | SkDrawGradientUnitMapper(SkAnimateMaker* maker, const char* script) : fMaker(maker), fScript(script) { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | SK_DECLARE_UNFLATTENABLE_OBJECT() |
michael@0 | 31 | |
michael@0 | 32 | protected: |
michael@0 | 33 | virtual uint16_t mapUnit16(uint16_t x) { |
michael@0 | 34 | fUnit = SkUnitToScalar(x); |
michael@0 | 35 | SkScriptValue value; |
michael@0 | 36 | SkAnimatorScript engine(*fMaker, NULL, SkType_Float); |
michael@0 | 37 | engine.propertyCallBack(GetUnitValue, &fUnit); |
michael@0 | 38 | if (engine.evaluate(fScript, &value, SkType_Float)) |
michael@0 | 39 | x = SkScalarToUnit(value.fOperand.fScalar); |
michael@0 | 40 | return x; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | static bool GetUnitValue(const char* token, size_t len, void* unitPtr, SkScriptValue* value) { |
michael@0 | 44 | if (SK_LITERAL_STR_EQUAL("unit", token, len)) { |
michael@0 | 45 | value->fOperand.fScalar = *(SkScalar*) unitPtr; |
michael@0 | 46 | value->fType = SkType_Float; |
michael@0 | 47 | return true; |
michael@0 | 48 | } |
michael@0 | 49 | return false; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | SkAnimateMaker* fMaker; |
michael@0 | 53 | const char* fScript; |
michael@0 | 54 | SkScalar fUnit; |
michael@0 | 55 | }; |
michael@0 | 56 | |
michael@0 | 57 | |
michael@0 | 58 | #if SK_USE_CONDENSED_INFO == 0 |
michael@0 | 59 | |
michael@0 | 60 | const SkMemberInfo SkDrawGradient::fInfo[] = { |
michael@0 | 61 | SK_MEMBER_INHERITED, |
michael@0 | 62 | SK_MEMBER_ARRAY(offsets, Float), |
michael@0 | 63 | SK_MEMBER(unitMapper, String) |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | #endif |
michael@0 | 67 | |
michael@0 | 68 | DEFINE_GET_MEMBER(SkDrawGradient); |
michael@0 | 69 | |
michael@0 | 70 | SkDrawGradient::SkDrawGradient() : fUnitMapper(NULL) { |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | SkDrawGradient::~SkDrawGradient() { |
michael@0 | 74 | for (int index = 0; index < fDrawColors.count(); index++) |
michael@0 | 75 | delete fDrawColors[index]; |
michael@0 | 76 | delete fUnitMapper; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | bool SkDrawGradient::addChild(SkAnimateMaker& , SkDisplayable* child) { |
michael@0 | 80 | SkASSERT(child); |
michael@0 | 81 | if (child->isColor()) { |
michael@0 | 82 | SkDrawColor* color = (SkDrawColor*) child; |
michael@0 | 83 | *fDrawColors.append() = color; |
michael@0 | 84 | return true; |
michael@0 | 85 | } |
michael@0 | 86 | return false; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | int SkDrawGradient::addPrelude() { |
michael@0 | 90 | int count = fDrawColors.count(); |
michael@0 | 91 | fColors.setCount(count); |
michael@0 | 92 | for (int index = 0; index < count; index++) |
michael@0 | 93 | fColors[index] = fDrawColors[index]->color; |
michael@0 | 94 | return count; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | #ifdef SK_DUMP_ENABLED |
michael@0 | 98 | void SkDrawGradient::dumpRest(SkAnimateMaker* maker) { |
michael@0 | 99 | dumpAttrs(maker); |
michael@0 | 100 | //can a gradient have no colors? |
michael@0 | 101 | bool closedYet = false; |
michael@0 | 102 | SkDisplayList::fIndent += 4; |
michael@0 | 103 | for (SkDrawColor** ptr = fDrawColors.begin(); ptr < fDrawColors.end(); ptr++) { |
michael@0 | 104 | if (closedYet == false) { |
michael@0 | 105 | SkDebugf(">\n"); |
michael@0 | 106 | closedYet = true; |
michael@0 | 107 | } |
michael@0 | 108 | SkDrawColor* color = *ptr; |
michael@0 | 109 | color->dump(maker); |
michael@0 | 110 | } |
michael@0 | 111 | SkDisplayList::fIndent -= 4; |
michael@0 | 112 | dumpChildren(maker, closedYet); //dumps the matrix if it has one |
michael@0 | 113 | } |
michael@0 | 114 | #endif |
michael@0 | 115 | |
michael@0 | 116 | void SkDrawGradient::onEndElement(SkAnimateMaker& maker) { |
michael@0 | 117 | if (offsets.count() != 0) { |
michael@0 | 118 | if (offsets.count() != fDrawColors.count()) { |
michael@0 | 119 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsDontMatchColors); |
michael@0 | 120 | return; |
michael@0 | 121 | } |
michael@0 | 122 | if (offsets[0] != 0) { |
michael@0 | 123 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustStartWithZero); |
michael@0 | 124 | return; |
michael@0 | 125 | } |
michael@0 | 126 | if (offsets[offsets.count()-1] != SK_Scalar1) { |
michael@0 | 127 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustEndWithOne); |
michael@0 | 128 | return; |
michael@0 | 129 | } |
michael@0 | 130 | for (int i = 1; i < offsets.count(); i++) { |
michael@0 | 131 | if (offsets[i] <= offsets[i-1]) { |
michael@0 | 132 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustIncrease); |
michael@0 | 133 | return; |
michael@0 | 134 | } |
michael@0 | 135 | if (offsets[i] > SK_Scalar1) { |
michael@0 | 136 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustBeNoMoreThanOne); |
michael@0 | 137 | return; |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | if (unitMapper.size() > 0) |
michael@0 | 142 | fUnitMapper = new SkDrawGradientUnitMapper(&maker, unitMapper.c_str()); |
michael@0 | 143 | INHERITED::onEndElement(maker); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | #if SK_USE_CONDENSED_INFO == 0 |
michael@0 | 147 | |
michael@0 | 148 | const SkMemberInfo SkDrawLinearGradient::fInfo[] = { |
michael@0 | 149 | SK_MEMBER_INHERITED, |
michael@0 | 150 | SK_MEMBER_ARRAY(points, Float), |
michael@0 | 151 | }; |
michael@0 | 152 | |
michael@0 | 153 | #endif |
michael@0 | 154 | |
michael@0 | 155 | DEFINE_GET_MEMBER(SkDrawLinearGradient); |
michael@0 | 156 | |
michael@0 | 157 | SkDrawLinearGradient::SkDrawLinearGradient() { |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | void SkDrawLinearGradient::onEndElement(SkAnimateMaker& maker) |
michael@0 | 161 | { |
michael@0 | 162 | if (points.count() != 4) |
michael@0 | 163 | maker.setErrorCode(SkDisplayXMLParserError::kGradientPointsLengthMustBeFour); |
michael@0 | 164 | INHERITED::onEndElement(maker); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | #ifdef SK_DUMP_ENABLED |
michael@0 | 168 | void SkDrawLinearGradient::dump(SkAnimateMaker* maker) { |
michael@0 | 169 | dumpBase(maker); |
michael@0 | 170 | dumpRest(maker); |
michael@0 | 171 | } |
michael@0 | 172 | #endif |
michael@0 | 173 | |
michael@0 | 174 | SkShader* SkDrawLinearGradient::getShader() { |
michael@0 | 175 | if (addPrelude() == 0 || points.count() != 4) |
michael@0 | 176 | return NULL; |
michael@0 | 177 | SkShader* shader = SkGradientShader::CreateLinear((SkPoint*)points.begin(), |
michael@0 | 178 | fColors.begin(), offsets.begin(), fColors.count(), (SkShader::TileMode) tileMode, fUnitMapper); |
michael@0 | 179 | SkAutoTDelete<SkShader> autoDel(shader); |
michael@0 | 180 | addPostlude(shader); |
michael@0 | 181 | (void)autoDel.detach(); |
michael@0 | 182 | return shader; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | |
michael@0 | 186 | #if SK_USE_CONDENSED_INFO == 0 |
michael@0 | 187 | |
michael@0 | 188 | const SkMemberInfo SkDrawRadialGradient::fInfo[] = { |
michael@0 | 189 | SK_MEMBER_INHERITED, |
michael@0 | 190 | SK_MEMBER(center, Point), |
michael@0 | 191 | SK_MEMBER(radius, Float) |
michael@0 | 192 | }; |
michael@0 | 193 | |
michael@0 | 194 | #endif |
michael@0 | 195 | |
michael@0 | 196 | DEFINE_GET_MEMBER(SkDrawRadialGradient); |
michael@0 | 197 | |
michael@0 | 198 | SkDrawRadialGradient::SkDrawRadialGradient() : radius(0) { |
michael@0 | 199 | center.set(0, 0); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | #ifdef SK_DUMP_ENABLED |
michael@0 | 203 | void SkDrawRadialGradient::dump(SkAnimateMaker* maker) { |
michael@0 | 204 | dumpBase(maker); |
michael@0 | 205 | dumpRest(maker); |
michael@0 | 206 | } |
michael@0 | 207 | #endif |
michael@0 | 208 | |
michael@0 | 209 | SkShader* SkDrawRadialGradient::getShader() { |
michael@0 | 210 | if (addPrelude() == 0) |
michael@0 | 211 | return NULL; |
michael@0 | 212 | SkShader* shader = SkGradientShader::CreateRadial(center, |
michael@0 | 213 | radius, fColors.begin(), offsets.begin(), fColors.count(), (SkShader::TileMode) tileMode, fUnitMapper); |
michael@0 | 214 | SkAutoTDelete<SkShader> autoDel(shader); |
michael@0 | 215 | addPostlude(shader); |
michael@0 | 216 | (void)autoDel.detach(); |
michael@0 | 217 | return shader; |
michael@0 | 218 | } |