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