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

Side by Side Diff: gm/dcshader.cpp

Issue 816003002: Add device space skshader GM to test kDevice_GrCoordSet (Closed) Base URL: https://skia.googlesource.com/skia.git@kpos
Patch Set: add #if SK_SUPPORT_GPU, make FP a non-local class so it can be a template param in c++03. 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 | « no previous file | gyp/gmslides.gypi » ('j') | 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 /*
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 #if SK_SUPPORT_GPU
11 #include "GrFragmentProcessor.h"
12 #include "GrCoordTransform.h"
13 #include "gl/GrGLProcessor.h"
14 #include "gl/builders/GrGLProgramBuilder.h"
15 #include "Resources.h"
16 #include "SkShader.h"
17 #include "SkStream.h"
18 #include "SkTypeface.h"
19
20 namespace skiagm {
21
22 ///////////////////////////////////////////////////////////////////////////////
23
24 class DCShader : public SkShader {
25 public:
26 DCShader(const SkMatrix& matrix) : fDeviceMatrix(matrix) {}
27
28 Factory getFactory() const SK_OVERRIDE { return NULL; }
29
30 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v iewM,
31 const SkMatrix* localMatrix, GrColor* color,
32 GrFragmentProcessor** fp) const SK_OVERRIDE;
33 private:
34 const SkMatrix fDeviceMatrix;
35 };
36
37 class DCFP : public GrFragmentProcessor {
38 public:
39 DCFP(const SkMatrix& m) : fDeviceTransform(kDevice_GrCoordSet, m) {
40 this->addCoordTransform(&fDeviceTransform);
41 this->initClassID<DCFP>();
42 }
43
44 void getGLProcessorKey(const GrGLCaps& caps,
45 GrProcessorKeyBuilder* b) const SK_OVERRIDE {}
46
47 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE {
48 class DCGLFP : public GrGLFragmentProcessor {
49 void emitCode(GrGLFPBuilder* builder,
50 const GrFragmentProcessor& fp,
51 const char* outputColor,
52 const char* inputColor,
53 const TransformedCoordsArray& coords,
54 const TextureSamplerArray& samplers) {
55 GrGLFPFragmentBuilder* fpb = builder->getFragmentShaderBuilder() ;
56 fpb->codeAppendf("vec2 c = %s;", fpb->ensureFSCoords2D(coords, 0 ).c_str());
57 fpb->codeAppend("vec2 r = mod(c, vec2(20.0));");
58 fpb->codeAppend("vec4 color = vec4(0.5*sin(c.x / 15.0) + 0.5,"
59 "0.5*cos((c.x + c.y) / 15.0) + 0.5,"
60 "(r.x + r.y) / 20.0,"
61 "distance(r, vec2(15.0)) / 2 0.0 + 0.2);");
62 fpb->codeAppendf("color.rgb *= color.a;"
63 "%s = color * %s;",
64 outputColor, GrGLSLExpr4(inputColor).c_str() );
65 }
66 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O VERRIDE {}
67 };
68 return SkNEW(DCGLFP);
69 }
70
71 const char* name() const SK_OVERRIDE { return "DCFP"; }
72
73 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
74 inout->mulByUnknownFourComponents();
75 }
76
77 private:
78 bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { return true; }
79
80 GrCoordTransform fDeviceTransform;
81 };
82
83 bool DCShader::asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMat rix& viewM,
84 const SkMatrix* localMatrix, GrColor* color,
85 GrFragmentProcessor** fp) const {
86 *fp = SkNEW_ARGS(DCFP, (fDeviceMatrix));
87 *color = GrColorPackA4(paint.getAlpha());
88 return true;
89 }
90
91 class DCShaderGM : public GM {
92 public:
93 DCShaderGM() {
94 this->setBGColor(0xFFAABBCC);
95 }
96
97 ~DCShaderGM() SK_OVERRIDE {
98 for (int i = 0; i < fPrims.count(); ++i) {
99 SkDELETE(fPrims[i]);
100 }
101 }
102
103 protected:
104 uint32_t onGetFlags() const SK_OVERRIDE {
105 return kGPUOnly_Flag;
106 }
107
108 SkString onShortName() SK_OVERRIDE {
109 return SkString("dcshader");
110 }
111
112 SkISize onISize() SK_OVERRIDE { return SkISize::Make(1000, 900); }
113
114 void onOnceBeforeDraw() SK_OVERRIDE {
115 struct Rect : public Prim {
116 SkRect draw(SkCanvas* canvas, const SkPaint& paint) SK_OVERRIDE {
117 SkRect rect = SkRect::MakeXYWH(0, 0, 50, 50);
118 canvas->drawRect(rect, paint);
119 return rect;
120 }
121 };
122
123 struct Circle : public Prim {
124 SkRect draw(SkCanvas* canvas, const SkPaint& paint) SK_OVERRIDE {
125 static const SkScalar radius = 25;
126 canvas->drawCircle(radius, radius, radius, paint);
127 return SkRect::MakeXYWH(0, 0, 2 * radius, 2 * radius);
128 }
129 };
130
131 struct RRect : public Prim {
132 SkRect draw(SkCanvas* canvas, const SkPaint& paint) SK_OVERRIDE {
133 SkRRect rrect;
134 rrect.setRectXY(SkRect::MakeXYWH(0, 0, 50, 50), 10, 10);
135 canvas->drawRRect(rrect, paint);
136 return rrect.getBounds();
137 }
138 };
139
140 struct DRRect : public Prim {
141 SkRect draw(SkCanvas* canvas, const SkPaint& paint) SK_OVERRIDE {
142 SkRRect outerRRect;
143 outerRRect.setRectXY(SkRect::MakeXYWH(0, 0, 50, 50), 5, 5);
144 SkRRect innerRRect;
145 innerRRect.setRectXY(SkRect::MakeXYWH(5, 8, 35, 30), 8, 3);
146 canvas->drawDRRect(outerRRect, innerRRect, paint);
147 return outerRRect.getBounds();
148 }
149 };
150 struct Path : public Prim {
151 SkRect draw(SkCanvas* canvas, const SkPaint& paint) SK_OVERRIDE {
152 SkPath path;
153 path.addCircle(15, 15, 10);
154 path.addOval(SkRect::MakeXYWH(2, 2, 22, 37));
155 path.setFillType(SkPath::kEvenOdd_FillType);
156 canvas->drawPath(path, paint);
157 return path.getBounds();
158 }
159 };
160
161 struct Points : public Prim {
162 Points(SkCanvas::PointMode mode) : fMode(mode) {}
163
164 SkRect draw(SkCanvas* canvas, const SkPaint& paint) SK_OVERRIDE {
165 SkRandom random;
166 SkPoint points[500];
167 SkRect bounds = SkRect::MakeWH(50, 50);
168 int count = SkToInt(SK_ARRAY_COUNT(points));
169 if (SkCanvas::kPoints_PointMode != fMode) {
170 count = SkTMin(count, 10);
171 }
172 for (int p = 0; p < count; ++p) {
173 points[p].fX = random.nextUScalar1() * bounds.width();
174 points[p].fY = random.nextUScalar1() * bounds.width();
175 }
176 canvas->drawPoints(fMode, count, points, paint);
177 return bounds;
178 }
179 SkCanvas::PointMode fMode;
180 };
181
182 struct Text : public Prim {
183 SkRect draw(SkCanvas* canvas, const SkPaint& origPaint) SK_OVERRIDE {
184 SkPaint paint = origPaint;
185 paint.setTextSize(30.f);
186 this->setFont(&paint);
187 const char* text = this->text();
188 static const SkVector offset = SkVector::Make(10, 10);
189 canvas->drawText(text, strlen(text), offset.fX, offset.fY, paint );
190 SkRect bounds;
191 paint.measureText(text, strlen(text), &bounds);
192 bounds.offset(offset);
193 return bounds;
194 }
195
196 virtual void setFont(SkPaint* paint) {
197 sk_tool_utils::set_portable_typeface(paint);
198 }
199
200 virtual const char* text() const { return "Hello, Skia!"; }
201 };
202
203 struct BmpText : public Text {
204 void setFont(SkPaint* paint) SK_OVERRIDE {
205 if (!fTypeface) {
206 SkString filename = GetResourcePath("/Funkster.ttf");
207 SkAutoTUnref<SkFILEStream> stream(new SkFILEStream(filename. c_str()));
208 if (!stream->isValid()) {
209 SkDebugf("Could not find Funkster.ttf, please set --reso urcePath "
210 "correctly.\n");
211 return;
212 }
213 fTypeface.reset(SkTypeface::CreateFromStream(stream));
214 }
215 paint->setTypeface(fTypeface);
216 }
217
218 const char* text() const SK_OVERRIDE { return "Hi, Skia!"; }
219
220 SkAutoTUnref<SkTypeface> fTypeface;
221 };
222 fPrims.push_back(SkNEW(Rect));
223 fPrims.push_back(SkNEW(Circle));
224 fPrims.push_back(SkNEW(RRect));
225 fPrims.push_back(SkNEW(DRRect));
226 fPrims.push_back(SkNEW(Path));
227 fPrims.push_back(SkNEW(Points(SkCanvas::kPoints_PointMode)));
228 fPrims.push_back(SkNEW(Points(SkCanvas::kLines_PointMode)));
229 fPrims.push_back(SkNEW(Points(SkCanvas::kPolygon_PointMode)));
230 fPrims.push_back(SkNEW(Text));
231 fPrims.push_back(SkNEW(BmpText));
232 }
233
234 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
235 SkPaint paint;
236 SkTArray<SkMatrix> devMats;
237 devMats.push_back().reset();
238 devMats.push_back().setRotate(45, 500, 500);
239 devMats.push_back().setRotate(-30, 200, 200);
240 devMats.back().setPerspX(-SkScalarToPersp(SK_Scalar1 / 2000));
241 devMats.back().setPerspY(SkScalarToPersp(SK_Scalar1 / 1000));
242
243
244 SkTArray<SkMatrix> viewMats;
245 viewMats.push_back().setScale(0.75f, 0.75f);
246 viewMats.push_back().setRotate(45, 50, 50);
247 viewMats.back().postScale(0.5f, 1.1f);
248
249 canvas->translate(10, 20);
250 canvas->save();
251 SkScalar tx = 0, maxTy = 0;
252 static const SkScalar kW = 900;
253
254 for (int aa = 0; aa < 2; ++aa) {
255 for (int i = 0; i < fPrims.count(); ++i) {
256 for (int j = 0; j < devMats.count(); ++j) {
257 for (int k = 0; k < viewMats.count(); ++k) {
258 paint.setShader(SkNEW_ARGS(DCShader, (devMats[j])))->unr ef();
259 paint.setAntiAlias(SkToBool(aa));
260 canvas->save();
261 canvas->concat(viewMats[k]);
262 SkRect bounds = fPrims[i]->draw(canvas, paint);
263 canvas->restore();
264 viewMats[k].mapRect(&bounds);
265 // add margins
266 bounds.fRight += 20;
267 bounds.fBottom += 20;
268 canvas->translate(bounds.fRight, 0);
269 tx += bounds.fRight;
270 maxTy = SkTMax(bounds.fBottom, maxTy);
271 if (tx > kW) {
272 tx = 0;
273 canvas->restore();
274 canvas->translate(0, maxTy);
275 canvas->save();
276 maxTy = 0;
277 }
278 }
279 }
280 }
281 }
282 canvas->restore();
283 }
284
285 private:
286 struct Prim {
287 virtual ~Prim() {}
288 virtual SkRect draw(SkCanvas*, const SkPaint&) = 0;
289 };
290
291 SkTArray<Prim*> fPrims;
292
293 typedef GM INHERITED;
294 };
295
296 DEF_GM( return SkNEW(DCShaderGM); )
297 }
298 #endif
OLDNEW
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698