commit-bot@chromium.org | 1803f4eb | 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" |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 9 | #include "gm.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 12 | #include "SampleCode.h" |
| 13 | #include "SkBlurMaskFilter.h" |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 15 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 1803f4eb | 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 | |
| 22 | class SubpixelTranslateView : public SampleView { |
| 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 | 1803f4eb | 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 | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 41 | float fHorizontalVelocity, fVerticalVelocity; |
| 42 | |
| 43 | SkPoint fCurPos; |
| 44 | |
| 45 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | bool onQuery(SkEvent* evt) override { |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 47 | if (SampleCode::TitleQ(*evt)) { |
| 48 | SampleCode::TitleR(evt, "SubpixelTranslate"); |
| 49 | return true; |
| 50 | } |
| 51 | return this->INHERITED::onQuery(evt); |
| 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDrawContent(SkCanvas* canvas) override { |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 55 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 56 | static const SkFilterQuality gQualitys[] = { |
| 57 | kNone_SkFilterQuality, |
| 58 | kLow_SkFilterQuality, |
| 59 | kMedium_SkFilterQuality, |
| 60 | kHigh_SkFilterQuality |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | SkPaint paint; |
| 64 | paint.setTextSize(48); |
humper | f75a130 | 2015-01-29 10:26:37 -0800 | [diff] [blame] | 65 | paint.setSubpixelText(true); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 66 | |
| 67 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 68 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 69 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 70 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize ); |
| 71 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 72 | } |
| 73 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 74 | canvas->drawString( "AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint ); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 75 | |
| 76 | paint.setAntiAlias(false); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 77 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 78 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 79 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize ); |
| 80 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 81 | } |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 82 | canvas->drawString( "Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint ); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 83 | |
| 84 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 85 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 86 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 87 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint ); |
| 88 | } |
| 89 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 90 | 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 | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 91 | |
| 92 | paint.setAntiAlias(false); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 93 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 94 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 95 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint ); |
| 96 | } |
| 97 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 98 | 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 | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | fCurPos.fX += fHorizontalVelocity; |
| 102 | fCurPos.fY += fVerticalVelocity; |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private: |
| 106 | typedef SampleView INHERITED; |
| 107 | }; |
| 108 | |
| 109 | ////////////////////////////////////////////////////////////////////////////// |
| 110 | |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 111 | static SkView* MyFactory() { return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); } |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 112 | static SkViewRegister reg(MyFactory); |