• 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 "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkDevice.h"
12 #include "SkPaint.h"
13 #include "SkShader.h"
14 
createBitmap(int n)15 static SkBitmap createBitmap(int n) {
16     SkBitmap bitmap;
17     bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n);
18     bitmap.allocPixels();
19     bitmap.eraseColor(SK_ColorGREEN);
20 
21     SkCanvas canvas(bitmap);
22     SkRect r;
23     r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
24     SkPaint paint;
25     paint.setAntiAlias(true);
26 
27     paint.setColor(SK_ColorRED);
28     canvas.drawOval(r, paint);
29     paint.setColor(SK_ColorBLUE);
30     paint.setStrokeWidth(SkIntToScalar(n)/15);
31     paint.setStyle(SkPaint::kStroke_Style);
32     canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
33     canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
34 
35     return bitmap;
36 }
37 
38 class AARectView : public SampleView {
39     SkBitmap fBitmap;
40     enum {
41         N = 64
42     };
43 public:
AARectView()44     AARectView() {
45         fBitmap = createBitmap(N);
46 
47         fWidth = N;
48     }
49 
50 protected:
51     // overrides from SkEventSink
onQuery(SkEvent * evt)52     virtual bool onQuery(SkEvent* evt) {
53         if (SampleCode::TitleQ(*evt)) {
54             SampleCode::TitleR(evt, "AA Rects");
55             return true;
56         }
57         return this->INHERITED::onQuery(evt);
58     }
59 
onDrawContent(SkCanvas * canvas)60     virtual void onDrawContent(SkCanvas* canvas) {
61         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
62 
63         SkPaint bluePaint;
64         bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
65         SkPaint bmpPaint;
66         SkShader* bmpShader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
67         bmpPaint.setShader(bmpShader);
68         bmpShader->unref();
69 
70         bluePaint.setStrokeWidth(3);
71         bmpPaint.setStrokeWidth(3);
72 
73         SkPaint paints[] = { bluePaint, bmpPaint };
74 
75         SkRect rect;
76 
77         SkScalar dx = SkIntToScalar(80);
78         SkScalar dy = SkIntToScalar(100);
79         SkMatrix matrix;
80         for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
81             for (int stroke = 0; stroke < 2; ++stroke) {
82                 paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
83                 for (int a = 0; a < 3; ++ a) {
84                     paints[p].setAntiAlias(a > 0);
85                     paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
86 
87                     canvas->save();
88                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.f),
89                                                 SkFloatToScalar(0.f),
90                                                 SkFloatToScalar(40.f),
91                                                 SkFloatToScalar(40.f));
92                         canvas->drawRect(rect, paints[p]);
93                         canvas->translate(dx, 0);
94 
95                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
96                                                 SkFloatToScalar(0.5f),
97                                                 SkFloatToScalar(40.5f),
98                                                 SkFloatToScalar(40.5f));
99                         canvas->drawRect(rect, paints[p]);
100                         canvas->translate(dx, 0);
101 
102                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
103                                                 SkFloatToScalar(0.5f),
104                                                 SkFloatToScalar(40.f),
105                                                 SkFloatToScalar(40.f));
106                         canvas->drawRect(rect, paints[p]);
107                         canvas->translate(dx, 0);
108 
109                         rect = SkRect::MakeLTRB(SkFloatToScalar(0.75f),
110                                                 SkFloatToScalar(0.75f),
111                                                 SkFloatToScalar(40.75f),
112                                                 SkFloatToScalar(40.75f));
113                         canvas->drawRect(rect, paints[p]);
114                         canvas->translate(dx, 0);
115 
116                         canvas->save();
117                             canvas->translate(SkFloatToScalar(.33f), SkFloatToScalar(.67f));
118                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
119                                                     SkFloatToScalar(0.0f),
120                                                     SkFloatToScalar(40.0f),
121                                                     SkFloatToScalar(40.0f));
122                             canvas->drawRect(rect, paints[p]);
123                         canvas->restore();
124                         canvas->translate(dx, 0);
125 
126                         canvas->save();
127                             matrix.setRotate(SkFloatToScalar(45.f));
128                             canvas->concat(matrix);
129                             canvas->translate(SkFloatToScalar(20.0f / sqrtf(2.f)),
130                                                 SkFloatToScalar(20.0f / sqrtf(2.f)));
131                             rect = SkRect::MakeLTRB(SkFloatToScalar(-20.0f),
132                                                     SkFloatToScalar(-20.0f),
133                                                     SkFloatToScalar(20.0f),
134                                                     SkFloatToScalar(20.0f));
135                             canvas->drawRect(rect, paints[p]);
136                         canvas->restore();
137                         canvas->translate(dx, 0);
138 
139                         canvas->save();
140                             canvas->rotate(SkFloatToScalar(90.f));
141                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
142                                                     SkFloatToScalar(0.0f),
143                                                     SkFloatToScalar(40.0f),
144                                                     SkFloatToScalar(-40.0f));
145                             canvas->drawRect(rect, paints[p]);
146                         canvas->restore();
147                         canvas->translate(dx, 0);
148 
149                         canvas->save();
150                             canvas->rotate(SkFloatToScalar(90.f));
151                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
152                                                     SkFloatToScalar(0.5f),
153                                                     SkFloatToScalar(40.5f),
154                                                     SkFloatToScalar(-40.5f));
155                             canvas->drawRect(rect, paints[p]);
156                         canvas->restore();
157                         canvas->translate(dx, 0);
158 
159                         canvas->save();
160                             matrix.setScale(SkFloatToScalar(-1.f), SkFloatToScalar(-1.f));
161                             canvas->concat(matrix);
162                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
163                                                     SkFloatToScalar(0.5f),
164                                                     SkFloatToScalar(-40.5f),
165                                                     SkFloatToScalar(-40.5f));
166                             canvas->drawRect(rect, paints[p]);
167                         canvas->restore();
168                         canvas->translate(dx, 0);
169 
170                         canvas->save();
171                             matrix.setScale(SkFloatToScalar(2.1f), SkFloatToScalar(4.1f));
172                             canvas->concat(matrix);
173                             rect = SkRect::MakeLTRB(SkFloatToScalar(0.1f),
174                                                     SkFloatToScalar(0.1f),
175                                                     SkFloatToScalar(19.1f),
176                                                     SkFloatToScalar(9.1f));
177                             canvas->drawRect(rect, paints[p]);
178                         canvas->restore();
179                         canvas->translate(dx, 0);
180 
181                     canvas->restore();
182                     canvas->translate(0, dy);
183                 }
184             }
185         }
186     }
187 
188 private:
189     int fWidth;
190 
191     typedef SampleView INHERITED;
192 };
193 
194 //////////////////////////////////////////////////////////////////////////////
195 
MyFactory()196 static SkView* MyFactory() { return new AARectView; }
197 static SkViewRegister reg(MyFactory);
198 
199