• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Sample.h"
9 
10 #include "DecodeFile.h"
11 #include "Resources.h"
12 #include "SkBlurMaskFilter.h"
13 #include "SkCanvas.h"
14 #include "SkColorPriv.h"
15 #include "SkFont.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 
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 
onQuery(Sample::Event * evt)45     bool onQuery(Sample::Event* evt) override {
46         if (Sample::TitleQ(*evt)) {
47             Sample::TitleR(evt, "SubpixelTranslate");
48             return true;
49         }
50         return this->INHERITED::onQuery(evt);
51     }
52 
onDrawContent(SkCanvas * canvas)53     void onDrawContent(SkCanvas* canvas) override {
54 
55         static const SkFilterQuality gQualitys[] = {
56             kNone_SkFilterQuality,
57             kLow_SkFilterQuality,
58             kMedium_SkFilterQuality,
59             kHigh_SkFilterQuality
60         };
61 
62         SkPaint paint;
63         SkFont font(nullptr, 48);
64         font.setSubpixel(true);
65 
66         paint.setAntiAlias(true);
67         for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
68             paint.setFilterQuality(gQualitys[i]);
69             SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
70             canvas->drawBitmapRect( fBM, r, &paint );
71         }
72 
73         canvas->drawString("AA Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10),
74                            fCurPos.fY + fSize/2, font, paint);
75 
76         paint.setAntiAlias(false);
77         font.setEdging(SkFont::Edging::kAlias);
78         for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
79             paint.setFilterQuality(gQualitys[i]);
80             SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
81             canvas->drawBitmapRect( fBM, r, &paint );
82         }
83         canvas->drawString("Scaled", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10),
84                            fCurPos.fY + fSize + 10 + fSize/2, font, paint);
85 
86         paint.setAntiAlias(true);
87         font.setEdging(SkFont::Edging::kAntiAlias);
88         for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
89             paint.setFilterQuality(gQualitys[i]);
90             canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
91         }
92 
93         canvas->drawString("AA No Scale",
94                            fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10),
95                            fCurPos.fY + 2*(fSize + 10) + fSize/2, font, paint);
96 
97         paint.setAntiAlias(false);
98         font.setEdging(SkFont::Edging::kAlias);
99         for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
100             paint.setFilterQuality(gQualitys[i]);
101             canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
102         }
103 
104         canvas->drawString("No Scale", fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10),
105                            fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, font, paint);
106 
107 
108         fCurPos.fX += fHorizontalVelocity;
109         fCurPos.fY += fVerticalVelocity;
110     }
111 
112 private:
113     typedef Sample INHERITED;
114 };
115 
116 //////////////////////////////////////////////////////////////////////////////
117 
118 DEF_SAMPLE( return new SubpixelTranslateView("images/mandrill_256.png", .05f, .05f); )
119