1 /* 2 * Copyright 2015 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/gm.h" 9 #include "include/core/SkBlendMode.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkMatrix.h" 13 #include "include/core/SkPaint.h" 14 #include "include/core/SkRRect.h" 15 #include "include/core/SkRect.h" 16 #include "include/core/SkScalar.h" 17 #include "include/core/SkSize.h" 18 #include "include/core/SkString.h" 19 #include "include/core/SkTypes.h" 20 #include "include/private/gpu/ganesh/GrTypesPriv.h" 21 #include "src/core/SkCanvasPriv.h" 22 #include "src/gpu/ganesh/GrCanvas.h" 23 #include "src/gpu/ganesh/GrCaps.h" 24 #include "src/gpu/ganesh/GrFragmentProcessor.h" 25 #include "src/gpu/ganesh/GrPaint.h" 26 #include "src/gpu/ganesh/GrRecordingContextPriv.h" 27 #include "src/gpu/ganesh/SurfaceDrawContext.h" 28 #include "src/gpu/ganesh/effects/GrPorterDuffXferProcessor.h" 29 #include "src/gpu/ganesh/effects/GrRRectEffect.h" 30 #include "src/gpu/ganesh/ops/FillRectOp.h" 31 #include "src/gpu/ganesh/ops/GrDrawOp.h" 32 #include "tools/ToolUtils.h" 33 34 #include <memory> 35 #include <utility> 36 37 namespace skiagm { 38 39 /////////////////////////////////////////////////////////////////////////////// 40 41 class BigRRectAAEffectGM : public GpuGM { 42 public: BigRRectAAEffectGM(const SkRRect & rrect,const char * name)43 BigRRectAAEffectGM(const SkRRect& rrect, const char* name) 44 : fRRect(rrect) 45 , fName(name) { 46 this->setBGColor(ToolUtils::color_to_565(SK_ColorBLUE)); 47 // Each test case draws the rrect with gaps around it. 48 fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap; 49 fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap; 50 51 // Add a pad between test cases. 52 fTestOffsetX = fTestWidth + kPad; 53 fTestOffsetY = fTestHeight + kPad; 54 55 // We draw two tests in x (fill and inv-fill) and pad around 56 // all four sides of the image. 57 fWidth = 2 * fTestOffsetX + kPad; 58 fHeight = fTestOffsetY + kPad; 59 } 60 61 protected: getName() const62 SkString getName() const override { 63 SkString name; 64 name.printf("big_rrect_%s_aa_effect", fName); 65 return name; 66 } 67 getISize()68 SkISize getISize() override { return SkISize::Make(fWidth, fHeight); } 69 onDraw(GrRecordingContext * rContext,SkCanvas * canvas,SkString * errorMsg)70 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { 71 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas); 72 if (!sdc) { 73 *errorMsg = kErrorMsg_DrawSkippedGpuOnly; 74 return DrawResult::kSkip; 75 } 76 77 int y = kPad; 78 int x = kPad; 79 constexpr GrClipEdgeType kEdgeTypes[] = { 80 GrClipEdgeType::kFillAA, 81 GrClipEdgeType::kInverseFillAA, 82 }; 83 SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); 84 for (size_t et = 0; et < std::size(kEdgeTypes); ++et) { 85 GrClipEdgeType edgeType = kEdgeTypes[et]; 86 canvas->save(); 87 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 88 89 // Draw a background for the test case 90 SkPaint paint; 91 paint.setColor(SK_ColorWHITE); 92 canvas->drawRect(testBounds, paint); 93 94 SkRRect rrect = fRRect; 95 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); 96 const auto& caps = *rContext->priv().caps()->shaderCaps(); 97 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, edgeType, rrect, 98 caps); 99 SkASSERT(success); 100 if (success) { 101 SkASSERT(fp); 102 GrPaint grPaint; 103 grPaint.setColor4f({ 0, 0, 0, 1.f }); 104 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); 105 grPaint.setCoverageFragmentProcessor(std::move(fp)); 106 107 SkRect bounds = testBounds; 108 bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); 109 110 sdc->addDrawOp(skgpu::ganesh::FillRectOp::MakeNonAARect( 111 rContext, std::move(grPaint), SkMatrix::I(), bounds)); 112 } 113 canvas->restore(); 114 x = x + fTestOffsetX; 115 } 116 117 return DrawResult::kOk; 118 } 119 120 private: 121 // pad between test cases 122 inline static constexpr int kPad = 7; 123 // gap between rect for each case that is rendered and exterior of rrect 124 inline static constexpr int kGap = 3; 125 126 SkRRect fRRect; 127 int fWidth; 128 int fHeight; 129 int fTestWidth; 130 int fTestHeight; 131 int fTestOffsetX; 132 int fTestOffsetY; 133 const char* fName; 134 using INHERITED = GM; 135 }; 136 137 /////////////////////////////////////////////////////////////////////////////// 138 // This value is motivated by bug chromium:477684. It has to be large to cause overflow in 139 // the shader 140 constexpr int kSize = 700; 141 142 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) 143 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) 144 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) 145 // The next two have small linear segments between the corners 146 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 10.f), "circular_corner"); ) 147 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 15.f), "elliptical_corner"); ) 148 149 } // namespace skiagm 150