| 1 |
/* |
| 2 |
* Copyright 2021 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 |
/* |
| 9 |
* This GM creates the same gradients as the Chromium test fillrect_gradient: |
| 10 |
* http://osscs/chromium/chromium/src/+/main:third_party/blink/web_tests/fast/canvas/fillrect_gradient.html |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "gm/gm.h" |
| 14 |
#include "include/core/SkCanvas.h" |
| 15 |
#include "include/core/SkColor.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkRect.h" |
| 19 |
#include "include/core/SkRefCnt.h" |
| 20 |
#include "include/core/SkScalar.h" |
| 21 |
#include "include/core/SkShader.h" |
| 22 |
#include "include/core/SkSize.h" |
| 23 |
#include "include/core/SkString.h" |
| 24 |
#include "include/core/SkTileMode.h" |
| 25 |
#include "include/effects/SkGradient.h" |
| 26 |
|
| 27 |
#include <vector> |
| 28 |
|
| 29 |
const int kCellSize = 50; |
| 30 |
const int kNumColumns = 2; |
| 31 |
const int kNumRows = 9; |
| 32 |
const int kPadSize = 10; |
| 33 |
|
| 34 |
class FillrectGradientGM : public skiagm::GM { |
| 35 |
public: |
| 36 |
FillrectGradientGM() {} |
| 37 |
|
| 38 |
protected: |
| 39 |
struct GradientStop { |
| 40 |
float pos; |
| 41 |
SkColor color; |
| 42 |
}; |
| 43 |
|
| 44 |
SkString getName() const override { return SkString("fillrect_gradient"); } |
| 45 |
|
| 46 |
SkISize getISize() override { |
| 47 |
return SkISize::Make(kNumColumns * (kCellSize + kPadSize), |
| 48 |
kNumRows * (kCellSize + kPadSize)); |
| 49 |
} |
| 50 |
|
| 51 |
void drawGradient(SkCanvas* canvas, std::initializer_list<GradientStop> stops) { |
| 52 |
std::vector<SkColor4f> colors; |
| 53 |
std::vector<SkScalar> positions; |
| 54 |
colors.reserve(stops.size()); |
| 55 |
positions.reserve(stops.size()); |
| 56 |
|
| 57 |
for (const GradientStop& stop : stops) { |
| 58 |
colors.push_back(SkColor4f::FromColor(stop.color)); |
| 59 |
positions.push_back(stop.pos); |
| 60 |
} |
| 61 |
|
| 62 |
static constexpr SkPoint points[] = { |
| 63 |
SkPoint::Make(kCellSize, 0), |
| 64 |
SkPoint::Make(kCellSize, kCellSize), |
| 65 |
}; |
| 66 |
|
| 67 |
// Draw the gradient linearly. |
| 68 |
sk_sp<SkShader> shader = SkShaders::LinearGradient(points, |
| 69 |
{{colors, positions, SkTileMode::kClamp}, {}}); |
| 70 |
SkPaint paint; |
| 71 |
paint.setShader(shader); |
| 72 |
canvas->drawRect(SkRect::MakeXYWH(0, 0, kCellSize, kCellSize), paint); |
| 73 |
|
| 74 |
canvas->save(); |
| 75 |
canvas->translate(kCellSize + kPadSize, 0); |
| 76 |
|
| 77 |
// Draw the gradient radially. |
| 78 |
shader = SkShaders::RadialGradient(SkPoint::Make(kCellSize / 2, kCellSize / 2), |
| 79 |
kCellSize / 2, |
| 80 |
{{colors, positions, SkTileMode::kClamp}, {}}); |
| 81 |
paint.setShader(shader); |
| 82 |
canvas->drawRect(SkRect::MakeXYWH(0, 0, kCellSize, kCellSize), paint); |
| 83 |
|
| 84 |
canvas->restore(); |
| 85 |
canvas->translate(0, kCellSize + kPadSize); |
| 86 |
} |
| 87 |
|
| 88 |
void onDraw(SkCanvas* canvas) override { |
| 89 |
// Simple gradient: Green to white |
| 90 |
this->drawGradient(canvas, {{0.0f, SK_ColorGREEN}, {1.0f, SK_ColorWHITE}}); |
| 91 |
|
| 92 |
// Multiple sections: Green to white to red |
| 93 |
this->drawGradient(canvas, |
| 94 |
{{0.0f, SK_ColorGREEN}, {0.5f, SK_ColorWHITE}, {1.0f, SK_ColorRED}}); |
| 95 |
|
| 96 |
// No stops at 0.0 or 1.0: Larger green to white to larger red |
| 97 |
this->drawGradient(canvas, |
| 98 |
{{0.4f, SK_ColorGREEN}, {0.5f, SK_ColorWHITE}, {0.6f, SK_ColorRED}}); |
| 99 |
|
| 100 |
// Only one stop, at zero: Solid red |
| 101 |
this->drawGradient(canvas, {{0.0f, SK_ColorRED}}); |
| 102 |
|
| 103 |
// Only one stop, at 1.0: Solid red |
| 104 |
this->drawGradient(canvas, {{1.0f, SK_ColorRED}}); |
| 105 |
|
| 106 |
// Only one stop, in the middle: Solid red |
| 107 |
this->drawGradient(canvas, {{0.5f, SK_ColorRED}}); |
| 108 |
|
| 109 |
// Disjoint gradients (multiple stops at the same offset) |
| 110 |
// Blue to white in the top (inner) half, red to yellow in the bottom (outer) half |
| 111 |
this->drawGradient(canvas, |
| 112 |
{{0.0f, SK_ColorBLUE}, |
| 113 |
{0.5f, SK_ColorWHITE}, |
| 114 |
{0.5f, SK_ColorRED}, |
| 115 |
{1.0f, SK_ColorYELLOW}}); |
| 116 |
|
| 117 |
// Ignored stops: Blue to white, red to yellow (same as previous) |
| 118 |
this->drawGradient(canvas, |
| 119 |
{{0.0f, SK_ColorBLUE}, |
| 120 |
{0.5f, SK_ColorWHITE}, |
| 121 |
{0.5f, SK_ColorGRAY}, |
| 122 |
{0.5f, SK_ColorCYAN}, |
| 123 |
{0.5f, SK_ColorRED}, |
| 124 |
{1.0f, SK_ColorYELLOW}}); |
| 125 |
|
| 126 |
// Unsorted stops: Blue to white, red to yellow |
| 127 |
// Unlike Chrome, we don't sort the stops, so this renders differently than the prior cell. |
| 128 |
this->drawGradient(canvas, |
| 129 |
{{0.5f, SK_ColorWHITE}, |
| 130 |
{0.5f, SK_ColorGRAY}, |
| 131 |
{1.0f, SK_ColorYELLOW}, |
| 132 |
{0.5f, SK_ColorCYAN}, |
| 133 |
{0.5f, SK_ColorRED}, |
| 134 |
{0.0f, SK_ColorBLUE}}); |
| 135 |
} |
| 136 |
|
| 137 |
private: |
| 138 |
using INHERITED = skiagm::GM; |
| 139 |
}; |
| 140 |
|
| 141 |
DEF_GM(return new FillrectGradientGM;) |