1b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
2b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org/*
3b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org * Copyright 2013 Google Inc.
4b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org *
5b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
6b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org * found in the LICENSE file.
7b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org */
8b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org#include "gm.h"
9b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org#include "SkGradientShader.h"
10b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
11b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgusing namespace skiagm;
12b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
13b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgstruct GradData {
14b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    int             fCount;
15b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    const SkColor*  fColors;
16b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    const SkScalar* fPos;
17b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org};
18b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
19b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgstatic const SkColor gColors[] = {
20b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
21b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
22b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
23b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
24b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
25b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
26b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
27b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
28b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org};
29b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
30b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org//static const SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 };
31b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
32b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgstatic const GradData gGradData[] = {
332880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    { 40, gColors, nullptr },
34b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    //  { 2, gColors, gPos },
352880df2609eba09b555ca37be04b6ad89290c765Tom Hudson    //  { 2, gCol2, nullptr },
36b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org};
37b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
3883f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.orgstatic SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
3983f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.org    return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm);
40b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org}
41b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
4283f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.orgstatic SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
43b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SkPoint center;
44b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    center.set(SkScalarAve(pts[0].fX, pts[1].fX),
45b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org               SkScalarAve(pts[0].fY, pts[1].fY));
46b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    return SkGradientShader::CreateRadial(center, center.fX, data.fColors,
4783f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.org                                          data.fPos, data.fCount, tm);
48b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org}
49b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
5083f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.orgstatic SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
51b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    SkPoint center;
52b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    center.set(SkScalarAve(pts[0].fX, pts[1].fX),
53b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org               SkScalarAve(pts[0].fY, pts[1].fY));
5483f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.org    return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount);
55b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org}
56b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
57b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
5883f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.orgtypedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm);
5983f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.org
60b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgstatic const GradMaker gGradMakers[] = {
619e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com    MakeLinear, MakeRadial, MakeSweep,
62b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org};
63b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
64b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org///////////////////////////////////////////////////////////////////////////////
65b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
66b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgclass GradientsGM : public GM {
67b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgpublic:
68b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    GradientsGM() {
6965cdba6ba78aaec0c0a4596bb5941020c789482bcaryclark        this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
70b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    }
71b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
72b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgprotected:
7336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkString onShortName() override { return SkString("gradient_dirty_laundry"); }
7436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkISize onISize() override { return SkISize::Make(640, 615); }
75b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
7636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onDraw(SkCanvas* canvas) override {
77b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        SkPoint pts[2] = { { 0, 0 },
78b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org                           { SkIntToScalar(100), SkIntToScalar(100) }
79b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        };
80b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        SkShader::TileMode tm = SkShader::kClamp_TileMode;
81b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
82b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        SkPaint paint;
83b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        paint.setAntiAlias(true);
84b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
85b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
86b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
87b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org            canvas->save();
88b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org            for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
8983f23d87f1d67e6e73873e1ef7cda621c43703a0commit-bot@chromium.org                SkShader* shader = gGradMakers[j](pts, gGradData[i], tm);
90b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org                paint.setShader(shader)->unref();
91b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org                canvas->drawRect(r, paint);
92b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org                canvas->translate(0, SkIntToScalar(120));
93b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org            }
94b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org            canvas->restore();
95b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org            canvas->translate(SkIntToScalar(120), 0);
96b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org        }
97b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    }
98b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
99b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgprivate:
100b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org    typedef GM INHERITED;
101b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org};
102b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
103b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org///////////////////////////////////////////////////////////////////////////////
104b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.org
105b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgstatic GM* MyFactory(void*) { return new GradientsGM; }
106b686539ab5270c66ab4994b4d6a8740143dec73acommit-bot@chromium.orgstatic GMRegistry reg(MyFactory);
107