| 1 |
/* |
| 2 |
* Copyright 2024 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 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/effects/SkGradient.h" |
| 13 |
#include "src/shaders/SkEmptyShader.h" |
| 14 |
|
| 15 |
namespace skiagm { |
| 16 |
|
| 17 |
namespace { |
| 18 |
|
| 19 |
sk_sp<SkShader> empty(SkRect r) { return sk_make_sp<SkEmptyShader>(); } |
| 20 |
|
| 21 |
sk_sp<SkShader> degen_sweep(SkRect r) { |
| 22 |
// A too small angle between start and end falls back to an empty shader |
| 23 |
const float startAngle = 0.0f; |
| 24 |
const float endAngle = nextafter(startAngle, 360.0f); |
| 25 |
const SkColor4f colors[2] = { SkColors::kRed, SkColors::kGreen }; |
| 26 |
|
| 27 |
return SkShaders::SweepGradient(r.center(), startAngle, endAngle, |
| 28 |
{{colors, {}, SkTileMode::kDecal}, {}}); |
| 29 |
} |
| 30 |
|
| 31 |
sk_sp<SkShader> degen_linear(SkRect r) { |
| 32 |
// Having the two positions be the same causes a fallback to an empty shader |
| 33 |
const SkPoint pts[2] = { r.center(), r.center() }; |
| 34 |
const SkColor4f colors[2] = { SkColors::kRed, SkColors::kGreen }; |
| 35 |
|
| 36 |
return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kDecal}, {}}); |
| 37 |
} |
| 38 |
|
| 39 |
sk_sp<SkShader> degen_radial(SkRect r) { |
| 40 |
const SkColor4f colors[2] = { SkColors::kRed, SkColors::kGreen }; |
| 41 |
|
| 42 |
// Having a radius of 0.0 causes a fallback to an empty shader |
| 43 |
return SkShaders::RadialGradient(r.center(), 0, {{colors, {}, SkTileMode::kDecal}, {}}); |
| 44 |
} |
| 45 |
|
| 46 |
sk_sp<SkShader> degen_conical(SkRect r) { |
| 47 |
const SkColor4f colors[2] = { SkColors::kRed, SkColors::kGreen }; |
| 48 |
|
| 49 |
// Having the start and end radii be the same causes a fallback to an empty shader |
| 50 |
return SkShaders::TwoPointConicalGradient(r.center(), /* startRadius= */ 0.0f, |
| 51 |
r.center(), /* endRadius= */ 0.0f, |
| 52 |
{{colors, {}, SkTileMode::kDecal}, {}}); |
| 53 |
} |
| 54 |
|
| 55 |
} // anonymous namespace |
| 56 |
|
| 57 |
class EmptyShaderGM : public GM { |
| 58 |
public: |
| 59 |
EmptyShaderGM() { |
| 60 |
this->setBGColor(0xFFCCCCCC); |
| 61 |
} |
| 62 |
|
| 63 |
protected: |
| 64 |
SkString getName() const override { return SkString("emptyshader"); } |
| 65 |
|
| 66 |
SkISize getISize() override { return SkISize::Make(128, 88); } |
| 67 |
|
| 68 |
void onDraw(SkCanvas* canvas) override { |
| 69 |
SkPaint stroke; |
| 70 |
stroke.setStyle(SkPaint::kStroke_Style); |
| 71 |
|
| 72 |
int left = kPad, top = kPad; |
| 73 |
for (auto f : { empty, degen_sweep, degen_linear, degen_radial, degen_conical }) { |
| 74 |
SkRect r = SkRect::MakeXYWH(left, top, kSize, kSize); |
| 75 |
|
| 76 |
SkPaint p; |
| 77 |
p.setColor(SK_ColorBLUE); |
| 78 |
p.setShader(f(r)); |
| 79 |
|
| 80 |
canvas->drawRect(r, p); |
| 81 |
canvas->drawRect(r, stroke); |
| 82 |
|
| 83 |
left += kSize + kPad; |
| 84 |
if (left >= this->getISize().width()) { |
| 85 |
left = kPad; |
| 86 |
top += kSize + kPad; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
private: |
| 92 |
static constexpr int kPad = 8; |
| 93 |
static constexpr int kSize = 32; |
| 94 |
}; |
| 95 |
|
| 96 |
////////////////////////////////////////////////////////////////////////////// |
| 97 |
|
| 98 |
DEF_GM(return new EmptyShaderGM;) |
| 99 |
|
| 100 |
} // namespace skiagm |