1/* 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" 9 10#include "Resources.h" 11#include "SampleCode.h" 12#include "SkBlurMaskFilter.h" 13#include "SkCanvas.h" 14#include "SkColorPriv.h" 15#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) 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); 34 } 35 if (codec) { 36 stream.rewind(); 37 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); 38 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; 49 SkScalar fSize; 50 float fHorizontalVelocity, fVerticalVelocity; 51 52 SkPoint fCurPos; 53 54 // overrides from SkEventSink 55 bool onQuery(SkEvent* evt) override { 56 if (SampleCode::TitleQ(*evt)) { 57 SampleCode::TitleR(evt, "SubpixelTranslate"); 58 return true; 59 } 60 return this->INHERITED::onQuery(evt); 61 } 62 63 void onDrawContent(SkCanvas* canvas) override { 64 65 static const SkFilterQuality gQualitys[] = { 66 kNone_SkFilterQuality, 67 kLow_SkFilterQuality, 68 kMedium_SkFilterQuality, 69 kHigh_SkFilterQuality 70 }; 71 72 SkPaint paint; 73 paint.setTextSize(48); 74 paint.setSubpixelText(true); 75 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 ); 81 } 82 83 canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint ); 84 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 ); 90 } 91 canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint ); 92 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 ); 97 } 98 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 ); 100 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 ); 105 } 106 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 ); 108 109 110 fCurPos.fX += fHorizontalVelocity; 111 fCurPos.fY += fVerticalVelocity; 112 this->inval(NULL); 113 } 114 115private: 116 typedef SampleView INHERITED; 117}; 118 119////////////////////////////////////////////////////////////////////////////// 120 121static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); } 122static SkViewRegister reg(MyFactory); 123