OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDither.h" | 8 #include "SkDither.h" |
9 #include "SkPerlinNoiseShader.h" | 9 #include "SkPerlinNoiseShader.h" |
10 #include "SkColorFilter.h" | 10 #include "SkColorFilter.h" |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 SkScalar tileHeight = SkIntToScalar(fTileSize.height()); | 205 SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
206 SkASSERT(tileWidth > 0 && tileHeight > 0); | 206 SkASSERT(tileWidth > 0 && tileHeight > 0); |
207 // When stitching tiled turbulence, the frequencies must be adjusted | 207 // When stitching tiled turbulence, the frequencies must be adjusted |
208 // so that the tile borders will be continuous. | 208 // so that the tile borders will be continuous. |
209 if (fBaseFrequency.fX) { | 209 if (fBaseFrequency.fX) { |
210 SkScalar lowFrequencx = | 210 SkScalar lowFrequencx = |
211 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth
; | 211 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth
; |
212 SkScalar highFrequencx = | 212 SkScalar highFrequencx = |
213 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; | 213 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
214 // BaseFrequency should be non-negative according to the standard. | 214 // BaseFrequency should be non-negative according to the standard. |
215 if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequenc
y.fX) { | 215 if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) < |
| 216 SkScalarDiv(highFrequencx, fBaseFrequency.fX)) { |
216 fBaseFrequency.fX = lowFrequencx; | 217 fBaseFrequency.fX = lowFrequencx; |
217 } else { | 218 } else { |
218 fBaseFrequency.fX = highFrequencx; | 219 fBaseFrequency.fX = highFrequencx; |
219 } | 220 } |
220 } | 221 } |
221 if (fBaseFrequency.fY) { | 222 if (fBaseFrequency.fY) { |
222 SkScalar lowFrequency = | 223 SkScalar lowFrequency = |
223 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeig
ht; | 224 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeig
ht; |
224 SkScalar highFrequency = | 225 SkScalar highFrequency = |
225 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeigh
t; | 226 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeigh
t; |
226 if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequenc
y.fY) { | 227 if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) < |
| 228 SkScalarDiv(highFrequency, fBaseFrequency.fY)) { |
227 fBaseFrequency.fY = lowFrequency; | 229 fBaseFrequency.fY = lowFrequency; |
228 } else { | 230 } else { |
229 fBaseFrequency.fY = highFrequency; | 231 fBaseFrequency.fY = highFrequency; |
230 } | 232 } |
231 } | 233 } |
232 // Set up TurbulenceInitial stitch values. | 234 // Set up TurbulenceInitial stitch values. |
233 fStitchDataInit.fWidth = | 235 fStitchDataInit.fWidth = |
234 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); | 236 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
235 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; | 237 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
236 fStitchDataInit.fHeight = | 238 fStitchDataInit.fHeight = |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 if (perlinNoiseShader.fStitchTiles) { | 377 if (perlinNoiseShader.fStitchTiles) { |
376 // Set up TurbulenceInitial stitch values. | 378 // Set up TurbulenceInitial stitch values. |
377 stitchData = fPaintingData->fStitchDataInit; | 379 stitchData = fPaintingData->fStitchDataInit; |
378 } | 380 } |
379 SkScalar turbulenceFunctionResult = 0; | 381 SkScalar turbulenceFunctionResult = 0; |
380 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBas
eFrequency.fX), | 382 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBas
eFrequency.fX), |
381 SkScalarMul(point.y(), fPaintingData->fBas
eFrequency.fY))); | 383 SkScalarMul(point.y(), fPaintingData->fBas
eFrequency.fY))); |
382 SkScalar ratio = SK_Scalar1; | 384 SkScalar ratio = SK_Scalar1; |
383 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { | 385 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { |
384 SkScalar noise = noise2D(channel, stitchData, noiseVector); | 386 SkScalar noise = noise2D(channel, stitchData, noiseVector); |
385 SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ? | 387 turbulenceFunctionResult += SkScalarDiv( |
386 noise : SkScalarAbs(noise); | 388 (perlinNoiseShader.fType == kFractalNoise_Type) ? noise : SkScalarAb
s(noise), ratio); |
387 turbulenceFunctionResult += numer / ratio; | |
388 noiseVector.fX *= 2; | 389 noiseVector.fX *= 2; |
389 noiseVector.fY *= 2; | 390 noiseVector.fY *= 2; |
390 ratio *= 2; | 391 ratio *= 2; |
391 if (perlinNoiseShader.fStitchTiles) { | 392 if (perlinNoiseShader.fStitchTiles) { |
392 // Update stitch values | 393 // Update stitch values |
393 stitchData.fWidth *= 2; | 394 stitchData.fWidth *= 2; |
394 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; | 395 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; |
395 stitchData.fHeight *= 2; | 396 stitchData.fHeight *= 2; |
396 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; | 397 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; |
397 } | 398 } |
398 } | 399 } |
399 | 400 |
400 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResu
lt) + 1) / 2 | 401 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResu
lt) + 1) / 2 |
401 // by fractalNoise and (turbulenceFunctionResult) by turbulence. | 402 // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
402 if (perlinNoiseShader.fType == kFractalNoise_Type) { | 403 if (perlinNoiseShader.fType == kFractalNoise_Type) { |
403 turbulenceFunctionResult = | 404 turbulenceFunctionResult = |
404 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf
; | 405 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf
; |
405 } | 406 } |
406 | 407 |
407 if (channel == 3) { // Scale alpha by paint value | 408 if (channel == 3) { // Scale alpha by paint value |
408 turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255; | 409 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult, |
| 410 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255))); |
409 } | 411 } |
410 | 412 |
411 // Clamp result | 413 // Clamp result |
412 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); | 414 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); |
413 } | 415 } |
414 | 416 |
415 SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade( | 417 SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade( |
416 const SkPoint& point, StitchData& stitchData) const { | 418 const SkPoint& point, StitchData& stitchData) const { |
417 SkPoint newPoint; | 419 SkPoint newPoint; |
418 fMatrix.mapPoints(&newPoint, &point, 1); | 420 fMatrix.mapPoints(&newPoint, &point, 1); |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 b->add32(key); | 924 b->add32(key); |
923 } | 925 } |
924 | 926 |
925 void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrProce
ssor& processor) { | 927 void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrProce
ssor& processor) { |
926 INHERITED::setData(pdman, processor); | 928 INHERITED::setData(pdman, processor); |
927 | 929 |
928 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(
); | 930 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(
); |
929 | 931 |
930 const SkVector& baseFrequency = turbulence.baseFrequency(); | 932 const SkVector& baseFrequency = turbulence.baseFrequency(); |
931 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); | 933 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
932 pdman.set1f(fAlphaUni, SkIntToScalar(turbulence.alpha()) / 255); | 934 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntT
oScalar(255))); |
933 | 935 |
934 if (turbulence.stitchTiles()) { | 936 if (turbulence.stitchTiles()) { |
935 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); | 937 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); |
936 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), | 938 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
937 SkIntToScalar(stitchData.fHeight)); | 939 SkIntToScalar(stitchData.fHeight)); |
938 } | 940 } |
939 } | 941 } |
940 | 942 |
941 ///////////////////////////////////////////////////////////////////// | 943 ///////////////////////////////////////////////////////////////////// |
942 | 944 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1030 str->append(" seed: "); | 1032 str->append(" seed: "); |
1031 str->appendScalar(fSeed); | 1033 str->appendScalar(fSeed); |
1032 str->append(" stitch tiles: "); | 1034 str->append(" stitch tiles: "); |
1033 str->append(fStitchTiles ? "true " : "false "); | 1035 str->append(fStitchTiles ? "true " : "false "); |
1034 | 1036 |
1035 this->INHERITED::toString(str); | 1037 this->INHERITED::toString(str); |
1036 | 1038 |
1037 str->append(")"); | 1039 str->append(")"); |
1038 } | 1040 } |
1039 #endif | 1041 #endif |
OLD | NEW |