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" 11 #include "include/gpu/GrContextOptions.h" 12 #include "include/gpu/GrRecordingContext.h" 13 #include "src/core/SkCanvasPriv.h" 14 #include "src/gpu/GrDirectContextPriv.h" 15 #include "src/gpu/GrDrawingManager.h" 16 #include "src/gpu/GrRecordingContextPriv.h" 17 #include "src/gpu/v1/SurfaceDrawContext_v1.h" 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 /** 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. 35 */ 36 class PreserveFillRuleGM : public GpuGM { 37 public: PreserveFillRuleGM(bool big)38 PreserveFillRuleGM(bool big) : fBig(big) , fStarSize((big) ? 200 : 20) {} 39 40 private: onShortName()41 SkString onShortName() override { 42 SkString name("preservefillrule"); 43 name += (fBig) ? "_big" : "_little"; 44 return name; 45 } onISize()46 SkISize onISize() override { return SkISize::Make(fStarSize * 2, fStarSize * 2); } 47 modifyGrContextOptions(GrContextOptions * ctxOptions)48 void modifyGrContextOptions(GrContextOptions* ctxOptions) override { 49 ctxOptions->fAllowPathMaskCaching = true; 50 } 51 onDraw(GrRecordingContext * rContext,SkCanvas * canvas,SkString * errorMsg)52 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { 53 auto dContext = GrAsDirectContext(rContext); 54 auto sfc = SkCanvasPriv::TopDeviceSurfaceFillContext(canvas); 55 if (!dContext || !sfc) { 56 *errorMsg = "Requires a direct context."; 57 return skiagm::DrawResult::kSkip; 58 } 59 60 auto starRect = SkRect::MakeWH(fStarSize, fStarSize); 61 SkPath star7_winding = ToolUtils::make_star(starRect, 7); 62 star7_winding.setFillType(SkPathFillType::kWinding); 63 64 SkPath star7_evenOdd = star7_winding; 65 star7_evenOdd.transform(SkMatrix::Translate(0, fStarSize)); 66 star7_evenOdd.setFillType(SkPathFillType::kEvenOdd); 67 68 SkPath star5_winding = ToolUtils::make_star(starRect, 5); 69 star5_winding.transform(SkMatrix::Translate(fStarSize, 0)); 70 star5_winding.setFillType(SkPathFillType::kWinding); 71 72 SkPath star5_evenOdd = star5_winding; 73 star5_evenOdd.transform(SkMatrix::Translate(0, fStarSize)); 74 star5_evenOdd.setFillType(SkPathFillType::kEvenOdd); 75 76 SkPaint paint; 77 paint.setColor(SK_ColorGREEN); 78 paint.setAntiAlias(true); 79 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); 85 dContext->priv().flushSurface(sfc->asSurfaceProxy()); 86 87 return DrawResult::kOk; 88 } 89 90 private: 91 const bool fBig; 92 const int fStarSize; 93 }; 94 95 DEF_GM( return new PreserveFillRuleGM(true); ) 96 DEF_GM( return new PreserveFillRuleGM(false); ) 97 98 } // namespace skiagm 99