bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkRRect.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 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" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 20 | #include "include/private/GrTypesPriv.h" |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 21 | #include "src/core/SkCanvasPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrCaps.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrFragmentProcessor.h" |
| 24 | #include "src/gpu/GrPaint.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 25 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "src/gpu/effects/GrRRectEffect.h" |
Robert Phillips | fbf0214 | 2021-09-01 16:31:34 -0400 | [diff] [blame] | 27 | #include "src/gpu/ops/FillRectOp.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/ops/GrDrawOp.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 29 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "tools/ToolUtils.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 31 | |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 32 | #include <memory> |
| 33 | #include <utility> |
| 34 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 35 | namespace skiagm { |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 39 | class BigRRectAAEffectGM : public GpuGM { |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 40 | public: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 41 | BigRRectAAEffectGM(const SkRRect& rrect, const char* name) |
| 42 | : fRRect(rrect) |
| 43 | , fName(name) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 44 | this->setBGColor(ToolUtils::color_to_565(SK_ColorBLUE)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 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; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | protected: |
| 60 | SkString onShortName() override { |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 61 | SkString name; |
| 62 | name.printf("big_rrect_%s_aa_effect", fName); |
| 63 | return name; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 66 | SkISize onISize() override { return SkISize::Make(fWidth, fHeight); } |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 67 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 68 | DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 69 | auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas); |
| 70 | if (!sdc) { |
| 71 | *errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 72 | return DrawResult::kSkip; |
| 73 | } |
| 74 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 75 | int y = kPad; |
| 76 | int x = kPad; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 77 | constexpr GrClipEdgeType kEdgeTypes[] = { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 78 | GrClipEdgeType::kFillAA, |
| 79 | GrClipEdgeType::kInverseFillAA, |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 80 | }; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 81 | SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 82 | for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 83 | GrClipEdgeType edgeType = kEdgeTypes[et]; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 84 | canvas->save(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 85 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 86 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 87 | // Draw a background for the test case |
| 88 | SkPaint paint; |
| 89 | paint.setColor(SK_ColorWHITE); |
| 90 | canvas->drawRect(testBounds, paint); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 91 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 92 | SkRRect rrect = fRRect; |
| 93 | rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 94 | const auto& caps = *rContext->priv().caps()->shaderCaps(); |
John Stiles | 851b90e | 2020-06-17 13:53:37 -0400 | [diff] [blame] | 95 | auto [success, fp] = GrRRectEffect::Make(/*inputFP=*/nullptr, edgeType, rrect, |
| 96 | caps); |
| 97 | SkASSERT(success); |
| 98 | if (success) { |
| 99 | SkASSERT(fp); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 100 | GrPaint grPaint; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 101 | grPaint.setColor4f({ 0, 0, 0, 1.f }); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 102 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
John Stiles | 41d91b6 | 2020-07-21 14:39:40 -0400 | [diff] [blame] | 103 | grPaint.setCoverageFragmentProcessor(std::move(fp)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 104 | |
| 105 | SkRect bounds = testBounds; |
| 106 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 107 | |
Robert Phillips | fbf0214 | 2021-09-01 16:31:34 -0400 | [diff] [blame] | 108 | sdc->addDrawOp(skgpu::v1::FillRectOp::MakeNonAARect( |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 109 | rContext, std::move(grPaint), SkMatrix::I(), bounds)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 110 | } |
| 111 | canvas->restore(); |
| 112 | x = x + fTestOffsetX; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 113 | } |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 114 | |
| 115 | return DrawResult::kOk; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 116 | } |
| 117 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 118 | private: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 119 | // pad between test cases |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 120 | static constexpr int kPad = 7; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 121 | // gap between rect for each case that is rendered and exterior of rrect |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 122 | static constexpr int kGap = 3; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 123 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 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; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 132 | using INHERITED = GM; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 136 | // This value is motivated by bug chromium:477684. It has to be large to cause overflow in |
| 137 | // the shader |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 138 | constexpr int kSize = 700; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 139 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 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"); ) |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 146 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 147 | } // namespace skiagm |