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 | |
| 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 10 | #if SK_SUPPORT_GPU |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 11 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 12 | #include "GrRenderTargetContextPriv.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 13 | #include "SkRRect.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 14 | #include "effects/GrRRectEffect.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 15 | #include "ops/GrDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 16 | #include "ops/GrRectOpFactory.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | /////////////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | class BigRRectAAEffectGM : public GM { |
| 23 | public: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 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; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | protected: |
| 43 | SkString onShortName() override { |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 44 | SkString name; |
| 45 | name.printf("big_rrect_%s_aa_effect", fName); |
| 46 | return name; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 47 | } |
| 48 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 49 | SkISize onISize() override { return SkISize::Make(fWidth, fHeight); } |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 50 | |
| 51 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 52 | GrRenderTargetContext* renderTargetContext = |
| 53 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 54 | if (!renderTargetContext) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 55 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 59 | SkPaint paint; |
| 60 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 61 | int y = kPad; |
| 62 | int x = kPad; |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 63 | constexpr GrClipEdgeType kEdgeTypes[] = { |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 64 | GrClipEdgeType::kFillAA, |
| 65 | GrClipEdgeType::kInverseFillAA, |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 66 | }; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 67 | SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 68 | for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 69 | GrClipEdgeType edgeType = kEdgeTypes[et]; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 70 | canvas->save(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 71 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 72 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 73 | // Draw a background for the test case |
| 74 | SkPaint paint; |
| 75 | paint.setColor(SK_ColorWHITE); |
| 76 | canvas->drawRect(testBounds, paint); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 77 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 78 | SkRRect rrect = fRRect; |
| 79 | rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); |
Brian Salomon | 1447177 | 2017-12-05 10:35:15 -0500 | [diff] [blame] | 80 | const auto& caps = *renderTargetContext->caps()->shaderCaps(); |
| 81 | auto fp = GrRRectEffect::Make(edgeType, rrect, caps); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 82 | SkASSERT(fp); |
| 83 | if (fp) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 84 | GrPaint grPaint; |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 85 | grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 86 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 87 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 88 | |
| 89 | SkRect bounds = testBounds; |
| 90 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 91 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 92 | renderTargetContext->priv().testingOnly_addDrawOp( |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 93 | GrRectOpFactory::MakeNonAAFill(std::move(grPaint), SkMatrix::I(), |
| 94 | bounds, GrAAType::kNone)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 95 | } |
| 96 | canvas->restore(); |
| 97 | x = x + fTestOffsetX; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 101 | private: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 102 | // pad between test cases |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 103 | static constexpr int kPad = 7; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 104 | // gap between rect for each case that is rendered and exterior of rrect |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 105 | static constexpr int kGap = 3; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 106 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 107 | SkRRect fRRect; |
| 108 | int fWidth; |
| 109 | int fHeight; |
| 110 | int fTestWidth; |
| 111 | int fTestHeight; |
| 112 | int fTestOffsetX; |
| 113 | int fTestOffsetY; |
| 114 | const char* fName; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 115 | typedef GM INHERITED; |
| 116 | }; |
| 117 | |
| 118 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 119 | // This value is motivated by bug chromium:477684. It has to be large to cause overflow in |
| 120 | // the shader |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 121 | constexpr int kSize = 700; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 122 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 123 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) |
| 124 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) |
| 125 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) |
| 126 | // The next two have small linear segments between the corners |
| 127 | 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"); ) |
| 128 | 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] | 129 | |
| 130 | } |
| 131 | #endif |