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 "samplecode/Sample.h" 9 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColorPriv.h" 12 #include "include/core/SkFont.h" 13 #include "include/core/SkStream.h" 14 #include "include/effects/SkBlurMaskFilter.h" 15 #include "include/utils/SkRandom.h" 16 #include "samplecode/DecodeFile.h" 17 #include "tools/Resources.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 22 class SubpixelTranslateView : public Sample { 23 public: SubpixelTranslateView(const char imageFilename[],float horizontalVelocity,float verticalVelocity)24 SubpixelTranslateView(const char imageFilename[], 25 float horizontalVelocity, 26 float verticalVelocity) 27 : fHorizontalVelocity(horizontalVelocity) 28 , fVerticalVelocity(verticalVelocity) 29 { 30 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) { 31 fBM.allocN32Pixels(1, 1); 32 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 33 } 34 fCurPos = SkPoint::Make(0,0); 35 fSize = 200; 36 } 37 38 protected: 39 SkBitmap fBM; 40 SkScalar fSize; 41 float fHorizontalVelocity, fVerticalVelocity; 42 43 SkPoint fCurPos; 44 name()45 SkString name() override { return SkString("SubpixelTranslate"); } 46 onDrawContent(SkCanvas * canvas)47 void onDrawContent(SkCanvas* canvas) override { 48 49 static const SkFilterQuality gQualitys[] = { 50 kNone_SkFilterQuality, 51 kLow_SkFilterQuality, 52 kMedium_SkFilterQuality, 53 kHigh_SkFilterQuality 54 }; 55 56 SkPaint paint; 57 SkFont font(nullptr, 48); 58 font.setSubpixel(true); 59 60 paint.setAntiAlias(true); 61 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { 62 paint.setFilterQuality(gQualitys[i]); 63 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize ); 64 canvas->drawBitmapRect( fBM, r, &paint ); 65 } 66 67 canvas->drawString("AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), 68 fCurPos.fY + fSize/2, font, paint); 69 70 paint.setAntiAlias(false); 71 font.setEdging(SkFont::Edging::kAlias); 72 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { 73 paint.setFilterQuality(gQualitys[i]); 74 SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize ); 75 canvas->drawBitmapRect( fBM, r, &paint ); 76 } 77 canvas->drawString("Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), 78 fCurPos.fY + fSize + 10 + fSize/2, font, paint); 79 80 paint.setAntiAlias(true); 81 font.setEdging(SkFont::Edging::kAntiAlias); 82 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { 83 paint.setFilterQuality(gQualitys[i]); 84 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint ); 85 } 86 87 canvas->drawString("AA No Scale", 88 fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10), 89 fCurPos.fY + 2*(fSize + 10) + fSize/2, font, paint); 90 91 paint.setAntiAlias(false); 92 font.setEdging(SkFont::Edging::kAlias); 93 for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) { 94 paint.setFilterQuality(gQualitys[i]); 95 canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint ); 96 } 97 98 canvas->drawString("No Scale", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10), 99 fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, font, paint); 100 101 102 fCurPos.fX += fHorizontalVelocity; 103 fCurPos.fY += fVerticalVelocity; 104 } 105 106 private: 107 typedef Sample INHERITED; 108 }; 109 110 ////////////////////////////////////////////////////////////////////////////// 111 112 DEF_SAMPLE( return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); ) 113