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