blob: 3d8385f8a3d99dcdf3a3a077a7b0a10a9dbdc30e [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/utils/SkRandom.h"
15#include "samplecode/DecodeFile.h"
16#include "tools/Resources.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000017
18// Intended to exercise pixel snapping observed with scaled images (and
19// with non-scaled images, but for a different reason): Bug 1145
20
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040021class SubpixelTranslateView : public Sample {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000022public:
23 SubpixelTranslateView(const char imageFilename[],
24 float horizontalVelocity,
25 float verticalVelocity)
Mike Reed0933bc92017-12-09 01:27:41 +000026 : fHorizontalVelocity(horizontalVelocity)
27 , fVerticalVelocity(verticalVelocity)
28 {
29 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
30 fBM.allocN32Pixels(1, 1);
31 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
32 }
33 fCurPos = SkPoint::Make(0,0);
34 fSize = 200;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000035 }
36
37protected:
38 SkBitmap fBM;
reed@google.com687a26d2014-05-30 13:45:36 +000039 SkScalar fSize;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000040 float fHorizontalVelocity, fVerticalVelocity;
41
42 SkPoint fCurPos;
43
Hal Canary8a027312019-07-03 10:55:44 -040044 SkString name() override { return SkString("SubpixelTranslate"); }
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000045
mtklein36352bf2015-03-25 18:17:31 -070046 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000047
reed93a12152015-03-16 10:08:34 -070048 static const SkFilterQuality gQualitys[] = {
49 kNone_SkFilterQuality,
50 kLow_SkFilterQuality,
51 kMedium_SkFilterQuality,
52 kHigh_SkFilterQuality
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000053 };
54
55 SkPaint paint;
Hal Canary4484b8f2019-01-08 14:00:08 -050056 SkFont font(nullptr, 48);
57 font.setSubpixel(true);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000058
59 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070060 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
61 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000062 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
63 canvas->drawBitmapRect( fBM, r, &paint );
64 }
65
Hal Canary4484b8f2019-01-08 14:00:08 -050066 canvas->drawString("AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10),
67 fCurPos.fY + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000068
69 paint.setAntiAlias(false);
Hal Canary4484b8f2019-01-08 14:00:08 -050070 font.setEdging(SkFont::Edging::kAlias);
reed93a12152015-03-16 10:08:34 -070071 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
72 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000073 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
74 canvas->drawBitmapRect( fBM, r, &paint );
75 }
Hal Canary4484b8f2019-01-08 14:00:08 -050076 canvas->drawString("Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10),
77 fCurPos.fY + fSize + 10 + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000078
79 paint.setAntiAlias(true);
Hal Canary4484b8f2019-01-08 14:00:08 -050080 font.setEdging(SkFont::Edging::kAntiAlias);
reed93a12152015-03-16 10:08:34 -070081 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
82 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000083 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
84 }
85
Hal Canary4484b8f2019-01-08 14:00:08 -050086 canvas->drawString("AA No Scale",
87 fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10),
88 fCurPos.fY + 2*(fSize + 10) + fSize/2, font, paint);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000089
90 paint.setAntiAlias(false);
Hal Canary4484b8f2019-01-08 14:00:08 -050091 font.setEdging(SkFont::Edging::kAlias);
reed93a12152015-03-16 10:08:34 -070092 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
93 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000094 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
95 }
96
Hal Canary4484b8f2019-01-08 14:00:08 -050097 canvas->drawString("No Scale", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10),
98 fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, font, 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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400106 typedef Sample INHERITED;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000107};
108
109//////////////////////////////////////////////////////////////////////////////
110
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400111DEF_SAMPLE( return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); )