| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkRRect.h" |
| 14 |
#include "include/core/SkSize.h" |
| 15 |
#include "include/core/SkString.h" |
| 16 |
#include "include/core/SkSurface.h" |
| 17 |
#include "include/effects/SkGradient.h" |
| 18 |
#include "include/effects/SkImageFilters.h" |
| 19 |
#include "include/effects/SkRuntimeEffect.h" |
| 20 |
#include "src/base/SkRandom.h" |
| 21 |
#include "src/core/SkColorSpacePriv.h" |
| 22 |
#include "src/core/SkRuntimeEffectPriv.h" |
| 23 |
#include "tools/DecodeUtils.h" |
| 24 |
#include "tools/Resources.h" |
| 25 |
|
| 26 |
#include <cmath> |
| 27 |
|
| 28 |
class RippleShaderGM : public skiagm::GM { |
| 29 |
public: |
| 30 |
static constexpr SkISize kSize = {512, 512}; |
| 31 |
|
| 32 |
void onOnceBeforeDraw() override { |
| 33 |
// Load the mandrill into a shader. |
| 34 |
sk_sp<SkImage> img = ToolUtils::GetResourceAsImage("images/mandrill_512.png"); |
| 35 |
if (!img) { |
| 36 |
SkDebugf("Unable to load mandrill_512 from resources directory"); |
| 37 |
return; |
| 38 |
} |
| 39 |
fMandrill = img->makeShader(SkSamplingOptions()); |
| 40 |
|
| 41 |
// Load RippleShader.rts into a SkRuntimeEffect. |
| 42 |
sk_sp<SkData> shaderData = GetResourceAsData("sksl/realistic/RippleShader.rts"); |
| 43 |
if (!shaderData) { |
| 44 |
SkDebugf("Unable to load ripple shader from resources directory"); |
| 45 |
return; |
| 46 |
} |
| 47 |
auto [effect, error] = SkRuntimeEffect::MakeForShader( |
| 48 |
SkString(static_cast<const char*>(shaderData->data()), shaderData->size())); |
| 49 |
if (!effect) { |
| 50 |
SkDebugf("Ripple shader failed to compile\n\n%s\n", error.c_str()); |
| 51 |
} |
| 52 |
fEffect = std::move(effect); |
| 53 |
} |
| 54 |
|
| 55 |
SkString getName() const override { return SkString("rippleshader"); } |
| 56 |
SkISize getISize() override { return kSize; } |
| 57 |
bool onAnimate(double nanos) override { |
| 58 |
fMillis = nanos / (1000. * 1000.); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
void onDraw(SkCanvas* canvas) override { |
| 63 |
SkPaint base; |
| 64 |
base.setShader(fMandrill); |
| 65 |
canvas->drawRect(SkRect::MakeWH(kSize.width(), kSize.height()), base); |
| 66 |
|
| 67 |
// Uniform setting logic was imperfectly adapted from: |
| 68 |
// frameworks/base/graphics/java/android/graphics/drawable/RippleShader.java |
| 69 |
// frameworks/base/graphics/java/android/graphics/drawable/RippleAnimationSession.java |
| 70 |
|
| 71 |
SkRuntimeShaderBuilder builder(fEffect); |
| 72 |
constexpr float ANIM_DURATION = 1500.0f; |
| 73 |
constexpr float NOISE_ANIMATION_DURATION = 7000.0f; |
| 74 |
constexpr float MAX_NOISE_PHASE = NOISE_ANIMATION_DURATION / 214.0f; |
| 75 |
constexpr float PI_ROTATE_RIGHT = SK_ScalarPI * 0.0078125f; |
| 76 |
constexpr float PI_ROTATE_LEFT = SK_ScalarPI * -0.0078125f; |
| 77 |
|
| 78 |
builder.uniform("in_origin") = SkV2{kSize.width() / 2, kSize.height() / 2}; |
| 79 |
builder.uniform("in_touch") = SkV2{kSize.width() / 2, kSize.height() / 2}; |
| 80 |
// Note that `in_progress` should actually be interpolated via FAST_OUT_SLOW_IN. |
| 81 |
builder.uniform("in_progress") = this->sawtoothLerp(0.0f, 1.0f, ANIM_DURATION); |
| 82 |
builder.uniform("in_maxRadius") = 400.0f; |
| 83 |
builder.uniform("in_resolutionScale") = SkV2{1.0f / kSize.width(), 1.0f / kSize.height()}; |
| 84 |
builder.uniform("in_noiseScale") = SkV2{2.1f / kSize.width(), 2.1f / kSize.height()}; |
| 85 |
builder.uniform("in_hasMask") = 1.0f; |
| 86 |
|
| 87 |
float phase = this->sawtoothLerp(0, MAX_NOISE_PHASE, NOISE_ANIMATION_DURATION); |
| 88 |
builder.uniform("in_noisePhase") = phase; |
| 89 |
builder.uniform("in_turbulencePhase") = phase * 1000.0f; |
| 90 |
|
| 91 |
const float scale = 1.5f; |
| 92 |
builder.uniform("in_tCircle1") = SkV2{scale * .5f + (phase * 0.01f * cosf(scale * .55f)), |
| 93 |
scale * .5f + (phase * 0.01f * sinf(scale * .55f))}; |
| 94 |
builder.uniform("in_tCircle2") = SkV2{scale * .2f + (phase * -.0066f * cosf(scale * .45f)), |
| 95 |
scale * .2f + (phase * -.0066f * sinf(scale * .45f))}; |
| 96 |
builder.uniform("in_tCircle3") = SkV2{scale + (phase * -.0066f * cosf(scale * .35f)), |
| 97 |
scale + (phase * -.0066f * sinf(scale * .35f))}; |
| 98 |
|
| 99 |
float rotation1 = phase * PI_ROTATE_RIGHT + 1.7f * SK_ScalarPI; |
| 100 |
builder.uniform("in_tRotation1") = SkV2{cosf(rotation1), sinf(rotation1)}; |
| 101 |
|
| 102 |
float rotation2 = phase * PI_ROTATE_LEFT + 2.0f * SK_ScalarPI; |
| 103 |
builder.uniform("in_tRotation2") = SkV2{cosf(rotation2), sinf(rotation2)}; |
| 104 |
|
| 105 |
float rotation3 = phase * PI_ROTATE_RIGHT + 2.75f * SK_ScalarPI; |
| 106 |
builder.uniform("in_tRotation3") = SkV2{cosf(rotation3), sinf(rotation3)}; |
| 107 |
|
| 108 |
builder.uniform("in_color") = SkV4{0.0f, 0.6f, 0.0f, 1.0f}; // green |
| 109 |
builder.uniform("in_sparkleColor") = SkV4{1.0f, 1.0f, 1.0f, 1.0f}; // white |
| 110 |
builder.child("in_shader") = fMandrill; |
| 111 |
|
| 112 |
SkPaint sparkle; |
| 113 |
sparkle.setShader(builder.makeShader()); |
| 114 |
canvas->drawRect(SkRect::MakeWH(kSize.width(), kSize.height()), sparkle); |
| 115 |
} |
| 116 |
|
| 117 |
float sawtoothLerp(float a, float b, float windowMs) { |
| 118 |
float t = std::fmod(fMillis, windowMs) / windowMs; |
| 119 |
return a * (1. - t) + b * t; |
| 120 |
} |
| 121 |
|
| 122 |
protected: |
| 123 |
sk_sp<SkRuntimeEffect> fEffect; |
| 124 |
sk_sp<SkShader> fMandrill; |
| 125 |
float fMillis = 500.0f; // this allows a non-animated single-frame capture to show the effect |
| 126 |
|
| 127 |
}; |
| 128 |
|
| 129 |
DEF_GM(return new RippleShaderGM;) |