Label constants with k prefix (not g) and use constexpr.

Correct prefixes are important for readability. The `g` prefix briefly
led me to believe that our random number generation had static state.

Change-Id: I9187f1da1283e1a42f250ba0c3502807d7b711a7
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308918
Auto-Submit: John Stiles <johnstiles@google.com>
Commit-Queue: Florin Malita <fmalita@google.com>
Reviewed-by: Florin Malita <fmalita@google.com>
This commit is contained in:
John Stiles 2020-08-10 09:51:42 -04:00 committed by Skia Commit-Bot
parent cac1764968
commit 039f681a84

View File

@ -185,13 +185,16 @@ public:
#endif
inline int random() {
static const int gRandAmplitude = 16807; // 7**5; primitive root of m
static const int gRandQ = 127773; // m / a
static const int gRandR = 2836; // m % a
// See https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
// m = kRandMaximum, 2**31 - 1 (2147483647)
static constexpr int kRandAmplitude = 16807; // 7**5; primitive root of m
static constexpr int kRandQ = 127773; // m / a
static constexpr int kRandR = 2836; // m % a
int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
if (result <= 0)
int result = kRandAmplitude * (fSeed % kRandQ) - kRandR * (fSeed / kRandQ);
if (result <= 0) {
result += kRandMaximum;
}
fSeed = result;
return result;
}
@ -199,8 +202,6 @@ public:
// Only called once. Could be part of the constructor.
void init(SkScalar seed)
{
static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
// According to the SVG spec, we must truncate (not round) the seed value.
fSeed = SkScalarTruncToInt(seed);
// The seed value clamp to the range [1, kRandMaximum - 1].
@ -248,20 +249,21 @@ public:
}
// Half of the largest possible value for 16 bit unsigned int
static const SkScalar gHalfMax16bits = 32767.5f;
static constexpr SkScalar kHalfMax16bits = 32767.5f;
// Compute gradients from permutated noise data
static constexpr SkScalar kInvBlockSizef = 1.0 / SkIntToScalar(kBlockSize);
for (int channel = 0; channel < 4; ++channel) {
for (int i = 0; i < kBlockSize; ++i) {
fGradient[channel][i] = SkPoint::Make(
(fNoise[channel][i][0] - kBlockSize) * gInvBlockSizef,
(fNoise[channel][i][1] - kBlockSize) * gInvBlockSizef);
(fNoise[channel][i][0] - kBlockSize) * kInvBlockSizef,
(fNoise[channel][i][1] - kBlockSize) * kInvBlockSizef);
fGradient[channel][i].normalize();
// Put the normalized gradient back into the noise data
fNoise[channel][i][0] = SkScalarRoundToInt(
(fGradient[channel][i].fX + 1) * gHalfMax16bits);
fNoise[channel][i][1] = SkScalarRoundToInt(
(fGradient[channel][i].fY + 1) * gHalfMax16bits);
fNoise[channel][i][0] =
SkScalarRoundToInt((fGradient[channel][i].fX + 1) * kHalfMax16bits);
fNoise[channel][i][1] =
SkScalarRoundToInt((fGradient[channel][i].fY + 1) * kHalfMax16bits);
}
}
}
@ -549,7 +551,7 @@ SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::calculateTurbulenceV
ratio *= 2;
if (perlinNoiseShader.fStitchTiles) {
// Update stitch values
stitchData = StitchData(SkIntToScalar(stitchData.fWidth) * 2,
stitchData = StitchData(SkIntToScalar(stitchData.fWidth) * 2,
SkIntToScalar(stitchData.fHeight) * 2);
}
}