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" |
| 9 | #if SK_SUPPORT_GPU |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 10 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 11 | #include "GrRenderTargetContextPriv.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 12 | #include "SkRRect.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 13 | #include "batches/GrDrawBatch.h" |
| 14 | #include "batches/GrRectBatchFactory.h" |
| 15 | #include "effects/GrRRectEffect.h" |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | class BigRRectAAEffectGM : public GM { |
| 22 | public: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 23 | BigRRectAAEffectGM(const SkRRect& rrect, const char* name) |
| 24 | : fRRect(rrect) |
| 25 | , fName(name) { |
| 26 | this->setBGColor(sk_tool_utils::color_to_565(SK_ColorBLUE)); |
| 27 | // Each test case draws the rrect with gaps around it. |
| 28 | fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap; |
| 29 | fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap; |
| 30 | |
| 31 | // Add a pad between test cases. |
| 32 | fTestOffsetX = fTestWidth + kPad; |
| 33 | fTestOffsetY = fTestHeight + kPad; |
| 34 | |
| 35 | // We draw two tests in x (fill and inv-fill) and pad around |
| 36 | // all four sides of the image. |
| 37 | fWidth = 2 * fTestOffsetX + kPad; |
| 38 | fHeight = fTestOffsetY + kPad; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
| 42 | SkString onShortName() override { |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 43 | SkString name; |
| 44 | name.printf("big_rrect_%s_aa_effect", fName); |
| 45 | return name; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 46 | } |
| 47 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 48 | SkISize onISize() override { return SkISize::Make(fWidth, fHeight); } |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 49 | |
| 50 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 51 | GrRenderTargetContext* renderTargetContext = |
| 52 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 53 | if (!renderTargetContext) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 54 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 58 | SkPaint paint; |
| 59 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 60 | int y = kPad; |
| 61 | int x = kPad; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 62 | constexpr GrPrimitiveEdgeType kEdgeTypes[] = { |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 63 | kFillAA_GrProcessorEdgeType, |
| 64 | kInverseFillAA_GrProcessorEdgeType, |
| 65 | }; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 66 | SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 67 | for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { |
| 68 | GrPrimitiveEdgeType edgeType = kEdgeTypes[et]; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 69 | canvas->save(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 70 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 71 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 72 | // Draw a background for the test case |
| 73 | SkPaint paint; |
| 74 | paint.setColor(SK_ColorWHITE); |
| 75 | canvas->drawRect(testBounds, paint); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 76 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 77 | GrPaint grPaint; |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 78 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 79 | |
| 80 | SkRRect rrect = fRRect; |
| 81 | rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 82 | sk_sp<GrFragmentProcessor> fp(GrRRectEffect::Make(edgeType, rrect)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 83 | SkASSERT(fp); |
| 84 | if (fp) { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 85 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 86 | |
| 87 | SkRect bounds = testBounds; |
| 88 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 89 | |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 90 | sk_sp<GrDrawBatch> batch( |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 91 | GrRectBatchFactory::CreateNonAAFill(0xff000000, SkMatrix::I(), bounds, |
| 92 | nullptr, nullptr)); |
Hal Canary | cefc431 | 2016-11-04 16:26:16 -0400 | [diff] [blame] | 93 | renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get()); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 94 | } |
| 95 | canvas->restore(); |
| 96 | x = x + fTestOffsetX; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 100 | private: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 101 | // pad between test cases |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 102 | static constexpr int kPad = 7; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 103 | // gap between rect for each case that is rendered and exterior of rrect |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 104 | static constexpr int kGap = 3; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 105 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 106 | SkRRect fRRect; |
| 107 | int fWidth; |
| 108 | int fHeight; |
| 109 | int fTestWidth; |
| 110 | int fTestHeight; |
| 111 | int fTestOffsetX; |
| 112 | int fTestOffsetY; |
| 113 | const char* fName; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 114 | typedef GM INHERITED; |
| 115 | }; |
| 116 | |
| 117 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 118 | // This value is motivated by bug chromium:477684. It has to be large to cause overflow in |
| 119 | // the shader |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 120 | constexpr int kSize = 700; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 121 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 122 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) |
| 123 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) |
| 124 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) |
| 125 | // The next two have small linear segments between the corners |
| 126 | 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"); ) |
| 127 | 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] | 128 | |
| 129 | } |
| 130 | #endif |