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