blob: 02993632cf92ea9200f2a86d25a243e1b28cff50 [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
8#include "gm.h"
tfarinabcbc1782014-06-18 14:32:48 -07009
10#include "Resources.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000011#include "SampleCode.h"
12#include "SkBlurMaskFilter.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000013#include "SkCanvas.h"
tfarinabcbc1782014-06-18 14:32:48 -070014#include "SkColorPriv.h"
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000015#include "SkImageDecoder.h"
16#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);
tfarinabcbc1782014-06-18 14:32:48 -070030 SkImageDecoder* codec = NULL;
31 SkFILEStream stream(resourcePath.c_str());
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000032 if (stream.isValid()) {
33 codec = SkImageDecoder::Factory(&stream);
34 }
35 if (codec) {
36 stream.rewind();
reedbfefc7c2014-06-12 17:40:00 -070037 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000038 SkDELETE(codec);
39 } else {
40 fBM.allocN32Pixels(1, 1);
41 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
42 }
43 fCurPos = SkPoint::Make(0,0);
44 fSize = 200;
45 }
46
47protected:
48 SkBitmap fBM;
reed@google.com687a26d2014-05-30 13:45:36 +000049 SkScalar fSize;
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000050 float fHorizontalVelocity, fVerticalVelocity;
51
52 SkPoint fCurPos;
53
54 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070055 bool onQuery(SkEvent* evt) override {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000056 if (SampleCode::TitleQ(*evt)) {
57 SampleCode::TitleR(evt, "SubpixelTranslate");
58 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000064
reed93a12152015-03-16 10:08:34 -070065 static const SkFilterQuality gQualitys[] = {
66 kNone_SkFilterQuality,
67 kLow_SkFilterQuality,
68 kMedium_SkFilterQuality,
69 kHigh_SkFilterQuality
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000070 };
71
72 SkPaint paint;
73 paint.setTextSize(48);
humperf75a1302015-01-29 10:26:37 -080074 paint.setSubpixelText(true);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000075
76 paint.setAntiAlias(true);
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, fSize );
80 canvas->drawBitmapRect( fBM, r, &paint );
81 }
82
reed93a12152015-03-16 10:08:34 -070083 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 +000084
85 paint.setAntiAlias(false);
reed93a12152015-03-16 10:08:34 -070086 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
87 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000088 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
89 canvas->drawBitmapRect( fBM, r, &paint );
90 }
reed93a12152015-03-16 10:08:34 -070091 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 +000092
93 paint.setAntiAlias(true);
reed93a12152015-03-16 10:08:34 -070094 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
95 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +000096 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
97 }
98
reed93a12152015-03-16 10:08:34 -070099 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 +0000100
101 paint.setAntiAlias(false);
reed93a12152015-03-16 10:08:34 -0700102 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
103 paint.setFilterQuality(gQualitys[i]);
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000104 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
105 }
106
reed93a12152015-03-16 10:08:34 -0700107 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 +0000108
109
110 fCurPos.fX += fHorizontalVelocity;
111 fCurPos.fY += fVerticalVelocity;
112 this->inval(NULL);
113 }
114
115private:
116 typedef SampleView INHERITED;
117};
118
119//////////////////////////////////////////////////////////////////////////////
120
reed@google.com687a26d2014-05-30 13:45:36 +0000121static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
commit-bot@chromium.org1803f4eb2014-05-29 22:01:08 +0000122static SkViewRegister reg(MyFactory);