skia2/gm/perlinnoise.cpp
commit-bot@chromium.org 344cf45a40 *** Perlin noise GM needs to be rebaselined ***
Enabling Perlin Noise on Android

I enabled the Perlin Noise shader on Android after doing some minor modifications to the shader, specifically for Android (and #ifdefed for Android, to make sure none of this affects other platforms).

For Tegra devices (Nexus 7, Xoom), a precision issue related to the color values read from textures caused the noise to read the wrong indices and produce bad noise. I fixed this by adding a founding of the values read by simply doing the equivalent of "colorValue = floor(colorValue * 255.0) / 255.0" to make sure we retrieve the colors that were written in the texture originally.

For non-Tegra devices (Nexus 10), dealing with values in the order of 4096.0 was problematic without using the "highp" precision setting. To solve this, a few variables were given the high precision setting.

Since both fixes don't seem to do considerable harm to the platforms that are not being targetted, I left both fixes on all android devices for now.

I also reduced the Perlin noise gm so that it takes less time to test it on the Xoom (Original time was about 20 seconds, this shold take less than 10, hopefully)

BUG=
R=senorblanco@google.com, bsalomon@google.com, sugoi@google.com, senorblanco@chromium.org

Author: sugoi@chromium.org

Review URL: https://chromiumcodereview.appspot.com/16818013

git-svn-id: http://skia.googlecode.com/svn/trunk@9637 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-06-17 14:19:01 +00:00

90 lines
3.0 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkPerlinNoiseShader.h"
namespace skiagm {
class PerlinNoiseGM : public GM {
public:
PerlinNoiseGM() {
this->setBGColor(0xFF000000);
fSize = SkISize::Make(80, 80);
}
protected:
virtual SkString onShortName() {
return SkString("perlinnoise");
}
virtual SkISize onISize() {
return make_isize(200, 400);
}
void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
canvas->save();
canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height())));
SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
SkIntToScalar(fSize.width()),
SkIntToScalar(fSize.height()));
canvas->drawRect(r, paint);
canvas->restore();
}
void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
bool stitchTiles) {
SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
seed, stitchTiles ? &fSize : NULL) :
SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, numOctaves,
seed, stitchTiles ? &fSize : NULL);
SkPaint paint;
paint.setShader(shader)->unref();
drawClippedRect(canvas, x, y, paint);
}
virtual void onDraw(SkCanvas* canvas) {
canvas->clear(0x00000000);
test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
0.1f, 0.1f, 2, 0, false);
test(canvas, 100, 0, SkPerlinNoiseShader::kFractalNoise_Type,
0.2f, 0.4f, 5, 0, true);
test(canvas, 0, 100, SkPerlinNoiseShader::kTurbulence_Type,
0.1f, 0.1f, 2, 0, true);
test(canvas, 100, 100, SkPerlinNoiseShader::kTurbulence_Type,
0.2f, 0.4f, 5, 0, false);
test(canvas, 0, 200, SkPerlinNoiseShader::kFractalNoise_Type,
0.1f, 0.1f, 3, 1, false);
test(canvas, 100, 200, SkPerlinNoiseShader::kFractalNoise_Type,
0.1f, 0.1f, 3, 4, false);
canvas->scale(SkFloatToScalar(0.75f), SkFloatToScalar(1.0f));
test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type,
0.1f, 0.1f, 2, 0, false);
test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type,
0.2f, 0.4f, 5, 0, true);
}
private:
typedef GM INHERITED;
SkISize fSize;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new PerlinNoiseGM; }
static GMRegistry reg(MyFactory);
}