| 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) { |
| 74 SkMatrix::MapXYProc proc = fDstToIndexProc; | 92 SkMatrix::MapXYProc proc = fDstToIndexProc; |
| 75 const SkMatrix& matrix = fDstToIndex; | 93 const SkMatrix& matrix = fDstToIndex; |
| 76 const SkPMColor* SK_RESTRICT cache = this->getCache32(); | 94 const SkPMColor* SK_RESTRICT cache = fCache->getCache32(); |
| 77 int toggle = init_dither_toggle(x, y); | 95 int toggle = init_dither_toggle(x, y); |
| 78 SkPoint srcPt; | 96 SkPoint srcPt; |
| 79 | 97 |
| 80 if (fDstToIndexClass != kPerspective_MatrixClass) { | 98 if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 81 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, | 99 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 82 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 100 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 83 SkScalar dx, fx = srcPt.fX; | 101 SkScalar dx, fx = srcPt.fX; |
| 84 SkScalar dy, fy = srcPt.fY; | 102 SkScalar dy, fy = srcPt.fY; |
| 85 | 103 |
| 86 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { | 104 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 104 } else { // perspective case | 122 } else { // perspective case |
| 105 for (int stop = x + count; x < stop; x++) { | 123 for (int stop = x + count; x < stop; x++) { |
| 106 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, | 124 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 107 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 125 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 108 *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; | 126 *dstC++ = cache[toggle + SkATan2_255(srcPt.fY, srcPt.fX)]; |
| 109 toggle = next_dither_toggle(toggle); | 127 toggle = next_dither_toggle(toggle); |
| 110 } | 128 } |
| 111 } | 129 } |
| 112 } | 130 } |
| 113 | 131 |
| 114 void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, | 132 void SkSweepGradient::SweepGradientContext::shadeSpan16(int x, int y, uint16_t*
SK_RESTRICT dstC, |
| 115 int count) { | 133 int count) { |
| 116 SkMatrix::MapXYProc proc = fDstToIndexProc; | 134 SkMatrix::MapXYProc proc = fDstToIndexProc; |
| 117 const SkMatrix& matrix = fDstToIndex; | 135 const SkMatrix& matrix = fDstToIndex; |
| 118 const uint16_t* SK_RESTRICT cache = this->getCache16(); | 136 const uint16_t* SK_RESTRICT cache = fCache->getCache16(); |
| 119 int toggle = init_dither_toggle16(x, y); | 137 int toggle = init_dither_toggle16(x, y); |
| 120 SkPoint srcPt; | 138 SkPoint srcPt; |
| 121 | 139 |
| 122 if (fDstToIndexClass != kPerspective_MatrixClass) { | 140 if (fDstToIndexClass != kPerspective_MatrixClass) { |
| 123 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, | 141 proc(matrix, SkIntToScalar(x) + SK_ScalarHalf, |
| 124 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); | 142 SkIntToScalar(y) + SK_ScalarHalf, &srcPt); |
| 125 SkScalar dx, fx = srcPt.fX; | 143 SkScalar dx, fx = srcPt.fX; |
| 126 SkScalar dy, fy = srcPt.fY; | 144 SkScalar dy, fy = srcPt.fY; |
| 127 | 145 |
| 128 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { | 146 if (fDstToIndexClass == kFixedStepInX_MatrixClass) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 str->appendScalar(fCenter.fX); | 311 str->appendScalar(fCenter.fX); |
| 294 str->append(", "); | 312 str->append(", "); |
| 295 str->appendScalar(fCenter.fY); | 313 str->appendScalar(fCenter.fY); |
| 296 str->append(") "); | 314 str->append(") "); |
| 297 | 315 |
| 298 this->INHERITED::toString(str); | 316 this->INHERITED::toString(str); |
| 299 | 317 |
| 300 str->append(")"); | 318 str->append(")"); |
| 301 } | 319 } |
| 302 #endif | 320 #endif |
| OLD | NEW |