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