epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame^] | 11 | #include "SkBitmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkRegion.h" |
| 17 | #include "SkShader.h" |
| 18 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkColorPriv.h" |
| 20 | #include "SkColorFilter.h" |
| 21 | #include "SkTime.h" |
| 22 | #include "SkTypeface.h" |
| 23 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkOSFile.h" |
| 25 | #include "SkStream.h" |
| 26 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 27 | #define INT_SIZE 64 |
| 28 | #define SCALAR_SIZE SkIntToScalar(INT_SIZE) |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 29 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 30 | static void make_bitmap(SkBitmap* bitmap) { |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 31 | bitmap->allocN32Pixels(INT_SIZE, INT_SIZE); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 32 | SkCanvas canvas(*bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 33 | |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 34 | canvas.drawColor(SK_ColorRED); |
| 35 | SkPaint paint; |
| 36 | paint.setAntiAlias(true); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 37 | const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 38 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 39 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, |
| 40 | SkShader::kClamp_TileMode)); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 41 | canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint); |
| 42 | } |
| 43 | |
| 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 | |
| 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 | |
| 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); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 65 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 67 | class BitmapRectView : public SampleView { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 68 | 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 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 78 | void resetBounce() { |
| 79 | fSrcPts[0].set(0, 0); |
| 80 | fSrcPts[1].set(SCALAR_SIZE, SCALAR_SIZE); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 81 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 82 | fSrcVec[0] = unit_vec(30); |
| 83 | fSrcVec[1] = unit_vec(107); |
| 84 | } |
| 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | BitmapRectView() { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 88 | this->setBGColor(SK_ColorGRAY); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 89 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 90 | this->resetBounce(); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 105 | bool onQuery(SkEvent* evt) override { |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 106 | if (SampleCode::TitleQ(*evt)) { |
| 107 | SampleCode::TitleR(evt, "BitmapRect"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | return this->INHERITED::onQuery(evt); |
| 111 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 112 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 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); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 118 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | SkPaint paint; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 120 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 121 | paint.setColor(SK_ColorYELLOW); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 122 | |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 123 | SkBitmap bitmap; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 124 | make_bitmap(&bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 125 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 126 | canvas->translate(20, 20); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 127 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 128 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 129 | canvas->drawRect(srcR, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 130 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 131 | for (int i = 0; i < 2; ++i) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 132 | paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 133 | canvas->drawBitmapRect(bitmap, srcR, fDstR[i], &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 134 | SkCanvas::kStrict_SrcRectConstraint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 135 | canvas->drawRect(fDstR[i], paint); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 136 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 137 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 140 | if (timer.isStopped()) { |
| 141 | this->resetBounce(); |
| 142 | } else if (timer.isRunning()) { |
| 143 | this->bounce(); |
| 144 | } |
| 145 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 147 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | private: |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 149 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | ////////////////////////////////////////////////////////////////////////////// |
| 153 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 170 | bm->allocN32Pixels(BIG_W, BIG_H); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 171 | bm->eraseColor(SK_ColorWHITE); |
| 172 | |
| 173 | SkCanvas canvas(*bm); |
| 174 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 175 | canvas.drawString(gText, 0, paint.getTextSize()*4/5, paint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | class BitmapRectView2 : public SampleView { |
| 179 | SkBitmap fBitmap; |
| 180 | |
| 181 | SkRect fSrcR; |
| 182 | SkRect fLimitR; |
| 183 | SkScalar fDX; |
| 184 | SkRect fDstR[2]; |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 185 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 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 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 191 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 192 | void resetBounce() { |
| 193 | fSrcR.iset(0, 0, fBitmap.height() * 3, fBitmap.height()); |
| 194 | fDX = SK_Scalar1; |
| 195 | } |
| 196 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 197 | public: |
| 198 | BitmapRectView2() { |
| 199 | make_big_bitmap(&fBitmap); |
| 200 | |
| 201 | this->setBGColor(SK_ColorGRAY); |
| 202 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 203 | this->resetBounce(); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 204 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 205 | fLimitR.iset(0, 0, fBitmap.width(), fBitmap.height()); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 206 | |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 207 | fDstR[0] = SkRect::MakeXYWH(20, 20, 600, 200); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 208 | fDstR[1] = fDstR[0]; |
| 209 | fDstR[1].offset(0, fDstR[0].height() * 5/4); |
| 210 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 211 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 212 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 213 | bool onQuery(SkEvent* evt) override { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 214 | if (SampleCode::TitleQ(*evt)) { |
| 215 | SampleCode::TitleR(evt, "BigBitmapRect"); |
| 216 | return true; |
| 217 | } |
| 218 | return this->INHERITED::onQuery(evt); |
| 219 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 220 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 221 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 222 | SkPaint paint; |
| 223 | paint.setStyle(SkPaint::kStroke_Style); |
| 224 | paint.setColor(SK_ColorYELLOW); |
| 225 | |
| 226 | for (int i = 0; i < 2; ++i) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 227 | paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 228 | canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 229 | SkCanvas::kStrict_SrcRectConstraint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 230 | canvas->drawRect(fDstR[i], paint); |
| 231 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 232 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 233 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 235 | if (timer.isStopped()) { |
| 236 | this->resetBounce(); |
| 237 | } else if (timer.isRunning()) { |
| 238 | this->bounceMe(); |
| 239 | } |
| 240 | return true; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 241 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 242 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 243 | private: |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 244 | typedef SampleView INHERITED; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | ////////////////////////////////////////////////////////////////////////////// |
| 248 | |
| 249 | static SkView* F0() { return new BitmapRectView; } |
| 250 | static SkView* F1() { return new BitmapRectView2; } |
| 251 | static SkViewRegister gR0(F0); |
| 252 | static SkViewRegister gR1(F1); |