2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "Resources.h"
11 #include "SampleCode.h"
12 #include "SkBlurMaskFilter.h"
14 #include "SkColorPriv.h"
15 #include "SkImageDecoder.h"
19 // Intended to exercise pixel snapping observed with scaled images (and
20 // with non-scaled images, but for a different reason): Bug 1145
22 class SubpixelTranslateView : public SampleView {
24 SubpixelTranslateView(const char imageFilename[],
25 float horizontalVelocity,
26 float verticalVelocity)
27 : fHorizontalVelocity(horizontalVelocity),
28 fVerticalVelocity(verticalVelocity) {
29 SkString resourcePath = GetResourcePath(imageFilename);
30 SkImageDecoder* codec = NULL;
31 SkFILEStream stream(resourcePath.c_str());
32 if (stream.isValid()) {
33 codec = SkImageDecoder::Factory(&stream);
37 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
40 fBM.allocN32Pixels(1, 1);
41 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
43 fCurPos = SkPoint::Make(0,0);
50 float fHorizontalVelocity, fVerticalVelocity;
54 // overrides from SkEventSink
55 bool onQuery(SkEvent* evt) SK_OVERRIDE {
56 if (SampleCode::TitleQ(*evt)) {
57 SampleCode::TitleR(evt, "SubpixelTranslate");
60 return this->INHERITED::onQuery(evt);
63 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
65 static const SkFilterQuality gQualitys[] = {
66 kNone_SkFilterQuality,
68 kMedium_SkFilterQuality,
73 paint.setTextSize(48);
74 paint.setSubpixelText(true);
76 paint.setAntiAlias(true);
77 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
78 paint.setFilterQuality(gQualitys[i]);
79 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
80 canvas->drawBitmapRect( fBM, r, &paint );
83 canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint );
85 paint.setAntiAlias(false);
86 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
87 paint.setFilterQuality(gQualitys[i]);
88 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
89 canvas->drawBitmapRect( fBM, r, &paint );
91 canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
93 paint.setAntiAlias(true);
94 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
95 paint.setFilterQuality(gQualitys[i]);
96 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
99 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 );
101 paint.setAntiAlias(false);
102 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
103 paint.setFilterQuality(gQualitys[i]);
104 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
107 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 );
110 fCurPos.fX += fHorizontalVelocity;
111 fCurPos.fY += fVerticalVelocity;
116 typedef SampleView INHERITED;
119 //////////////////////////////////////////////////////////////////////////////
121 static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
122 static SkViewRegister reg(MyFactory);