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.h" 9 #include "sk_tool_utils.h" 10 #if SK_SUPPORT_GPU 11 #include "GrContext.h" 12 #include "GrRenderTargetContextPriv.h" 13 #include "SkRRect.h" 14 #include "effects/GrRRectEffect.h" 15 #include "ops/GrDrawOp.h" 16 #include "ops/GrRectOpFactory.h" 17 18 namespace skiagm { 19 20 /////////////////////////////////////////////////////////////////////////////// 21 22 class BigRRectAAEffectGM : public GM { 23 public: BigRRectAAEffectGM(const SkRRect & rrect,const char * name)24 BigRRectAAEffectGM(const SkRRect& rrect, const char* name) 25 : fRRect(rrect) 26 , fName(name) { 27 this->setBGColor(sk_tool_utils::color_to_565(SK_ColorBLUE)); 28 // Each test case draws the rrect with gaps around it. 29 fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap; 30 fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap; 31 32 // Add a pad between test cases. 33 fTestOffsetX = fTestWidth + kPad; 34 fTestOffsetY = fTestHeight + kPad; 35 36 // We draw two tests in x (fill and inv-fill) and pad around 37 // all four sides of the image. 38 fWidth = 2 * fTestOffsetX + kPad; 39 fHeight = fTestOffsetY + kPad; 40 } 41 42 protected: onShortName()43 SkString onShortName() override { 44 SkString name; 45 name.printf("big_rrect_%s_aa_effect", fName); 46 return name; 47 } 48 onISize()49 SkISize onISize() override { return SkISize::Make(fWidth, fHeight); } 50 onDraw(SkCanvas * canvas)51 void onDraw(SkCanvas* canvas) override { 52 GrRenderTargetContext* renderTargetContext = 53 canvas->internal_private_accessTopLayerRenderTargetContext(); 54 if (!renderTargetContext) { 55 skiagm::GM::DrawGpuOnlyMessage(canvas); 56 return; 57 } 58 59 SkPaint paint; 60 61 int y = kPad; 62 int x = kPad; 63 constexpr GrClipEdgeType kEdgeTypes[] = { 64 GrClipEdgeType::kFillAA, 65 GrClipEdgeType::kInverseFillAA, 66 }; 67 SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); 68 for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { 69 GrClipEdgeType edgeType = kEdgeTypes[et]; 70 canvas->save(); 71 canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); 72 73 // Draw a background for the test case 74 SkPaint paint; 75 paint.setColor(SK_ColorWHITE); 76 canvas->drawRect(testBounds, paint); 77 78 SkRRect rrect = fRRect; 79 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); 80 const auto& caps = *renderTargetContext->caps()->shaderCaps(); 81 auto fp = GrRRectEffect::Make(edgeType, rrect, caps); 82 SkASSERT(fp); 83 if (fp) { 84 GrPaint grPaint; 85 grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f)); 86 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); 87 grPaint.addCoverageFragmentProcessor(std::move(fp)); 88 89 SkRect bounds = testBounds; 90 bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); 91 92 renderTargetContext->priv().testingOnly_addDrawOp( 93 GrRectOpFactory::MakeNonAAFill(std::move(grPaint), SkMatrix::I(), 94 bounds, GrAAType::kNone)); 95 } 96 canvas->restore(); 97 x = x + fTestOffsetX; 98 } 99 } 100 101 private: 102 // pad between test cases 103 static constexpr int kPad = 7; 104 // gap between rect for each case that is rendered and exterior of rrect 105 static constexpr int kGap = 3; 106 107 SkRRect fRRect; 108 int fWidth; 109 int fHeight; 110 int fTestWidth; 111 int fTestHeight; 112 int fTestOffsetX; 113 int fTestOffsetY; 114 const char* fName; 115 typedef GM INHERITED; 116 }; 117 118 /////////////////////////////////////////////////////////////////////////////// 119 // This value is motivated by bug chromium:477684. It has to be large to cause overflow in 120 // the shader 121 constexpr int kSize = 700; 122 123 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) 124 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) 125 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) 126 // The next two have small linear segments between the corners 127 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"); ) 128 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"); ) 129 130 } 131 #endif 132