blob: 9ad5bdcaaed92feaa42b42cc507a52aaaba864e0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com67981c52011-02-17 14:21:01 +00008#include "gm.h"
9
10namespace skiagm {
11
12class Poly2PolyGM : public GM {
13public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000014 Poly2PolyGM() {}
15
reed@google.com67981c52011-02-17 14:21:01 +000016protected:
17 virtual SkString onShortName() {
18 return SkString("poly2poly");
19 }
20
rmistry@google.comd6176b02012-08-23 18:14:13 +000021 virtual SkISize onISize() {
reed@google.com67981c52011-02-17 14:21:01 +000022 return make_isize(835, 840);
23 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000024
reed@google.com67981c52011-02-17 14:21:01 +000025 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.comd6176b02012-08-23 18:14:13 +000029
reed@google.com67981c52011-02-17 14:21:01 +000030 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.comd6176b02012-08-23 18:14:13 +000034
reed@google.com67981c52011-02-17 14:21:01 +000035 canvas->save();
36 matrix.setPolyToPoly(src, dst, count);
37 canvas->concat(matrix);
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
reed@google.com67981c52011-02-17 14:21:01 +000039 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.comd6176b02012-08-23 18:14:13 +000045
reed@google.com67981c52011-02-17 14:21:01 +000046 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.com72e49b82011-10-27 21:47:03 +000051 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
reed@google.com67981c52011-02-17 14:21:01 +000052 SkString str;
53 str.appendS32(count);
54 canvas->drawText(str.c_str(), str.size(), x, y, *paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +000055
reed@google.com67981c52011-02-17 14:21:01 +000056 canvas->restore();
57 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
59 virtual void onDraw(SkCanvas* canvas) {
reed@google.com67981c52011-02-17 14:21:01 +000060 SkPaint paint;
61 paint.setAntiAlias(true);
62 paint.setStrokeWidth(SkIntToScalar(4));
63 paint.setTextSize(SkIntToScalar(40));
64 paint.setTextAlign(SkPaint::kCenter_Align);
rmistry@google.comd6176b02012-08-23 18:14:13 +000065
reed@google.com67981c52011-02-17 14:21:01 +000066 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.comd6176b02012-08-23 18:14:13 +000073
reed@google.com67981c52011-02-17 14:21:01 +000074 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.comd6176b02012-08-23 18:14:13 +000081
reed@google.com67981c52011-02-17 14:21:01 +000082 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.comd6176b02012-08-23 18:14:13 +000089
reed@google.com67981c52011-02-17 14:21:01 +000090 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.comd6176b02012-08-23 18:14:13 +000098
reed@google.com67981c52011-02-17 14:21:01 +000099private:
100 typedef GM INHERITED;
101};
102
103//////////////////////////////////////////////////////////////////////////////
104
105static GM* MyFactory(void*) { return new Poly2PolyGM; }
106static GMRegistry reg(MyFactory);
107
108}
109