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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/gpu/GrContext.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 21 | #include "include/private/GrSharedEnums.h" |
| 22 | #include "include/private/GrTypesPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/GrCaps.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrFragmentProcessor.h" |
| 25 | #include "src/gpu/GrPaint.h" |
| 26 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 28 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/effects/GrRRectEffect.h" |
| 30 | #include "src/gpu/ops/GrDrawOp.h" |
| 31 | #include "src/gpu/ops/GrFillRectOp.h" |
| 32 | #include "tools/ToolUtils.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 33 | |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 34 | #include <memory> |
| 35 | #include <utility> |
| 36 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 37 | namespace skiagm { |
| 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 41 | class BigRRectAAEffectGM : public GpuGM { |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 42 | public: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 43 | BigRRectAAEffectGM(const SkRRect& rrect, const char* name) |
| 44 | : fRRect(rrect) |
| 45 | , fName(name) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 46 | this->setBGColor(ToolUtils::color_to_565(SK_ColorBLUE)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 47 | // Each test case draws the rrect with gaps around it. |
| 48 | fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap; |
| 49 | fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap; |
| 50 | |
| 51 | // Add a pad between test cases. |
| 52 | fTestOffsetX = fTestWidth + kPad; |
| 53 | fTestOffsetY = fTestHeight + kPad; |
| 54 | |
| 55 | // We draw two tests in x (fill and inv-fill) and pad around |
| 56 | // all four sides of the image. |
| 57 | fWidth = 2 * fTestOffsetX + kPad; |
| 58 | fHeight = fTestOffsetY + kPad; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
| 62 | SkString onShortName() override { |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 63 | SkString name; |
| 64 | name.printf("big_rrect_%s_aa_effect", fName); |
| 65 | return name; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 66 | } |
| 67 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 68 | SkISize onISize() override { return SkISize::Make(fWidth, fHeight); } |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 69 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 70 | void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 71 | SkCanvas* canvas) override { |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 72 | SkPaint paint; |
| 73 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 74 | int y = kPad; |
| 75 | int x = kPad; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 76 | constexpr GrClipEdgeType kEdgeTypes[] = { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 77 | GrClipEdgeType::kFillAA, |
| 78 | GrClipEdgeType::kInverseFillAA, |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 79 | }; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 80 | SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 81 | for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 82 | GrClipEdgeType edgeType = kEdgeTypes[et]; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 83 | canvas->save(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 84 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 85 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 86 | // Draw a background for the test case |
| 87 | SkPaint paint; |
| 88 | paint.setColor(SK_ColorWHITE); |
| 89 | canvas->drawRect(testBounds, paint); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 90 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 91 | SkRRect rrect = fRRect; |
| 92 | rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 93 | const auto& caps = *renderTargetContext->caps()->shaderCaps(); |
| 94 | auto fp = GrRRectEffect::Make(edgeType, rrect, caps); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 95 | SkASSERT(fp); |
| 96 | if (fp) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 97 | GrPaint grPaint; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 98 | grPaint.setColor4f({ 0, 0, 0, 1.f }); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 99 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 100 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 101 | |
| 102 | SkRect bounds = testBounds; |
| 103 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 104 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 105 | renderTargetContext->priv().testingOnly_addDrawOp( |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame^] | 106 | GrFillRectOp::MakeNonAARect(context, std::move(grPaint), |
| 107 | SkMatrix::I(), bounds)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 108 | } |
| 109 | canvas->restore(); |
| 110 | x = x + fTestOffsetX; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 114 | private: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 115 | // pad between test cases |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 116 | static constexpr int kPad = 7; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 117 | // gap between rect for each case that is rendered and exterior of rrect |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 118 | static constexpr int kGap = 3; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 119 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 120 | SkRRect fRRect; |
| 121 | int fWidth; |
| 122 | int fHeight; |
| 123 | int fTestWidth; |
| 124 | int fTestHeight; |
| 125 | int fTestOffsetX; |
| 126 | int fTestOffsetY; |
| 127 | const char* fName; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 128 | typedef GM INHERITED; |
| 129 | }; |
| 130 | |
| 131 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 132 | // This value is motivated by bug chromium:477684. It has to be large to cause overflow in |
| 133 | // the shader |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 134 | constexpr int kSize = 700; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 135 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 136 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) |
| 137 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) |
| 138 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) |
| 139 | // The next two have small linear segments between the corners |
| 140 | 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"); ) |
| 141 | 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] | 142 | |
| 143 | } |