blob: 6bd1a89c0a7caf358ca33361ea1a91ecf1b3e5c7 [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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkGraphics.h"
14#include "include/core/SkPath.h"
15#include "include/core/SkRegion.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkTime.h"
18#include "include/core/SkTypeface.h"
19#include "include/effects/SkGradientShader.h"
20#include "samplecode/Sample.h"
21#include "src/utils/SkUTF.h"
22#include "tools/timer/AnimTimer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkStream.h"
25#include "src/core/SkOSFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
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 };
Mike Reedfae8fce2019-04-03 10:27:45 -040039 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
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));
Brian Osman4428f2c2019-04-02 10:59:28 -040045 return SkPoint::Make(SkScalarCos(rad), SkScalarSin(rad));
reed@google.com33535f32012-09-25 15:37:50 +000046}
47
48static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
49 *value += *delta;
50 if (*value < min) {
51 *value = min;
52 *delta = - *delta;
53 } else if (*value > max) {
54 *value = max;
55 *delta = - *delta;
56 }
57}
58
59static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
60 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
61 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000062}
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040064class BitmapRectView : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +000065 SkPoint fSrcPts[2];
66 SkPoint fSrcVec[2];
67 SkRect fSrcLimit;
68 SkRect fDstR[2];
69
70 void bounce() {
71 bounce_pt(&fSrcPts[0], &fSrcVec[0], fSrcLimit);
72 bounce_pt(&fSrcPts[1], &fSrcVec[1], fSrcLimit);
73 }
74
reed76113a92015-02-02 12:55:02 -080075 void resetBounce() {
76 fSrcPts[0].set(0, 0);
77 fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE);
halcanary9d524f22016-03-29 09:03:52 -070078
reed76113a92015-02-02 12:55:02 -080079 fSrcVec[0] = unit_vec(30);
80 fSrcVec[1] = unit_vec(107);
81 }
82
reed@android.com8a1c16f2008-12-17 15:59:43 +000083public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 BitmapRectView() {
reed@google.comf2183392011-04-22 14:10:48 +000085 this->setBGColor(SK_ColorGRAY);
reed@google.com33535f32012-09-25 15:37:50 +000086
reed76113a92015-02-02 12:55:02 -080087 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +000088
89 fSrcLimit.set(-SCALAR_SIZE/4, -SCALAR_SIZE/4,
90 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4);
91
92 fDstR[0] = SkRect::MakeXYWH(SkIntToScalar(10), SkIntToScalar(100),
93 SkIntToScalar(250), SkIntToScalar(300));
94 fDstR[1] = fDstR[0];
95 fDstR[1].offset(fDstR[0].width() * 5/4, 0);
96
97 fSrcPts[0].set(32, 32);
98 fSrcPts[1].set(90, 90);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101protected:
Hal Canary8a027312019-07-03 10:55:44 -0400102 SkString name() override { return SkString("BitmapRect"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000103
mtklein36352bf2015-03-25 18:17:31 -0700104 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000105 SkRect srcR;
106 srcR.set(fSrcPts[0], fSrcPts[1]);
107 srcR = SkRect::MakeXYWH(fSrcPts[0].fX, fSrcPts[0].fY, 32, 32);
108 srcR.offset(-srcR.width()/2, -srcR.height()/2);
reed@android.comfead49e2009-10-15 18:51:46 +0000109
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110 SkPaint paint;
reed@android.comfead49e2009-10-15 18:51:46 +0000111 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com33535f32012-09-25 15:37:50 +0000112 paint.setColor(SK_ColorYELLOW);
reed@android.comfead49e2009-10-15 18:51:46 +0000113
reed@google.com3cec4d72011-07-06 13:59:47 +0000114 SkBitmap bitmap;
reed@google.com33535f32012-09-25 15:37:50 +0000115 make_bitmap(&bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +0000116
reed@google.com33535f32012-09-25 15:37:50 +0000117 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +0000118
reed@google.com33535f32012-09-25 15:37:50 +0000119 canvas->drawBitmap(bitmap, 0, 0, &paint);
120 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +0000121
reed@google.com33535f32012-09-25 15:37:50 +0000122 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700123 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700124 canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700125 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000126 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +0000127 }
reed76113a92015-02-02 12:55:02 -0800128 }
reed@google.com33535f32012-09-25 15:37:50 +0000129
Mike Kleincd5104e2019-03-20 11:55:08 -0500130 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800131 if (timer.isStopped()) {
132 this->resetBounce();
133 } else if (timer.isRunning()) {
134 this->bounce();
135 }
136 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400140 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141};
142
143//////////////////////////////////////////////////////////////////////////////
144
reed@google.com33535f32012-09-25 15:37:50 +0000145static void make_big_bitmap(SkBitmap* bm) {
146 static const char gText[] =
147 "We the people, in order to form a more perfect union, establish justice,"
148 " ensure domestic tranquility, provide for the common defense, promote the"
149 " general welfare and ensure the blessings of liberty to ourselves and our"
150 " posterity, do ordain and establish this constitution for the United"
151 " States of America.";
152
153 const int BIG_H = 120;
154
Mike Reed91919132019-01-02 12:21:01 -0500155 SkFont font;
156 font.setSize(SkIntToScalar(BIG_H));
reed@google.com33535f32012-09-25 15:37:50 +0000157
Ben Wagner51e15a62019-05-07 15:38:46 -0400158 const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), SkTextEncoding::kUTF8));
reed@google.com33535f32012-09-25 15:37:50 +0000159
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000160 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000161 bm->eraseColor(SK_ColorWHITE);
162
163 SkCanvas canvas(*bm);
164
Ben Wagner51e15a62019-05-07 15:38:46 -0400165 canvas.drawSimpleText(gText, strlen(gText), SkTextEncoding::kUTF8, 0, font.getSize()*4/5, font, SkPaint());
reed@google.com33535f32012-09-25 15:37:50 +0000166}
167
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400168class BitmapRectView2 : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +0000169 SkBitmap fBitmap;
170
171 SkRect fSrcR;
172 SkRect fLimitR;
173 SkScalar fDX;
174 SkRect fDstR[2];
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000175
reed@google.com33535f32012-09-25 15:37:50 +0000176 void bounceMe() {
177 SkScalar width = fSrcR.width();
178 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
179 fSrcR.fRight = fSrcR.fLeft + width;
180 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000181
reed76113a92015-02-02 12:55:02 -0800182 void resetBounce() {
183 fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height());
184 fDX = SK_Scalar1;
185 }
186
reed@google.com33535f32012-09-25 15:37:50 +0000187public:
Ben Wagner4d516112018-06-07 13:11:37 -0400188 BitmapRectView2() { }
189
190protected:
Hal Canary8a027312019-07-03 10:55:44 -0400191 SkString name() override { return SkString("BigBitmapRect"); }
Ben Wagner4d516112018-06-07 13:11:37 -0400192
193 void onOnceBeforeDraw() override {
reed@google.com33535f32012-09-25 15:37:50 +0000194 make_big_bitmap(&fBitmap);
195
196 this->setBGColor(SK_ColorGRAY);
197
reed76113a92015-02-02 12:55:02 -0800198 this->resetBounce();
reed@google.com33535f32012-09-25 15:37:50 +0000199
reed76113a92015-02-02 12:55:02 -0800200 fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height());
reed@google.com33535f32012-09-25 15:37:50 +0000201
reed76113a92015-02-02 12:55:02 -0800202 fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200);
reed@google.com33535f32012-09-25 15:37:50 +0000203 fDstR[1] = fDstR[0];
204 fDstR[1].offset(0, fDstR[0].height() * 5/4);
205 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000206
mtklein36352bf2015-03-25 18:17:31 -0700207 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000208 SkPaint paint;
209 paint.setStyle(SkPaint::kStroke_Style);
210 paint.setColor(SK_ColorYELLOW);
211
212 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700213 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700214 canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700215 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000216 canvas->drawRect(fDstR[i], paint);
217 }
reed76113a92015-02-02 12:55:02 -0800218 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000219
Mike Kleincd5104e2019-03-20 11:55:08 -0500220 bool onAnimate(const AnimTimer& timer) override {
reed76113a92015-02-02 12:55:02 -0800221 if (timer.isStopped()) {
222 this->resetBounce();
223 } else if (timer.isRunning()) {
224 this->bounceMe();
225 }
226 return true;
reed@google.com33535f32012-09-25 15:37:50 +0000227 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000228
reed@google.com33535f32012-09-25 15:37:50 +0000229private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400230 typedef Sample INHERITED;
reed@google.com33535f32012-09-25 15:37:50 +0000231};
232
233//////////////////////////////////////////////////////////////////////////////
234
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400235DEF_SAMPLE( return new BitmapRectView(); )
236DEF_SAMPLE( return new BitmapRectView2(); )