blob: ccf2d26fe91a47f32c79a6837f920e911c4bdb7f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
reed76113a92015-02-02 12:55:02 -08007
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkView.h"
11#include "SkCanvas.h"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkRegion.h"
16#include "SkShader.h"
17#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkColorPriv.h"
19#include "SkColorFilter.h"
20#include "SkTime.h"
21#include "SkTypeface.h"
22
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkOSFile.h"
24#include "SkStream.h"
25
reed@google.com33535f32012-09-25 15:37:50 +000026#define INT_SIZE 64
27#define SCALAR_SIZE SkIntToScalar(INT_SIZE)
reed@google.com3cec4d72011-07-06 13:59:47 +000028
reed@google.com33535f32012-09-25 15:37:50 +000029static void make_bitmap(SkBitmap* bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000030 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
reed@google.com33535f32012-09-25 15:37:50 +000031 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000032
reed@android.comfead49e2009-10-15 18:51:46 +000033 canvas.drawColor(SK_ColorRED);
34 SkPaint paint;
35 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000036 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000037 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
reed8a21c9f2016-03-08 18:50:00 -080038 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
39 SkShader::kClamp_TileMode));
reed@google.com33535f32012-09-25 15:37:50 +000040 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
41}
42
43static 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
50static 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
61static 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);
reed@android.comfead49e2009-10-15 18:51:46 +000064}
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
reed@google.comf2183392011-04-22 14:10:48 +000066class BitmapRectView : public SampleView {
reed@google.com33535f32012-09-25 15:37:50 +000067 SkPoint fSrcPts[2];
68 SkPoint fSrcVec[2];
69 SkRect fSrcLimit;
70 SkRect fDstR[2];
71
72 void bounce() {
73 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
74 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
75 }
76
reed76113a92015-02-02 12:55:02 -080077 void resetBounce() {
78 fSrcPts[0].set(0, 0);
79 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
halcanary9d524f22016-03-29 09:03:52 -070080
reed76113a92015-02-02 12:55:02 -080081 fSrcVec[0] = unit_vec(30);
82 fSrcVec[1] = unit_vec(107);
83 }
84
reed@android.com8a1c16f2008-12-17 15:59:43 +000085public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000086 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000087 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000088
reed76113a92015-02-02 12:55:02 -080089 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +000090
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);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103protected:
mtklein36352bf2015-03-25 18:17:31 -0700104 bool onQuery(SkEvent* evt) override {
reed@android.comfead49e2009-10-15 18:51:46 +0000105 if (SampleCode::TitleQ(*evt)) {
106 SampleCode::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 return true;
108 }
109 return this->INHERITED::onQuery(evt);
110 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
mtklein36352bf2015-03-25 18:17:31 -0700112 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000113 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);
reed@android.comfead49e2009-10-15 18:51:46 +0000117
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000119 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000120 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000121
reed@google.com3cec4d72011-07-06 13:59:47 +0000122 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000123 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000124
reed@google.com33535f32012-09-25 15:37:50 +0000125 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000126
reed@google.com33535f32012-09-25 15:37:50 +0000127 canvas->drawBitmap(bitmap, 0, 0, &paint);
128 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000129
reed@google.com33535f32012-09-25 15:37:50 +0000130 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700131 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700132 canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700133 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000134 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000135 }
reed76113a92015-02-02 12:55:02 -0800136 }
reed@google.com33535f32012-09-25 15:37:50 +0000137
mtklein36352bf2015-03-25 18:17:31 -0700138 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800139 if (timer.isStopped()) {
140 this->resetBounce();
141 } else if (timer.isRunning()) {
142 this->bounce();
143 }
144 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147private:
reed76113a92015-02-02 12:55:02 -0800148 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149};
150
151//////////////////////////////////////////////////////////////////////////////
152
reed@google.com33535f32012-09-25 15:37:50 +0000153static 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
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000169 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000170 bm->eraseColor(SK_ColorWHITE);
171
172 SkCanvas canvas(*bm);
173
Cary Clark2a475ea2017-04-28 15:35:12 -0400174 canvas.drawString(gText, 0, paint.getTextSize()*4/5, paint);
reed@google.com33535f32012-09-25 15:37:50 +0000175}
176
177class BitmapRectView2 : public SampleView {
178 SkBitmap fBitmap;
179
180 SkRect fSrcR;
181 SkRect fLimitR;
182 SkScalar fDX;
183 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000184
reed@google.com33535f32012-09-25 15:37:50 +0000185 void bounceMe() {
186 SkScalar width = fSrcR.width();
187 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
188 fSrcR.fRight = fSrcR.fLeft + width;
189 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000190
reed76113a92015-02-02 12:55:02 -0800191 void resetBounce() {
192 fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
193 fDX = SK_Scalar1;
194 }
195
reed@google.com33535f32012-09-25 15:37:50 +0000196public:
197 BitmapRectView2() {
198 make_big_bitmap(&fBitmap);
199
200 this->setBGColor(SK_ColorGRAY);
201
reed76113a92015-02-02 12:55:02 -0800202 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +0000203
reed76113a92015-02-02 12:55:02 -0800204 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
reed@google.com33535f32012-09-25 15:37:50 +0000205
reed76113a92015-02-02 12:55:02 -0800206 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
reed@google.com33535f32012-09-25 15:37:50 +0000207 fDstR[1] = fDstR[0];
208 fDstR[1].offset(0, fDstR[0].height() * 5/4);
209 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000210
reed@google.com33535f32012-09-25 15:37:50 +0000211protected:
mtklein36352bf2015-03-25 18:17:31 -0700212 bool onQuery(SkEvent* evt) override {
reed@google.com33535f32012-09-25 15:37:50 +0000213 if (SampleCode::TitleQ(*evt)) {
214 SampleCode::TitleR(evt, "BigBitmapRect");
215 return true;
216 }
217 return this->INHERITED::onQuery(evt);
218 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000219
mtklein36352bf2015-03-25 18:17:31 -0700220 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000221 SkPaint paint;
222 paint.setStyle(SkPaint::kStroke_Style);
223 paint.setColor(SK_ColorYELLOW);
224
225 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700226 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700227 canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700228 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000229 canvas->drawRect(fDstR[i], paint);
230 }
reed76113a92015-02-02 12:55:02 -0800231 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000232
mtklein36352bf2015-03-25 18:17:31 -0700233 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800234 if (timer.isStopped()) {
235 this->resetBounce();
236 } else if (timer.isRunning()) {
237 this->bounceMe();
238 }
239 return true;
reed@google.com33535f32012-09-25 15:37:50 +0000240 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000241
reed@google.com33535f32012-09-25 15:37:50 +0000242private:
reed76113a92015-02-02 12:55:02 -0800243 typedef SampleView INHERITED;
reed@google.com33535f32012-09-25 15:37:50 +0000244};
245
246//////////////////////////////////////////////////////////////////////////////
247
248static SkView* F0() { return new BitmapRectView; }
249static SkView* F1() { return new BitmapRectView2; }
250static SkViewRegister gR0(F0);
251static SkViewRegister gR1(F1);