blob: 4652bc37adde15890e281183f3da0bbc2eea869f [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
8#include "gm.h"
tfarinabcbc1782014-06-18 14:32:48 -07009
10#include "Resources.h"
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000011#include "SampleCode.h"
12#include "SkBlurMaskFilter.h"
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +000013#include "SkCanvas.h"
tfarinabcbc1782014-06-18 14:32:48 -070014#include "SkColorPriv.h"
commit-bot@chromium.org1803f4e2014-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.org1803f4e2014-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.org1803f4e2014-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.org1803f4e2014-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.org1803f4e2014-05-29 22:01:08 +000050 float fHorizontalVelocity, fVerticalVelocity;
51
52 SkPoint fCurPos;
53
54 // overrides from SkEventSink
55 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
56 if (SampleCode::TitleQ(*evt)) {
57 SampleCode::TitleR(evt, "SubpixelTranslate");
58 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
62
63 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
64
65 static const SkPaint::FilterLevel gLevels[] = {
66 SkPaint::kNone_FilterLevel,
67 SkPaint::kLow_FilterLevel,
68 SkPaint::kMedium_FilterLevel,
69 SkPaint::kHigh_FilterLevel
70 };
71
72 SkPaint paint;
73 paint.setTextSize(48);
74
75 paint.setAntiAlias(true);
76 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
77 paint.setFilterLevel(gLevels[i]);
78 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
79 canvas->drawBitmapRect( fBM, r, &paint );
80 }
81
82 canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fSize + 10), fCurPos.fY + fSize/2, paint );
83
84 paint.setAntiAlias(false);
85 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
86 paint.setFilterLevel(gLevels[i]);
87 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
88 canvas->drawBitmapRect( fBM, r, &paint );
89 }
90 canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
91
92 paint.setAntiAlias(true);
93 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
94 paint.setFilterLevel(gLevels[i]);
95 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
96 }
97
98 canvas->drawText( "AA No Scale", strlen("AA No Scale"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fSize/2, paint );
99
100 paint.setAntiAlias(false);
101 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) {
102 paint.setFilterLevel(gLevels[i]);
103 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
104 }
105
106 canvas->drawText( "No Scale", strlen("No Scale"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, paint );
107
108
109 fCurPos.fX += fHorizontalVelocity;
110 fCurPos.fY += fVerticalVelocity;
111 this->inval(NULL);
112 }
113
114private:
115 typedef SampleView INHERITED;
116};
117
118//////////////////////////////////////////////////////////////////////////////
119
reed@google.com687a26d2014-05-30 13:45:36 +0000120static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
commit-bot@chromium.org1803f4e2014-05-29 22:01:08 +0000121static SkViewRegister reg(MyFactory);