| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 return noiseValue; | 44 return noiseValue; |
| 45 } | 45 } |
| 46 | 46 |
| 47 inline SkScalar smoothCurve(SkScalar t) { | 47 inline SkScalar smoothCurve(SkScalar t) { |
| 48 static const SkScalar SK_Scalar3 = SkFloatToScalar(3.0f); | 48 static const SkScalar SK_Scalar3 = SkFloatToScalar(3.0f); |
| 49 | 49 |
| 50 // returns t * t * (3 - 2 * t) | 50 // returns t * t * (3 - 2 * t) |
| 51 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t); | 51 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t); |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool perlin_noise_type_is_valid(SkPerlinNoiseShader::Type type) { |
| 55 return (SkPerlinNoiseShader::kFractalNoise_Type == type) || |
| 56 (SkPerlinNoiseShader::kTurbulence_Type == type); |
| 57 } |
| 58 |
| 54 } // end namespace | 59 } // end namespace |
| 55 | 60 |
| 56 struct SkPerlinNoiseShader::StitchData { | 61 struct SkPerlinNoiseShader::StitchData { |
| 57 StitchData() | 62 StitchData() |
| 58 : fWidth(0) | 63 : fWidth(0) |
| 59 , fWrapX(0) | 64 , fWrapX(0) |
| 60 , fHeight(0) | 65 , fHeight(0) |
| 61 , fWrapY(0) | 66 , fWrapY(0) |
| 62 {} | 67 {} |
| 63 | 68 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 | 277 |
| 273 SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, | 278 SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, |
| 274 SkScalar baseFrequencyX, | 279 SkScalar baseFrequencyX, |
| 275 SkScalar baseFrequencyY, | 280 SkScalar baseFrequencyY, |
| 276 int numOctaves, | 281 int numOctaves, |
| 277 SkScalar seed, | 282 SkScalar seed, |
| 278 const SkISize* tileSize) | 283 const SkISize* tileSize) |
| 279 : fType(type) | 284 : fType(type) |
| 280 , fBaseFrequencyX(baseFrequencyX) | 285 , fBaseFrequencyX(baseFrequencyX) |
| 281 , fBaseFrequencyY(baseFrequencyY) | 286 , fBaseFrequencyY(baseFrequencyY) |
| 282 , fNumOctaves(numOctaves & 0xFF /*[0,255] octaves allowed*/) | 287 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) |
| 283 , fSeed(seed) | 288 , fSeed(seed) |
| 284 , fStitchTiles((tileSize != NULL) && !tileSize->isEmpty()) | 289 , fStitchTiles((tileSize != NULL) && !tileSize->isEmpty()) |
| 285 , fPaintingData(NULL) | 290 , fPaintingData(NULL) |
| 286 { | 291 { |
| 287 SkASSERT(numOctaves >= 0 && numOctaves < 256); | 292 SkASSERT(numOctaves >= 0 && numOctaves < 256); |
| 288 setTileSize(fStitchTiles ? *tileSize : SkISize::Make(0,0)); | 293 setTileSize(fStitchTiles ? *tileSize : SkISize::Make(0,0)); |
| 289 fMatrix.reset(); | 294 fMatrix.reset(); |
| 290 } | 295 } |
| 291 | 296 |
| 292 SkPerlinNoiseShader::SkPerlinNoiseShader(SkFlattenableReadBuffer& buffer) : | 297 SkPerlinNoiseShader::SkPerlinNoiseShader(SkFlattenableReadBuffer& buffer) : |
| 293 INHERITED(buffer), fPaintingData(NULL) { | 298 INHERITED(buffer), fPaintingData(NULL) { |
| 294 fType = (SkPerlinNoiseShader::Type) buffer.readInt(); | 299 fType = (SkPerlinNoiseShader::Type) buffer.readInt(); |
| 295 fBaseFrequencyX = buffer.readScalar(); | 300 fBaseFrequencyX = buffer.readScalar(); |
| 296 fBaseFrequencyY = buffer.readScalar(); | 301 fBaseFrequencyY = buffer.readScalar(); |
| 297 fNumOctaves = buffer.readInt(); | 302 fNumOctaves = buffer.readInt(); |
| 298 fSeed = buffer.readScalar(); | 303 fSeed = buffer.readScalar(); |
| 299 fStitchTiles = buffer.readBool(); | 304 fStitchTiles = buffer.readBool(); |
| 300 fTileSize.fWidth = buffer.readInt(); | 305 fTileSize.fWidth = buffer.readInt(); |
| 301 fTileSize.fHeight = buffer.readInt(); | 306 fTileSize.fHeight = buffer.readInt(); |
| 302 setTileSize(fTileSize); | 307 setTileSize(fTileSize); |
| 303 fMatrix.reset(); | 308 fMatrix.reset(); |
| 309 buffer.validate(perlin_noise_type_is_valid(fType) && |
| 310 (fNumOctaves >= 0) && (fNumOctaves <= 255)); |
| 304 } | 311 } |
| 305 | 312 |
| 306 SkPerlinNoiseShader::~SkPerlinNoiseShader() { | 313 SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
| 307 // Safety, should have been done in endContext() | 314 // Safety, should have been done in endContext() |
| 308 SkDELETE(fPaintingData); | 315 SkDELETE(fPaintingData); |
| 309 } | 316 } |
| 310 | 317 |
| 311 void SkPerlinNoiseShader::flatten(SkFlattenableWriteBuffer& buffer) const { | 318 void SkPerlinNoiseShader::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 312 this->INHERITED::flatten(buffer); | 319 this->INHERITED::flatten(buffer); |
| 313 buffer.writeInt((int) fType); | 320 buffer.writeInt((int) fType); |
| (...skipping 1064 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1378 str->append(" seed: "); | 1385 str->append(" seed: "); |
| 1379 str->appendScalar(fSeed); | 1386 str->appendScalar(fSeed); |
| 1380 str->append(" stitch tiles: "); | 1387 str->append(" stitch tiles: "); |
| 1381 str->append(fStitchTiles ? "true " : "false "); | 1388 str->append(fStitchTiles ? "true " : "false "); |
| 1382 | 1389 |
| 1383 this->INHERITED::toString(str); | 1390 this->INHERITED::toString(str); |
| 1384 | 1391 |
| 1385 str->append(")"); | 1392 str->append(")"); |
| 1386 } | 1393 } |
| 1387 #endif | 1394 #endif |
| OLD | NEW |