epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | |
| 10 | namespace skiagm { |
| 11 | |
| 12 | class Poly2PolyGM : public GM { |
| 13 | public: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 14 | Poly2PolyGM() {} |
| 15 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 16 | protected: |
| 17 | virtual SkString onShortName() { |
| 18 | return SkString("poly2poly"); |
| 19 | } |
| 20 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | virtual SkISize onISize() { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 22 | return make_isize(835, 840); |
| 23 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 25 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], |
| 26 | const int idst[], int count) { |
| 27 | SkMatrix matrix; |
| 28 | SkPoint src[4], dst[4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 29 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 30 | for (int i = 0; i < count; i++) { |
| 31 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); |
| 32 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); |
| 33 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 34 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 35 | canvas->save(); |
| 36 | matrix.setPolyToPoly(src, dst, count); |
| 37 | canvas->concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 38 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 39 | paint->setColor(SK_ColorGRAY); |
| 40 | paint->setStyle(SkPaint::kStroke_Style); |
| 41 | const SkScalar D = SkIntToScalar(64); |
| 42 | canvas->drawRectCoords(0, 0, D, D, *paint); |
| 43 | canvas->drawLine(0, 0, D, D, *paint); |
| 44 | canvas->drawLine(0, D, D, 0, *paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 45 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 46 | SkPaint::FontMetrics fm; |
| 47 | paint->getFontMetrics(&fm); |
| 48 | paint->setColor(SK_ColorRED); |
| 49 | paint->setStyle(SkPaint::kFill_Style); |
| 50 | SkScalar x = D/2; |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 51 | SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 52 | SkString str; |
| 53 | str.appendS32(count); |
| 54 | canvas->drawText(str.c_str(), str.size(), x, y, *paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 56 | canvas->restore(); |
| 57 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 58 | |
| 59 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 60 | SkPaint paint; |
| 61 | paint.setAntiAlias(true); |
| 62 | paint.setStrokeWidth(SkIntToScalar(4)); |
| 63 | paint.setTextSize(SkIntToScalar(40)); |
| 64 | paint.setTextAlign(SkPaint::kCenter_Align); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 65 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 66 | canvas->save(); |
| 67 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 68 | // translate (1 point) |
| 69 | const int src1[] = { 0, 0 }; |
| 70 | const int dst1[] = { 5, 5 }; |
| 71 | doDraw(canvas, &paint, src1, dst1, 1); |
| 72 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 73 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 74 | canvas->save(); |
| 75 | canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); |
| 76 | // rotate/uniform-scale (2 points) |
| 77 | const int src2[] = { 32, 32, 64, 32 }; |
| 78 | const int dst2[] = { 32, 32, 64, 48 }; |
| 79 | doDraw(canvas, &paint, src2, dst2, 2); |
| 80 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 81 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 82 | canvas->save(); |
| 83 | canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); |
| 84 | // rotate/skew (3 points) |
| 85 | const int src3[] = { 0, 0, 64, 0, 0, 64 }; |
| 86 | const int dst3[] = { 0, 0, 96, 0, 24, 64 }; |
| 87 | doDraw(canvas, &paint, src3, dst3, 3); |
| 88 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 89 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 90 | canvas->save(); |
| 91 | canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); |
| 92 | // perspective (4 points) |
| 93 | const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; |
| 94 | const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; |
| 95 | doDraw(canvas, &paint, src4, dst4, 4); |
| 96 | canvas->restore(); |
| 97 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 99 | private: |
| 100 | typedef GM INHERITED; |
| 101 | }; |
| 102 | |
| 103 | ////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | static GM* MyFactory(void*) { return new Poly2PolyGM; } |
| 106 | static GMRegistry reg(MyFactory); |
| 107 | |
| 108 | } |
| 109 | |