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