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" |
commit-bot@chromium.org | 1803f4e | 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 | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 12 | #include "SampleCode.h" |
| 13 | #include "SkBlurMaskFilter.h" |
commit-bot@chromium.org | 1803f4e | 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 | 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 | |
| 22 | class SubpixelTranslateView : public SampleView { |
| 23 | public: |
| 24 | SubpixelTranslateView(const char imageFilename[], |
| 25 | float horizontalVelocity, |
| 26 | float verticalVelocity) |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 27 | : fHorizontalVelocity(horizontalVelocity), |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 28 | fVerticalVelocity(verticalVelocity) { |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 29 | SkString resourcePath = GetResourcePath(imageFilename); |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 30 | if (!decode_file(resourcePath.c_str(), &fBM)) { |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 31 | fBM.allocN32Pixels(1, 1); |
| 32 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 33 | } |
| 34 | fCurPos = SkPoint::Make(0,0); |
| 35 | fSize = 200; |
| 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 | |
| 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 | 1803f4e | 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 | 1803f4e | 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 | 1803f4e | 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 | 1803f4e | 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 | 1803f4e | 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 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 74 | canvas->drawText( "AA Scaled", strlen("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] | 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 | 1803f4e | 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 | } |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 82 | canvas->drawText( "Scaled", strlen("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] | 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 | 1803f4e | 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 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 90 | canvas->drawText( "AA No Scale", strlen("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] | 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 | 1803f4e | 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 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 98 | canvas->drawText( "No Scale", strlen("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] | 99 | |
| 100 | |
| 101 | fCurPos.fX += fHorizontalVelocity; |
| 102 | fCurPos.fY += fVerticalVelocity; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 103 | this->inval(nullptr); |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private: |
| 107 | typedef SampleView INHERITED; |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
reed@google.com | 687a26d | 2014-05-30 13:45:36 +0000 | [diff] [blame] | 112 | static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); } |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 113 | static SkViewRegister reg(MyFactory); |