183f7c659461d602e498569dab63f04b1b578b742reed@google.com/*
283f7c659461d602e498569dab63f04b1b578b742reed@google.com * Copyright 2013 Google Inc.
383f7c659461d602e498569dab63f04b1b578b742reed@google.com *
483f7c659461d602e498569dab63f04b1b578b742reed@google.com * Use of this source code is governed by a BSD-style license that can be
583f7c659461d602e498569dab63f04b1b578b742reed@google.com * found in the LICENSE file.
683f7c659461d602e498569dab63f04b1b578b742reed@google.com */
783f7c659461d602e498569dab63f04b1b578b742reed@google.com
883f7c659461d602e498569dab63f04b1b578b742reed@google.com#include "gm.h"
983f7c659461d602e498569dab63f04b1b578b742reed@google.com#include "SkGradientShader.h"
1083f7c659461d602e498569dab63f04b1b578b742reed@google.com
1183f7c659461d602e498569dab63f04b1b578b742reed@google.comtypedef SkShader* (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
1283f7c659461d602e498569dab63f04b1b578b742reed@google.com
1383f7c659461d602e498569dab63f04b1b578b742reed@google.comstatic SkShader* shader_linear(const SkColor colors[], int count, const SkSize& size) {
1483f7c659461d602e498569dab63f04b1b578b742reed@google.com    SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
1583f7c659461d602e498569dab63f04b1b578b742reed@google.com    return SkGradientShader::CreateLinear(pts, colors, NULL, count,
1683f7c659461d602e498569dab63f04b1b578b742reed@google.com                                          SkShader::kClamp_TileMode);
1783f7c659461d602e498569dab63f04b1b578b742reed@google.com}
1883f7c659461d602e498569dab63f04b1b578b742reed@google.com
1983f7c659461d602e498569dab63f04b1b578b742reed@google.comstatic SkShader* shader_radial(const SkColor colors[], int count, const SkSize& size) {
2083f7c659461d602e498569dab63f04b1b578b742reed@google.com    SkPoint center = { size.width()/2, size.height()/2 };
2183f7c659461d602e498569dab63f04b1b578b742reed@google.com    return SkGradientShader::CreateRadial(center, size.width()/2, colors, NULL, count,
2283f7c659461d602e498569dab63f04b1b578b742reed@google.com                                          SkShader::kClamp_TileMode);
2383f7c659461d602e498569dab63f04b1b578b742reed@google.com}
2483f7c659461d602e498569dab63f04b1b578b742reed@google.com
2583f7c659461d602e498569dab63f04b1b578b742reed@google.comstatic SkShader* shader_conical(const SkColor colors[], int count, const SkSize& size) {
2683f7c659461d602e498569dab63f04b1b578b742reed@google.com    SkPoint center = { size.width()/2, size.height()/2 };
2783f7c659461d602e498569dab63f04b1b578b742reed@google.com    return SkGradientShader::CreateTwoPointConical(center, size.width()/64,
2883f7c659461d602e498569dab63f04b1b578b742reed@google.com                                                   center, size.width()/2,
2983f7c659461d602e498569dab63f04b1b578b742reed@google.com                                                   colors, NULL, count,
3083f7c659461d602e498569dab63f04b1b578b742reed@google.com                                                   SkShader::kClamp_TileMode);
3183f7c659461d602e498569dab63f04b1b578b742reed@google.com}
3283f7c659461d602e498569dab63f04b1b578b742reed@google.com
3383f7c659461d602e498569dab63f04b1b578b742reed@google.comstatic SkShader* shader_sweep(const SkColor colors[], int count, const SkSize& size) {
3483f7c659461d602e498569dab63f04b1b578b742reed@google.com    return SkGradientShader::CreateSweep(size.width()/2, size.height()/2,
3583f7c659461d602e498569dab63f04b1b578b742reed@google.com                                         colors, NULL, count);
3683f7c659461d602e498569dab63f04b1b578b742reed@google.com}
3783f7c659461d602e498569dab63f04b1b578b742reed@google.com
3883f7c659461d602e498569dab63f04b1b578b742reed@google.comclass ShallowGradientGM : public skiagm::GM {
3983f7c659461d602e498569dab63f04b1b578b742reed@google.compublic:
4083f7c659461d602e498569dab63f04b1b578b742reed@google.com    ShallowGradientGM(MakeShaderProc proc, const char name[]) : fProc(proc) {
4183f7c659461d602e498569dab63f04b1b578b742reed@google.com        fName.printf("shallow_gradient_%s", name);
4283f7c659461d602e498569dab63f04b1b578b742reed@google.com    }
4383f7c659461d602e498569dab63f04b1b578b742reed@google.com
4483f7c659461d602e498569dab63f04b1b578b742reed@google.comprotected:
45a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org
4636352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkString onShortName() override {
4783f7c659461d602e498569dab63f04b1b578b742reed@google.com        return fName;
4883f7c659461d602e498569dab63f04b1b578b742reed@google.com    }
4983f7c659461d602e498569dab63f04b1b578b742reed@google.com
5036352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    SkISize onISize() override {
5183f7c659461d602e498569dab63f04b1b578b742reed@google.com        return SkISize::Make(800, 800);
5283f7c659461d602e498569dab63f04b1b578b742reed@google.com    }
5383f7c659461d602e498569dab63f04b1b578b742reed@google.com
5436352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onDraw(SkCanvas* canvas) override {
5583f7c659461d602e498569dab63f04b1b578b742reed@google.com        const SkColor colors[] = { 0xFF555555, 0xFF444444 };
5683f7c659461d602e498569dab63f04b1b578b742reed@google.com        const int colorCount = SK_ARRAY_COUNT(colors);
5783f7c659461d602e498569dab63f04b1b578b742reed@google.com
5883f7c659461d602e498569dab63f04b1b578b742reed@google.com        SkRect r = { 0, 0, this->width(), this->height() };
5983f7c659461d602e498569dab63f04b1b578b742reed@google.com        SkSize size = SkSize::Make(r.width(), r.height());
6083f7c659461d602e498569dab63f04b1b578b742reed@google.com
6183f7c659461d602e498569dab63f04b1b578b742reed@google.com        SkPaint paint;
62b62ce7074508c56f839d8fa1dd07161cfe9da327robertphillips@google.com        paint.setShader(fProc(colors, colorCount, size))->unref();
6383f7c659461d602e498569dab63f04b1b578b742reed@google.com        canvas->drawRect(r, paint);
6483f7c659461d602e498569dab63f04b1b578b742reed@google.com    }
6583f7c659461d602e498569dab63f04b1b578b742reed@google.com
6683f7c659461d602e498569dab63f04b1b578b742reed@google.comprivate:
6783f7c659461d602e498569dab63f04b1b578b742reed@google.com    MakeShaderProc fProc;
6883f7c659461d602e498569dab63f04b1b578b742reed@google.com    SkString fName;
6983f7c659461d602e498569dab63f04b1b578b742reed@google.com
7083f7c659461d602e498569dab63f04b1b578b742reed@google.com    typedef skiagm::GM INHERITED;
7183f7c659461d602e498569dab63f04b1b578b742reed@google.com};
7283f7c659461d602e498569dab63f04b1b578b742reed@google.com
7383f7c659461d602e498569dab63f04b1b578b742reed@google.com///////////////////////////////////////////////////////////////////////////////
7483f7c659461d602e498569dab63f04b1b578b742reed@google.com
7583f7c659461d602e498569dab63f04b1b578b742reed@google.comDEF_GM( return new ShallowGradientGM(shader_linear, "linear"); )
7683f7c659461d602e498569dab63f04b1b578b742reed@google.comDEF_GM( return new ShallowGradientGM(shader_radial, "radial"); )
7783f7c659461d602e498569dab63f04b1b578b742reed@google.comDEF_GM( return new ShallowGradientGM(shader_conical, "conical"); )
7883f7c659461d602e498569dab63f04b1b578b742reed@google.comDEF_GM( return new ShallowGradientGM(shader_sweep, "sweep"); )
79