| 1 |
/* |
| 2 |
* Copyright 2013 Google Inc. |
| 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/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkPaint.h" |
| 12 |
#include "include/core/SkPoint.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkShader.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTileMode.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/effects/SkGradient.h" |
| 22 |
#include "include/private/base/SkFloatingPoint.h" |
| 23 |
#include "src/core/SkColorPriv.h" |
| 24 |
|
| 25 |
using namespace skiagm; |
| 26 |
|
| 27 |
struct GradData { |
| 28 |
size_t fCount; |
| 29 |
const SkColor4f* fColors; |
| 30 |
const float* fPos; |
| 31 |
|
| 32 |
SkSpan<const SkColor4f> colors() const { return {fColors, fCount}; } |
| 33 |
SkSpan<const float> pos() const { |
| 34 |
if (fPos) { |
| 35 |
return {fPos, fCount}; |
| 36 |
} |
| 37 |
return {}; |
| 38 |
} |
| 39 |
|
| 40 |
SkGradient grad(SkTileMode tm) const { return {{this->colors(), this->pos(), tm}, {}}; } |
| 41 |
}; |
| 42 |
|
| 43 |
constexpr SkColor4f gColors[] = { |
| 44 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 45 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 46 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 47 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 48 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 49 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 50 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 51 |
SkColors::kRed, SkColors::kGreen, SkColors::kBlue, SkColors::kWhite, SkColors::kBlack, |
| 52 |
}; |
| 53 |
|
| 54 |
//constexpr SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 }; |
| 55 |
|
| 56 |
constexpr GradData gGradData[] = { |
| 57 |
{ 40, gColors, nullptr }, |
| 58 |
// { 2, gColors, gPos }, |
| 59 |
// { 2, gCol2, nullptr }, |
| 60 |
}; |
| 61 |
|
| 62 |
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
| 63 |
return SkShaders::LinearGradient(pts, data.grad(tm)); |
| 64 |
} |
| 65 |
|
| 66 |
static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
| 67 |
const SkPoint pt{ |
| 68 |
sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 69 |
sk_float_midpoint(pts[0].fY, pts[1].fY), |
| 70 |
}; |
| 71 |
return SkShaders::RadialGradient(pt, pt.fX, data.grad(tm)); |
| 72 |
} |
| 73 |
|
| 74 |
static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkTileMode tm) { |
| 75 |
const SkPoint pt{ |
| 76 |
sk_float_midpoint(pts[0].fX, pts[1].fX), |
| 77 |
sk_float_midpoint(pts[0].fY, pts[1].fY), |
| 78 |
}; |
| 79 |
return SkShaders::SweepGradient(pt, data.grad(tm)); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkTileMode); |
| 84 |
|
| 85 |
constexpr GradMaker gGradMakers[] = { |
| 86 |
MakeLinear, MakeRadial, MakeSweep, |
| 87 |
}; |
| 88 |
|
| 89 |
/////////////////////////////////////////////////////////////////////////////// |
| 90 |
|
| 91 |
class GradientsGM : public GM { |
| 92 |
public: |
| 93 |
GradientsGM() { |
| 94 |
this->setBGColor(0xFFDDDDDD); |
| 95 |
} |
| 96 |
|
| 97 |
protected: |
| 98 |
SkString getName() const override { return SkString("gradient_dirty_laundry"); } |
| 99 |
SkISize getISize() override { return SkISize::Make(640, 615); } |
| 100 |
|
| 101 |
void onDraw(SkCanvas* canvas) override { |
| 102 |
SkPoint pts[2] = { { 0, 0 }, |
| 103 |
{ SkIntToScalar(100), SkIntToScalar(100) } |
| 104 |
}; |
| 105 |
SkTileMode tm = SkTileMode::kClamp; |
| 106 |
SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 107 |
SkPaint paint; |
| 108 |
paint.setAntiAlias(true); |
| 109 |
|
| 110 |
canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 111 |
for (size_t i = 0; i < std::size(gGradData); i++) { |
| 112 |
canvas->save(); |
| 113 |
for (size_t j = 0; j < std::size(gGradMakers); j++) { |
| 114 |
paint.setShader(gGradMakers[j](pts, gGradData[i], tm)); |
| 115 |
canvas->drawRect(r, paint); |
| 116 |
canvas->translate(0, SkIntToScalar(120)); |
| 117 |
} |
| 118 |
canvas->restore(); |
| 119 |
canvas->translate(SkIntToScalar(120), 0); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
private: |
| 124 |
using INHERITED = GM; |
| 125 |
}; |
| 126 |
|
| 127 |
/////////////////////////////////////////////////////////////////////////////// |
| 128 |
|
| 129 |
DEF_GM( return new GradientsGM; ) |