blob: 3bb90562fa9ae6206b14858a1f346bf5b4b32f08 [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)
tfarinac846f4a2014-07-01 12:35:49 -070027 : fHorizontalVelocity(horizontalVelocity),
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000028 fVerticalVelocity(verticalVelocity) {
tfarinac846f4a2014-07-01 12:35:49 -070029 SkString resourcePath = GetResourcePath(imageFilename);
msarettd15750c2016-03-18 15:48:49 -070030 if (!decode_file(resourcePath.c_str(), &fBM)) {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000031 fBM.allocN32Pixels(1, 1);
32 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
33 }
34 fCurPos = SkPoint::Make(0,0);
35 fSize = 200;
36 }
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
reed93a12152015-03-16 10:08:34 -070074 canvas->drawText( "AA Scaled", strlen("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 }
reed93a12152015-03-16 10:08:34 -070082 canvas->drawText( "Scaled", strlen("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
reed93a12152015-03-16 10:08:34 -070090 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.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
reed93a12152015-03-16 10:08:34 -070098 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.org1803f4eb2014-05-29 22:01:08 +000099
100
101 fCurPos.fX += fHorizontalVelocity;
102 fCurPos.fY += fVerticalVelocity;
halcanary96fcdcc2015-08-27 07:41:13 -0700103 this->inval(nullptr);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000104 }
105
106private:
107 typedef SampleView INHERITED;
108};
109
110//////////////////////////////////////////////////////////////////////////////
111
reed@google.com687a26d2014-05-30 13:45:36 +0000112static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000113static SkViewRegister reg(MyFactory);