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