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