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" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 11 | #include "GrDrawContextPriv.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 { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 51 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 52 | if (!drawContext) { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 53 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 57 | SkPaint paint; |
| 58 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 59 | int y = kPad; |
| 60 | int x = kPad; |
| 61 | static const GrPrimitiveEdgeType kEdgeTypes[] = { |
| 62 | kFillAA_GrProcessorEdgeType, |
| 63 | kInverseFillAA_GrProcessorEdgeType, |
| 64 | }; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 65 | SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 66 | for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { |
| 67 | GrPrimitiveEdgeType edgeType = kEdgeTypes[et]; |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 68 | canvas->save(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 69 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 70 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 71 | // Draw a background for the test case |
| 72 | SkPaint paint; |
| 73 | paint.setColor(SK_ColorWHITE); |
| 74 | canvas->drawRect(testBounds, paint); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 75 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 76 | GrPaint grPaint; |
| 77 | grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 78 | |
| 79 | SkRRect rrect = fRRect; |
| 80 | rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 81 | sk_sp<GrFragmentProcessor> fp(GrRRectEffect::Make(edgeType, rrect)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 82 | SkASSERT(fp); |
| 83 | if (fp) { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 84 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 85 | |
| 86 | SkRect bounds = testBounds; |
| 87 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 88 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 89 | SkAutoTUnref<GrDrawBatch> batch( |
| 90 | GrRectBatchFactory::CreateNonAAFill(0xff000000, SkMatrix::I(), bounds, |
| 91 | nullptr, nullptr)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 92 | drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 93 | } |
| 94 | canvas->restore(); |
| 95 | x = x + fTestOffsetX; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 99 | private: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 100 | // pad between test cases |
| 101 | static const int kPad = 7; |
| 102 | // gap between rect for each case that is rendered and exterior of rrect |
| 103 | static const int kGap = 3; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 104 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 105 | SkRRect fRRect; |
| 106 | int fWidth; |
| 107 | int fHeight; |
| 108 | int fTestWidth; |
| 109 | int fTestHeight; |
| 110 | int fTestOffsetX; |
| 111 | int fTestOffsetY; |
| 112 | const char* fName; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 113 | typedef GM INHERITED; |
| 114 | }; |
| 115 | |
| 116 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 117 | // This value is motivated by bug chromium:477684. It has to be large to cause overflow in |
| 118 | // the shader |
| 119 | static const int kSize = 700; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 120 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 121 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) |
| 122 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) |
| 123 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) |
| 124 | // The next two have small linear segments between the corners |
| 125 | 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"); ) |
| 126 | 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] | 127 | |
| 128 | } |
| 129 | #endif |