Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: samplecode/SampleFilterQuality.cpp

Issue 788163007: add sample for filter quality (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « gyp/SampleApp.gyp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 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 "SkCanvas.h"
13 #include "SkInterpolator.h"
14 #include "SkSurface.h"
15 #include "SkRandom.h"
16 #include "SkTime.h"
17
18 #define N 128
19
20 static SkImage* make_image() {
21 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
22 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
23 SkCanvas* canvas = surface->getCanvas();
24
25 SkPath path;
26 path.setFillType(SkPath::kEvenOdd_FillType);
27
28 path.addRect(SkRect::MakeWH(N/2, N));
29 path.addRect(SkRect::MakeWH(N, N/2));
30 path.moveTo(0, 0); path.lineTo(N, 0); path.lineTo(0, N); path.close();
31
32 canvas->drawPath(path, SkPaint());
33 return surface->newImageSnapshot();
34 }
35
36 struct AnimValue {
37 SkScalar fValue;
38 SkScalar fMin;
39 SkScalar fMax;
40 SkScalar fMod;
41
42 operator SkScalar() const { return fValue; }
43
44 void set(SkScalar value, SkScalar min, SkScalar max) {
45 fValue = value;
46 fMin = min;
47 fMax = max;
48 fMod = 0;
49 }
50
51 void setMod(SkScalar value, SkScalar mod) {
52 fValue = value;
53 fMin = 0;
54 fMax = 0;
55 fMod = mod;
56 }
57
58 SkScalar inc(SkScalar delta) {
59 fValue += delta;
60 return this->fixUp();
61 }
62
63 SkScalar fixUp() {
64 if (fMod) {
65 fValue = SkScalarMod(fValue, fMod);
66 } else {
67 if (fValue > fMax) {
68 fValue = fMax;
69 } else if (fValue < fMin) {
70 fValue = fMin;
71 }
72 }
73 return fValue;
74 }
75 };
76
77 #define ANGLE_DELTA 3
78 #define SCALE_DELTA (SK_Scalar1 / 32)
79
80 class FilterQualityView : public SampleView {
81 SkAutoTUnref<SkImage> fImage;
82 AnimValue fScale, fAngle;
83
84 SkInterpolator fTrans;
85
86 public:
87 FilterQualityView() : fImage(make_image()), fTrans(2, 2) {
88 fScale.set(1, SK_Scalar1 / 8, 1);
89 fAngle.setMod(0, 360);
90
91 SkScalar values[2];
92 fTrans.setMirror(true);
93 fTrans.setReset(true);
94
95 fTrans.setRepeatCount(999);
96 values[0] = values[1] = 0;
97 fTrans.setKeyFrame(0, SkTime::GetMSecs(), values);
98 values[0] = values[1] = 1;
99 fTrans.setKeyFrame(1, SkTime::GetMSecs() + 2000, values);
100 }
101
102 protected:
103
104 // overrides from SkEventSink
105 bool onQuery(SkEvent* evt) SK_OVERRIDE {
106 if (SampleCode::TitleQ(*evt)) {
107 SampleCode::TitleR(evt, "FilterQuality");
108 return true;
109 }
110 SkUnichar uni;
111 if (SampleCode::CharQ(*evt, &uni)) {
112 switch (uni) {
113 case '1': fAngle.inc(-ANGLE_DELTA); this->inval(NULL); return tr ue;
114 case '2': fAngle.inc( ANGLE_DELTA); this->inval(NULL); return tr ue;
115 case '3': fScale.inc(-SCALE_DELTA); this->inval(NULL); return tr ue;
116 case '4': fScale.inc( SCALE_DELTA); this->inval(NULL); return tr ue;
117 default: break;
118 }
119 }
120 return this->INHERITED::onQuery(evt);
121 }
122
123 void drawHere(SkCanvas* canvas, SkScalar x, SkScalar y, SkPaint::FilterLevel filter) {
124 SkAutoCanvasRestore acr(canvas, true);
125 SkPaint paint;
126 paint.setAntiAlias(true);
127 paint.setFilterLevel(filter);
128
129 canvas->translate(x, y);
130 canvas->scale(fScale, fScale);
131 canvas->rotate(fAngle);
132 canvas->drawImage(fImage, -SkScalarHalf(fImage->width()), -SkScalarHalf( fImage->height()),
133 &paint);
134 }
135
136 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
137 SkScalar trans[2];
138 fTrans.timeToValues(SkTime::GetMSecs(), trans);
139 canvas->translate(trans[0], trans[1]);
140 this->inval(NULL);
141
142 const struct {
143 SkScalar fX;
144 SkScalar fY;
145 SkPaint::FilterLevel fFilter;
146 } rec[] = {
147 { 100, 100, SkPaint::kNone_FilterLevel },
148 { 300, 100, SkPaint::kLow_FilterLevel },
149 { 100, 300, SkPaint::kMedium_FilterLevel },
150 { 300, 300, SkPaint::kHigh_FilterLevel },
151 };
152
153 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
154 this->drawHere(canvas, rec[i].fX, rec[i].fY, rec[i].fFilter);
155 }
156
157 SkPaint paint;
158 paint.setAntiAlias(true);
159 paint.setTextSize(40);
160 SkString str;
161 str.appendScalar(fScale);
162 canvas->drawText(str.c_str(), str.size(), 450, 100, paint);
163 str.reset(); str.appendScalar(fAngle);
164 canvas->drawText(str.c_str(), str.size(), 450, 150, paint);
165
166 str.reset(); str.appendScalar(trans[0]);
167 canvas->drawText(str.c_str(), str.size(), 450, 200, paint);
168 str.reset(); str.appendScalar(trans[1]);
169 canvas->drawText(str.c_str(), str.size(), 450, 250, paint);
170 }
171
172 virtual bool handleKey(SkKey key) {
173 this->inval(NULL);
174 return true;
175 }
176
177 private:
178 typedef SampleView INHERITED;
179 };
180
181 //////////////////////////////////////////////////////////////////////////////
182
183 static SkView* MyFactory() { return new FilterQualityView; }
184 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « gyp/SampleApp.gyp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698