blob: 7ce18858920e59d6ab0806eea6d9bbfe47002a26 [file] [log] [blame]
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +00001/*
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
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +00009#include "gm.h"
tfarinabcbc1782014-06-18 14:32:48 -070010
11#include "Resources.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000012#include "SampleCode.h"
13#include "SkBlurMaskFilter.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000014#include "SkCanvas.h"
tfarinabcbc1782014-06-18 14:32:48 -070015#include "SkColorPriv.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000016#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
22class SubpixelTranslateView : public SampleView {
23public:
24 SubpixelTranslateView(const char imageFilename[],
25 float horizontalVelocity,
26 float verticalVelocity)
Mike Reed0933bc92017-12-09 01:27:41 +000027 : 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.org1803f4eb2014-05-29 22:01:08 +000036 }
37
38protected:
39 SkBitmap fBM;
reed@google.com687a26d2014-05-30 13:45:36 +000040 SkScalar fSize;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000041 float fHorizontalVelocity, fVerticalVelocity;
42
43 SkPoint fCurPos;
44
45 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070046 bool onQuery(SkEvent* evt) override {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000047 if (SampleCode::TitleQ(*evt)) {
48 SampleCode::TitleR(evt, "SubpixelTranslate");
49 return true;
50 }
51 return this->INHERITED::onQuery(evt);
52 }
53
mtklein36352bf2015-03-25 18:17:31 -070054 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000055
reed93a12152015-03-16 10:08:34 -070056 static const SkFilterQuality gQualitys[] = {
57 kNone_SkFilterQuality,
58 kLow_SkFilterQuality,
59 kMedium_SkFilterQuality,
60 kHigh_SkFilterQuality
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000061 };
62
63 SkPaint paint;
64 paint.setTextSize(48);
humperf75a1302015-01-29 10:26:37 -080065 paint.setSubpixelText(true);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000066
67 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070068 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
69 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000070 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
71 canvas->drawBitmapRect( fBM, r, &paint );
72 }
73
Cary Clark2a475ea2017-04-28 15:35:12 -040074 canvas->drawString( "AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint );
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000075
76 paint.setAntiAlias(false);
reed93a12152015-03-16 10:08:34 -070077 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
78 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000079 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
80 canvas->drawBitmapRect( fBM, r, &paint );
81 }
Cary Clark2a475ea2017-04-28 15:35:12 -040082 canvas->drawString( "Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000083
84 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070085 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
86 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000087 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
88 }
89
Cary Clark2a475ea2017-04-28 15:35:12 -040090 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.org1803f4eb2014-05-29 22:01:08 +000091
92 paint.setAntiAlias(false);
reed93a12152015-03-16 10:08:34 -070093 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
94 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000095 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
96 }
97
Cary Clark2a475ea2017-04-28 15:35:12 -040098 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.org1803f4eb2014-05-29 22:01:08 +000099
100
101 fCurPos.fX += fHorizontalVelocity;
102 fCurPos.fY += fVerticalVelocity;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000103 }
104
105private:
106 typedef SampleView INHERITED;
107};
108
109//////////////////////////////////////////////////////////////////////////////
110
Hal Canaryc465d132017-12-08 10:21:31 -0500111static SkView* MyFactory() { return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); }
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000112static SkViewRegister reg(MyFactory);