• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrCaps.h"
23 #include "src/gpu/ganesh/GrFragmentProcessor.h"
24 #include "src/gpu/ganesh/GrPaint.h"
25 #include "src/gpu/ganesh/SurfaceDrawContext.h"
26 #include "src/gpu/ganesh/effects/GrPorterDuffXferProcessor.h"
27 #include "src/gpu/ganesh/effects/GrRRectEffect.h"
28 #include "src/gpu/ganesh/ops/FillRectOp.h"
29 #include "src/gpu/ganesh/ops/GrDrawOp.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 * rContext,SkCanvas * canvas,SkString * errorMsg)68     DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
69         auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
70         if (!sdc) {
71             *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
72             return DrawResult::kSkip;
73         }
74 
75         int y = kPad;
76         int x = kPad;
77         constexpr GrClipEdgeType kEdgeTypes[] = {
78             GrClipEdgeType::kFillAA,
79             GrClipEdgeType::kInverseFillAA,
80         };
81         SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight);
82         for (size_t et = 0; et < std::size(kEdgeTypes); ++et) {
83             GrClipEdgeType edgeType = kEdgeTypes[et];
84             canvas->save();
85                 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
86 
87                 // Draw a background for the test case
88                 SkPaint paint;
89                 paint.setColor(SK_ColorWHITE);
90                 canvas->drawRect(testBounds, paint);
91 
92                 SkRRect rrect = fRRect;
93                 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap));
94                 const auto& caps = *rContext->priv().caps()->shaderCaps();
95                 auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, edgeType, rrect,
96                                                          caps);
97                 SkASSERT(success);
98                 if (success) {
99                     SkASSERT(fp);
100                     GrPaint grPaint;
101                     grPaint.setColor4f({ 0, 0, 0, 1.f });
102                     grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
103                     grPaint.setCoverageFragmentProcessor(std::move(fp));
104 
105                     SkRect bounds = testBounds;
106                     bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
107 
108                     sdc->addDrawOp(skgpu::v1::FillRectOp::MakeNonAARect(
109                             rContext, std::move(grPaint), SkMatrix::I(), bounds));
110                 }
111             canvas->restore();
112             x = x + fTestOffsetX;
113         }
114 
115         return DrawResult::kOk;
116     }
117 
118 private:
119     // pad between test cases
120     inline static constexpr int kPad = 7;
121     // gap between rect for each case that is rendered and exterior of rrect
122     inline static constexpr int kGap = 3;
123 
124     SkRRect fRRect;
125     int fWidth;
126     int fHeight;
127     int fTestWidth;
128     int fTestHeight;
129     int fTestOffsetX;
130     int fTestOffsetY;
131     const char* fName;
132     using INHERITED = GM;
133 };
134 
135 ///////////////////////////////////////////////////////////////////////////////
136 // This value is motivated by bug chromium:477684. It has to be large to cause overflow in
137 // the shader
138 constexpr int kSize = 700;
139 
140 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); )
141 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); )
142 DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); )
143 // The next two have small linear segments between the corners
144 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"); )
145 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"); )
146 
147 }  // namespace skiagm
148