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