blob: 2cf3f5e01dc0303817a304e6dd0f81dc4a4067c7 [file] [log] [blame]
reed@android.com0bfffc52009-06-26 20:22:44 +00001#include "gm.h"
2#include "SkPicture.h"
3#include "SkRectShape.h"
4#include "SkGroupShape.h"
5
6namespace skiagm {
7
8static SkRect make_rect(int l, int t, int r, int b) {
9 SkRect rect;
10 rect.set(SkIntToScalar(l), SkIntToScalar(t),
11 SkIntToScalar(r), SkIntToScalar(b));
12 return rect;
13}
14
15static SkShape* make_shape0(bool red) {
16 SkRectShape* s = new SkRectShape;
17 s->setRect(make_rect(10, 10, 90, 90));
18 if (red) {
19 s->paint().setColor(SK_ColorRED);
20 }
21 return s;
22}
23
24static SkShape* make_shape1() {
25 SkRectShape* s = new SkRectShape;
26 s->setOval(make_rect(10, 10, 90, 90));
27 s->paint().setColor(SK_ColorBLUE);
28 return s;
29}
30
31static SkShape* make_shape2() {
32 SkRectShape* s = new SkRectShape;
33 s->setRRect(make_rect(10, 10, 90, 90),
34 SkIntToScalar(20), SkIntToScalar(20));
35 s->paint().setColor(SK_ColorGREEN);
36 return s;
37}
38
39///////////////////////////////////////////////////////////////////////////////
40
41class ShapesGM : public GM {
42 SkGroupShape fGroup;
43 SkMatrixRef* fMatrixRefs[4];
44public:
45 ShapesGM() {
46 SkMatrix m;
47 fGroup.appendShape(make_shape0(false))->unref();
48 m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
49 m.postTranslate(0, SkIntToScalar(120));
50 fGroup.appendShape(make_shape0(true), m)->unref();
51
52 m.setTranslate(SkIntToScalar(120), 0);
53 fGroup.appendShape(make_shape1(), m)->unref();
54 m.postTranslate(0, SkIntToScalar(120));
55 fGroup.appendShape(make_shape2(), m)->unref();
56
57 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
58 SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
59 }
60
61 fAngle = 0;
62 }
63
64 virtual ~ShapesGM() {
65 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
66 SkSafeUnref(fMatrixRefs[i]);
67 }
68 }
69
70protected:
71 virtual SkString onShortName() {
72 return SkString("shapes");
73 }
74
75 virtual SkISize onISize() {
76 return make_isize(380, 480);
77 }
78
79 void drawBG(SkCanvas* canvas) {
80 canvas->drawColor(0xFFDDDDDD);
81 }
82
83 int fAngle;
84
85 virtual void onDraw(SkCanvas* canvas) {
86 this->drawBG(canvas);
87
88 SkMatrix saveM = *fMatrixRefs[3];
89 fAngle = (fAngle + 5) % 360;
90 SkScalar c = SkIntToScalar(50);
91 fMatrixRefs[3]->preRotate(SkIntToScalar(fAngle), c, c);
92
93 SkMatrix matrix;
94
95 SkGroupShape* gs = new SkGroupShape;
96 SkAutoUnref aur(gs);
97 gs->appendShape(&fGroup);
98 matrix.setScale(-SK_Scalar1, SK_Scalar1);
99 matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
100 gs->appendShape(&fGroup, matrix);
101 matrix.setTranslate(SkIntToScalar(240), 0);
102 matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
103 gs->appendShape(&fGroup, matrix);
104
105#if 0
106 canvas->drawShape(gs);
107#else
108 SkPicture pict;
109 SkCanvas* cv = pict.beginRecording(1000, 1000);
110 cv->scale(SK_ScalarHalf, SK_ScalarHalf);
111 cv->drawShape(gs);
112 cv->translate(SkIntToScalar(680), SkIntToScalar(480));
113 cv->scale(-SK_Scalar1, SK_Scalar1);
114 cv->drawShape(gs);
115 pict.endRecording();
116 canvas->drawPicture(pict);
117#endif
118
119 *fMatrixRefs[3] = saveM;
120}
121
122private:
123 typedef GM INHERITED;
124};
125
126///////////////////////////////////////////////////////////////////////////////
127
128static GM* MyFactory(void*) { return new ShapesGM; }
129static GMRegistry reg(MyFactory);
130
131}