blob: d39412790307f86326ea0a2a57744298af0973e9 [file] [log] [blame]
commit-bot@chromium.org1803f4e2014-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"
tfarinabcbc1782014-06-18 14:32:48 -07009#include "Resources.h"
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040010#include "Sample.h"
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000011#include "SkBlurMaskFilter.h"
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000012#include "SkCanvas.h"
tfarinabcbc1782014-06-18 14:32:48 -070013#include "SkColorPriv.h"
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000014#include "SkRandom.h"
15#include "SkStream.h"
16
17// Intended to exercise pixel snapping observed with scaled images (and
18// with non-scaled images, but for a different reason): Bug 1145
19
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040020class SubpixelTranslateView : public Sample {
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000021public:
22 SubpixelTranslateView(const char imageFilename[],
23 float horizontalVelocity,
24 float verticalVelocity)
Mike Reed0933bc92017-12-09 01:27:41 +000025 : fHorizontalVelocity(horizontalVelocity)
26 , fVerticalVelocity(verticalVelocity)
27 {
28 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
29 fBM.allocN32Pixels(1, 1);
30 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
31 }
32 fCurPos = SkPoint::Make(0,0);
33 fSize = 200;
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000034 }
35
36protected:
37 SkBitmap fBM;
reed@google.com687a26d2014-05-30 13:45:36 +000038 SkScalar fSize;
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000039 float fHorizontalVelocity, fVerticalVelocity;
40
41 SkPoint fCurPos;
42
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040043 bool onQuery(Sample::Event* evt) override {
44 if (Sample::TitleQ(*evt)) {
45 Sample::TitleR(evt, "SubpixelTranslate");
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000046 return true;
47 }
48 return this->INHERITED::onQuery(evt);
49 }
50
mtklein36352bf2015-03-25 18:17:31 -070051 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000052
reed93a12152015-03-16 10:08:34 -070053 static const SkFilterQuality gQualitys[] = {
54 kNone_SkFilterQuality,
55 kLow_SkFilterQuality,
56 kMedium_SkFilterQuality,
57 kHigh_SkFilterQuality
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000058 };
59
60 SkPaint paint;
61 paint.setTextSize(48);
humperf75a1302015-01-29 10:26:37 -080062 paint.setSubpixelText(true);
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000063
64 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070065 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
66 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000067 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
68 canvas->drawBitmapRect( fBM, r, &paint );
69 }
70
Cary Clark2a475ea2017-04-28 15:35:12 -040071 canvas->drawString( "AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint );
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000072
73 paint.setAntiAlias(false);
reed93a12152015-03-16 10:08:34 -070074 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
75 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000076 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
77 canvas->drawBitmapRect( fBM, r, &paint );
78 }
Cary Clark2a475ea2017-04-28 15:35:12 -040079 canvas->drawString( "Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000080
81 paint.setAntiAlias(true);
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.org1803f4e2014-05-29 22:01:08 +000084 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
85 }
86
Cary Clark2a475ea2017-04-28 15:35:12 -040087 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.org1803f4e2014-05-29 22:01:08 +000088
89 paint.setAntiAlias(false);
reed93a12152015-03-16 10:08:34 -070090 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
91 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000092 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
93 }
94
Cary Clark2a475ea2017-04-28 15:35:12 -040095 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.org1803f4e2014-05-29 22:01:08 +000096
97
98 fCurPos.fX += fHorizontalVelocity;
99 fCurPos.fY += fVerticalVelocity;
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +0000100 }
101
102private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400103 typedef Sample INHERITED;
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +0000104};
105
106//////////////////////////////////////////////////////////////////////////////
107
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108DEF_SAMPLE( return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); )