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