blob: d5261adefb6b854bc25ae59de139660e6c290524 [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
Mike Kleincd5104e2019-03-20 11:55:08 -05008#include "AnimTimer.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04009#include "Sample.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 Kleincd5104e2019-03-20 11:55:08 -050012#include "SkColorFilter.h"
13#include "SkColorPriv.h"
Mike Reed91919132019-01-02 12:21:01 -050014#include "SkFont.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkGradientShader.h"
16#include "SkGraphics.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018#include "SkRegion.h"
19#include "SkShader.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#include "SkTime.h"
21#include "SkTypeface.h"
Mike Kleincd5104e2019-03-20 11:55:08 -050022#include "SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
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));
Brian Osman4428f2c2019-04-02 10:59:28 -040046 return SkPoint::Make(SkScalarCos(rad), SkScalarSin(rad));
reed@google.com33535f32012-09-25 15:37:50 +000047}
48
49static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
50 *value += *delta;
51 if (*value < min) {
52 *value = min;
53 *delta = - *delta;
54 } else if (*value > max) {
55 *value = max;
56 *delta = - *delta;
57 }
58}
59
60static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
61 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
62 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000063}
reed@android.com8a1c16f2008-12-17 15:59:43 +000064
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040065class BitmapRectView : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +000066 SkPoint fSrcPts[2];
67 SkPoint fSrcVec[2];
68 SkRect fSrcLimit;
69 SkRect fDstR[2];
70
71 void bounce() {
72 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
73 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
74 }
75
reed76113a92015-02-02 12:55:02 -080076 void resetBounce() {
77 fSrcPts[0].set(0, 0);
78 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
halcanary9d524f22016-03-29 09:03:52 -070079
reed76113a92015-02-02 12:55:02 -080080 fSrcVec[0] = unit_vec(30);
81 fSrcVec[1] = unit_vec(107);
82 }
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000085 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000086 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000087
reed76113a92015-02-02 12:55:02 -080088 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +000089
90 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
91 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
92
93 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
94 SkIntToScalar(250), SkIntToScalar(300));
95 fDstR[1] = fDstR[0];
96 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
97
98 fSrcPts[0].set(32, 32);
99 fSrcPts[1].set(90, 90);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000101
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400103 bool onQuery(Sample::Event* evt) override {
104 if (Sample::TitleQ(*evt)) {
105 Sample::TitleR(evt, "BitmapRect");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 return true;
107 }
108 return this->INHERITED::onQuery(evt);
109 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
mtklein36352bf2015-03-25 18:17:31 -0700111 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000112 SkRect srcR;
113 srcR.set(fSrcPts[0], fSrcPts[1]);
114 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
115 srcR.offset(-srcR.width()/2, -srcR.height()/2);
reed@android.comfead49e2009-10-15 18:51:46 +0000116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000118 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000119 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000120
reed@google.com3cec4d72011-07-06 13:59:47 +0000121 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000122 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000123
reed@google.com33535f32012-09-25 15:37:50 +0000124 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000125
reed@google.com33535f32012-09-25 15:37:50 +0000126 canvas->drawBitmap(bitmap, 0, 0, &paint);
127 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000128
reed@google.com33535f32012-09-25 15:37:50 +0000129 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700130 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700131 canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700132 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000133 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000134 }
reed76113a92015-02-02 12:55:02 -0800135 }
reed@google.com33535f32012-09-25 15:37:50 +0000136
Mike Kleincd5104e2019-03-20 11:55:08 -0500137 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800138 if (timer.isStopped()) {
139 this->resetBounce();
140 } else if (timer.isRunning()) {
141 this->bounce();
142 }
143 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400147 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148};
149
150//////////////////////////////////////////////////////////////////////////////
151
reed@google.com33535f32012-09-25 15:37:50 +0000152static void make_big_bitmap(SkBitmap* bm) {
153 static const char gText[] =
154 "We the people, in order to form a more perfect union, establish justice,"
155 " ensure domestic tranquility, provide for the common defense, promote the"
156 " general welfare and ensure the blessings of liberty to ourselves and our"
157 " posterity, do ordain and establish this constitution for the United"
158 " States of America.";
159
160 const int BIG_H = 120;
161
Mike Reed91919132019-01-02 12:21:01 -0500162 SkFont font;
163 font.setSize(SkIntToScalar(BIG_H));
reed@google.com33535f32012-09-25 15:37:50 +0000164
Mike Reed91919132019-01-02 12:21:01 -0500165 const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), kUTF8_SkTextEncoding));
reed@google.com33535f32012-09-25 15:37:50 +0000166
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000167 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000168 bm->eraseColor(SK_ColorWHITE);
169
170 SkCanvas canvas(*bm);
171
Mike Reed91919132019-01-02 12:21:01 -0500172 canvas.drawSimpleText(gText, strlen(gText), kUTF8_SkTextEncoding, 0, font.getSize()*4/5, font, SkPaint());
reed@google.com33535f32012-09-25 15:37:50 +0000173}
174
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400175class BitmapRectView2 : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +0000176 SkBitmap fBitmap;
177
178 SkRect fSrcR;
179 SkRect fLimitR;
180 SkScalar fDX;
181 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000182
reed@google.com33535f32012-09-25 15:37:50 +0000183 void bounceMe() {
184 SkScalar width = fSrcR.width();
185 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
186 fSrcR.fRight = fSrcR.fLeft + width;
187 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000188
reed76113a92015-02-02 12:55:02 -0800189 void resetBounce() {
190 fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
191 fDX = SK_Scalar1;
192 }
193
reed@google.com33535f32012-09-25 15:37:50 +0000194public:
Ben Wagner4d516112018-06-07 13:11:37 -0400195 BitmapRectView2() { }
196
197protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400198 bool onQuery(Sample::Event* evt) override {
199 if (Sample::TitleQ(*evt)) {
200 Sample::TitleR(evt, "BigBitmapRect");
Ben Wagner4d516112018-06-07 13:11:37 -0400201 return true;
202 }
203 return this->INHERITED::onQuery(evt);
204 }
205
206 void onOnceBeforeDraw() override {
reed@google.com33535f32012-09-25 15:37:50 +0000207 make_big_bitmap(&fBitmap);
208
209 this->setBGColor(SK_ColorGRAY);
210
reed76113a92015-02-02 12:55:02 -0800211 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +0000212
reed76113a92015-02-02 12:55:02 -0800213 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
reed@google.com33535f32012-09-25 15:37:50 +0000214
reed76113a92015-02-02 12:55:02 -0800215 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
reed@google.com33535f32012-09-25 15:37:50 +0000216 fDstR[1] = fDstR[0];
217 fDstR[1].offset(0, fDstR[0].height() * 5/4);
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
Mike Kleincd5104e2019-03-20 11:55:08 -0500233 bool onAnimate(const AnimTimer& 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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400243 typedef Sample INHERITED;
reed@google.com33535f32012-09-25 15:37:50 +0000244};
245
246//////////////////////////////////////////////////////////////////////////////
247
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400248DEF_SAMPLE( return new BitmapRectView(); )
249DEF_SAMPLE( return new BitmapRectView2(); )