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 #include "GrCaps.h"
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/GrFillRectOp.h"
17 
18 namespace skiagm {
19 
20 ///////////////////////////////////////////////////////////////////////////////
21 
22 class BigRRectAAEffectGM : public GpuGM {
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(GrContext * context,GrRenderTargetContext * renderTargetContext,SkCanvas * canvas)51     void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
52                 SkCanvas* canvas) override {
53         SkPaint paint;
54 
55         int y = kPad;
56         int x = kPad;
57         constexpr GrClipEdgeType kEdgeTypes[] = {
58             GrClipEdgeType::kFillAA,
59             GrClipEdgeType::kInverseFillAA,
60         };
61         SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight);
62         for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
63             GrClipEdgeType edgeType = kEdgeTypes[et];
64             canvas->save();
65                 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
66 
67                 // Draw a background for the test case
68                 SkPaint paint;
69                 paint.setColor(SK_ColorWHITE);
70                 canvas->drawRect(testBounds, paint);
71 
72                 SkRRect rrect = fRRect;
73                 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap));
74                 const auto& caps = *renderTargetContext->caps()->shaderCaps();
75                 auto fp = GrRRectEffect::Make(edgeType, rrect, caps);
76                 SkASSERT(fp);
77                 if (fp) {
78                     GrPaint grPaint;
79                     grPaint.setColor4f({ 0, 0, 0, 1.f });
80                     grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
81                     grPaint.addCoverageFragmentProcessor(std::move(fp));
82 
83                     SkRect bounds = testBounds;
84                     bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
85 
86                     renderTargetContext->priv().testingOnly_addDrawOp(
87                             GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone,
88                                                SkMatrix::I(), bounds));
89                 }
90             canvas->restore();
91             x = x + fTestOffsetX;
92         }
93     }
94 
95 private:
96     // pad between test cases
97     static constexpr int kPad = 7;
98     // gap between rect for each case that is rendered and exterior of rrect
99     static constexpr int kGap = 3;
100 
101     SkRRect fRRect;
102     int fWidth;
103     int fHeight;
104     int fTestWidth;
105     int fTestHeight;
106     int fTestOffsetX;
107     int fTestOffsetY;
108     const char* fName;
109     typedef GM INHERITED;
110 };
111 
112 ///////////////////////////////////////////////////////////////////////////////
113 // This value is motivated by bug chromium:477684. It has to be large to cause overflow in
114 // the shader
115 constexpr int kSize = 700;
116 
117 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); )
118 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); )
119 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); )
120 // The next two have small linear segments between the corners
121 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"); )
122 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"); )
123 
124 }
125