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