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