shallowgradient.cpp revision fe17456d5e528078ce69b5f15cf7adf1fab963f9
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.h"
9#include "sk_tool_utils.h"
10#include "SkGradientShader.h"
11
12typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
13
14static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
15    SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
16    return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkShader::kClamp_TileMode);
17}
18
19static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
20    SkPoint center = { size.width()/2, size.height()/2 };
21    return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
22                                        SkShader::kClamp_TileMode);
23}
24
25static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
26    SkPoint center = { size.width()/2, size.height()/2 };
27    return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
28                                                colors, nullptr, count, SkShader::kClamp_TileMode);
29}
30
31static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
32    return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
33}
34
35class ShallowGradientGM : public skiagm::GM {
36public:
37    ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
38        : fProc(proc)
39        , fDither(dither) {
40        fName.printf("shallow_gradient_%s", name);
41    }
42
43protected:
44
45    SkString onShortName() override {
46        return fName;
47    }
48
49    SkISize onISize() override {
50        return SkISize::Make(800, 800);
51    }
52
53    void onDraw(SkCanvas* canvas) override {
54        const SkColor colors[] = { sk_tool_utils::color_to_565(0xFF555555),
55                sk_tool_utils::color_to_565(0xFF444444) };
56        const int colorCount = SK_ARRAY_COUNT(colors);
57
58        SkRect r = { 0, 0, this->width(), this->height() };
59        SkSize size = SkSize::Make(r.width(), r.height());
60
61        SkPaint paint;
62        paint.setShader(fProc(colors, colorCount, size));
63        paint.setDither(fDither);
64        canvas->drawRect(r, paint);
65    }
66
67private:
68    MakeShaderProc fProc;
69    SkString fName;
70    bool fDither;
71
72    typedef skiagm::GM INHERITED;
73};
74
75///////////////////////////////////////////////////////////////////////////////
76
77DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
78DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
79DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
80DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
81
82DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
83DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
84DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
85DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )
86