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 */ 8 #include "gm.h" 9 10 namespace skiagm { 11 12 class Poly2PolyGM : public GM { 13 public: 14 Poly2PolyGM() {} 15 16 protected: 17 virtual SkString onShortName() { 18 return SkString("poly2poly"); 19 } 20 21 virtual SkISize onISize() { 22 return make_isize(835, 840); 23 } 24 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]; 29 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 } 34 35 canvas->save(); 36 matrix.setPolyToPoly(src, dst, count); 37 canvas->concat(matrix); 38 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); 45 46 SkPaint::FontMetrics fm; 47 paint->getFontMetrics(&fm); 48 paint->setColor(SK_ColorRED); 49 paint->setStyle(SkPaint::kFill_Style); 50 SkScalar x = D/2; 51 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2; 52 SkString str; 53 str.appendS32(count); 54 canvas->drawText(str.c_str(), str.size(), x, y, *paint); 55 56 canvas->restore(); 57 } 58 59 virtual void onDraw(SkCanvas* canvas) { 60 SkPaint paint; 61 paint.setAntiAlias(true); 62 paint.setStrokeWidth(SkIntToScalar(4)); 63 paint.setTextSize(SkIntToScalar(40)); 64 paint.setTextAlign(SkPaint::kCenter_Align); 65 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(); 73 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(); 81 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(); 89 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 } 98 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