Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
RippleShaderGM Class Reference
Inheritance diagram for RippleShaderGM:
skiagm::GM

Public Member Functions

void onOnceBeforeDraw () override
 
SkString getName () const override
 
SkISize getISize () override
 
bool onAnimate (double nanos) override
 
void onDraw (SkCanvas *canvas) override
 
float sawtoothLerp (float a, float b, float windowMs)
 
- Public Member Functions inherited from skiagm::GM
 GM (SkColor backgroundColor=SK_ColorWHITE)
 
virtual ~GM ()
 
void setMode (Mode mode)
 
Mode getMode () const
 
DrawResult gpuSetup (SkCanvas *, SkString *errorMsg, GraphiteTestContext *=nullptr)
 
void gpuTeardown ()
 
void onceBeforeDraw ()
 
DrawResult draw (SkCanvas *canvas)
 
DrawResult draw (SkCanvas *, SkString *errorMsg)
 
void drawBackground (SkCanvas *)
 
DrawResult drawContent (SkCanvas *canvas)
 
DrawResult drawContent (SkCanvas *, SkString *errorMsg)
 
virtual SkISize getISize ()=0
 
virtual SkString getName () const =0
 
virtual bool runAsBench () const
 
SkScalar width ()
 
SkScalar height ()
 
SkColor getBGColor () const
 
void setBGColor (SkColor)
 
void drawSizeBounds (SkCanvas *, SkColor)
 
bool animate (double)
 
virtual bool onChar (SkUnichar)
 
bool getControls (SkMetaData *controls)
 
void setControls (const SkMetaData &controls)
 
virtual void modifyGrContextOptions (GrContextOptions *)
 
virtual void modifyGraphiteContextOptions (skgpu::graphite::ContextOptions *) const
 
virtual bool isBazelOnly () const
 
virtual std::map< std::string, std::string > getGoldKeys () const
 

Static Public Attributes

static constexpr SkISize kSize = {512, 512}
 
- Static Public Attributes inherited from skiagm::GM
static constexpr char kErrorMsg_DrawSkippedGpuOnly []
 

Protected Attributes

sk_sp< SkRuntimeEffectfEffect
 
sk_sp< SkShaderfMandrill
 
float fMillis = 500.0f
 

Additional Inherited Members

- Public Types inherited from skiagm::GM
enum  Mode { kGM_Mode , kSample_Mode , kBench_Mode }
 
using DrawResult = skiagm::DrawResult
 
using GraphiteTestContext = skiatest::graphite::GraphiteTestContext
 
- Protected Member Functions inherited from skiagm::GM
virtual DrawResult onGpuSetup (SkCanvas *, SkString *, GraphiteTestContext *)
 
virtual void onGpuTeardown ()
 
virtual void onOnceBeforeDraw ()
 
virtual DrawResult onDraw (SkCanvas *, SkString *errorMsg)
 
virtual void onDraw (SkCanvas *)
 
virtual bool onAnimate (double)
 
virtual bool onGetControls (SkMetaData *)
 
virtual void onSetControls (const SkMetaData &)
 
GraphiteTestContextgraphiteTestContext () const
 

Detailed Description

Definition at line 29 of file rippleshadergm.cpp.

Member Function Documentation

◆ getISize()

SkISize RippleShaderGM::getISize ( )
inlineoverridevirtual

Implements skiagm::GM.

Definition at line 57 of file rippleshadergm.cpp.

57{ return kSize; }
static constexpr SkISize kSize

◆ getName()

SkString RippleShaderGM::getName ( ) const
inlineoverridevirtual

Implements skiagm::GM.

Definition at line 56 of file rippleshadergm.cpp.

56{ return SkString("rippleshader"); }

◆ onAnimate()

bool RippleShaderGM::onAnimate ( double  nanos)
inlineoverridevirtual

Reimplemented from skiagm::GM.

Definition at line 58 of file rippleshadergm.cpp.

58 {
59 fMillis = nanos / (1000. * 1000.);
60 return true;
61 }

◆ onDraw()

void RippleShaderGM::onDraw ( SkCanvas canvas)
inlineoverridevirtual

Reimplemented from skiagm::GM.

Definition at line 63 of file rippleshadergm.cpp.

