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