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 | |
| 8 | #include "gm.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 9 | |
| 10 | #include "Resources.h" |
commit-bot@chromium.org | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 11 | #include "SampleCode.h" |
| 12 | #include "SkBlurMaskFilter.h" |
commit-bot@chromium.org | 1803f4e | 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 | 1803f4e | 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 | 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); |
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 | 1803f4e | 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 | 1803f4e | 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 | 1803f4e | 2014-05-29 22:01:08 +0000 | [diff] [blame] | 50 | float fHorizontalVelocity, fVerticalVelocity; |
| 51 | |
| 52 | SkPoint fCurPos; |
| 53 | |
| 54 | // overrides from SkEventSink |
| 55 | virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
| 56 | if (SampleCode::TitleQ(*evt)) { |
| 57 | SampleCode::TitleR(evt, "SubpixelTranslate"); |
| 58 | return true; |
| 59 | } |
| 60 | return this->INHERITED::onQuery(evt); |
| 61 | } |
| 62 | |
| 63 | virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { |
| 64 | |
| 65 | static const SkPaint::FilterLevel gLevels[] = { |
| 66 | SkPaint::kNone_FilterLevel, |
| 67 | SkPaint::kLow_FilterLevel, |
| 68 | SkPaint::kMedium_FilterLevel, |
| 69 | SkPaint::kHigh_FilterLevel |
| 70 | }; |
| 71 | |
| 72 | SkPaint paint; |
| 73 | paint.setTextSize(48); |
| 74 | |
| 75 | paint.setAntiAlias(true); |
| 76 | for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { |
| 77 | paint.setFilterLevel(gLevels[i]); |
| 78 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize ); |
| 79 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 80 | } |
| 81 | |
| 82 | canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fSize + 10), fCurPos.fY + fSize/2, paint ); |
| 83 | |
| 84 | paint.setAntiAlias(false); |
| 85 | for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { |
| 86 | paint.setFilterLevel(gLevels[i]); |
| 87 | SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize ); |
| 88 | canvas->drawBitmapRect( fBM, r, &paint ); |
| 89 | } |
| 90 | canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint ); |
| 91 | |
| 92 | paint.setAntiAlias(true); |
| 93 | for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { |
| 94 | paint.setFilterLevel(gLevels[i]); |
| 95 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint ); |
| 96 | } |
| 97 | |
| 98 | canvas->drawText( "AA No Scale", strlen("AA No Scale"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fSize/2, paint ); |
| 99 | |
| 100 | paint.setAntiAlias(false); |
| 101 | for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { |
| 102 | paint.setFilterLevel(gLevels[i]); |
| 103 | canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint ); |
| 104 | } |
| 105 | |
| 106 | canvas->drawText( "No Scale", strlen("No Scale"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, paint ); |
| 107 | |
| 108 | |
| 109 | fCurPos.fX += fHorizontalVelocity; |
| 110 | fCurPos.fY += fVerticalVelocity; |
| 111 | this->inval(NULL); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | typedef SampleView INHERITED; |
| 116 | }; |
| 117 | |
| 118 | ////////////////////////////////////////////////////////////////////////////// |
| 119 | |
reed@google.com | 687a26d | 2014-05-30 13:45:36 +0000 | [diff] [blame] | 120 | 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] | 121 | static SkViewRegister reg(MyFactory); |