2013-04-05 13:47:09 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2013-04-25 14:54:15 +00:00
|
|
|
class SK_API SkPerlinNoiseShader : public SkShader {
|
2013-04-05 13:47:09 +00:00
|
|
|
public:
|
|
|
|
struct StitchData;
|
2014-06-27 20:35:52 +00:00
|
|
|
struct PaintingData;
|
2013-04-05 13:47:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-03-25 16:08:00 +00:00
|
|
|
static sk_sp<SkShader> MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
|
|
|
|
int numOctaves, SkScalar seed,
|
|
|
|
const SkISize* tileSize = nullptr);
|
|
|
|
static sk_sp<SkShader> MakeTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
|
|
|
|
int numOctaves, SkScalar seed,
|
|
|
|
const SkISize* tileSize = nullptr);
|
|
|
|
|
Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/249643002/)
Reason for revert:
Chromium side change landed along side DEPS roll that includes r14323.
Original issue's description:
> Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/207683004/)
>
> Reason for revert:
> This is blocking the DEPS roll into Chromium. Failures can be seen here:
>
> http://build.chromium.org/p/tryserver.chromium/builders/android_dbg/builds/174333
>
> Original issue's description:
> > Extract most of the mutable state of SkShader into a separate Context object.
> >
> > SkShader currently stores some state during draw calls via setContext(...).
> > Move that mutable state into a separate SkShader::Context class that is
> > constructed on demand for the duration of the draw.
> >
> > Calls to setContext() are replaced with createContext() which returns a context
> > corresponding to the shader object or NULL if the parameters to createContext
> > are invalid.
> >
> > TEST=out/Debug/dm
> > BUG=skia:1976
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14216
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14323
>
> TBR=scroggo@google.com,skyostil@chromium.org,tomhudson@chromium.org,senorblanco@chromium.org,reed@google.com,bungeman@google.com,dominikg@chromium.org
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:1976
>
> Committed: http://code.google.com/p/skia/source/detail?r=14326
R=scroggo@google.com, skyostil@chromium.org, tomhudson@chromium.org, senorblanco@chromium.org, reed@google.com, bungeman@google.com, dominikg@chromium.org
TBR=bungeman@google.com, dominikg@chromium.org, reed@google.com, scroggo@google.com, senorblanco@chromium.org, skyostil@chromium.org, tomhudson@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=skia:1976
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/246403013
git-svn-id: http://skia.googlecode.com/svn/trunk@14328 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-23 19:10:51 +00:00
|
|
|
class PerlinNoiseShaderContext : public SkShader::Context {
|
|
|
|
public:
|
2014-05-01 19:31:31 +00:00
|
|
|
PerlinNoiseShaderContext(const SkPerlinNoiseShader& shader, const ContextRec&);
|
2014-06-27 20:35:52 +00:00
|
|
|
virtual ~PerlinNoiseShaderContext();
|
Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/249643002/)
Reason for revert:
Chromium side change landed along side DEPS roll that includes r14323.
Original issue's description:
> Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/207683004/)
>
> Reason for revert:
> This is blocking the DEPS roll into Chromium. Failures can be seen here:
>
> http://build.chromium.org/p/tryserver.chromium/builders/android_dbg/builds/174333
>
> Original issue's description:
> > Extract most of the mutable state of SkShader into a separate Context object.
> >
> > SkShader currently stores some state during draw calls via setContext(...).
> > Move that mutable state into a separate SkShader::Context class that is
> > constructed on demand for the duration of the draw.
> >
> > Calls to setContext() are replaced with createContext() which returns a context
> > corresponding to the shader object or NULL if the parameters to createContext
> > are invalid.
> >
> > TEST=out/Debug/dm
> > BUG=skia:1976
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14216
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14323
>
> TBR=scroggo@google.com,skyostil@chromium.org,tomhudson@chromium.org,senorblanco@chromium.org,reed@google.com,bungeman@google.com,dominikg@chromium.org
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:1976
>
> Committed: http://code.google.com/p/skia/source/detail?r=14326
R=scroggo@google.com, skyostil@chromium.org, tomhudson@chromium.org, senorblanco@chromium.org, reed@google.com, bungeman@google.com, dominikg@chromium.org
TBR=bungeman@google.com, dominikg@chromium.org, reed@google.com, scroggo@google.com, senorblanco@chromium.org, skyostil@chromium.org, tomhudson@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=skia:1976
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/246403013
git-svn-id: http://skia.googlecode.com/svn/trunk@14328 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-23 19:10:51 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void shadeSpan(int x, int y, SkPMColor[], int count) override;
|
Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/249643002/)
Reason for revert:
Chromium side change landed along side DEPS roll that includes r14323.
Original issue's description:
> Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/207683004/)
>
> Reason for revert:
> This is blocking the DEPS roll into Chromium. Failures can be seen here:
>
> http://build.chromium.org/p/tryserver.chromium/builders/android_dbg/builds/174333
>
> Original issue's description:
> > Extract most of the mutable state of SkShader into a separate Context object.
> >
> > SkShader currently stores some state during draw calls via setContext(...).
> > Move that mutable state into a separate SkShader::Context class that is
> > constructed on demand for the duration of the draw.
> >
> > Calls to setContext() are replaced with createContext() which returns a context
> > corresponding to the shader object or NULL if the parameters to createContext
> > are invalid.
> >
> > TEST=out/Debug/dm
> > BUG=skia:1976
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14216
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14323
>
> TBR=scroggo@google.com,skyostil@chromium.org,tomhudson@chromium.org,senorblanco@chromium.org,reed@google.com,bungeman@google.com,dominikg@chromium.org
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:1976
>
> Committed: http://code.google.com/p/skia/source/detail?r=14326
R=scroggo@google.com, skyostil@chromium.org, tomhudson@chromium.org, senorblanco@chromium.org, reed@google.com, bungeman@google.com, dominikg@chromium.org
TBR=bungeman@google.com, dominikg@chromium.org, reed@google.com, scroggo@google.com, senorblanco@chromium.org, skyostil@chromium.org, tomhudson@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=skia:1976
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/246403013
git-svn-id: http://skia.googlecode.com/svn/trunk@14328 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-23 19:10:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SkPMColor shade(const SkPoint& point, StitchData& stitchData) const;
|
|
|
|
SkScalar calculateTurbulenceValueForPoint(
|
2014-06-27 20:35:52 +00:00
|
|
|
int channel,
|
Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/249643002/)
Reason for revert:
Chromium side change landed along side DEPS roll that includes r14323.
Original issue's description:
> Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/207683004/)
>
> Reason for revert:
> This is blocking the DEPS roll into Chromium. Failures can be seen here:
>
> http://build.chromium.org/p/tryserver.chromium/builders/android_dbg/builds/174333
>
> Original issue's description:
> > Extract most of the mutable state of SkShader into a separate Context object.
> >
> > SkShader currently stores some state during draw calls via setContext(...).
> > Move that mutable state into a separate SkShader::Context class that is
> > constructed on demand for the duration of the draw.
> >
> > Calls to setContext() are replaced with createContext() which returns a context
> > corresponding to the shader object or NULL if the parameters to createContext
> > are invalid.
> >
> > TEST=out/Debug/dm
> > BUG=skia:1976
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14216
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14323
>
> TBR=scroggo@google.com,skyostil@chromium.org,tomhudson@chromium.org,senorblanco@chromium.org,reed@google.com,bungeman@google.com,dominikg@chromium.org
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:1976
>
> Committed: http://code.google.com/p/skia/source/detail?r=14326
R=scroggo@google.com, skyostil@chromium.org, tomhudson@chromium.org, senorblanco@chromium.org, reed@google.com, bungeman@google.com, dominikg@chromium.org
TBR=bungeman@google.com, dominikg@chromium.org, reed@google.com, scroggo@google.com, senorblanco@chromium.org, skyostil@chromium.org, tomhudson@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=skia:1976
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/246403013
git-svn-id: http://skia.googlecode.com/svn/trunk@14328 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-23 19:10:51 +00:00
|
|
|
StitchData& stitchData, const SkPoint& point) const;
|
2014-06-27 20:35:52 +00:00
|
|
|
SkScalar noise2D(int channel,
|
Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/249643002/)
Reason for revert:
Chromium side change landed along side DEPS roll that includes r14323.
Original issue's description:
> Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/207683004/)
>
> Reason for revert:
> This is blocking the DEPS roll into Chromium. Failures can be seen here:
>
> http://build.chromium.org/p/tryserver.chromium/builders/android_dbg/builds/174333
>
> Original issue's description:
> > Extract most of the mutable state of SkShader into a separate Context object.
> >
> > SkShader currently stores some state during draw calls via setContext(...).
> > Move that mutable state into a separate SkShader::Context class that is
> > constructed on demand for the duration of the draw.
> >
> > Calls to setContext() are replaced with createContext() which returns a context
> > corresponding to the shader object or NULL if the parameters to createContext
> > are invalid.
> >
> > TEST=out/Debug/dm
> > BUG=skia:1976
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14216
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14323
>
> TBR=scroggo@google.com,skyostil@chromium.org,tomhudson@chromium.org,senorblanco@chromium.org,reed@google.com,bungeman@google.com,dominikg@chromium.org
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:1976
>
> Committed: http://code.google.com/p/skia/source/detail?r=14326
R=scroggo@google.com, skyostil@chromium.org, tomhudson@chromium.org, senorblanco@chromium.org, reed@google.com, bungeman@google.com, dominikg@chromium.org
TBR=bungeman@google.com, dominikg@chromium.org, reed@google.com, scroggo@google.com, senorblanco@chromium.org, skyostil@chromium.org, tomhudson@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=skia:1976
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/246403013
git-svn-id: http://skia.googlecode.com/svn/trunk@14328 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-23 19:10:51 +00:00
|
|
|
const StitchData& stitchData, const SkPoint& noiseVector) const;
|
|
|
|
|
|
|
|
SkMatrix fMatrix;
|
2014-06-27 20:35:52 +00:00
|
|
|
PaintingData* fPaintingData;
|
Revert of Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/249643002/)
Reason for revert:
Chromium side change landed along side DEPS roll that includes r14323.
Original issue's description:
> Revert of Extract most of the mutable state of SkShader into a separate Context object. (https://codereview.chromium.org/207683004/)
>
> Reason for revert:
> This is blocking the DEPS roll into Chromium. Failures can be seen here:
>
> http://build.chromium.org/p/tryserver.chromium/builders/android_dbg/builds/174333
>
> Original issue's description:
> > Extract most of the mutable state of SkShader into a separate Context object.
> >
> > SkShader currently stores some state during draw calls via setContext(...).
> > Move that mutable state into a separate SkShader::Context class that is
> > constructed on demand for the duration of the draw.
> >
> > Calls to setContext() are replaced with createContext() which returns a context
> > corresponding to the shader object or NULL if the parameters to createContext
> > are invalid.
> >
> > TEST=out/Debug/dm
> > BUG=skia:1976
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14216
> >
> > Committed: http://code.google.com/p/skia/source/detail?r=14323
>
> TBR=scroggo@google.com,skyostil@chromium.org,tomhudson@chromium.org,senorblanco@chromium.org,reed@google.com,bungeman@google.com,dominikg@chromium.org
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:1976
>
> Committed: http://code.google.com/p/skia/source/detail?r=14326
R=scroggo@google.com, skyostil@chromium.org, tomhudson@chromium.org, senorblanco@chromium.org, reed@google.com, bungeman@google.com, dominikg@chromium.org
TBR=bungeman@google.com, dominikg@chromium.org, reed@google.com, scroggo@google.com, senorblanco@chromium.org, skyostil@chromium.org, tomhudson@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=skia:1976
Author: bsalomon@google.com
Review URL: https://codereview.chromium.org/246403013
git-svn-id: http://skia.googlecode.com/svn/trunk@14328 2bbb7eff-a529-9590-31e7-b0007b416f81
2014-04-23 19:10:51 +00:00
|
|
|
|
|
|
|
typedef SkShader::Context INHERITED;
|
|
|
|
};
|
2013-04-05 13:47:09 +00:00
|
|
|
|
2015-08-29 01:46:56 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2016-07-22 18:04:53 +00:00
|
|
|
sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
|
2015-08-29 01:46:56 +00:00
|
|
|
#endif
|
2013-04-05 13:47:09 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
SK_TO_STRING_OVERRIDE()
|
2013-04-05 13:47:09 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader)
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void flatten(SkWriteBuffer&) const override;
|
|
|
|
Context* onCreateContext(const ContextRec&, void* storage) const override;
|
2016-03-04 02:18:25 +00:00
|
|
|
size_t onContextSize(const ContextRec&) const override;
|
2013-04-05 13:47:09 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX,
|
|
|
|
SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
|
2014-03-06 15:13:53 +00:00
|
|
|
const SkISize* tileSize);
|
2013-04-05 13:47:09 +00:00
|
|
|
virtual ~SkPerlinNoiseShader();
|
|
|
|
|
2014-12-01 19:47:08 +00:00
|
|
|
const SkPerlinNoiseShader::Type fType;
|
|
|
|
const SkScalar fBaseFrequencyX;
|
|
|
|
const SkScalar fBaseFrequencyY;
|
|
|
|
const int fNumOctaves;
|
|
|
|
const SkScalar fSeed;
|
|
|
|
const SkISize fTileSize;
|
|
|
|
const bool fStitchTiles;
|
2013-04-05 13:47:09 +00:00
|
|
|
|
|
|
|
typedef SkShader INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|