63 {
65 base.setShader(fMandrill);
67
68 // Uniform setting logic was imperfectly adapted from:
69 // frameworks/base/graphics/java/android/graphics/drawable/RippleShader.java
70 // frameworks/base/graphics/java/android/graphics/drawable/RippleAnimationSession.java
71
73 constexpr float ANIM_DURATION = 1500.0f;
74 constexpr float NOISE_ANIMATION_DURATION = 7000.0f;
75 constexpr float MAX_NOISE_PHASE = NOISE_ANIMATION_DURATION / 214.0f;
76 constexpr float PI_ROTATE_RIGHT = SK_ScalarPI * 0.0078125f;
77 constexpr float PI_ROTATE_LEFT = SK_ScalarPI * -0.0078125f;
78
79 builder.uniform("in_origin") = SkV2{kSize.width() / 2, kSize.height() / 2};
80 builder.uniform("in_touch") = SkV2{kSize.width() / 2, kSize.height() / 2};
81 // Note that `in_progress` should actually be interpolated via FAST_OUT_SLOW_IN.
82 builder.uniform("in_progress") = this->sawtoothLerp(0.0f, 1.0f, ANIM_DURATION);
83 builder.uniform("in_maxRadius") = 400.0f;
84 builder.uniform("in_resolutionScale") = SkV2{1.0f / kSize.width(), 1.0f / kSize.height()};
85 builder.uniform("in_noiseScale") = SkV2{2.1f / kSize.width(), 2.1f / kSize.height()};
86 builder.uniform("in_hasMask") = 1.0f;
87
88 float phase = this->sawtoothLerp(0, MAX_NOISE_PHASE, NOISE_ANIMATION_DURATION);
89 builder.uniform("in_noisePhase") = phase;
90 builder.uniform("in_turbulencePhase") = phase * 1000.0f;
91
92 const float scale = 1.5f;
93 builder.uniform("in_tCircle1") = SkV2{scale * .5f + (phase * 0.01f * cosf(scale * .55f)),
94 scale * .5f + (phase * 0.01f * sinf(scale * .55f))};
95 builder.uniform("in_tCircle2") = SkV2{scale * .2f + (phase * -.0066f * cosf(scale * .45f)),
96 scale * .2f + (phase * -.0066f * sinf(scale * .45f))};
97 builder.uniform("in_tCircle3") = SkV2{scale + (phase * -.0066f * cosf(scale * .35f)),
98 scale + (phase * -.0066f * sinf(scale * .35f))};
99
100 float rotation1 = phase * PI_ROTATE_RIGHT + 1.7f * SK_ScalarPI;
101 builder.uniform("in_tRotation1") = SkV2{cosf(rotation1), sinf(rotation1)};
102
103 float rotation2 = phase * PI_ROTATE_LEFT + 2.0f * SK_ScalarPI;
104 builder.uniform("in_tRotation2") = SkV2{cosf(rotation2), sinf(rotation2)};
105
106 float rotation3 = phase * PI_ROTATE_RIGHT + 2.75f * SK_ScalarPI;
107 builder.uniform("in_tRotation3") = SkV2{cosf(rotation3), sinf(rotation3)};
108
109 builder.uniform("in_color") = SkV4{0.0f, 0.6f, 0.0f, 1.0f}; // green
110 builder.uniform("in_sparkleColor") = SkV4{1.0f, 1.0f, 1.0f, 1.0f}; // white
111 builder.child("in_shader") = fMandrill;
112
113 SkPaint sparkle;
114 sparkle.setShader(builder.makeShader());
115 canvas->drawRect(SkRect::MakeWH(kSize.width(), kSize.height()), sparkle);
116 }
#define SK_ScalarPI
Definition: SkScalar.h:21
float sawtoothLerp(float a, float b, float windowMs)
sk_sp< SkRuntimeEffect > fEffect
sk_sp< SkShader > fMandrill
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void setShader(sk_sp< SkShader > shader)
const Scalar scale
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
Definition: SkM44.h:19
Definition: SkM44.h:98

◆ onOnceBeforeDraw()

void RippleShaderGM::onOnceBeforeDraw ( )
inlineoverridevirtual

Reimplemented from skiagm::GM.

Definition at line 33 of file rippleshadergm.cpp.

33 {
34 // Load the mandrill into a shader.
35 sk_sp<SkImage> img = ToolUtils::GetResourceAsImage("images/mandrill_512.png");
36 if (!img) {
37 SkDebugf("Unable to load mandrill_512 from resources directory");
38 return;
39 }
41
42 // Load RippleShader.rts into a SkRuntimeEffect.
43 sk_sp<SkData> shaderData = GetResourceAsData("sksl/realistic/RippleShader.rts");
44 if (!shaderData) {
45 SkDebugf("Unable to load ripple shader from resources directory");
46 return;
47 }
49 SkString(static_cast<const char*>(shaderData->data()), shaderData->size()));
50 if (!effect) {
51 SkDebugf("Ripple shader failed to compile\n\n%s\n", error.c_str());
52 }
53 fEffect = std::move(effect);
54 }
sk_sp< SkData > GetResourceAsData(const char *resource)
Definition: Resources.cpp:42
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
const void * data() const
Definition: SkData.h:37
size_t size() const
Definition: SkData.h:30
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
static Result MakeForShader(SkString sksl, const Options &)
const uint8_t uint32_t uint32_t GError ** error
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
SkSamplingOptions(SkFilterMode::kLinear))

◆ sawtoothLerp()

float RippleShaderGM::sawtoothLerp ( float  a,
float  b,
float  windowMs 
)
inline

Definition at line 118 of file rippleshadergm.cpp.

118 {
119 float t = std::fmod(fMillis, windowMs) / windowMs;
120 return a * (1. - t) + b * t;
121 }
static bool b
struct MyStruct a[10]

Member Data Documentation

◆ fEffect

sk_sp<SkRuntimeEffect> RippleShaderGM::fEffect
protected

Definition at line 124 of file rippleshadergm.cpp.

◆ fMandrill

sk_sp<SkShader> RippleShaderGM::fMandrill
protected

Definition at line 125 of file rippleshadergm.cpp.

◆ fMillis

float RippleShaderGM::fMillis = 500.0f
protected

Definition at line 126 of file rippleshadergm.cpp.

◆ kSize

constexpr SkISize RippleShaderGM::kSize = {512, 512}
staticconstexpr

Definition at line 31 of file rippleshadergm.cpp.


The documentation for this class was generated from the following file: