blob: e402fc0c2b7ee69ea62749a2806b9383e5ee1d80 [file] [log] [blame]
bsalomonb0ae6492014-12-29 07:05:27 -08001/*
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 "gm.h"
9#if SK_SUPPORT_GPU
10#include "GrFragmentProcessor.h"
11#include "GrCoordTransform.h"
bsalomon87ba62e2015-09-22 06:41:59 -070012#include "effects/GrXfermodeFragmentProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080013#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080014#include "glsl/GrGLSLFragmentShaderBuilder.h"
15#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070016#include "glsl/GrGLSLProgramDataManager.h"
bsalomonb0ae6492014-12-29 07:05:27 -080017#include "Resources.h"
mtklein36a364a2015-01-07 08:02:28 -080018#include "SkReadBuffer.h"
bsalomonb0ae6492014-12-29 07:05:27 -080019#include "SkShader.h"
20#include "SkStream.h"
21#include "SkTypeface.h"
mtklein36a364a2015-01-07 08:02:28 -080022#include "SkWriteBuffer.h"
bsalomonb0ae6492014-12-29 07:05:27 -080023
24namespace skiagm {
25
26///////////////////////////////////////////////////////////////////////////////
27
28class DCShader : public SkShader {
29public:
30 DCShader(const SkMatrix& matrix) : fDeviceMatrix(matrix) {}
31
mtklein36a364a2015-01-07 08:02:28 -080032 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DCShader);
33
mtklein36352bf2015-03-25 18:17:31 -070034 void flatten(SkWriteBuffer& buf) const override {
mtklein36a364a2015-01-07 08:02:28 -080035 buf.writeMatrix(fDeviceMatrix);
36 }
bsalomonb0ae6492014-12-29 07:05:27 -080037
bsalomonc21b09e2015-08-28 18:46:56 -070038 const GrFragmentProcessor* asFragmentProcessor(GrContext*,
39 const SkMatrix& viewM,
40 const SkMatrix* localMatrix,
bsalomon4a339522015-10-06 08:40:50 -070041 SkFilterQuality) const override;
robertphillips42dbfa82015-01-26 06:08:52 -080042
43#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -070044 void toString(SkString* str) const override {
robertphillips42dbfa82015-01-26 06:08:52 -080045 str->appendf("DCShader: ()");
46 }
47#endif
48
bsalomonb0ae6492014-12-29 07:05:27 -080049private:
50 const SkMatrix fDeviceMatrix;
51};
52
reed60c9b582016-04-03 09:11:13 -070053sk_sp<SkFlattenable> DCShader::CreateProc(SkReadBuffer& buf) {
mtklein36a364a2015-01-07 08:02:28 -080054 SkMatrix matrix;
55 buf.readMatrix(&matrix);
reed60c9b582016-04-03 09:11:13 -070056 return sk_make_sp<DCShader>(matrix);
mtklein36a364a2015-01-07 08:02:28 -080057}
58
bsalomonb0ae6492014-12-29 07:05:27 -080059class DCFP : public GrFragmentProcessor {
60public:
bsalomon4a339522015-10-06 08:40:50 -070061 DCFP(const SkMatrix& m) : fDeviceTransform(kDevice_GrCoordSet, m) {
bsalomonb0ae6492014-12-29 07:05:27 -080062 this->addCoordTransform(&fDeviceTransform);
63 this->initClassID<DCFP>();
64 }
65
egdaniel57d3b032015-11-13 11:57:27 -080066 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
egdaniel64c47282015-11-13 06:54:19 -080067 class DCGLFP : public GrGLSLFragmentProcessor {
wangyix7c157a92015-07-22 15:08:53 -070068 void emitCode(EmitArgs& args) override {
egdaniel4ca2e602015-11-18 08:01:26 -080069 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
70 fragBuilder->codeAppendf("vec2 c = %s;",
71 fragBuilder->ensureFSCoords2D(args.fCoords, 0).c_str());
72 fragBuilder->codeAppend("vec2 r = mod(c, vec2(20.0));");
73 fragBuilder->codeAppend("vec4 color = vec4(0.5*sin(c.x / 15.0) + 0.5,"
74 "0.5*cos((c.x + c.y) / 15.0) + 0.5,"
75 "(r.x + r.y) / 20.0,"
76 "distance(r, vec2(15.0)) / 20.0 + 0.2);");
77 fragBuilder->codeAppendf("color.rgb *= color.a;"
78 "%s = color * %s;",
79 args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_str());
bsalomonb0ae6492014-12-29 07:05:27 -080080 }
egdaniel018fb622015-10-28 07:26:40 -070081 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override {}
bsalomonb0ae6492014-12-29 07:05:27 -080082 };
halcanary385fe4d2015-08-26 13:07:48 -070083 return new DCGLFP;
bsalomonb0ae6492014-12-29 07:05:27 -080084 }
85
mtklein36352bf2015-03-25 18:17:31 -070086 const char* name() const override { return "DCFP"; }
bsalomonb0ae6492014-12-29 07:05:27 -080087
mtklein36352bf2015-03-25 18:17:31 -070088 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
bsalomonb0ae6492014-12-29 07:05:27 -080089 inout->mulByUnknownFourComponents();
90 }
91
92private:
egdaniel57d3b032015-11-13 11:57:27 -080093 void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
94 GrProcessorKeyBuilder* b) const override {}
wangyix4b3050b2015-08-04 07:59:37 -070095
mtklein36352bf2015-03-25 18:17:31 -070096 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
bsalomonb0ae6492014-12-29 07:05:27 -080097
98 GrCoordTransform fDeviceTransform;
99};
100
bsalomon4a339522015-10-06 08:40:50 -0700101const GrFragmentProcessor* DCShader::asFragmentProcessor(GrContext*,
102 const SkMatrix& viewM,
103 const SkMatrix* localMatrix,
104 SkFilterQuality) const {
105 SkAutoTUnref<const GrFragmentProcessor> inner(new DCFP(fDeviceMatrix));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700106 return GrFragmentProcessor::MulOutputByInputAlpha(inner);
bsalomonb0ae6492014-12-29 07:05:27 -0800107}
108
109class DCShaderGM : public GM {
110public:
111 DCShaderGM() {
caryclark65cdba62015-06-15 06:51:08 -0700112 this->setBGColor(sk_tool_utils::color_to_565(0xFFAABBCC));
bsalomonb0ae6492014-12-29 07:05:27 -0800113 }
114
mtklein36352bf2015-03-25 18:17:31 -0700115 ~DCShaderGM() override {
bsalomonb0ae6492014-12-29 07:05:27 -0800116 for (int i = 0; i < fPrims.count(); ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700117 delete fPrims[i];
bsalomonb0ae6492014-12-29 07:05:27 -0800118 }
119 }
120
121protected:
bsalomonb0ae6492014-12-29 07:05:27 -0800122
mtklein36352bf2015-03-25 18:17:31 -0700123 SkString onShortName() override {
bsalomonb0ae6492014-12-29 07:05:27 -0800124 return SkString("dcshader");
125 }
126
mtklein36352bf2015-03-25 18:17:31 -0700127 SkISize onISize() override { return SkISize::Make(1000, 900); }
bsalomonb0ae6492014-12-29 07:05:27 -0800128
mtklein36352bf2015-03-25 18:17:31 -0700129 void onOnceBeforeDraw() override {
bsalomonb0ae6492014-12-29 07:05:27 -0800130 struct Rect : public Prim {
mtklein36352bf2015-03-25 18:17:31 -0700131 SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800132 SkRect rect = SkRect::MakeXYWH(0, 0, 50, 50);
133 canvas->drawRect(rect, paint);
134 return rect;
135 }
136 };
137
138 struct Circle : public Prim {
mtklein36352bf2015-03-25 18:17:31 -0700139 SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800140 static const SkScalar radius = 25;
141 canvas->drawCircle(radius, radius, radius, paint);
142 return SkRect::MakeXYWH(0, 0, 2 * radius, 2 * radius);
143 }
144 };
145
146 struct RRect : public Prim {
mtklein36352bf2015-03-25 18:17:31 -0700147 SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800148 SkRRect rrect;
149 rrect.setRectXY(SkRect::MakeXYWH(0, 0, 50, 50), 10, 10);
150 canvas->drawRRect(rrect, paint);
151 return rrect.getBounds();
152 }
153 };
154
155 struct DRRect : public Prim {
mtklein36352bf2015-03-25 18:17:31 -0700156 SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800157 SkRRect outerRRect;
158 outerRRect.setRectXY(SkRect::MakeXYWH(0, 0, 50, 50), 5, 5);
159 SkRRect innerRRect;
160 innerRRect.setRectXY(SkRect::MakeXYWH(5, 8, 35, 30), 8, 3);
161 canvas->drawDRRect(outerRRect, innerRRect, paint);
162 return outerRRect.getBounds();
163 }
164 };
165 struct Path : public Prim {
mtklein36352bf2015-03-25 18:17:31 -0700166 SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800167 SkPath path;
168 path.addCircle(15, 15, 10);
169 path.addOval(SkRect::MakeXYWH(2, 2, 22, 37));
170 path.setFillType(SkPath::kEvenOdd_FillType);
171 canvas->drawPath(path, paint);
172 return path.getBounds();
173 }
174 };
175
176 struct Points : public Prim {
177 Points(SkCanvas::PointMode mode) : fMode(mode) {}
178
mtklein36352bf2015-03-25 18:17:31 -0700179 SkRect draw(SkCanvas* canvas, const SkPaint& paint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800180 SkRandom random;
181 SkPoint points[500];
182 SkRect bounds = SkRect::MakeWH(50, 50);
183 int count = SkToInt(SK_ARRAY_COUNT(points));
184 if (SkCanvas::kPoints_PointMode != fMode) {
185 count = SkTMin(count, 10);
186 }
187 for (int p = 0; p < count; ++p) {
188 points[p].fX = random.nextUScalar1() * bounds.width();
189 points[p].fY = random.nextUScalar1() * bounds.width();
190 }
191 canvas->drawPoints(fMode, count, points, paint);
192 return bounds;
193 }
194 SkCanvas::PointMode fMode;
195 };
196
197 struct Text : public Prim {
mtklein36352bf2015-03-25 18:17:31 -0700198 SkRect draw(SkCanvas* canvas, const SkPaint& origPaint) override {
bsalomonb0ae6492014-12-29 07:05:27 -0800199 SkPaint paint = origPaint;
200 paint.setTextSize(30.f);
201 this->setFont(&paint);
202 const char* text = this->text();
203 static const SkVector offset = SkVector::Make(10, 10);
204 canvas->drawText(text, strlen(text), offset.fX, offset.fY, paint);
205 SkRect bounds;
206 paint.measureText(text, strlen(text), &bounds);
207 bounds.offset(offset);
208 return bounds;
209 }
210
211 virtual void setFont(SkPaint* paint) {
caryclark1818acb2015-07-24 12:09:25 -0700212 sk_tool_utils::set_portable_typeface(paint);
bsalomonb0ae6492014-12-29 07:05:27 -0800213 }
214
215 virtual const char* text() const { return "Hello, Skia!"; }
216 };
217
halcanary385fe4d2015-08-26 13:07:48 -0700218 fPrims.push_back(new Rect);
219 fPrims.push_back(new Circle);
220 fPrims.push_back(new RRect);
221 fPrims.push_back(new DRRect);
222 fPrims.push_back(new Path);
223 fPrims.push_back(new Points(SkCanvas::kPoints_PointMode));
224 fPrims.push_back(new Points(SkCanvas::kLines_PointMode));
225 fPrims.push_back(new Points(SkCanvas::kPolygon_PointMode));
226 fPrims.push_back(new Text);
bsalomonb0ae6492014-12-29 07:05:27 -0800227 }
228
mtklein36352bf2015-03-25 18:17:31 -0700229 void onDraw(SkCanvas* canvas) override {
bsalomonb62da802015-01-31 07:51:14 -0800230 // This GM exists to test a specific feature of the GPU backend. It does not work with the
231 // sw rasterizer, tile modes, etc.
halcanary96fcdcc2015-08-27 07:41:13 -0700232 if (nullptr == canvas->getGrContext()) {
halcanary2a243382015-09-09 08:16:41 -0700233 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomonb62da802015-01-31 07:51:14 -0800234 return;
235 }
bsalomonb0ae6492014-12-29 07:05:27 -0800236 SkPaint paint;
237 SkTArray<SkMatrix> devMats;
238 devMats.push_back().reset();
239 devMats.push_back().setRotate(45, 500, 500);
240 devMats.push_back().setRotate(-30, 200, 200);
reed3f43f8a2015-01-20 19:58:36 -0800241 devMats.back().setPerspX(-SK_Scalar1 / 2000);
242 devMats.back().setPerspY(SK_Scalar1 / 1000);
bsalomonb0ae6492014-12-29 07:05:27 -0800243
244
245 SkTArray<SkMatrix> viewMats;
246 viewMats.push_back().setScale(0.75f, 0.75f);
247 viewMats.push_back().setRotate(45, 50, 50);
248 viewMats.back().postScale(0.5f, 1.1f);
249
250 canvas->translate(10, 20);
251 canvas->save();
252 SkScalar tx = 0, maxTy = 0;
253 static const SkScalar kW = 900;
254
255 for (int aa = 0; aa < 2; ++aa) {
256 for (int i = 0; i < fPrims.count(); ++i) {
257 for (int j = 0; j < devMats.count(); ++j) {
258 for (int k = 0; k < viewMats.count(); ++k) {
reedfe630452016-03-25 09:08:00 -0700259 paint.setShader(sk_make_sp<DCShader>(devMats[j]));
bsalomonb0ae6492014-12-29 07:05:27 -0800260 paint.setAntiAlias(SkToBool(aa));
261 canvas->save();
262 canvas->concat(viewMats[k]);
263 SkRect bounds = fPrims[i]->draw(canvas, paint);
264 canvas->restore();
265 viewMats[k].mapRect(&bounds);
266 // add margins
267 bounds.fRight += 20;
268 bounds.fBottom += 20;
269 canvas->translate(bounds.fRight, 0);
270 tx += bounds.fRight;
271 maxTy = SkTMax(bounds.fBottom, maxTy);
272 if (tx > kW) {
273 tx = 0;
274 canvas->restore();
275 canvas->translate(0, maxTy);
276 canvas->save();
277 maxTy = 0;
278 }
279 }
280 }
281 }
282 }
283 canvas->restore();
284 }
285
286private:
287 struct Prim {
288 virtual ~Prim() {}
289 virtual SkRect draw(SkCanvas*, const SkPaint&) = 0;
290 };
291
292 SkTArray<Prim*> fPrims;
293
294 typedef GM INHERITED;
295};
296
halcanary385fe4d2015-08-26 13:07:48 -0700297DEF_GM(return new DCShaderGM;)
bsalomonb0ae6492014-12-29 07:05:27 -0800298}
299#endif