1 /* 2 * Copyright 2012 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 "SkCanvas.h" 10 #include "SkPaint.h" 11 #include "SkGradientShader.h" 12 13 static void intToScalars(SkScalar dst[], const int src[], int n) { 14 for (int i = 0; i < n; ++i) { 15 dst[i] = SkIntToScalar(src[i]); 16 } 17 } 18 19 static void drawGrad(SkCanvas* canvas, const SkScalar d0[], const SkScalar d1[]) { 20 const SkRect bounds = SkRect::MakeXYWH(SkIntToScalar(-50), 21 SkIntToScalar(-50), 22 SkIntToScalar(200), 23 SkIntToScalar(100)); 24 25 SkPoint c0 = { d0[0], d0[1] }; 26 SkScalar r0 = d0[2]; 27 SkPoint c1 = { d1[0], d1[1] }; 28 SkScalar r1 = d1[2]; 29 30 SkColor colors[] = { SK_ColorGREEN, SK_ColorRED }; 31 SkPaint paint; 32 paint.setAntiAlias(true); 33 34 SkString str; 35 str.printf("%g,%g,%g %g,%g,%g", 36 SkScalarToFloat(c0.fX), SkScalarToFloat(c0.fY), SkScalarToFloat(r0), 37 SkScalarToFloat(c1.fX), SkScalarToFloat(c1.fY), SkScalarToFloat(r1)); 38 canvas->drawText(str.c_str(), str.size(), 39 bounds.fLeft, bounds.fTop - paint.getTextSize()/2, paint); 40 41 paint.setShader(SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, 42 colors, NULL, 2, 43 SkShader::kClamp_TileMode))->unref(); 44 canvas->drawRect(bounds, paint); 45 46 paint.setShader(NULL); 47 paint.setColor(0x66000000); 48 paint.setStyle(SkPaint::kStroke_Style); 49 canvas->drawCircle(c0.fX, c0.fY, r0, paint); 50 canvas->drawCircle(c1.fX, c1.fY, r1, paint); 51 canvas->drawRect(bounds, paint); 52 } 53 54 class TwoPointRadialGM : public skiagm::GM { 55 public: 56 TwoPointRadialGM() {} 57 58 protected: 59 SkString onShortName() { 60 return SkString("twopointconical"); 61 } 62 63 SkISize onISize() { return skiagm::make_isize(480, 780); } 64 65 virtual void onDraw(SkCanvas* canvas) { 66 if (false) { 67 SkPaint paint; 68 paint.setColor(SK_ColorBLUE); 69 canvas->drawRect( 70 SkRect::MakeWH(SkIntToScalar(this->getISize().fWidth), 71 SkIntToScalar(this->getISize().fHeight)), 72 paint); 73 } 74 SkPaint paint; 75 const int R0 = 20; 76 const int R1 = 40; 77 78 const SkScalar DX = SkIntToScalar(250); 79 const SkScalar DY = SkIntToScalar(130); 80 81 canvas->translate(SkIntToScalar(60), SkIntToScalar(70)); 82 83 static const int gData[] = { 84 0, 0, R0, 0, 0, R1, 85 0, 0, R0, 20, 0, R1, 86 0, 0, R0, 25, 0, R1, 87 0, 0, R0, 100, 0, R1, 88 0, 0, R0, 25, 0, R0, 89 0, 0, R0, 100, 0, R0, 90 }; 91 92 int count = SK_ARRAY_COUNT(gData) / 6; 93 for (int i = 0; i < count; ++i) { 94 SkScalar data[6]; 95 intToScalars(data, &gData[i * 6], 6); 96 97 int n = canvas->save(); 98 drawGrad(canvas, &data[0], &data[3]); 99 canvas->translate(DX, 0); 100 drawGrad(canvas, &data[3], &data[0]); 101 canvas->restoreToCount(n); 102 canvas->translate(0, DY); 103 } 104 } 105 }; 106 107 ////////////////////////////////////////////////////////////////////////////// 108 109 static skiagm::GM* F(void*) { return new TwoPointRadialGM; } 110 111 static skiagm::GMRegistry gR(F); 112