New SVG turbulence in Skia
This cl contains the code for both CPU and GPU generation of noise. Both codepaths yield equivalent results. TEST:Added 'perlinnoise' gm Review URL: https://codereview.chromium.org/13047005 git-svn-id: http://skia.googlecode.com/svn/trunk@8371 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
c452d82c82
commit
2daa365123
113
gm/perlinnoise.cpp
Normal file
113
gm/perlinnoise.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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(500, 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(x, 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.4f, 0.2f, 3, 0, true);
|
||||
test(canvas, 200, 0, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.3f, 0.6f, 4, 0, false);
|
||||
test(canvas, 300, 0, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.2f, 0.4f, 5, 0, true);
|
||||
test(canvas, 400, 0, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.5f, 0.8f, 6, 0, false);
|
||||
|
||||
test(canvas, 0, 100, SkPerlinNoiseShader::kTurbulence_Type,
|
||||
0.1f, 0.1f, 2, 0, true);
|
||||
test(canvas, 100, 100, SkPerlinNoiseShader::kTurbulence_Type,
|
||||
0.4f, 0.2f, 3, 0, false);
|
||||
test(canvas, 200, 100, SkPerlinNoiseShader::kTurbulence_Type,
|
||||
0.3f, 0.6f, 4, 0, true);
|
||||
test(canvas, 300, 100, SkPerlinNoiseShader::kTurbulence_Type,
|
||||
0.2f, 0.4f, 5, 0, false);
|
||||
test(canvas, 400, 100, SkPerlinNoiseShader::kTurbulence_Type,
|
||||
0.5f, 0.8f, 6, 0, true);
|
||||
|
||||
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, 2, false);
|
||||
test(canvas, 200, 200, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.1f, 0.1f, 3, 3, false);
|
||||
test(canvas, 300, 200, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.1f, 0.1f, 3, 4, false);
|
||||
test(canvas, 400, 200, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.1f, 0.1f, 3, 5, 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.4f, 0.2f, 3, 0, true);
|
||||
test(canvas, 200, 300, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.3f, 0.6f, 4, 0, false);
|
||||
test(canvas, 300, 300, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.2f, 0.4f, 5, 0, true);
|
||||
test(canvas, 400, 300, SkPerlinNoiseShader::kFractalNoise_Type,
|
||||
0.5f, 0.8f, 6, 0, false);
|
||||
}
|
||||
|
||||
private:
|
||||
typedef GM INHERITED;
|
||||
SkISize fSize;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static GM* MyFactory(void*) { return new PerlinNoiseGM; }
|
||||
static GMRegistry reg(MyFactory);
|
||||
|
||||
}
|
@ -41,6 +41,7 @@
|
||||
'<(skia_src_path)/effects/SkMorphologyImageFilter.cpp',
|
||||
'<(skia_src_path)/effects/SkOffsetImageFilter.cpp',
|
||||
'<(skia_src_path)/effects/SkPaintFlagsDrawFilter.cpp',
|
||||
'<(skia_src_path)/effects/SkPerlinNoiseShader.cpp',
|
||||
'<(skia_src_path)/effects/SkPixelXorXfermode.cpp',
|
||||
'<(skia_src_path)/effects/SkPorterDuff.cpp',
|
||||
'<(skia_src_path)/effects/SkRectShaderImageFilter.cpp',
|
||||
@ -96,6 +97,7 @@
|
||||
'<(skia_include_path)/effects/SkOffsetImageFilter.h',
|
||||
'<(skia_include_path)/effects/SkMorphologyImageFilter.h',
|
||||
'<(skia_include_path)/effects/SkPaintFlagsDrawFilter.h',
|
||||
'<(skia_include_path)/effects/SkPerlinNoiseShader.h',
|
||||
'<(skia_include_path)/effects/SkPixelXorXfermode.h',
|
||||
'<(skia_include_path)/effects/SkPorterDuff.h',
|
||||
'<(skia_include_path)/effects/SkRectShaderImageFilter.h',
|
||||
|
@ -62,6 +62,7 @@
|
||||
'../gm/pathfill.cpp',
|
||||
'../gm/pathinterior.cpp',
|
||||
'../gm/pathreverse.cpp',
|
||||
'../gm/perlinnoise.cpp',
|
||||
'../gm/points.cpp',
|
||||
'../gm/poly2poly.cpp',
|
||||
'../gm/quadpaths.cpp',
|
||||
|
111
include/effects/SkPerlinNoiseShader.h
Normal file
111
include/effects/SkPerlinNoiseShader.h
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2013 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef SkPerlinNoiseShader_DEFINED
|
||||
#define SkPerlinNoiseShader_DEFINED
|
||||
|
||||
#include "SkShader.h"
|
||||
|
||||
/** \class SkPerlinNoiseShader
|
||||
|
||||
SkPerlinNoiseShader creates an image using the Perlin turbulence function.
|
||||
|
||||
It can produce tileable noise if asked to stitch tiles and provided a tile size.
|
||||
In order to fill a large area with repeating noise, set the stitchTiles flag to
|
||||
true, and render exactly a single tile of noise. Without this flag, the result
|
||||
will contain visible seams between tiles.
|
||||
|
||||
The algorithm used is described here :
|
||||
http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement
|
||||
*/
|
||||
class SkPerlinNoiseShader : public SkShader {
|
||||
struct PaintingData;
|
||||
public:
|
||||
struct StitchData;
|
||||
|
||||
/**
|
||||
* About the noise types : the difference between the 2 is just minor tweaks to the algorithm,
|
||||
* they're not 2 entirely different noises. The output looks different, but once the noise is
|
||||
* generated in the [1, -1] range, the output is brought back in the [0, 1] range by doing :
|
||||
* kFractalNoise_Type : noise * 0.5 + 0.5
|
||||
* kTurbulence_Type : abs(noise)
|
||||
* Very little differences between the 2 types, although you can tell the difference visually.
|
||||
*/
|
||||
enum Type {
|
||||
kFractalNoise_Type,
|
||||
kTurbulence_Type,
|
||||
kFirstType = kFractalNoise_Type,
|
||||
kLastType = kTurbulence_Type
|
||||
};
|
||||
/**
|
||||
* This will construct Perlin noise of the given type (Fractal Noise or Turbulence).
|
||||
*
|
||||
* Both base frequencies (X and Y) have a usual range of (0..1).
|
||||
*
|
||||
* The number of octaves provided should be fairly small, although no limit is enforced.
|
||||
* Each octave doubles the frequency, so 10 octaves would produce noise from
|
||||
* baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small
|
||||
* periods and resembles regular unstructured noise rather than Perlin noise.
|
||||
*
|
||||
* If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify
|
||||
* the frequencies so that the noise will be tileable for the given tile size. If tileSize
|
||||
* is NULL or an empty size, the frequencies will be used as is without modification.
|
||||
*/
|
||||
static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
|
||||
int numOctaves, SkScalar seed,
|
||||
const SkISize* tileSize = NULL);
|
||||
static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
|
||||
int numOctaves, SkScalar seed,
|
||||
const SkISize* tileSize = NULL);
|
||||
|
||||
virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
|
||||
const SkMatrix& matrix);
|
||||
virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE;
|
||||
virtual void shadeSpan16(int x, int y, uint16_t[], int count) SK_OVERRIDE;
|
||||
|
||||
virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint&) const SK_OVERRIDE;
|
||||
|
||||
SK_DEVELOPER_TO_STRING()
|
||||
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader)
|
||||
|
||||
protected:
|
||||
SkPerlinNoiseShader(SkFlattenableReadBuffer&);
|
||||
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
|
||||
|
||||
private:
|
||||
SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX,
|
||||
SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
|
||||
const SkISize* tileSize = NULL);
|
||||
virtual ~SkPerlinNoiseShader();
|
||||
|
||||
void setTileSize(const SkISize&);
|
||||
|
||||
void initPaint(PaintingData& paintingData);
|
||||
|
||||
SkScalar noise2D(int channel, const PaintingData& paintingData,
|
||||
const StitchData& stitchData, const SkPoint& noiseVector);
|
||||
|
||||
SkScalar calculateTurbulenceValueForPoint(int channel, const PaintingData& paintingData,
|
||||
StitchData& stitchData, const SkPoint& point);
|
||||
|
||||
SkPMColor shade(const SkPoint& point, StitchData& stitchData);
|
||||
|
||||
SkPerlinNoiseShader::Type fType;
|
||||
SkScalar fBaseFrequencyX;
|
||||
SkScalar fBaseFrequencyY;
|
||||
int fNumOctaves;
|
||||
SkScalar fSeed;
|
||||
SkISize fTileSize;
|
||||
bool fStitchTiles;
|
||||
SkMatrix fMatrix;
|
||||
|
||||
PaintingData* fPaintingData;
|
||||
|
||||
typedef SkShader INHERITED;
|
||||
};
|
||||
|
||||
#endif
|
1000
src/effects/SkPerlinNoiseShader.cpp
Normal file
1000
src/effects/SkPerlinNoiseShader.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@ -48,6 +48,7 @@
|
||||
#include "SkMergeImageFilter.h"
|
||||
#include "SkMorphologyImageFilter.h"
|
||||
#include "SkOffsetImageFilter.h"
|
||||
#include "SkPerlinNoiseShader.h"
|
||||
#include "SkPixelXorXfermode.h"
|
||||
#include "SkStippleMaskFilter.h"
|
||||
#include "SkTableColorFilter.h"
|
||||
@ -84,6 +85,7 @@ void SkFlattenable::InitializeFlattenables() {
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(Sk2DPathEffect)
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLine2DPathEffect)
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath2DPathEffect)
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPerlinNoiseShader)
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPixelXorXfermode)
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkStippleMaskFilter)
|
||||
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSumPathEffect)
|
||||
|
Loading…
Reference in New Issue
Block a user