Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "Test.h" |
| 9 | |
| 10 | #if SK_SUPPORT_GPU |
| 11 | #include "GrClip.h" |
| 12 | #include "GrRenderTargetContext.h" |
| 13 | #include "GrStyle.h" |
| 14 | #include "GrTypesPriv.h" |
| 15 | |
| 16 | #include "effects/GrConstColorProcessor.h" |
| 17 | |
| 18 | static void allow_default_and_msaa(GrContextOptions* options) { |
| 19 | options->fGpuPathRenderers = GpuPathRenderers::kMSAA; |
| 20 | } |
| 21 | |
| 22 | static void only_allow_default(GrContextOptions* options) { |
| 23 | options->fGpuPathRenderers = GpuPathRenderers::kNone; |
| 24 | } |
| 25 | |
| 26 | static SkBitmap read_back(GrRenderTargetContext* rtc, int width, int height) { |
| 27 | |
| 28 | SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height); |
| 29 | |
| 30 | SkBitmap bm; |
| 31 | bm.allocPixels(dstII); |
| 32 | |
| 33 | rtc->readPixels(dstII, bm.getAddr(0, 0), bm.rowBytes(), 0, 0, 0); |
| 34 | |
| 35 | return bm; |
| 36 | } |
| 37 | |
| 38 | static SkPath make_path(const SkRect& outer, int inset, SkPath::FillType fill) { |
| 39 | SkPath p; |
| 40 | |
| 41 | p.addRect(outer, SkPath::kCW_Direction); |
| 42 | p.addRect(outer.makeInset(inset, inset), SkPath::kCCW_Direction); |
| 43 | p.setFillType(fill); |
| 44 | return p; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | static const int kBigSize = 64; // This should be a power of 2 |
| 49 | static const int kPad = 3; |
| 50 | |
| 51 | // From crbug.com/769898: |
| 52 | // create an approx fit render target context that will have extra space (i.e., npot) |
| 53 | // draw an inverse wound concave path into it - forcing use of the stencil-using path renderer |
| 54 | // throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused |
| 55 | // create a new render target context that will reuse the prior GrSurface |
| 56 | // draw a normally wound concave path that touches outside of the approx fit RTC's content rect |
| 57 | // |
| 58 | // When the bug manifests the GrDefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil |
| 59 | // buffer outside of the first content rect in a bad state and the second draw would be incorrect. |
| 60 | |
| 61 | static void run_test(GrContext* ctx, skiatest::Reporter* reporter) { |
| 62 | SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize), |
| 63 | kBigSize/2-1, SkPath::kInverseWinding_FillType); |
| 64 | SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize), |
| 65 | kPad, SkPath::kWinding_FillType); |
| 66 | |
| 67 | GrStyle style(SkStrokeRec::kFill_InitStyle); |
| 68 | |
| 69 | { |
| 70 | auto rtc = ctx->makeDeferredRenderTargetContext(SkBackingFit::kApprox, |
| 71 | kBigSize/2+1, kBigSize/2+1, |
| 72 | kRGBA_8888_GrPixelConfig, nullptr); |
| 73 | |
| 74 | rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF), true); |
| 75 | |
| 76 | GrPaint paint; |
| 77 | |
| 78 | const GrColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f }; |
| 79 | auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::kIgnore_InputMode); |
| 80 | paint.addColorFragmentProcessor(std::move(fp)); |
| 81 | |
| 82 | rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo, |
| 83 | SkMatrix::I(), invPath, style); |
| 84 | |
| 85 | rtc->prepareForExternalIO(0, nullptr); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | auto rtc = ctx->makeDeferredRenderTargetContext(SkBackingFit::kExact, kBigSize, kBigSize, |
| 90 | kRGBA_8888_GrPixelConfig, nullptr); |
| 91 | |
| 92 | rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF), true); |
| 93 | |
| 94 | GrPaint paint; |
| 95 | |
| 96 | const GrColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f }; |
| 97 | auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::kIgnore_InputMode); |
| 98 | paint.addColorFragmentProcessor(std::move(fp)); |
| 99 | |
| 100 | rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo, |
| 101 | SkMatrix::I(), path, style); |
| 102 | |
| 103 | SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize); |
| 104 | |
| 105 | bool correct = true; |
| 106 | for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) { |
| 107 | for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) { |
| 108 | correct = bm.getColor(x, y) == SK_ColorBLACK; |
| 109 | REPORTER_ASSERT(reporter, correct); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest, |
| 117 | sk_gpu_test::GrContextFactory::IsRenderingContext, |
| 118 | reporter, ctxInfo, only_allow_default) { |
| 119 | GrContext* ctx = ctxInfo.grContext(); |
| 120 | |
| 121 | run_test(ctx, reporter); |
| 122 | } |
| 123 | |
| 124 | DEF_GPUTEST_FOR_CONTEXTS(GrMSAAPathRendererTest, |
| 125 | sk_gpu_test::GrContextFactory::IsRenderingContext, |
| 126 | reporter, ctxInfo, allow_default_and_msaa) { |
| 127 | GrContext* ctx = ctxInfo.grContext(); |
| 128 | |
| 129 | if (!ctx->caps()->sampleShadingSupport()) { // The MSAAPathRenderer requires this |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | run_test(ctx, reporter); |
| 134 | } |
| 135 | |
| 136 | #endif |