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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #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" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkStream.h" |
| 24 | #include "src/core/SkOSFile.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 26 | static constexpr int INT_SIZE = 64; |
| 27 | static constexpr float SCALAR_SIZE = (float)INT_SIZE; |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 28 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 29 | static void make_bitmap(SkBitmap* bitmap) { |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 30 | bitmap->allocN32Pixels(INT_SIZE, INT_SIZE); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 31 | SkCanvas canvas(*bitmap); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 32 | |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 33 | canvas.drawColor(SK_ColorRED); |
| 34 | SkPaint paint; |
| 35 | paint.setAntiAlias(true); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 36 | const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } }; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 37 | const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 38 | paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp)); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 39 | canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint); |
| 40 | } |
| 41 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 42 | static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) { |
| 43 | *value += *delta; |
| 44 | if (*value < min) { |
| 45 | *value = min; |
| 46 | *delta = - *delta; |
| 47 | } else if (*value > max) { |
| 48 | *value = max; |
| 49 | *delta = - *delta; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) { |
| 54 | bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight); |
| 55 | bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 56 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 58 | class BitmapRectView : public Sample { |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 59 | SkPoint fSrcPt = {0, 0}; |
| 60 | SkPoint fSrcVec = {0.866025f, 0.5f}; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 61 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 62 | SkRect fSrcLimit = {-SCALAR_SIZE/4, -SCALAR_SIZE/4, |
| 63 | SCALAR_SIZE*5/4, SCALAR_SIZE*5/4}; |
| 64 | SkRect fDstR[2] = {{10, 100, 260, 400}, {322.5, 100, 572.5, 400}}; |
| 65 | SkBitmap fBitmap; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 66 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 67 | SkString name() override { return SkString("BitmapRect"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 69 | void onOnceBeforeDraw() override { |
| 70 | this->setBGColor(SK_ColorGRAY); |
| 71 | make_bitmap(&fBitmap); |
| 72 | } |
| 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | void onDrawContent(SkCanvas* canvas) override { |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 75 | SkRect srcR = {fSrcPt.fX - 16, fSrcPt.fY - 16, |
| 76 | fSrcPt.fX + 16, fSrcPt.fY + 16}; |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 77 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 78 | SkPaint paint(SkColors::kYellow); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 79 | paint.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 3cec4d7 | 2011-07-06 13:59:47 +0000 | [diff] [blame] | 80 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 81 | canvas->translate(20, 20); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 82 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 83 | canvas->drawBitmap(fBitmap, 0, 0, &paint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 84 | canvas->drawRect(srcR, paint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 85 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 86 | for (int i = 0; i < 2; ++i) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 87 | paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 88 | canvas->drawBitmapRect(fBitmap, srcR, fDstR[i], &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 89 | SkCanvas::kStrict_SrcRectConstraint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 90 | canvas->drawRect(fDstR[i], paint); |
reed@android.com | fead49e | 2009-10-15 18:51:46 +0000 | [diff] [blame] | 91 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 92 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 93 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 94 | bool onAnimate(double nanos) override { |
| 95 | bounce_pt(&fSrcPt, &fSrcVec, fSrcLimit); |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 96 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 100 | static constexpr int BIG_H = 120; |
| 101 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 102 | static void make_big_bitmap(SkBitmap* bm) { |
| 103 | static const char gText[] = |
| 104 | "We the people, in order to form a more perfect union, establish justice," |
| 105 | " ensure domestic tranquility, provide for the common defense, promote the" |
| 106 | " general welfare and ensure the blessings of liberty to ourselves and our" |
| 107 | " posterity, do ordain and establish this constitution for the United" |
| 108 | " States of America."; |
| 109 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 110 | SkFont font; |
| 111 | font.setSize(SkIntToScalar(BIG_H)); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 112 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 113 | const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), SkTextEncoding::kUTF8)); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 114 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 115 | bm->allocN32Pixels(BIG_W, BIG_H); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 116 | bm->eraseColor(SK_ColorWHITE); |
| 117 | |
| 118 | SkCanvas canvas(*bm); |
| 119 | |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 120 | canvas.drawSimpleText(gText, strlen(gText), SkTextEncoding::kUTF8, 0, font.getSize()*4/5, font, SkPaint()); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 123 | class BitmapRectView2 : public Sample { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 124 | SkBitmap fBitmap; |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 125 | SkRect fSrcR = {0, 0, 3 * BIG_H, BIG_H}; |
| 126 | SkRect fLimitR; |
| 127 | SkScalar fDX = 1; |
| 128 | SkRect fDstR[2] = {{20, 20, 620, 220}, {20, 270, 620, 470}}; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 129 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 130 | SkString name() override { return SkString("BigBitmapRect"); } |
Ben Wagner | 4d51611 | 2018-06-07 13:11:37 -0400 | [diff] [blame] | 131 | |
| 132 | void onOnceBeforeDraw() override { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 133 | this->setBGColor(SK_ColorGRAY); |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 134 | make_big_bitmap(&fBitmap); |
| 135 | fLimitR = SkRect::Make(fBitmap.dimensions()); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 136 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 137 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 139 | SkPaint paint; |
| 140 | paint.setStyle(SkPaint::kStroke_Style); |
| 141 | paint.setColor(SK_ColorYELLOW); |
| 142 | |
| 143 | for (int i = 0; i < 2; ++i) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 144 | paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 145 | canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint, |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 146 | SkCanvas::kStrict_SrcRectConstraint); |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 147 | canvas->drawRect(fDstR[i], paint); |
| 148 | } |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 149 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 150 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 151 | bool onAnimate(double nanos) override { |
| 152 | SkScalar width = fSrcR.width(); |
| 153 | bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width); |
| 154 | fSrcR.fRight = fSrcR.fLeft + width; |
reed | 76113a9 | 2015-02-02 12:55:02 -0800 | [diff] [blame] | 155 | return true; |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 156 | } |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 159 | DEF_SAMPLE( return new BitmapRectView(); ) |
| 160 | DEF_SAMPLE( return new BitmapRectView2(); ) |