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 | |
| 8 | #include "gm.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 9 | |
| 10 | #include "Resources.h" |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 11 | #include "SampleCode.h" |
| 12 | #include "SkBlurMaskFilter.h" |
commit-bot@chromium.org | 1803f4eb | 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" |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 15 | #include "SkImageDecoder.h" |
| 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 | 1803f4eb | 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); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 30 | SkImageDecoder* codec = NULL; |
| 31 | SkFILEStream stream(resourcePath.c_str()); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 32 | if (stream.isValid()) { |
| 33 | codec = SkImageDecoder::Factory(&stream); |
| 34 | } |
| 35 | if (codec) { |
| 36 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 37 | codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 38 | SkDELETE(codec); |
| 39 | } else { |
| 40 | fBM.allocN32Pixels(1, 1); |
| 41 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 42 | } |
| 43 | fCurPos = SkPoint::Make(0,0); |
| 44 | fSize = 200; |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | SkBitmap fBM; |
reed@google.com | 687a26d | 2014-05-30 13:45:36 +0000 | [diff] [blame] | 49 | SkScalar fSize; |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 50 | float fHorizontalVelocity, fVerticalVelocity; |
| 51 | |
| 52 | SkPoint fCurPos; |
| 53 | |
| 54 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | bool onQuery(SkEvent* evt) override { |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 56 | if (SampleCode::TitleQ(*evt)) { |
| 57 | SampleCode::TitleR(evt, "SubpixelTranslate"); |
| 58 | return true; |
| 59 | } |
| 60 | return this->INHERITED::onQuery(evt); |
| 61 | } |
| 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | void onDrawContent(SkCanvas* canvas) override { |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 64 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 65 | static const SkFilterQuality gQualitys[] = { |
| 66 | kNone_SkFilterQuality, |
| 67 | kLow_SkFilterQuality, |
| 68 | kMedium_SkFilterQuality, |
| 69 | kHigh_SkFilterQuality |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | SkPaint paint; |
| 73 | paint.setTextSize(48); |
humper | f75a130 | 2015-01-29 10:26:37 -0800 | [diff] [blame] | 74 | paint.setSubpixelText(true); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 75 | |
| 76 | paint.setAntiAlias(true); |
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, fSize ); |
| 80 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 81 | } |
| 82 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 83 | canvas->drawText( "AA Scaled", strlen("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] | 84 | |
| 85 | paint.setAntiAlias(false); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 86 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 87 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 88 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize ); |
| 89 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 90 | } |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 91 | canvas->drawText( "Scaled", strlen("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] | 92 | |
| 93 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 94 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 95 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 96 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint ); |
| 97 | } |
| 98 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 99 | 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 | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 100 | |
| 101 | paint.setAntiAlias(false); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 102 | for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { |
| 103 | paint.setFilterQuality(gQualitys[i]); |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 104 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint ); |
| 105 | } |
| 106 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 107 | 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 | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 108 | |
| 109 | |
| 110 | fCurPos.fX += fHorizontalVelocity; |
| 111 | fCurPos.fY += fVerticalVelocity; |
| 112 | this->inval(NULL); |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | typedef SampleView INHERITED; |
| 117 | }; |
| 118 | |
| 119 | ////////////////////////////////////////////////////////////////////////////// |
| 120 | |
reed@google.com | 687a26d | 2014-05-30 13:45:36 +0000 | [diff] [blame] | 121 | static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); } |
commit-bot@chromium.org | 1803f4eb | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 122 | static SkViewRegister reg(MyFactory); |