blob: 324ce7e8712117aea0f089d556ae98e725b164f4 [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();
reed@google.com0e4a8852011-01-12 17:14:04 +000056
reed@android.com0bfffc52009-06-26 20:22:44 +000057 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
58 SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
59 }
reed@android.com0bfffc52009-06-26 20:22:44 +000060 }
reed@google.com0e4a8852011-01-12 17:14:04 +000061
reed@android.com0bfffc52009-06-26 20:22:44 +000062 virtual ~ShapesGM() {
63 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
64 SkSafeUnref(fMatrixRefs[i]);
65 }
66 }
reed@google.com0e4a8852011-01-12 17:14:04 +000067
reed@android.com0bfffc52009-06-26 20:22:44 +000068protected:
69 virtual SkString onShortName() {
70 return SkString("shapes");
71 }
reed@google.com0e4a8852011-01-12 17:14:04 +000072
reed@android.com0bfffc52009-06-26 20:22:44 +000073 virtual SkISize onISize() {
74 return make_isize(380, 480);
75 }
reed@google.com0e4a8852011-01-12 17:14:04 +000076
reed@android.com0bfffc52009-06-26 20:22:44 +000077 void drawBG(SkCanvas* canvas) {
78 canvas->drawColor(0xFFDDDDDD);
79 }
reed@google.com0e4a8852011-01-12 17:14:04 +000080
reed@android.com0bfffc52009-06-26 20:22:44 +000081 virtual void onDraw(SkCanvas* canvas) {
82 this->drawBG(canvas);
reed@google.com0e4a8852011-01-12 17:14:04 +000083
reed@android.com0bfffc52009-06-26 20:22:44 +000084 SkMatrix saveM = *fMatrixRefs[3];
reed@android.com0bfffc52009-06-26 20:22:44 +000085 SkScalar c = SkIntToScalar(50);
reed@android.com0ad336f2009-06-29 16:02:20 +000086 fMatrixRefs[3]->preRotate(SkIntToScalar(30), c, c);
reed@google.com0e4a8852011-01-12 17:14:04 +000087
reed@android.com0bfffc52009-06-26 20:22:44 +000088 SkMatrix matrix;
reed@google.com0e4a8852011-01-12 17:14:04 +000089
reed@android.com0bfffc52009-06-26 20:22:44 +000090 SkGroupShape* gs = new SkGroupShape;
91 SkAutoUnref aur(gs);
92 gs->appendShape(&fGroup);
93 matrix.setScale(-SK_Scalar1, SK_Scalar1);
94 matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
95 gs->appendShape(&fGroup, matrix);
96 matrix.setTranslate(SkIntToScalar(240), 0);
97 matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
98 gs->appendShape(&fGroup, matrix);
reed@google.com0e4a8852011-01-12 17:14:04 +000099
100#if 0
reed@android.com0bfffc52009-06-26 20:22:44 +0000101 canvas->drawShape(gs);
102#else
reed@google.com0e4a8852011-01-12 17:14:04 +0000103 SkPicture* pict = new SkPicture;
104 SkCanvas* cv = pict->beginRecording(1000, 1000);
reed@android.com0bfffc52009-06-26 20:22:44 +0000105 cv->scale(SK_ScalarHalf, SK_ScalarHalf);
106 cv->drawShape(gs);
107 cv->translate(SkIntToScalar(680), SkIntToScalar(480));
108 cv->scale(-SK_Scalar1, SK_Scalar1);
109 cv->drawShape(gs);
reed@google.com0e4a8852011-01-12 17:14:04 +0000110 pict->endRecording();
111 canvas->drawPicture(*pict);
112 pict->unref();
reed@android.com0bfffc52009-06-26 20:22:44 +0000113#endif
114
115 *fMatrixRefs[3] = saveM;
116}
reed@google.com0e4a8852011-01-12 17:14:04 +0000117
reed@android.com0bfffc52009-06-26 20:22:44 +0000118private:
119 typedef GM INHERITED;
120};
121
122///////////////////////////////////////////////////////////////////////////////
123
124static GM* MyFactory(void*) { return new ShapesGM; }
125static GMRegistry reg(MyFactory);
126
127}