bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 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 "gm.h" |
| 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrFragmentProcessor.h" |
| 11 | #include "GrCoordTransform.h" |
bsalomon | 87ba62e | 2015-09-22 06:41:59 -0700 | [diff] [blame] | 12 | #include "effects/GrXfermodeFragmentProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 15 | #include "glsl/GrGLSLProgramBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 16 | #include "glsl/GrGLSLProgramDataManager.h" |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 17 | #include "Resources.h" |
mtklein | 36a364a | 2015-01-07 08:02:28 -0800 | [diff] [blame] | 18 | #include "SkReadBuffer.h" |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 19 | #include "SkShader.h" |
| 20 | #include "SkStream.h" |
| 21 | #include "SkTypeface.h" |
mtklein | 36a364a | 2015-01-07 08:02:28 -0800 | [diff] [blame] | 22 | #include "SkWriteBuffer.h" |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 23 | |
| 24 | namespace skiagm { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | class DCShader : public SkShader { |
| 29 | public: |
| 30 | DCShader(const SkMatrix& matrix) : fDeviceMatrix(matrix) {} |
| 31 | |
mtklein | 36a364a | 2015-01-07 08:02:28 -0800 | [diff] [blame] | 32 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(DCShader); |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | void flatten(SkWriteBuffer& buf) const override { |
mtklein | 36a364a | 2015-01-07 08:02:28 -0800 | [diff] [blame] | 35 | buf.writeMatrix(fDeviceMatrix); |
| 36 | } |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 37 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 38 | const GrFragmentProcessor* asFragmentProcessor(GrContext*, |
| 39 | const SkMatrix& viewM, |
| 40 | const SkMatrix* localMatrix, |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 41 | SkFilterQuality) const override; |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 42 | |
| 43 | #ifndef SK_IGNORE_TO_STRING |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void toString(SkString* str) const override { |
robertphillips | 42dbfa8 | 2015-01-26 06:08:52 -0800 | [diff] [blame] | 45 | str->appendf("DCShader: ()"); |
| 46 | } |
| 47 | #endif |
| 48 | |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 49 | private: |
| 50 | const SkMatrix fDeviceMatrix; |
| 51 | }; |
| 52 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 53 | sk_sp<SkFlattenable> DCShader::CreateProc(SkReadBuffer& buf) { |
mtklein | 36a364a | 2015-01-07 08:02:28 -0800 | [diff] [blame] | 54 | SkMatrix matrix; |
| 55 | buf.readMatrix(&matrix); |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 56 | return sk_make_sp<DCShader>(matrix); |
mtklein | 36a364a | 2015-01-07 08:02:28 -0800 | [diff] [blame] | 57 | } |
| 58 | |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 59 | class DCFP : public GrFragmentProcessor { |
| 60 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 61 | DCFP(const SkMatrix& m) : fDeviceTransform(kDevice_GrCoordSet, m) { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 62 | this->addCoordTransform(&fDeviceTransform); |
| 63 | this->initClassID<DCFP>(); |
| 64 | } |
| 65 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 66 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 67 | class DCGLFP : public GrGLSLFragmentProcessor { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 68 | void emitCode(EmitArgs& args) override { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 69 | 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()); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 80 | } |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 81 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override {} |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 82 | }; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 83 | return new DCGLFP; |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 84 | } |
| 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | const char* name() const override { return "DCFP"; } |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 89 | inout->mulByUnknownFourComponents(); |
| 90 | } |
| 91 | |
| 92 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 93 | void onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 94 | GrProcessorKeyBuilder* b) const override {} |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 97 | |
| 98 | GrCoordTransform fDeviceTransform; |
| 99 | }; |
| 100 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 101 | const GrFragmentProcessor* DCShader::asFragmentProcessor(GrContext*, |
| 102 | const SkMatrix& viewM, |
| 103 | const SkMatrix* localMatrix, |
| 104 | SkFilterQuality) const { |
| 105 | SkAutoTUnref<const GrFragmentProcessor> inner(new DCFP(fDeviceMatrix)); |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 106 | return GrFragmentProcessor::MulOutputByInputAlpha(inner); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | class DCShaderGM : public GM { |
| 110 | public: |
| 111 | DCShaderGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 112 | this->setBGColor(sk_tool_utils::color_to_565(0xFFAABBCC)); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 113 | } |
| 114 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 115 | ~DCShaderGM() override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 116 | for (int i = 0; i < fPrims.count(); ++i) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 117 | delete fPrims[i]; |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | protected: |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 122 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 123 | SkString onShortName() override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 124 | return SkString("dcshader"); |
| 125 | } |
| 126 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 127 | SkISize onISize() override { return SkISize::Make(1000, 900); } |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 128 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 129 | void onOnceBeforeDraw() override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 130 | struct Rect : public Prim { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 131 | SkRect draw(SkCanvas* canvas, const SkPaint& paint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 132 | SkRect rect = SkRect::MakeXYWH(0, 0, 50, 50); |
| 133 | canvas->drawRect(rect, paint); |
| 134 | return rect; |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | struct Circle : public Prim { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | SkRect draw(SkCanvas* canvas, const SkPaint& paint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 140 | 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 { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | SkRect draw(SkCanvas* canvas, const SkPaint& paint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 148 | 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 { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 156 | SkRect draw(SkCanvas* canvas, const SkPaint& paint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 157 | 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 { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 166 | SkRect draw(SkCanvas* canvas, const SkPaint& paint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 167 | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 179 | SkRect draw(SkCanvas* canvas, const SkPaint& paint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 180 | 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 { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 198 | SkRect draw(SkCanvas* canvas, const SkPaint& origPaint) override { |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 199 | 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) { |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 212 | sk_tool_utils::set_portable_typeface(paint); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | virtual const char* text() const { return "Hello, Skia!"; } |
| 216 | }; |
| 217 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 218 | 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); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 227 | } |
| 228 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 229 | void onDraw(SkCanvas* canvas) override { |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 230 | // This GM exists to test a specific feature of the GPU backend. It does not work with the |
| 231 | // sw rasterizer, tile modes, etc. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | if (nullptr == canvas->getGrContext()) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 233 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
bsalomon | b62da80 | 2015-01-31 07:51:14 -0800 | [diff] [blame] | 234 | return; |
| 235 | } |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 236 | 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); |
reed | 3f43f8a | 2015-01-20 19:58:36 -0800 | [diff] [blame] | 241 | devMats.back().setPerspX(-SK_Scalar1 / 2000); |
| 242 | devMats.back().setPerspY(SK_Scalar1 / 1000); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 243 | |
| 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) { |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 259 | paint.setShader(sk_make_sp<DCShader>(devMats[j])); |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 260 | 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 | |
| 286 | private: |
| 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 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 297 | DEF_GM(return new DCShaderGM;) |
bsalomon | b0ae649 | 2014-12-29 07:05:27 -0800 | [diff] [blame] | 298 | } |
| 299 | #endif |