blob: 9dcf98c3cbc9fc0b953e4825efd3dd3d8bd55988 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed76113a92015-02-02 12:55:02 -08009#include "SkAnimTimer.h"
Mike Reed75ae4212018-01-23 11:24:08 -050010#include "SkBitmap.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000011#include "SkCanvas.h"
Mike Reed91919132019-01-02 12:21:01 -050012#include "SkFont.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRegion.h"
17#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040018#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
23
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#include "SkOSFile.h"
25#include "SkStream.h"
26
reed@google.com33535f32012-09-25 15:37:50 +000027#define INT_SIZE 64
28#define SCALAR_SIZE SkIntToScalar(INT_SIZE)
reed@google.com3cec4d72011-07-06 13:59:47 +000029
reed@google.com33535f32012-09-25 15:37:50 +000030static void make_bitmap(SkBitmap* bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000031 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
reed@google.com33535f32012-09-25 15:37:50 +000032 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000033
reed@android.comfead49e2009-10-15 18:51:46 +000034 canvas.drawColor(SK_ColorRED);
35 SkPaint paint;
36 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000037 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000038 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
reed8a21c9f2016-03-08 18:50:00 -080039 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
40 SkShader::kClamp_TileMode));
reed@google.com33535f32012-09-25 15:37:50 +000041 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
42}
43
44static 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
51static 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
62static 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);
reed@android.comfead49e2009-10-15 18:51:46 +000065}
reed@android.com8a1c16f2008-12-17 15:59:43 +000066
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040067class BitmapRectView : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +000068 SkPoint fSrcPts[2];
69 SkPoint fSrcVec[2];
70 SkRect fSrcLimit;
71 SkRect fDstR[2];
72
73 void bounce() {
74 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
75 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
76 }
77
reed76113a92015-02-02 12:55:02 -080078 void resetBounce() {
79 fSrcPts[0].set(0, 0);
80 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
halcanary9d524f22016-03-29 09:03:52 -070081
reed76113a92015-02-02 12:55:02 -080082 fSrcVec[0] = unit_vec(30);
83 fSrcVec[1] = unit_vec(107);
84 }
85
reed@android.com8a1c16f2008-12-17 15:59:43 +000086public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000088 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000089
reed76113a92015-02-02 12:55:02 -080090 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +000091
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);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400105 bool onQuery(Sample::Event* evt) override {
106 if (Sample::TitleQ(*evt)) {
107 Sample::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 return true;
109 }
110 return this->INHERITED::onQuery(evt);
111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
mtklein36352bf2015-03-25 18:17:31 -0700113 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000114 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);
reed@android.comfead49e2009-10-15 18:51:46 +0000118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000120 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000121 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000122
reed@google.com3cec4d72011-07-06 13:59:47 +0000123 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000124 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000125
reed@google.com33535f32012-09-25 15:37:50 +0000126 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000127
reed@google.com33535f32012-09-25 15:37:50 +0000128 canvas->drawBitmap(bitmap, 0, 0, &paint);
129 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000130
reed@google.com33535f32012-09-25 15:37:50 +0000131 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700132 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700133 canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700134 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000135 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000136 }
reed76113a92015-02-02 12:55:02 -0800137 }
reed@google.com33535f32012-09-25 15:37:50 +0000138
mtklein36352bf2015-03-25 18:17:31 -0700139 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800140 if (timer.isStopped()) {
141 this->resetBounce();
142 } else if (timer.isRunning()) {
143 this->bounce();
144 }
145 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400149 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150};
151
152//////////////////////////////////////////////////////////////////////////////
153
reed@google.com33535f32012-09-25 15:37:50 +0000154static 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
Mike Reed91919132019-01-02 12:21:01 -0500164 SkFont font;
165 font.setSize(SkIntToScalar(BIG_H));
reed@google.com33535f32012-09-25 15:37:50 +0000166
Mike Reed91919132019-01-02 12:21:01 -0500167 const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), kUTF8_SkTextEncoding));
reed@google.com33535f32012-09-25 15:37:50 +0000168
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
Mike Reed91919132019-01-02 12:21:01 -0500174 canvas.drawSimpleText(gText, strlen(gText), kUTF8_SkTextEncoding, 0, font.getSize()*4/5, font, SkPaint());
reed@google.com33535f32012-09-25 15:37:50 +0000175}
176
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400177class BitmapRectView2 : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +0000178 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:
Ben Wagner4d516112018-06-07 13:11:37 -0400197 BitmapRectView2() { }
198
199protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400200 bool onQuery(Sample::Event* evt) override {
201 if (Sample::TitleQ(*evt)) {
202 Sample::TitleR(evt, "BigBitmapRect");
Ben Wagner4d516112018-06-07 13:11:37 -0400203 return true;
204 }
205 return this->INHERITED::onQuery(evt);
206 }
207
208 void onOnceBeforeDraw() override {
reed@google.com33535f32012-09-25 15:37:50 +0000209 make_big_bitmap(&fBitmap);
210
211 this->setBGColor(SK_ColorGRAY);
212
reed76113a92015-02-02 12:55:02 -0800213 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +0000214
reed76113a92015-02-02 12:55:02 -0800215 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
reed@google.com33535f32012-09-25 15:37:50 +0000216
reed76113a92015-02-02 12:55:02 -0800217 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
reed@google.com33535f32012-09-25 15:37:50 +0000218 fDstR[1] = fDstR[0];
219 fDstR[1].offset(0, fDstR[0].height() * 5/4);
220 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000221
mtklein36352bf2015-03-25 18:17:31 -0700222 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000223 SkPaint paint;
224 paint.setStyle(SkPaint::kStroke_Style);
225 paint.setColor(SK_ColorYELLOW);
226
227 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700228 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700229 canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700230 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000231 canvas->drawRect(fDstR[i], paint);
232 }
reed76113a92015-02-02 12:55:02 -0800233 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000234
mtklein36352bf2015-03-25 18:17:31 -0700235 bool onAnimate(const SkAnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800236 if (timer.isStopped()) {
237 this->resetBounce();
238 } else if (timer.isRunning()) {
239 this->bounceMe();
240 }
241 return true;
reed@google.com33535f32012-09-25 15:37:50 +0000242 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000243
reed@google.com33535f32012-09-25 15:37:50 +0000244private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400245 typedef Sample INHERITED;
reed@google.com33535f32012-09-25 15:37:50 +0000246};
247
248//////////////////////////////////////////////////////////////////////////////
249
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400250DEF_SAMPLE( return new BitmapRectView(); )
251DEF_SAMPLE( return new BitmapRectView2(); )