• 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 "DecodeFile.h"
9 #include "gm.h"
10 
11 #include "Resources.h"
12 #include "SampleCode.h"
13 #include "SkBlurMaskFilter.h"
14 #include "SkCanvas.h"
15 #include "SkColorPriv.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 SampleView {
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       SkString resourcePath = GetResourcePath(imageFilename);
30       if (!decode_file(resourcePath.c_str(), &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 
45     // overrides from SkEventSink
onQuery(SkEvent * evt)46     bool onQuery(SkEvent* evt) override {
47         if (SampleCode::TitleQ(*evt)) {
48             SampleCode::TitleR(evt, "SubpixelTranslate");
49             return true;
50         }
51         return this->INHERITED::onQuery(evt);
52     }
53 
onDrawContent(SkCanvas * canvas)54     void onDrawContent(SkCanvas* canvas) override {
55 
56         static const SkFilterQuality gQualitys[] = {
57             kNone_SkFilterQuality,
58             kLow_SkFilterQuality,
59             kMedium_SkFilterQuality,
60             kHigh_SkFilterQuality
61         };
62 
63         SkPaint paint;
64         paint.setTextSize(48);
65         paint.setSubpixelText(true);
66 
67         paint.setAntiAlias(true);
68         for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
69             paint.setFilterQuality(gQualitys[i]);
70             SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
71             canvas->drawBitmapRect( fBM, r, &paint );
72         }
73 
74         canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint );
75 
76         paint.setAntiAlias(false);
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 + 10, fSize, fSize );
80             canvas->drawBitmapRect( fBM, r, &paint );
81         }
82         canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
83 
84         paint.setAntiAlias(true);
85         for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
86             paint.setFilterQuality(gQualitys[i]);
87             canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
88         }
89 
90         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 );
91 
92         paint.setAntiAlias(false);
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->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 );
99 
100 
101         fCurPos.fX += fHorizontalVelocity;
102         fCurPos.fY += fVerticalVelocity;
103         this->inval(nullptr);
104     }
105 
106 private:
107     typedef SampleView INHERITED;
108 };
109 
110 //////////////////////////////////////////////////////////////////////////////
111 
MyFactory()112 static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
113 static SkViewRegister reg(MyFactory);
114