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