Chromium Code Reviews| 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" |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkShader.h" | |
| 13 #include "SkString.h" | |
| 14 #include "SkThread.h" | |
| 15 #include "SkUnPreMultiply.h" | |
| 12 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
| 13 #include "SkShader.h" | |
| 14 #include "SkUnPreMultiply.h" | |
| 15 #include "SkString.h" | |
| 16 | 17 |
| 17 #if SK_SUPPORT_GPU | 18 #if SK_SUPPORT_GPU |
| 18 #include "GrContext.h" | 19 #include "GrContext.h" |
| 19 #include "GrCoordTransform.h" | 20 #include "GrCoordTransform.h" |
| 20 #include "gl/GrGLEffect.h" | 21 #include "gl/GrGLEffect.h" |
| 21 #include "GrTBackendEffectFactory.h" | 22 #include "GrTBackendEffectFactory.h" |
| 22 #include "SkGr.h" | 23 #include "SkGr.h" |
| 23 #endif | 24 #endif |
| 24 | 25 |
| 25 static const int kBlockSize = 256; | 26 static const int kBlockSize = 256; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 fWrapY == other.fWrapY; | 75 fWrapY == other.fWrapY; |
| 75 } | 76 } |
| 76 | 77 |
| 77 int fWidth; // How much to subtract to wrap for stitching. | 78 int fWidth; // How much to subtract to wrap for stitching. |
| 78 int fWrapX; // Minimum value to wrap. | 79 int fWrapX; // Minimum value to wrap. |
| 79 int fHeight; | 80 int fHeight; |
| 80 int fWrapY; | 81 int fWrapY; |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 struct SkPerlinNoiseShader::PaintingData { | 84 struct SkPerlinNoiseShader::PaintingData { |
| 84 PaintingData(const SkISize& tileSize) | 85 PaintingData(const SkISize& tileSize, SkScalar seed, |
| 85 : fSeed(0) | 86 SkScalar baseFrequencyX, SkScalar baseFrequencyY) |
| 86 , fTileSize(tileSize) | 87 : fTileSize(tileSize) |
| 88 , fBaseFrequency(SkPoint::Make(baseFrequencyX, baseFrequencyY)) | |
| 87 , fPermutationsBitmap(NULL) | 89 , fPermutationsBitmap(NULL) |
| 88 , fNoiseBitmap(NULL) | 90 , fNoiseBitmap(NULL) |
| 89 {} | 91 { |
| 92 this->init(seed); | |
| 93 if (!fTileSize.isEmpty()) { | |
| 94 this->stitch(); | |
| 95 } | |
| 96 } | |
| 90 | 97 |
| 91 ~PaintingData() | 98 ~PaintingData() |
| 92 { | 99 { |
| 93 SkDELETE(fPermutationsBitmap); | 100 SkDELETE(fPermutationsBitmap); |
| 94 SkDELETE(fNoiseBitmap); | 101 SkDELETE(fNoiseBitmap); |
| 95 } | 102 } |
| 96 | 103 |
| 97 int fSeed; | 104 int fSeed; |
| 98 uint8_t fLatticeSelector[kBlockSize]; | 105 uint8_t fLatticeSelector[kBlockSize]; |
| 99 uint16_t fNoise[4][kBlockSize][2]; | 106 uint16_t fNoise[4][kBlockSize][2]; |
| 100 SkPoint fGradient[4][kBlockSize]; | 107 SkPoint fGradient[4][kBlockSize]; |
| 101 SkISize fTileSize; | 108 SkISize fTileSize; |
| 102 SkVector fBaseFrequency; | 109 SkVector fBaseFrequency; |
| 103 StitchData fStitchDataInit; | 110 StitchData fStitchDataInit; |
| 104 | 111 |
| 105 private: | 112 private: |
| 106 | 113 |
| 107 SkBitmap* fPermutationsBitmap; | 114 SkBitmap* fPermutationsBitmap; |
| 108 SkBitmap* fNoiseBitmap; | 115 SkBitmap* fNoiseBitmap; |
| 109 | 116 |
| 110 public: | 117 // Guards for the bitmap caches. |
| 118 SkMutex fPermutationsMutex; | |
| 119 SkMutex fNoiseMutex; | |
| 111 | 120 |
| 112 inline int random() { | 121 inline int random() { |
| 113 static const int gRandAmplitude = 16807; // 7**5; primitive root of m | 122 static const int gRandAmplitude = 16807; // 7**5; primitive root of m |
| 114 static const int gRandQ = 127773; // m / a | 123 static const int gRandQ = 127773; // m / a |
| 115 static const int gRandR = 2836; // m % a | 124 static const int gRandR = 2836; // m % a |
| 116 | 125 |
| 117 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRand Q); | 126 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRand Q); |
| 118 if (result <= 0) | 127 if (result <= 0) |
| 119 result += kRandMaximum; | 128 result += kRandMaximum; |
| 120 fSeed = result; | 129 fSeed = result; |
| 121 return result; | 130 return result; |
| 122 } | 131 } |
| 123 | 132 |
| 133 // Only called once. Could be part of the constructor. | |
| 124 void init(SkScalar seed) | 134 void init(SkScalar seed) |
| 125 { | 135 { |
| 126 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlo ckSize)); | 136 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlo ckSize)); |
| 127 | 137 |
| 128 // According to the SVG spec, we must truncate (not round) the seed valu e. | 138 // According to the SVG spec, we must truncate (not round) the seed valu e. |
| 129 fSeed = SkScalarTruncToInt(seed); | 139 fSeed = SkScalarTruncToInt(seed); |
| 130 // The seed value clamp to the range [1, kRandMaximum - 1]. | 140 // The seed value clamp to the range [1, kRandMaximum - 1]. |
| 131 if (fSeed <= 0) { | 141 if (fSeed <= 0) { |
| 132 fSeed = -(fSeed % (kRandMaximum - 1)) + 1; | 142 fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
| 133 } | 143 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 183 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize ), | 193 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize ), |
| 184 gInvBlockSizef)); | 194 gInvBlockSizef)); |
| 185 fGradient[channel][i].normalize(); | 195 fGradient[channel][i].normalize(); |
| 186 // Put the normalized gradient back into the noise data | 196 // Put the normalized gradient back into the noise data |
| 187 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul( | 197 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul( |
| 188 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits)); | 198 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits)); |
| 189 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul( | 199 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul( |
| 190 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits)); | 200 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits)); |
| 191 } | 201 } |
| 192 } | 202 } |
| 193 | |
| 194 // Invalidate bitmaps | |
| 195 SkDELETE(fPermutationsBitmap); | |
| 196 fPermutationsBitmap = NULL; | |
| 197 SkDELETE(fNoiseBitmap); | |
| 198 fNoiseBitmap = NULL; | |
| 199 } | 203 } |
| 200 | 204 |
| 205 // Only called once. Could be part of the constructor. | |
| 201 void stitch() { | 206 void stitch() { |
| 202 SkScalar tileWidth = SkIntToScalar(fTileSize.width()); | 207 SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
| 203 SkScalar tileHeight = SkIntToScalar(fTileSize.height()); | 208 SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
| 204 SkASSERT(tileWidth > 0 && tileHeight > 0); | 209 SkASSERT(tileWidth > 0 && tileHeight > 0); |
| 205 // When stitching tiled turbulence, the frequencies must be adjusted | 210 // When stitching tiled turbulence, the frequencies must be adjusted |
| 206 // so that the tile borders will be continuous. | 211 // so that the tile borders will be continuous. |
| 207 if (fBaseFrequency.fX) { | 212 if (fBaseFrequency.fX) { |
| 208 SkScalar lowFrequencx = | 213 SkScalar lowFrequencx = |
| 209 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth ; | 214 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth ; |
| 210 SkScalar highFrequencx = | 215 SkScalar highFrequencx = |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 231 } | 236 } |
| 232 // Set up TurbulenceInitial stitch values. | 237 // Set up TurbulenceInitial stitch values. |
| 233 fStitchDataInit.fWidth = | 238 fStitchDataInit.fWidth = |
| 234 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); | 239 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
| 235 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; | 240 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
| 236 fStitchDataInit.fHeight = | 241 fStitchDataInit.fHeight = |
| 237 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); | 242 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); |
| 238 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; | 243 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; |
| 239 } | 244 } |
| 240 | 245 |
| 246 public: | |
| 247 | |
| 241 SkBitmap* getPermutationsBitmap() | 248 SkBitmap* getPermutationsBitmap() |
| 242 { | 249 { |
| 243 if (!fPermutationsBitmap) { | 250 if (!fPermutationsBitmap) { |
| 244 fPermutationsBitmap = SkNEW(SkBitmap); | 251 SkAutoMutexAcquire autoMutex(fPermutationsMutex); |
|
Stephen White
2014/03/05 21:37:50
Maybe I'm being dense, but can these bitmaps just
| |
| 245 fPermutationsBitmap->allocPixels(SkImageInfo::MakeA8(kBlockSize, 1)) ; | 252 if (!fPermutationsBitmap) { |
| 246 uint8_t* bitmapPixels = fPermutationsBitmap->getAddr8(0, 0); | 253 fPermutationsBitmap = SkNEW(SkBitmap); |
| 247 memcpy(bitmapPixels, fLatticeSelector, sizeof(uint8_t) * kBlockSize) ; | 254 fPermutationsBitmap->allocPixels(SkImageInfo::MakeA8(kBlockSize, 1)); |
| 255 uint8_t* bitmapPixels = fPermutationsBitmap->getAddr8(0, 0); | |
| 256 memcpy(bitmapPixels, fLatticeSelector, sizeof(uint8_t) * kBlockS ize); | |
| 257 } | |
| 248 } | 258 } |
| 249 return fPermutationsBitmap; | 259 return fPermutationsBitmap; |
| 250 } | 260 } |
| 251 | 261 |
| 252 SkBitmap* getNoiseBitmap() | 262 SkBitmap* getNoiseBitmap() |
| 253 { | 263 { |
| 254 if (!fNoiseBitmap) { | 264 if (!fNoiseBitmap) { |
| 255 fNoiseBitmap = SkNEW(SkBitmap); | 265 SkAutoMutexAcquire autoMutex(fNoiseMutex); |
| 256 fNoiseBitmap->allocPixels(SkImageInfo::MakeN32Premul(kBlockSize, 4)) ; | 266 if (!fNoiseBitmap) { |
| 257 uint32_t* bitmapPixels = fNoiseBitmap->getAddr32(0, 0); | 267 fNoiseBitmap = SkNEW(SkBitmap); |
| 258 memcpy(bitmapPixels, fNoise[0][0], sizeof(uint16_t) * kBlockSize * 4 * 2); | 268 fNoiseBitmap->allocPixels(SkImageInfo::MakeN32Premul(kBlockSize, 4)); |
| 269 uint32_t* bitmapPixels = fNoiseBitmap->getAddr32(0, 0); | |
| 270 memcpy(bitmapPixels, fNoise[0][0], sizeof(uint16_t) * kBlockSize * 4 * 2); | |
| 271 } | |
| 259 } | 272 } |
| 260 return fNoiseBitmap; | 273 return fNoiseBitmap; |
| 261 } | 274 } |
| 262 }; | 275 }; |
| 263 | 276 |
| 264 SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkSca lar baseFrequencyY, | 277 SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkSca lar baseFrequencyY, |
| 265 int numOctaves, SkScalar seed, | 278 int numOctaves, SkScalar seed, |
| 266 const SkISize* tileSize) { | 279 const SkISize* tileSize) { |
| 267 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY, | 280 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY, |
| 268 numOctaves, seed, tileSize)); | 281 numOctaves, seed, tileSize)); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 279 SkScalar baseFrequencyX, | 292 SkScalar baseFrequencyX, |
| 280 SkScalar baseFrequencyY, | 293 SkScalar baseFrequencyY, |
| 281 int numOctaves, | 294 int numOctaves, |
| 282 SkScalar seed, | 295 SkScalar seed, |
| 283 const SkISize* tileSize) | 296 const SkISize* tileSize) |
| 284 : fType(type) | 297 : fType(type) |
| 285 , fBaseFrequencyX(baseFrequencyX) | 298 , fBaseFrequencyX(baseFrequencyX) |
| 286 , fBaseFrequencyY(baseFrequencyY) | 299 , fBaseFrequencyY(baseFrequencyY) |
| 287 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) | 300 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) |
| 288 , fSeed(seed) | 301 , fSeed(seed) |
| 289 , fStitchTiles((tileSize != NULL) && !tileSize->isEmpty()) | 302 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize) |
| 290 , fPaintingData(NULL) | 303 , fStitchTiles(!fTileSize.isEmpty()) |
| 291 { | 304 { |
| 292 SkASSERT(numOctaves >= 0 && numOctaves < 256); | 305 SkASSERT(numOctaves >= 0 && numOctaves < 256); |
| 293 setTileSize(fStitchTiles ? *tileSize : SkISize::Make(0,0)); | |
| 294 fMatrix.reset(); | 306 fMatrix.reset(); |
| 307 fPaintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY)); | |
| 295 } | 308 } |
| 296 | 309 |
| 297 SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer) : | 310 SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer) |
| 298 INHERITED(buffer), fPaintingData(NULL) { | 311 : INHERITED(buffer) |
| 312 { | |
| 299 fType = (SkPerlinNoiseShader::Type) buffer.readInt(); | 313 fType = (SkPerlinNoiseShader::Type) buffer.readInt(); |
| 300 fBaseFrequencyX = buffer.readScalar(); | 314 fBaseFrequencyX = buffer.readScalar(); |
| 301 fBaseFrequencyY = buffer.readScalar(); | 315 fBaseFrequencyY = buffer.readScalar(); |
| 302 fNumOctaves = buffer.readInt(); | 316 fNumOctaves = buffer.readInt(); |
| 303 fSeed = buffer.readScalar(); | 317 fSeed = buffer.readScalar(); |
| 304 fStitchTiles = buffer.readBool(); | 318 fStitchTiles = buffer.readBool(); |
| 305 fTileSize.fWidth = buffer.readInt(); | 319 fTileSize.fWidth = buffer.readInt(); |
| 306 fTileSize.fHeight = buffer.readInt(); | 320 fTileSize.fHeight = buffer.readInt(); |
| 307 setTileSize(fTileSize); | |
| 308 fMatrix.reset(); | 321 fMatrix.reset(); |
| 322 fPaintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY)); | |
| 309 buffer.validate(perlin_noise_type_is_valid(fType) && | 323 buffer.validate(perlin_noise_type_is_valid(fType) && |
| 310 (fNumOctaves >= 0) && (fNumOctaves <= 255)); | 324 (fNumOctaves >= 0) && (fNumOctaves <= 255) && |
| 325 (fStitchTiles != fTileSize.isEmpty())); | |
| 311 } | 326 } |
| 312 | 327 |
| 313 SkPerlinNoiseShader::~SkPerlinNoiseShader() { | 328 SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
| 314 // Safety, should have been done in endContext() | 329 // Safety, should have been done in endContext() |
| 315 SkDELETE(fPaintingData); | 330 SkDELETE(fPaintingData); |
| 316 } | 331 } |
| 317 | 332 |
| 318 void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { | 333 void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { |
| 319 this->INHERITED::flatten(buffer); | 334 this->INHERITED::flatten(buffer); |
| 320 buffer.writeInt((int) fType); | 335 buffer.writeInt((int) fType); |
| 321 buffer.writeScalar(fBaseFrequencyX); | 336 buffer.writeScalar(fBaseFrequencyX); |
| 322 buffer.writeScalar(fBaseFrequencyY); | 337 buffer.writeScalar(fBaseFrequencyY); |
| 323 buffer.writeInt(fNumOctaves); | 338 buffer.writeInt(fNumOctaves); |
| 324 buffer.writeScalar(fSeed); | 339 buffer.writeScalar(fSeed); |
| 325 buffer.writeBool(fStitchTiles); | 340 buffer.writeBool(fStitchTiles); |
| 326 buffer.writeInt(fTileSize.fWidth); | 341 buffer.writeInt(fTileSize.fWidth); |
| 327 buffer.writeInt(fTileSize.fHeight); | 342 buffer.writeInt(fTileSize.fHeight); |
| 328 } | 343 } |
| 329 | 344 |
| 330 void SkPerlinNoiseShader::initPaint(PaintingData& paintingData) | |
| 331 { | |
| 332 paintingData.init(fSeed); | |
| 333 | |
| 334 // Set frequencies to original values | |
| 335 paintingData.fBaseFrequency.set(fBaseFrequencyX, fBaseFrequencyY); | |
| 336 // Adjust frequecies based on size if stitching is enabled | |
| 337 if (fStitchTiles) { | |
| 338 paintingData.stitch(); | |
| 339 } | |
| 340 } | |
| 341 | |
| 342 void SkPerlinNoiseShader::setTileSize(const SkISize& tileSize) { | |
| 343 fTileSize = tileSize; | |
| 344 | |
| 345 if (NULL == fPaintingData) { | |
| 346 fPaintingData = SkNEW_ARGS(PaintingData, (fTileSize)); | |
| 347 initPaint(*fPaintingData); | |
| 348 } else { | |
| 349 // Set Size | |
| 350 fPaintingData->fTileSize = fTileSize; | |
| 351 // Set frequencies to original values | |
| 352 fPaintingData->fBaseFrequency.set(fBaseFrequencyX, fBaseFrequencyY); | |
| 353 // Adjust frequecies based on size if stitching is enabled | |
| 354 if (fStitchTiles) { | |
| 355 fPaintingData->stitch(); | |
| 356 } | |
| 357 } | |
| 358 } | |
| 359 | |
| 360 SkScalar SkPerlinNoiseShader::noise2D(int channel, const PaintingData& paintingD ata, | 345 SkScalar SkPerlinNoiseShader::noise2D(int channel, const PaintingData& paintingD ata, |
| 361 const StitchData& stitchData, const SkPoint & noiseVector) | 346 const StitchData& stitchData, |
| 362 { | 347 const SkPoint& noiseVector) const { |
| 363 struct Noise { | 348 struct Noise { |
| 364 int noisePositionIntegerValue; | 349 int noisePositionIntegerValue; |
| 365 SkScalar noisePositionFractionValue; | 350 SkScalar noisePositionFractionValue; |
| 366 Noise(SkScalar component) | 351 Noise(SkScalar component) |
| 367 { | 352 { |
| 368 SkScalar position = component + kPerlinNoise; | 353 SkScalar position = component + kPerlinNoise; |
| 369 noisePositionIntegerValue = SkScalarFloorToInt(position); | 354 noisePositionIntegerValue = SkScalarFloorToInt(position); |
| 370 noisePositionFractionValue = position - SkIntToScalar(noisePositionI ntegerValue); | 355 noisePositionFractionValue = position - SkIntToScalar(noisePositionI ntegerValue); |
| 371 } | 356 } |
| 372 }; | 357 }; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 398 v = paintingData.fGradient[channel][nextLatticeIndex & kBlockMask].dot(fract ionValue); | 383 v = paintingData.fGradient[channel][nextLatticeIndex & kBlockMask].dot(fract ionValue); |
| 399 SkScalar a = SkScalarInterp(u, v, sx); | 384 SkScalar a = SkScalarInterp(u, v, sx); |
| 400 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) | 385 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
| 401 v = paintingData.fGradient[channel][(nextLatticeIndex + 1) & kBlockMask].dot (fractionValue); | 386 v = paintingData.fGradient[channel][(nextLatticeIndex + 1) & kBlockMask].dot (fractionValue); |
| 402 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) | 387 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
| 403 u = paintingData.fGradient[channel][(latticeIndex + 1) & kBlockMask].dot(fra ctionValue); | 388 u = paintingData.fGradient[channel][(latticeIndex + 1) & kBlockMask].dot(fra ctionValue); |
| 404 SkScalar b = SkScalarInterp(u, v, sx); | 389 SkScalar b = SkScalarInterp(u, v, sx); |
| 405 return SkScalarInterp(a, b, sy); | 390 return SkScalarInterp(a, b, sy); |
| 406 } | 391 } |
| 407 | 392 |
| 408 SkScalar SkPerlinNoiseShader::calculateTurbulenceValueForPoint( | 393 SkScalar SkPerlinNoiseShader::calculateTurbulenceValueForPoint(int channel, |
| 409 int channel, const PaintingData& paintingData, StitchData& stitchData, const SkPoint& point) | 394 const PaintingDat a& paintingData, |
| 410 { | 395 StitchData& stitc hData, |
| 396 const SkPoint& po int) const { | |
| 411 if (fStitchTiles) { | 397 if (fStitchTiles) { |
| 412 // Set up TurbulenceInitial stitch values. | 398 // Set up TurbulenceInitial stitch values. |
| 413 stitchData = paintingData.fStitchDataInit; | 399 stitchData = paintingData.fStitchDataInit; |
| 414 } | 400 } |
| 415 SkScalar turbulenceFunctionResult = 0; | 401 SkScalar turbulenceFunctionResult = 0; |
| 416 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), paintingData.fBaseF requency.fX), | 402 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), paintingData.fBaseF requency.fX), |
| 417 SkScalarMul(point.y(), paintingData.fBaseF requency.fY))); | 403 SkScalarMul(point.y(), paintingData.fBaseF requency.fY))); |
| 418 SkScalar ratio = SK_Scalar1; | 404 SkScalar ratio = SK_Scalar1; |
| 419 for (int octave = 0; octave < fNumOctaves; ++octave) { | 405 for (int octave = 0; octave < fNumOctaves; ++octave) { |
| 420 SkScalar noise = noise2D(channel, paintingData, stitchData, noiseVector) ; | 406 SkScalar noise = noise2D(channel, paintingData, stitchData, noiseVector) ; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 441 | 427 |
| 442 if (channel == 3) { // Scale alpha by paint value | 428 if (channel == 3) { // Scale alpha by paint value |
| 443 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult, | 429 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult, |
| 444 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255))); | 430 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255))); |
| 445 } | 431 } |
| 446 | 432 |
| 447 // Clamp result | 433 // Clamp result |
| 448 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); | 434 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); |
| 449 } | 435 } |
| 450 | 436 |
| 451 SkPMColor SkPerlinNoiseShader::shade(const SkPoint& point, StitchData& stitchDat a) { | 437 SkPMColor SkPerlinNoiseShader::shade(const SkPoint& point, StitchData& stitchDat a) const { |
| 452 SkMatrix matrix = fMatrix; | 438 SkMatrix matrix = fMatrix; |
| 453 matrix.postConcat(getLocalMatrix()); | 439 matrix.postConcat(getLocalMatrix()); |
| 454 SkMatrix invMatrix; | 440 SkMatrix invMatrix; |
| 455 if (!matrix.invert(&invMatrix)) { | 441 if (!matrix.invert(&invMatrix)) { |
| 456 invMatrix.reset(); | 442 invMatrix.reset(); |
| 457 } else { | 443 } else { |
| 458 invMatrix.postConcat(invMatrix); // Square the matrix | 444 invMatrix.postConcat(invMatrix); // Square the matrix |
| 459 } | 445 } |
| 460 // This (1,1) translation is due to WebKit's 1 based coordinates for the noi se | 446 // This (1,1) translation is due to WebKit's 1 based coordinates for the noi se |
| 461 // (as opposed to 0 based, usually). The same adjustment is in the setData() function. | 447 // (as opposed to 0 based, usually). The same adjustment is in the setData() function. |
| (...skipping 924 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1386 str->append(" seed: "); | 1372 str->append(" seed: "); |
| 1387 str->appendScalar(fSeed); | 1373 str->appendScalar(fSeed); |
| 1388 str->append(" stitch tiles: "); | 1374 str->append(" stitch tiles: "); |
| 1389 str->append(fStitchTiles ? "true " : "false "); | 1375 str->append(fStitchTiles ? "true " : "false "); |
| 1390 | 1376 |
| 1391 this->INHERITED::toString(str); | 1377 this->INHERITED::toString(str); |
| 1392 | 1378 |
| 1393 str->append(")"); | 1379 str->append(")"); |
| 1394 } | 1380 } |
| 1395 #endif | 1381 #endif |
| OLD | NEW |