blob: b658bc3f51ff13823a8e99980009be93801aadd6 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "samplecode/Sample.h"
Hal Canary4484b8f2019-01-08 14:00:08 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkStream.h"
14#include "include/effects/SkBlurMaskFilter.h"
15#include "include/utils/SkRandom.h"
16#include "samplecode/DecodeFile.h"
17#include "tools/Resources.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000018
19// Intended to exercise pixel snapping observed with scaled images (and
20// with non-scaled images, but for a different reason): Bug 1145
21
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040022class SubpixelTranslateView : public Sample {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000023public:
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
Hal Canary8a027312019-07-03 10:55:44 -040045 SkString name() override { return SkString("SubpixelTranslate"); }
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000046
mtklein36352bf2015-03-25 18:17:31 -070047 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000048
reed93a12152015-03-16 10:08:34 -070049 static const SkFilterQuality gQualitys[] = {
50 kNone_SkFilterQuality,
51 kLow_SkFilterQuality,
52 kMedium_SkFilterQuality,
53 kHigh_SkFilterQuality
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000054 };
55
56 SkPaint paint;
Hal Canary4484b8f2019-01-08 14:00:08 -050057 SkFont font(nullptr, 48);
58 font.setSubpixel(true);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000059
60 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070061 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
62 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000063 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
64 canvas->drawBitmapRect( fBM, r, &paint );
65 }
66
Hal Canary4484b8f2019-01-08 14:00:08 -050067 canvas->drawString("AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10),
68 fCurPos.fY + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000069
70 paint.setAntiAlias(false);
Hal Canary4484b8f2019-01-08 14:00:08 -050071 font.setEdging(SkFont::Edging::kAlias);
reed93a12152015-03-16 10:08:34 -070072 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
73 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000074 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
75 canvas->drawBitmapRect( fBM, r, &paint );
76 }
Hal Canary4484b8f2019-01-08 14:00:08 -050077 canvas->drawString("Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10),
78 fCurPos.fY + fSize + 10 + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000079
80 paint.setAntiAlias(true);
Hal Canary4484b8f2019-01-08 14:00:08 -050081 font.setEdging(SkFont::Edging::kAntiAlias);
reed93a12152015-03-16 10:08:34 -070082 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
83 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000084 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
85 }
86
Hal Canary4484b8f2019-01-08 14:00:08 -050087 canvas->drawString("AA No Scale",
88 fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10),
89 fCurPos.fY + 2*(fSize + 10) + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000090
91 paint.setAntiAlias(false);
Hal Canary4484b8f2019-01-08 14:00:08 -050092 font.setEdging(SkFont::Edging::kAlias);
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
Hal Canary4484b8f2019-01-08 14:00:08 -050098 canvas->drawString("No Scale", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10),
99 fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000100
101
102 fCurPos.fX += fHorizontalVelocity;
103 fCurPos.fY += fVerticalVelocity;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000104 }
105
106private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400107 typedef Sample INHERITED;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000108};
109
110//////////////////////////////////////////////////////////////////////////////
111
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400112DEF_SAMPLE( return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); )