reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 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[]) { |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 20 | const SkRect bounds = SkRect::MakeXYWH(SkIntToScalar(-50), |
| 21 | SkIntToScalar(-50), |
| 22 | SkIntToScalar(200), |
| 23 | SkIntToScalar(100)); |
| 24 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 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); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 41 | paint.setShader(SkGradientShader::CreateTwoPointConical(c0, r0, c1, r1, |
| 42 | colors, NULL, 2, |
| 43 | SkShader::kClamp_TileMode))->unref(); |
| 44 | canvas->drawRect(bounds, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 45 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 46 | paint.setShader(NULL); |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 47 | paint.setColor(0x66000000); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 48 | paint.setStyle(SkPaint::kStroke_Style); |
| 49 | canvas->drawCircle(c0.fX, c0.fY, r0, paint); |
| 50 | canvas->drawCircle(c1.fX, c1.fY, r1, paint); |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 51 | canvas->drawRect(bounds, paint); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | class TwoPointRadialGM : public skiagm::GM { |
| 55 | public: |
| 56 | TwoPointRadialGM() {} |
| 57 | |
| 58 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 59 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 60 | return kSkipTiled_Flag; |
| 61 | } |
| 62 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 63 | SkString onShortName() { |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 64 | return SkString("twopointconical"); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 65 | } |
| 66 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 67 | SkISize onISize() { return SkISize::Make(480, 780); } |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 68 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 69 | virtual void onDraw(SkCanvas* canvas) { |
| 70 | if (false) { |
| 71 | SkPaint paint; |
| 72 | paint.setColor(SK_ColorBLUE); |
robertphillips@google.com | 4bdfb8c | 2012-06-12 21:23:49 +0000 | [diff] [blame] | 73 | canvas->drawRect( |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | SkRect::MakeWH(SkIntToScalar(this->getISize().fWidth), |
| 75 | SkIntToScalar(this->getISize().fHeight)), |
robertphillips@google.com | 4bdfb8c | 2012-06-12 21:23:49 +0000 | [diff] [blame] | 76 | paint); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 77 | } |
| 78 | SkPaint paint; |
| 79 | const int R0 = 20; |
| 80 | const int R1 = 40; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 82 | const SkScalar DX = SkIntToScalar(250); |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 83 | const SkScalar DY = SkIntToScalar(130); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 84 | |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 85 | canvas->translate(SkIntToScalar(60), SkIntToScalar(70)); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 86 | |
| 87 | static const int gData[] = { |
| 88 | 0, 0, R0, 0, 0, R1, |
reed@google.com | cb7be69 | 2012-06-06 20:31:56 +0000 | [diff] [blame] | 89 | 0, 0, R0, 20, 0, R1, |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 90 | 0, 0, R0, 25, 0, R1, |
| 91 | 0, 0, R0, 100, 0, R1, |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 92 | 0, 0, R0, 25, 0, R0, |
| 93 | 0, 0, R0, 100, 0, R0, |
| 94 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 95 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 96 | int count = SK_ARRAY_COUNT(gData) / 6; |
| 97 | for (int i = 0; i < count; ++i) { |
| 98 | SkScalar data[6]; |
| 99 | intToScalars(data, &gData[i * 6], 6); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 100 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 101 | int n = canvas->save(); |
| 102 | drawGrad(canvas, &data[0], &data[3]); |
| 103 | canvas->translate(DX, 0); |
| 104 | drawGrad(canvas, &data[3], &data[0]); |
| 105 | canvas->restoreToCount(n); |
| 106 | canvas->translate(0, DY); |
| 107 | } |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | ////////////////////////////////////////////////////////////////////////////// |
| 112 | |
| 113 | static skiagm::GM* F(void*) { return new TwoPointRadialGM; } |
| 114 | |
| 115 | static skiagm::GMRegistry gR(F); |