• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SampleCode.h"
9 #include "SkAnimTimer.h"
10 #include "SkView.h"
11 #include "SkCanvas.h"
12 #include "SkGradientShader.h"
13 #include "SkGraphics.h"
14 #include "SkPath.h"
15 #include "SkRegion.h"
16 #include "SkShader.h"
17 #include "SkUtils.h"
18 #include "SkColorPriv.h"
19 #include "SkColorFilter.h"
20 #include "SkTime.h"
21 #include "SkTypeface.h"
22 
23 #include "SkOSFile.h"
24 #include "SkStream.h"
25 
26 #define INT_SIZE        64
27 #define SCALAR_SIZE     SkIntToScalar(INT_SIZE)
28 
make_bitmap(SkBitmap * bitmap)29 static void make_bitmap(SkBitmap* bitmap) {
30     bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
31     SkCanvas canvas(*bitmap);
32 
33     canvas.drawColor(SK_ColorRED);
34     SkPaint paint;
35     paint.setAntiAlias(true);
36     const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
37     const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
38     paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
39                                                    SkShader::kClamp_TileMode));
40     canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
41 }
42 
unit_vec(int degrees)43 static SkPoint unit_vec(int degrees) {
44     SkScalar rad = SkDegreesToRadians(SkIntToScalar(degrees));
45     SkScalar s, c;
46     s = SkScalarSinCos(rad, &c);
47     return SkPoint::Make(c, s);
48 }
49 
bounce(SkScalar * value,SkScalar * delta,SkScalar min,SkScalar max)50 static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
51     *value += *delta;
52     if (*value < min) {
53         *value = min;
54         *delta = - *delta;
55     } else if (*value > max) {
56         *value = max;
57         *delta = - *delta;
58     }
59 }
60 
bounce_pt(SkPoint * pt,SkVector * vec,const SkRect & limit)61 static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
62     bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
63     bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
64 }
65 
66 class BitmapRectView : public SampleView {
67     SkPoint fSrcPts[2];
68     SkPoint fSrcVec[2];
69     SkRect  fSrcLimit;
70     SkRect  fDstR[2];
71 
bounce()72     void bounce() {
73         bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
74         bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
75     }
76 
resetBounce()77     void resetBounce() {
78         fSrcPts[0].set(0, 0);
79         fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
80 
81         fSrcVec[0] = unit_vec(30);
82         fSrcVec[1] = unit_vec(107);
83     }
84 
85 public:
BitmapRectView()86     BitmapRectView() {
87         this->setBGColor(SK_ColorGRAY);
88 
89         this->resetBounce();
90 
91         fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
92                       SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
93 
94         fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
95                                        SkIntToScalar(250), SkIntToScalar(300));
96         fDstR[1] = fDstR[0];
97         fDstR[1].offset(fDstR[0].width() * 5/4, 0);
98 
99         fSrcPts[0].set(32, 32);
100         fSrcPts[1].set(90, 90);
101     }
102 
103 protected:
onQuery(SkEvent * evt)104     bool onQuery(SkEvent* evt) override {
105         if (SampleCode::TitleQ(*evt)) {
106             SampleCode::TitleR(evt, "BitmapRect");
107             return true;
108         }
109         return this->INHERITED::onQuery(evt);
110     }
111 
onDrawContent(SkCanvas * canvas)112     void onDrawContent(SkCanvas* canvas) override {
113         SkRect srcR;
114         srcR.set(fSrcPts[0], fSrcPts[1]);
115         srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
116         srcR.offset(-srcR.width()/2, -srcR.height()/2);
117 
118         SkPaint paint;
119         paint.setStyle(SkPaint::kStroke_Style);
120         paint.setColor(SK_ColorYELLOW);
121 
122         SkBitmap bitmap;
123         make_bitmap(&bitmap);
124 
125         canvas->translate(20, 20);
126 
127         canvas->drawBitmap(bitmap, 0, 0, &paint);
128         canvas->drawRect(srcR, paint);
129 
130         for (int i = 0; i < 2; ++i) {
131             paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
132             canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint,
133                                    SkCanvas::kStrict_SrcRectConstraint);
134             canvas->drawRect(fDstR[i], paint);
135         }
136     }
137 
onAnimate(const SkAnimTimer & timer)138     bool onAnimate(const SkAnimTimer& timer) override {
139         if (timer.isStopped()) {
140             this->resetBounce();
141         } else if (timer.isRunning()) {
142             this->bounce();
143         }
144         return true;
145     }
146 
147 private:
148     typedef SampleView INHERITED;
149 };
150 
151 //////////////////////////////////////////////////////////////////////////////
152 
make_big_bitmap(SkBitmap * bm)153 static void make_big_bitmap(SkBitmap* bm) {
154     static const char gText[] =
155         "We the people, in order to form a more perfect union, establish justice,"
156         " ensure domestic tranquility, provide for the common defense, promote the"
157         " general welfare and ensure the blessings of liberty to ourselves and our"
158         " posterity, do ordain and establish this constitution for the United"
159         " States of America.";
160 
161     const int BIG_H = 120;
162 
163     SkPaint paint;
164     paint.setAntiAlias(true);
165     paint.setTextSize(SkIntToScalar(BIG_H));
166 
167     const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
168 
169     bm->allocN32Pixels(BIG_W, BIG_H);
170     bm->eraseColor(SK_ColorWHITE);
171 
172     SkCanvas canvas(*bm);
173 
174     canvas.drawString(gText, 0, paint.getTextSize()*4/5, paint);
175 }
176 
177 class BitmapRectView2 : public SampleView {
178     SkBitmap fBitmap;
179 
180     SkRect  fSrcR;
181     SkRect  fLimitR;
182     SkScalar fDX;
183     SkRect  fDstR[2];
184 
bounceMe()185     void bounceMe() {
186         SkScalar width = fSrcR.width();
187         bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
188         fSrcR.fRight = fSrcR.fLeft + width;
189     }
190 
resetBounce()191     void resetBounce() {
192         fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
193         fDX = SK_Scalar1;
194     }
195 
196 public:
BitmapRectView2()197     BitmapRectView2() {
198         make_big_bitmap(&fBitmap);
199 
200         this->setBGColor(SK_ColorGRAY);
201 
202         this->resetBounce();
203 
204         fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
205 
206         fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
207         fDstR[1] = fDstR[0];
208         fDstR[1].offset(0, fDstR[0].height() * 5/4);
209     }
210 
211 protected:
onQuery(SkEvent * evt)212     bool onQuery(SkEvent* evt) override {
213         if (SampleCode::TitleQ(*evt)) {
214             SampleCode::TitleR(evt, "BigBitmapRect");
215             return true;
216         }
217         return this->INHERITED::onQuery(evt);
218     }
219 
onDrawContent(SkCanvas * canvas)220     void onDrawContent(SkCanvas* canvas) override {
221         SkPaint paint;
222         paint.setStyle(SkPaint::kStroke_Style);
223         paint.setColor(SK_ColorYELLOW);
224 
225         for (int i = 0; i < 2; ++i) {
226             paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
227             canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
228                                    SkCanvas::kStrict_SrcRectConstraint);
229             canvas->drawRect(fDstR[i], paint);
230         }
231     }
232 
onAnimate(const SkAnimTimer & timer)233     bool onAnimate(const SkAnimTimer& timer) override {
234         if (timer.isStopped()) {
235             this->resetBounce();
236         } else if (timer.isRunning()) {
237             this->bounceMe();
238         }
239         return true;
240     }
241 
242 private:
243     typedef SampleView INHERITED;
244 };
245 
246 //////////////////////////////////////////////////////////////////////////////
247 
F0()248 static SkView* F0() { return new BitmapRectView; }
F1()249 static SkView* F1() { return new BitmapRectView2; }
250 static SkViewRegister gR0(F0);
251 static SkViewRegister gR1(F1);
252