commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
| 7 | |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 8 | #include "Sample.h" |
| 9 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 10 | #include "DecodeFile.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 11 | #include "Resources.h" |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 12 | #include "SkBlurMaskFilter.h" |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 14 | #include "SkColorPriv.h" |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 15 | #include "SkFont.h" |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 16 | #include "SkRandom.h" |
| 17 | #include "SkStream.h" |
| 18 | |
| 19 | // Intended to exercise pixel snapping observed with scaled images (and |
| 20 | // with non-scaled images, but for a different reason): Bug 1145 |
| 21 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 22 | class SubpixelTranslateView : public Sample { |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 23 | public: |
| 24 | SubpixelTranslateView(const char imageFilename[], |
| 25 | float horizontalVelocity, |
| 26 | float verticalVelocity) |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 27 | : fHorizontalVelocity(horizontalVelocity) |
| 28 | , fVerticalVelocity(verticalVelocity) |
| 29 | { |
| 30 | if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) { |
| 31 | fBM.allocN32Pixels(1, 1); |
| 32 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 33 | } |
| 34 | fCurPos = SkPoint::Make(0,0); |
| 35 | fSize = 200; |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | protected: |
| 39 | SkBitmap fBM; |
reed@google.com | 687a26d | 2014-05-30 13:45:36 +0000 | [diff] [blame] | 40 | SkScalar fSize; |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 41 | float fHorizontalVelocity, fVerticalVelocity; |
| 42 | |
| 43 | SkPoint fCurPos; |
| 44 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 45 | bool onQuery(Sample::Event* evt) override { |
| 46 | if (Sample::TitleQ(*evt)) { |
| 47 | Sample::TitleR(evt, "SubpixelTranslate"); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | return this->INHERITED::onQuery(evt); |
| 51 | } |
| 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | void onDrawContent(SkCanvas* canvas) override { |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 54 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 55 | static const SkFilterQuality gQualitys[] = { |
| 56 | kNone_SkFilterQuality, |
| 57 | kLow_SkFilterQuality, |
| 58 | kMedium_SkFilterQuality, |
| 59 | kHigh_SkFilterQuality |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | SkPaint paint; |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 63 | SkFont font(nullptr, 48); |
| 64 | font.setSubpixel(true); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 65 | |
| 66 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 67 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 68 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 69 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize ); |
| 70 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 71 | } |
| 72 | |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 73 | canvas->drawString("AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), |
| 74 | fCurPos.fY + fSize/2, font, paint); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 75 | |
| 76 | paint.setAntiAlias(false); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 77 | font.setEdging(SkFont::Edging::kAlias); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 79 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 80 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize ); |
| 81 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 82 | } |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 83 | canvas->drawString("Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), |
| 84 | fCurPos.fY + fSize + 10 + fSize/2, font, paint); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 85 | |
| 86 | paint.setAntiAlias(true); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 87 | font.setEdging(SkFont::Edging::kAntiAlias); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 88 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 89 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 90 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint ); |
| 91 | } |
| 92 | |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 93 | canvas->drawString("AA No Scale", |
| 94 | fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10), |
| 95 | fCurPos.fY + 2*(fSize + 10) + fSize/2, font, paint); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 96 | |
| 97 | paint.setAntiAlias(false); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 98 | font.setEdging(SkFont::Edging::kAlias); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 99 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 100 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 101 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint ); |
| 102 | } |
| 103 | |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame^] | 104 | canvas->drawString("No Scale", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10), |
| 105 | fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, font, paint); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | fCurPos.fX += fHorizontalVelocity; |
| 109 | fCurPos.fY += fVerticalVelocity; |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 113 | typedef Sample INHERITED; |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | ////////////////////////////////////////////////////////////////////////////// |
| 117 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 118 | DEF_SAMPLE( return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); ) |