Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC. |
| 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/gm.h" |
| 9 | |
| 10 | #include "include/core/SkPath.h" |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 11 | #include "include/gpu/GrContextOptions.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrRecordingContext.h" |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 13 | #include "src/core/SkCanvasPriv.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrDirectContextPriv.h" |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 15 | #include "src/gpu/GrDrawingManager.h" |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 17 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 18 | #include "tools/ToolUtils.h" |
| 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | #define ERR_MSG_ASSERT(COND) \ |
| 23 | do { \ |
| 24 | if (!(COND)) { \ |
| 25 | errorMsg->printf("preservefillrule.cpp(%i): assert(%s)", \ |
| 26 | __LINE__, #COND); \ |
| 27 | return DrawResult::kFail; \ |
| 28 | } \ |
| 29 | } while (false) |
| 30 | |
| 31 | |
| 32 | /** |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 33 | * This test originally ensured that the ccpr path cache preserved fill rules properly. CCRP is gone |
| 34 | * now, but we decided to keep the test. |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 35 | */ |
| 36 | class PreserveFillRuleGM : public GpuGM { |
| 37 | public: |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 38 | PreserveFillRuleGM(bool big) : fBig(big) , fStarSize((big) ? 200 : 20) {} |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 39 | |
| 40 | private: |
| 41 | SkString onShortName() override { |
| 42 | SkString name("preservefillrule"); |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 43 | name += (fBig) ? "_big" : "_little"; |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 44 | return name; |
| 45 | } |
| 46 | SkISize onISize() override { return SkISize::Make(fStarSize * 2, fStarSize * 2); } |
| 47 | |
| 48 | void modifyGrContextOptions(GrContextOptions* ctxOptions) override { |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 49 | ctxOptions->fAllowPathMaskCaching = true; |
| 50 | } |
| 51 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 52 | DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 53 | auto dContext = GrAsDirectContext(rContext); |
John Stiles | ab7ff17 | 2021-07-30 10:59:25 -0400 | [diff] [blame] | 54 | auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas); |
| 55 | if (!dContext || !sfc) { |
Robert Phillips | 267826c | 2020-11-10 11:10:09 -0500 | [diff] [blame] | 56 | *errorMsg = "Requires a direct context."; |
| 57 | return skiagm::DrawResult::kSkip; |
| 58 | } |
Robert Phillips | 27f283f | 2020-10-14 11:46:51 -0400 | [diff] [blame] | 59 | |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 60 | auto starRect = SkRect::MakeWH(fStarSize, fStarSize); |
| 61 | SkPath star7_winding = ToolUtils::make_star(starRect, 7); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 62 | star7_winding.setFillType(SkPathFillType::kWinding); |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 63 | |
| 64 | SkPath star7_evenOdd = star7_winding; |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 65 | star7_evenOdd.transform(SkMatrix::Translate(0, fStarSize)); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 66 | star7_evenOdd.setFillType(SkPathFillType::kEvenOdd); |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 67 | |
| 68 | SkPath star5_winding = ToolUtils::make_star(starRect, 5); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 69 | star5_winding.transform(SkMatrix::Translate(fStarSize, 0)); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 70 | star5_winding.setFillType(SkPathFillType::kWinding); |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 71 | |
| 72 | SkPath star5_evenOdd = star5_winding; |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 73 | star5_evenOdd.transform(SkMatrix::Translate(0, fStarSize)); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 74 | star5_evenOdd.setFillType(SkPathFillType::kEvenOdd); |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 75 | |
| 76 | SkPaint paint; |
| 77 | paint.setColor(SK_ColorGREEN); |
| 78 | paint.setAntiAlias(true); |
| 79 | |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 80 | canvas->clear(SK_ColorWHITE); |
| 81 | canvas->drawPath(star7_winding, paint); |
| 82 | canvas->drawPath(star7_evenOdd, paint); |
| 83 | canvas->drawPath(star5_winding, paint); |
| 84 | canvas->drawPath(star5_evenOdd, paint); |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 85 | dContext->priv().flushSurface(sfc->asSurfaceProxy()); |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 86 | |
| 87 | return DrawResult::kOk; |
| 88 | } |
| 89 | |
| 90 | private: |
Chris Dalton | 03730e6 | 2021-03-11 19:41:40 -0700 | [diff] [blame] | 91 | const bool fBig; |
Chris Dalton | 5b5403e | 2019-06-05 11:54:39 -0600 | [diff] [blame] | 92 | const int fStarSize; |
| 93 | }; |
| 94 | |
| 95 | DEF_GM( return new PreserveFillRuleGM(true); ) |
| 96 | DEF_GM( return new PreserveFillRuleGM(false); ) |
| 97 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 98 | } // namespace skiagm |