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" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 12 | #include "GrPipelineBuilder.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 "batches/GrDrawBatch.h" |
| 15 | #include "batches/GrRectBatchFactory.h" |
| 16 | #include "effects/GrRRectEffect.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 { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 52 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 53 | if (!drawContext) { |
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; |
| 62 | static const GrPrimitiveEdgeType kEdgeTypes[] = { |
| 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 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 77 | GrPipelineBuilder pipelineBuilder; |
| 78 | pipelineBuilder.setXPFactory( |
| 79 | GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref(); |
| 80 | |
| 81 | SkRRect rrect = fRRect; |
| 82 | rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap)); |
| 83 | SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType, rrect)); |
| 84 | SkASSERT(fp); |
| 85 | if (fp) { |
| 86 | pipelineBuilder.addCoverageFragmentProcessor(fp); |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 87 | pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget()); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 88 | |
| 89 | SkRect bounds = testBounds; |
| 90 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 91 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 92 | SkAutoTUnref<GrDrawBatch> batch( |
| 93 | GrRectBatchFactory::CreateNonAAFill(0xff000000, SkMatrix::I(), bounds, |
| 94 | nullptr, nullptr)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 95 | drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch); |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 96 | } |
| 97 | canvas->restore(); |
| 98 | x = x + fTestOffsetX; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 102 | private: |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 103 | // pad between test cases |
| 104 | static const int kPad = 7; |
| 105 | // gap between rect for each case that is rendered and exterior of rrect |
| 106 | static const int kGap = 3; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 107 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 108 | SkRRect fRRect; |
| 109 | int fWidth; |
| 110 | int fHeight; |
| 111 | int fTestWidth; |
| 112 | int fTestHeight; |
| 113 | int fTestOffsetX; |
| 114 | int fTestOffsetY; |
| 115 | const char* fName; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 116 | typedef GM INHERITED; |
| 117 | }; |
| 118 | |
| 119 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 120 | // This value is motivated by bug chromium:477684. It has to be large to cause overflow in |
| 121 | // the shader |
| 122 | static const int kSize = 700; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 123 | |
bsalomon | 4a4f14b | 2015-12-09 10:17:35 -0800 | [diff] [blame] | 124 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); ) |
| 125 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); ) |
| 126 | DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); ) |
| 127 | // The next two have small linear segments between the corners |
| 128 | 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"); ) |
| 129 | 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] | 130 | |
| 131 | } |
| 132 | #endif |