SampleSubpixelTranslate.cpp revision 1803f4eb341dd0209dde8cc7ee0fa4b409fb2704
1 2/* 3 * Copyright 2014 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9#include "gm.h" 10#include "SampleCode.h" 11#include "SkBlurMaskFilter.h" 12#include "SkColorPriv.h" 13#include "SkCanvas.h" 14#include "SkImageDecoder.h" 15#include "SkRandom.h" 16#include "SkStream.h" 17 18// Intended to exercise pixel snapping observed with scaled images (and 19// with non-scaled images, but for a different reason): Bug 1145 20 21class SubpixelTranslateView : public SampleView { 22public: 23 SubpixelTranslateView(const char imageFilename[], 24 float horizontalVelocity, 25 float verticalVelocity) 26 : fFilename(imageFilename), 27 fHorizontalVelocity(horizontalVelocity), 28 fVerticalVelocity(verticalVelocity) { 29 SkString path(skiagm::GM::GetResourcePath()); 30 path.append("/"); 31 path.append(fFilename); 32 33 SkImageDecoder *codec = NULL; 34 SkFILEStream stream(path.c_str()); 35 if (stream.isValid()) { 36 codec = SkImageDecoder::Factory(&stream); 37 } 38 if (codec) { 39 stream.rewind(); 40 codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config, 41 SkImageDecoder::kDecodePixels_Mode); 42 SkDELETE(codec); 43 } else { 44 fBM.allocN32Pixels(1, 1); 45 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 46 } 47 fCurPos = SkPoint::Make(0,0); 48 fSize = 200; 49 } 50 51protected: 52 SkBitmap fBM; 53 SkString fFilename; 54 int fSize; 55 float fHorizontalVelocity, fVerticalVelocity; 56 57 SkPoint fCurPos; 58 59 // overrides from SkEventSink 60 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { 61 if (SampleCode::TitleQ(*evt)) { 62 SampleCode::TitleR(evt, "SubpixelTranslate"); 63 return true; 64 } 65 return this->INHERITED::onQuery(evt); 66 } 67 68 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE { 69 70 static const SkPaint::FilterLevel gLevels[] = { 71 SkPaint::kNone_FilterLevel, 72 SkPaint::kLow_FilterLevel, 73 SkPaint::kMedium_FilterLevel, 74 SkPaint::kHigh_FilterLevel 75 }; 76 77 SkPaint paint; 78 paint.setTextSize(48); 79 80 paint.setAntiAlias(true); 81 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { 82 paint.setFilterLevel(gLevels[i]); 83 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize ); 84 canvas->drawBitmapRect( fBM, r, &paint ); 85 } 86 87 canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fSize + 10), fCurPos.fY + fSize/2, paint ); 88 89 paint.setAntiAlias(false); 90 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { 91 paint.setFilterLevel(gLevels[i]); 92 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize ); 93 canvas->drawBitmapRect( fBM, r, &paint ); 94 } 95 canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gLevels) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint ); 96 97 paint.setAntiAlias(true); 98 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { 99 paint.setFilterLevel(gLevels[i]); 100 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint ); 101 } 102 103 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 ); 104 105 paint.setAntiAlias(false); 106 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevels); ++i) { 107 paint.setFilterLevel(gLevels[i]); 108 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint ); 109 } 110 111 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 ); 112 113 114 fCurPos.fX += fHorizontalVelocity; 115 fCurPos.fY += fVerticalVelocity; 116 this->inval(NULL); 117 } 118 119private: 120 typedef SampleView INHERITED; 121}; 122 123////////////////////////////////////////////////////////////////////////////// 124 125static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05, .05); } 126static SkViewRegister reg(MyFactory); 127