epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkDrawGradient.h" |
| 11 | #include "SkAnimateMaker.h" |
| 12 | #include "SkAnimatorScript.h" |
| 13 | #include "SkGradientShader.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | #if SK_USE_CONDENSED_INFO == 0 |
| 16 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 17 | const SkMemberInfo SkDrawGradient::fInfo[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | SK_MEMBER_INHERITED, |
| 19 | SK_MEMBER_ARRAY(offsets, Float), |
| 20 | SK_MEMBER(unitMapper, String) |
| 21 | }; |
| 22 | |
| 23 | #endif |
| 24 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 25 | DEFINE_GET_MEMBER(SkDrawGradient); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 27 | SkDrawGradient::SkDrawGradient() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | } |
| 29 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 30 | SkDrawGradient::~SkDrawGradient() { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 31 | for (int index = 0; index < fDrawColors.count(); index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | delete fDrawColors[index]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | } |
| 34 | |
tfarina@chromium.org | 1d3c411 | 2012-12-03 14:38:08 +0000 | [diff] [blame] | 35 | bool SkDrawGradient::addChild(SkAnimateMaker& , SkDisplayable* child) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | SkASSERT(child); |
| 37 | if (child->isColor()) { |
| 38 | SkDrawColor* color = (SkDrawColor*) child; |
| 39 | *fDrawColors.append() = color; |
| 40 | return true; |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 45 | int SkDrawGradient::addPrelude() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | int count = fDrawColors.count(); |
| 47 | fColors.setCount(count); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 48 | for (int index = 0; index < count; index++) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | fColors[index] = fDrawColors[index]->color; |
| 50 | return count; |
| 51 | } |
| 52 | |
| 53 | #ifdef SK_DUMP_ENABLED |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 54 | void SkDrawGradient::dumpRest(SkAnimateMaker* maker) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | dumpAttrs(maker); |
| 56 | //can a gradient have no colors? |
| 57 | bool closedYet = false; |
| 58 | SkDisplayList::fIndent += 4; |
| 59 | for (SkDrawColor** ptr = fDrawColors.begin(); ptr < fDrawColors.end(); ptr++) { |
| 60 | if (closedYet == false) { |
| 61 | SkDebugf(">\n"); |
| 62 | closedYet = true; |
| 63 | } |
| 64 | SkDrawColor* color = *ptr; |
| 65 | color->dump(maker); |
| 66 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 67 | SkDisplayList::fIndent -= 4; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | dumpChildren(maker, closedYet); //dumps the matrix if it has one |
| 69 | } |
| 70 | #endif |
| 71 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 72 | void SkDrawGradient::onEndElement(SkAnimateMaker& maker) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | if (offsets.count() != 0) { |
| 74 | if (offsets.count() != fDrawColors.count()) { |
| 75 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsDontMatchColors); |
| 76 | return; |
| 77 | } |
| 78 | if (offsets[0] != 0) { |
| 79 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustStartWithZero); |
| 80 | return; |
| 81 | } |
| 82 | if (offsets[offsets.count()-1] != SK_Scalar1) { |
| 83 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustEndWithOne); |
| 84 | return; |
| 85 | } |
| 86 | for (int i = 1; i < offsets.count(); i++) { |
| 87 | if (offsets[i] <= offsets[i-1]) { |
| 88 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustIncrease); |
| 89 | return; |
| 90 | } |
| 91 | if (offsets[i] > SK_Scalar1) { |
| 92 | maker.setErrorCode(SkDisplayXMLParserError::kGradientOffsetsMustBeNoMoreThanOne); |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | INHERITED::onEndElement(maker); |
| 98 | } |
| 99 | |
| 100 | #if SK_USE_CONDENSED_INFO == 0 |
| 101 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 102 | const SkMemberInfo SkDrawLinearGradient::fInfo[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | SK_MEMBER_INHERITED, |
| 104 | SK_MEMBER_ARRAY(points, Float), |
| 105 | }; |
| 106 | |
| 107 | #endif |
| 108 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 109 | DEFINE_GET_MEMBER(SkDrawLinearGradient); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 110 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | SkDrawLinearGradient::SkDrawLinearGradient() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 114 | void SkDrawLinearGradient::onEndElement(SkAnimateMaker& maker) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | { |
| 116 | if (points.count() != 4) |
| 117 | maker.setErrorCode(SkDisplayXMLParserError::kGradientPointsLengthMustBeFour); |
| 118 | INHERITED::onEndElement(maker); |
| 119 | } |
| 120 | |
| 121 | #ifdef SK_DUMP_ENABLED |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 122 | void SkDrawLinearGradient::dump(SkAnimateMaker* maker) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | dumpBase(maker); |
| 124 | dumpRest(maker); |
| 125 | } |
| 126 | #endif |
| 127 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 128 | SkShader* SkDrawLinearGradient::getShader() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | if (addPrelude() == 0 || points.count() != 4) |
| 130 | return NULL; |
| 131 | SkShader* shader = SkGradientShader::CreateLinear((SkPoint*)points.begin(), |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 132 | fColors.begin(), offsets.begin(), fColors.count(), (SkShader::TileMode) tileMode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 133 | 0, getMatrix()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | SkAutoTDelete<SkShader> autoDel(shader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | (void)autoDel.detach(); |
| 136 | return shader; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | #if SK_USE_CONDENSED_INFO == 0 |
| 141 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 142 | const SkMemberInfo SkDrawRadialGradient::fInfo[] = { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | SK_MEMBER_INHERITED, |
| 144 | SK_MEMBER(center, Point), |
| 145 | SK_MEMBER(radius, Float) |
| 146 | }; |
| 147 | |
| 148 | #endif |
| 149 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 150 | DEFINE_GET_MEMBER(SkDrawRadialGradient); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 152 | SkDrawRadialGradient::SkDrawRadialGradient() : radius(0) { |
| 153 | center.set(0, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | #ifdef SK_DUMP_ENABLED |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 157 | void SkDrawRadialGradient::dump(SkAnimateMaker* maker) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | dumpBase(maker); |
| 159 | dumpRest(maker); |
| 160 | } |
| 161 | #endif |
| 162 | |
rileya@google.com | 9954776 | 2012-07-26 21:25:09 +0000 | [diff] [blame] | 163 | SkShader* SkDrawRadialGradient::getShader() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | if (addPrelude() == 0) |
| 165 | return NULL; |
| 166 | SkShader* shader = SkGradientShader::CreateRadial(center, |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 167 | radius, fColors.begin(), offsets.begin(), fColors.count(), (SkShader::TileMode) tileMode, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 168 | 0, getMatrix()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | SkAutoTDelete<SkShader> autoDel(shader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 170 | (void)autoDel.detach(); |
| 171 | return shader; |
| 172 | } |