OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
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 #include "SkSweepGradient.h" | 9 #include "SkSweepGradient.h" |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 SkSweepGradient::SkSweepGradient(SkReadBuffer& buffer) | 45 SkSweepGradient::SkSweepGradient(SkReadBuffer& buffer) |
46 : INHERITED(buffer), | 46 : INHERITED(buffer), |
47 fCenter(buffer.readPoint()) { | 47 fCenter(buffer.readPoint()) { |
48 } | 48 } |
49 | 49 |
50 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const { | 50 void SkSweepGradient::flatten(SkWriteBuffer& buffer) const { |
51 this->INHERITED::flatten(buffer); | 51 this->INHERITED::flatten(buffer); |
52 buffer.writePoint(fCenter); | 52 buffer.writePoint(fCenter); |
53 } | 53 } |
54 | 54 |
| 55 size_t SkSweepGradient::contextSize() const { |
| 56 return sizeof(SweepGradientContext); |
| 57 } |
| 58 |
| 59 SkShader::Context* SkSweepGradient::createContext(const SkBitmap& device, const
SkPaint& paint, |
| 60 const SkMatrix& matrix, void*
storage) const { |
| 61 if (!this->validContext(device, paint, matrix)) { |
| 62 return NULL; |
| 63 } |
| 64 |
| 65 return SkNEW_PLACEMENT_ARGS(storage, SweepGradientContext, (*this, device, p
aint, matrix)); |
| 66 } |
| 67 |
| 68 SkSweepGradient::SweepGradientContext::SweepGradientContext( |
| 69 const SkSweepGradient& shader, const SkBitmap& device, |
| 70 const SkPaint& paint, const SkMatrix& matrix) |
| 71 : INHERITED(shader, device, paint, matrix) {} |
| 72 |
55 // returns angle in a circle [0..2PI) -> [0..255] | 73 // returns angle in a circle [0..2PI) -> [0..255] |
56 static unsigned SkATan2_255(float y, float x) { | 74 static unsigned SkATan2_255(float y, float x) { |
57 // static const float g255Over2PI = 255 / (2 * SK_ScalarPI); | 75 // static const float g255Over2PI = 255 / (2 * SK_ScalarPI); |
58 static const float g255Over2PI = 40.584510488433314f; | 76 static const float g255Over2PI = 40.584510488433314f; |
59 | 77 |
60 float result = sk_float_atan2(y, x); | 78 float result = sk_float_atan2(y, x); |
61 if (result < 0) { | 79 if (result < 0) { |
62 result += 2 * SK_ScalarPI; | 80 result += 2 * SK_ScalarPI; |
63 } | 81 } |
64 SkASSERT(result >= 0); | 82 SkASSERT(result >= 0); |
65 // since our value is always >= 0, we can cast to int, which is faster than | 83 // since our value is always >= 0, we can cast to int, which is faster than |
66 // calling floorf() | 84 // calling floorf() |
67 int ir = (int)(result * g255Over2PI); | 85 int ir = (int)(result * g255Over2PI); |
68 SkASSERT(ir >= 0 && ir <= 255); | 86 SkASSERT(ir >= 0 && ir <= 255); |
69 return ir; | 87 return ir; |
70 } | 88 } |
71 | 89 |
72 void SkSweepGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, | 90 void SkSweepGradient::SweepGradientContext::shadeSpan(int x, int y, SkPMColor* S
K_RESTRICT dstC, |
73 int count) { | 91 int count) { |
| 92 const SkSweepGradient& sweepGradient = static_cast<const SkSweepGradient&>(f
Shader); |
| 93 |
74 SkMatrix::MapXYProc proc = fDstToIndexProc; | 94 SkMatrix::MapXYProc proc = fDstToIndexProc; |
75 const SkMatrix& matrix = fDstToIndex; | 95 const SkMatrix& matrix = fDstToIndex; |
76 const SkPMColor* SK_RESTRICT cache = this->getCache32(); | 96 const SkPMColor* SK_RESTRICT cache = fCache.getCache32(sweepGradient); |
77 int toggle = init_dither_toggle(x, y); | 97 int toggle = init_dither_toggle(x, y); |
78 SkPoint srcPt; | 98 SkPoint srcPt; |
79 | 99 |
80 if (fDstToIndexClass != kPerspective_MatrixClass) { | 100 if (fDstToIndexClass != kPerspective_MatrixClass) { |
81 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, | 101 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
82 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 102 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
83 SkScalar dx, fx = srcPt.fX; | 103 SkScalar dx, fx = srcPt.fX; |
84 SkScalar dy, fy = srcPt.fY; | 104 SkScalar dy, fy = srcPt.fY; |
85 | 105 |
86 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { | 106 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
(...skipping 17 matching lines...) Expand all Loading... |
104 } else { // perspective case | 124 } else { // perspective case |
105 for (int stop = x + count; x < stop; x++) { | 125 for (int stop = x + count; x < stop; x++) { |
106 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, | 126 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
107 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 127 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
108 *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; | 128 *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; |
109 toggle = next_dither_toggle(toggle); | 129 toggle = next_dither_toggle(toggle); |
110 } | 130 } |
111 } | 131 } |
112 } | 132 } |
113 | 133 |
114 void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, | 134 void SkSweepGradient::SweepGradientContext::shadeSpan16(int x, int y, uint16_t*
SK_RESTRICT dstC, |
115 int count) { | 135 int count) { |
| 136 const SkSweepGradient& sweepGradient = static_cast<const SkSweepGradient&>(f
Shader); |
| 137 |
116 SkMatrix::MapXYProc proc = fDstToIndexProc; | 138 SkMatrix::MapXYProc proc = fDstToIndexProc; |
117 const SkMatrix& matrix = fDstToIndex; | 139 const SkMatrix& matrix = fDstToIndex; |
118 const uint16_t* SK_RESTRICT cache = this->getCache16(); | 140 const uint16_t* SK_RESTRICT cache = fCache.getCache16(sweepGradient); |
119 int toggle = init_dither_toggle16(x, y); | 141 int toggle = init_dither_toggle16(x, y); |
120 SkPoint srcPt; | 142 SkPoint srcPt; |
121 | 143 |
122 if (fDstToIndexClass != kPerspective_MatrixClass) { | 144 if (fDstToIndexClass != kPerspective_MatrixClass) { |
123 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, | 145 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
124 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 146 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
125 SkScalar dx, fx = srcPt.fX; | 147 SkScalar dx, fx = srcPt.fX; |
126 SkScalar dy, fy = srcPt.fY; | 148 SkScalar dy, fy = srcPt.fY; |
127 | 149 |
128 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { | 150 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 str->appendScalar(fCenter.fX); | 315 str->appendScalar(fCenter.fX); |
294 str->append(", "); | 316 str->append(", "); |
295 str->appendScalar(fCenter.fY); | 317 str->appendScalar(fCenter.fY); |
296 str->append(") "); | 318 str->append(") "); |
297 | 319 |
298 this->INHERITED::toString(str); | 320 this->INHERITED::toString(str); |
299 | 321 |
300 str->append(")"); | 322 str->append(")"); |
301 } | 323 } |
302 #endif | 324 #endif |
OLD | NEW |