• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SampleCode.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkPicture.h"
12 #include "SkStream.h"
13 #include "SkView.h"
14 
15 #define DO_AA   true
16 
17 #include "SkRectShape.h"
18 #include "SkGroupShape.h"
19 
make_rect(int l,int t,int r,int b)20 static SkRect make_rect(int l, int t, int r, int b) {
21     SkRect rect;
22     rect.set(SkIntToScalar(l), SkIntToScalar(t),
23              SkIntToScalar(r), SkIntToScalar(b));
24     return rect;
25 }
26 
make_shape0(bool red)27 static SkShape* make_shape0(bool red) {
28     SkRectShape* s = new SkRectShape;
29     s->setRect(make_rect(10, 10, 90, 90));
30     if (red) {
31         s->paint().setColor(SK_ColorRED);
32     }
33     s->paint().setAntiAlias(DO_AA);
34     return s;
35 }
36 
make_shape1()37 static SkShape* make_shape1() {
38     SkRectShape* s = new SkRectShape;
39     s->setOval(make_rect(10, 10, 90, 90));
40     s->paint().setColor(SK_ColorBLUE);
41     s->paint().setAntiAlias(DO_AA);
42     return s;
43 }
44 
make_shape2()45 static SkShape* make_shape2() {
46     SkRectShape* s = new SkRectShape;
47     s->setRRect(make_rect(10, 10, 90, 90),
48                 SkIntToScalar(20), SkIntToScalar(20));
49     s->paint().setColor(SK_ColorGREEN);
50     s->paint().setAntiAlias(DO_AA);
51     return s;
52 }
53 
54 ///////////////////////////////////////////////////////////////////////////////
55 
56 class ShapesView : public SampleView {
57     SkGroupShape fGroup;
58     SkMatrixRef*    fMatrixRefs[4];
59 public:
ShapesView()60 	ShapesView() {
61         SkMatrix m;
62         fGroup.appendShape(make_shape0(false))->unref();
63         m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
64         m.postTranslate(0, SkIntToScalar(120));
65         fGroup.appendShape(make_shape0(true), m)->unref();
66 
67         m.setTranslate(SkIntToScalar(120), 0);
68         fGroup.appendShape(make_shape1(), m)->unref();
69         m.postTranslate(0, SkIntToScalar(120));
70         fGroup.appendShape(make_shape2(), m)->unref();
71 
72         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
73             SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
74         }
75 
76         this->setBGColor(0xFFDDDDDD);
77     }
78 
~ShapesView()79     virtual ~ShapesView() {
80         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
81             SkSafeUnref(fMatrixRefs[i]);
82         }
83     }
84 
85 protected:
86     // overrides from SkEventSink
onQuery(SkEvent * evt)87     virtual bool onQuery(SkEvent* evt) {
88         if (SampleCode::TitleQ(*evt)) {
89             SampleCode::TitleR(evt, "Shapes");
90             return true;
91         }
92         return this->INHERITED::onQuery(evt);
93     }
94 
drawpicture(SkCanvas * canvas,SkPicture & pict)95     void drawpicture(SkCanvas* canvas, SkPicture& pict) {
96 #if 0
97         SkDynamicMemoryWStream ostream;
98         pict.serialize(&ostream);
99 
100         SkMemoryStream istream(ostream.getStream(), ostream.getOffset());
101         SkPicture* newPict = new SkPicture(&istream);
102         canvas->drawPicture(*newPict);
103         newPict->unref();
104 #else
105         canvas->drawPicture(pict);
106 #endif
107     }
108 
onDrawContent(SkCanvas * canvas)109     virtual void onDrawContent(SkCanvas* canvas) {
110         SkScalar angle = SampleCode::GetAnimScalar(SkIntToScalar(180),
111                                                    SkIntToScalar(360));
112 
113         SkMatrix saveM = *fMatrixRefs[3];
114         SkScalar c = SkIntToScalar(50);
115         fMatrixRefs[3]->preRotate(angle, c, c);
116 
117         const SkScalar dx = 350;
118         const SkScalar dy = 500;
119         const int N = 1;
120         for (int v = -N; v <= N; v++) {
121             for (int h = -N; h <= N; h++) {
122                 SkAutoCanvasRestore acr(canvas, true);
123                 canvas->translate(h * dx, v * dy);
124 
125         SkMatrix matrix;
126 
127         SkGroupShape* gs = new SkGroupShape;
128         SkAutoUnref aur(gs);
129         gs->appendShape(&fGroup);
130         matrix.setScale(-SK_Scalar1, SK_Scalar1);
131         matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
132         gs->appendShape(&fGroup, matrix);
133         matrix.setTranslate(SkIntToScalar(240), 0);
134         matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
135         gs->appendShape(&fGroup, matrix);
136 
137 #if 1
138         SkPicture* pict = new SkPicture;
139         SkCanvas* cv = pict->beginRecording(1000, 1000);
140         cv->scale(SK_ScalarHalf, SK_ScalarHalf);
141         gs->draw(cv);
142         cv->translate(SkIntToScalar(680), SkIntToScalar(480));
143         cv->scale(-SK_Scalar1, SK_Scalar1);
144         gs->draw(cv);
145         pict->endRecording();
146 
147         drawpicture(canvas, *pict);
148         pict->unref();
149 #endif
150 
151         }}
152 
153         *fMatrixRefs[3] = saveM;
154         this->inval(NULL);
155 }
156 
157 private:
158     typedef SampleView INHERITED;
159 };
160 
161 ///////////////////////////////////////////////////////////////////////////////
162 
MyFactory()163 static SkView* MyFactory() { return new ShapesView; }
164 static SkViewRegister reg(MyFactory);
165 
166