2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkGradientShader.h"
12 static SkShader* MakeLinear(SkScalar width, SkScalar height, bool alternate) {
13 SkPoint pts[2] = { {0, 0}, {width, height}};
14 SkColor colors[2] = {SK_ColorRED, SK_ColorGREEN};
17 colors[0] = SK_ColorBLUE;
18 colors[1] = SK_ColorYELLOW;
20 return SkGradientShader::CreateLinear(pts, colors, NULL, 2,
21 SkShader::kClamp_TileMode, NULL);
24 ///////////////////////////////////////////////////////////////////////////////
26 class ShaderBoundsGM : public GM {
28 typedef SkShader* (*ShaderGenFunc)(SkScalar width, SkScalar height,
30 ShaderBoundsGM(ShaderGenFunc maker, const SkString& name)
31 : fShaderMaker(maker),
36 SkString onShortName() {
40 virtual SkISize onISize() { return make_isize(320, 240); }
42 virtual SkMatrix onGetInitialTransform() const SK_OVERRIDE {
44 SkScalar scale = 0.8f;
45 result.setScale(scale, scale);
46 result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
50 virtual void onDraw(SkCanvas* canvas) {
51 // The PDF device has already clipped to the content area, but we
52 // do it again here so that the raster and pdf results are consistent.
53 canvas->clipRect(SkRect::MakeWH(SkIntToScalar(320),
57 SkScalar scale = 0.7f;
58 canvasScale.setScale(scale, scale);
59 canvas->concat(canvasScale);
63 paint.setShader(MakeShader(559, 387, false))->unref();
64 SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
65 SkIntToScalar(571), SkIntToScalar(428));
66 canvas->drawRect(r, paint);
68 // Constrained shader.
69 paint.setShader(MakeShader(101, 151, true))->unref();
70 r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
71 SkIntToScalar(101), SkIntToScalar(151));
73 canvas->drawRect(r, paint);
76 SkShader* MakeShader(int width, int height, bool background) {
77 SkScalar scale = 0.5f;
81 SkScalar shaderWidth = SkScalarDiv(SkIntToScalar(width), scale);
82 SkScalar shaderHeight = SkScalarDiv(SkIntToScalar(height), scale);
83 SkShader* shader = fShaderMaker(shaderWidth, shaderHeight, background);
85 shaderScale.setScale(scale, scale);
86 shader->setLocalMatrix(shaderScale);
93 ShaderGenFunc fShaderMaker;
96 SkShader* MakeShader(bool background);
99 ///////////////////////////////////////////////////////////////////////////////
101 static GM* MyFactory(void*) {
102 return new ShaderBoundsGM(MakeLinear, SkString("shaderbounds_linear"));
104 static GMRegistry reg(MyFactory);