| 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/SkShader.h" |
| 16 |
#include "include/core/SkSize.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkTileMode.h" |
| 19 |
#include "include/core/SkTypes.h" |
| 20 |
#include "include/effects/SkGradient.h" |
| 21 |
|
| 22 |
typedef sk_sp<SkShader> (*MakeShaderProc)(const SkGradient& grad, const SkSize&); |
| 23 |
|
| 24 |
static sk_sp<SkShader> shader_linear(const SkGradient& grad, const SkSize& size) { |
| 25 |
SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } }; |
| 26 |
return SkShaders::LinearGradient(pts, grad); |
| 27 |
} |
| 28 |
|
| 29 |
static sk_sp<SkShader> shader_radial(const SkGradient& grad, const SkSize& size) { |
| 30 |
SkPoint center = { size.width()/2, size.height()/2 }; |
| 31 |
return SkShaders::RadialGradient(center, size.width()/2, grad); |
| 32 |
} |
| 33 |
|
| 34 |
static sk_sp<SkShader> shader_conical(const SkGradient& grad, const SkSize& size) { |
| 35 |
SkPoint center = { size.width()/2, size.height()/2 }; |
| 36 |
return SkShaders::TwoPointConicalGradient(center, size.width()/64, center, size.width()/2, |
| 37 |
grad); |
| 38 |
} |
| 39 |
|
| 40 |
static sk_sp<SkShader> shader_sweep(const SkGradient& grad, const SkSize& size) { |
| 41 |
return SkShaders::SweepGradient({size.width()/2, size.height()/2}, grad); |
| 42 |
} |
| 43 |
|
| 44 |
class ShallowGradientGM : public skiagm::GM { |
| 45 |
public: |
| 46 |
ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither) |
| 47 |
: fProc(proc), fName(name), fDither(dither) {} |
| 48 |
|
| 49 |
private: |
| 50 |
MakeShaderProc fProc; |
| 51 |
const char* fName; |
| 52 |
bool fDither; |
| 53 |
|
| 54 |
SkString getName() const override { |
| 55 |
return SkStringPrintf("shallow_gradient_%s%s", fName, fDither ? "" : "_nodither"); |
| 56 |
} |
| 57 |
|
| 58 |
SkISize getISize() override { return {800, 800}; } |
| 59 |
|
| 60 |
void onDraw(SkCanvas* canvas) override { |
| 61 |
const SkColor4f colors[] = { |
| 62 |
SkColor4f::FromColor(0xFF555555), SkColor4f::FromColor(0xFF444444) |
| 63 |
}; |
| 64 |
const SkGradient grad = {{colors, {}, SkTileMode::kClamp}, {}}; |
| 65 |
|
| 66 |
SkRect r = { 0, 0, this->width(), this->height() }; |
| 67 |
SkSize size = SkSize::Make(r.width(), r.height()); |
| 68 |
|
| 69 |
SkPaint paint; |
| 70 |
paint.setShader(fProc(grad, size)); |
| 71 |
paint.setDither(fDither); |
| 72 |
canvas->drawRect(r, paint); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
/////////////////////////////////////////////////////////////////////////////// |
| 77 |
|
| 78 |
#define M(PROC, DITHER) DEF_GM( return new ShallowGradientGM(shader_ ## PROC, #PROC, DITHER); ) |
| 79 |
M(linear, true) |
| 80 |
M(radial, true) |
| 81 |
M(conical, true) |
| 82 |
M(sweep, true) |
| 83 |
|
| 84 |
M(linear, false) |
| 85 |
M(radial, false) |
| 86 |
M(conical, false) |
| 87 |
M(sweep, false) |
| 88 |
#undef M |