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/GrContextOptions.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 21 | #include "include/gpu/GrDirectContext.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" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 26 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrFragmentProcessor.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 28 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/GrPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "src/gpu/GrStyle.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 31 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "tests/Test.h" |
| 33 | #include "tools/gpu/GrContextFactory.h" |
Ben Wagner | b5f2897 | 2018-04-17 17:42:08 -0400 | [diff] [blame] | 34 | |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 35 | #include <utility> |
| 36 | |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 37 | static void only_allow_default(GrContextOptions* options) { |
| 38 | options->fGpuPathRenderers = GpuPathRenderers::kNone; |
| 39 | } |
| 40 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 41 | static SkBitmap read_back(GrDirectContext* dContext, |
| 42 | skgpu::v1::SurfaceDrawContext* sdc, |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 43 | int width, int height) { |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 44 | |
| 45 | SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height); |
| 46 | |
| 47 | SkBitmap bm; |
| 48 | bm.allocPixels(dstII); |
| 49 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 50 | sdc->readPixels(dContext, bm.pixmap(), {0, 0}); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 51 | |
| 52 | return bm; |
| 53 | } |
| 54 | |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 55 | static SkPath make_path(const SkRect& outer, int inset, SkPathFillType fill) { |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 56 | SkPath p; |
| 57 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 58 | p.addRect(outer, SkPathDirection::kCW); |
| 59 | p.addRect(outer.makeInset(inset, inset), SkPathDirection::kCCW); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 60 | p.setFillType(fill); |
| 61 | return p; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static const int kBigSize = 64; // This should be a power of 2 |
| 66 | static const int kPad = 3; |
| 67 | |
| 68 | // From crbug.com/769898: |
| 69 | // create an approx fit render target context that will have extra space (i.e., npot) |
| 70 | // draw an inverse wound concave path into it - forcing use of the stencil-using path renderer |
| 71 | // throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused |
| 72 | // create a new render target context that will reuse the prior GrSurface |
| 73 | // draw a normally wound concave path that touches outside of the approx fit RTC's content rect |
| 74 | // |
Robert Phillips | 461c539 | 2021-08-17 16:42:51 -0400 | [diff] [blame] | 75 | // When the bug manifests the DefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 76 | // buffer outside of the first content rect in a bad state and the second draw would be incorrect. |
| 77 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 78 | static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter) { |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 79 | SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize), |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 80 | kBigSize/2-1, SkPathFillType::kInverseWinding); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 81 | SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize), |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 82 | kPad, SkPathFillType::kWinding); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 83 | |
| 84 | GrStyle style(SkStrokeRec::kFill_InitStyle); |
| 85 | |
| 86 | { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 87 | auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr, |
| 88 | SkBackingFit::kApprox, |
| 89 | {kBigSize/2 + 1, kBigSize/2 + 1}, |
| 90 | SkSurfaceProps()); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 91 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 92 | sdc->clear(SK_PMColor4fBLACK); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 93 | |
| 94 | GrPaint paint; |
| 95 | |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 96 | const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f }; |
Brian Salomon | 354147a | 2021-04-14 11:15:05 -0400 | [diff] [blame] | 97 | auto fp = GrFragmentProcessor::MakeColor(color); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 98 | paint.setColorFragmentProcessor(std::move(fp)); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 99 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 100 | sdc->drawPath(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), invPath, style); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 101 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 102 | dContext->priv().flushSurface(sdc->asSurfaceProxy()); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 106 | auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr, |
| 107 | SkBackingFit::kExact, {kBigSize, kBigSize}, |
| 108 | SkSurfaceProps()); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 109 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 110 | sdc->clear(SK_PMColor4fBLACK); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 111 | |
| 112 | GrPaint paint; |
| 113 | |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 114 | const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f }; |
Brian Salomon | 354147a | 2021-04-14 11:15:05 -0400 | [diff] [blame] | 115 | auto fp = GrFragmentProcessor::MakeColor(color); |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 116 | paint.setColorFragmentProcessor(std::move(fp)); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 117 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 118 | sdc->drawPath(nullptr, std::move(paint), GrAA::kNo, |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 119 | SkMatrix::I(), path, style); |
| 120 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 121 | SkBitmap bm = read_back(dContext, sdc.get(), kBigSize, kBigSize); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 122 | |
| 123 | bool correct = true; |
| 124 | for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) { |
| 125 | for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) { |
| 126 | correct = bm.getColor(x, y) == SK_ColorBLACK; |
| 127 | REPORTER_ASSERT(reporter, correct); |
| 128 | } |
| 129 | } |
| 130 | } |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Robert Phillips | 461c539 | 2021-08-17 16:42:51 -0400 | [diff] [blame] | 133 | DEF_GPUTEST_FOR_CONTEXTS(DefaultPathRendererTest, |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 134 | sk_gpu_test::GrContextFactory::IsRenderingContext, |
| 135 | reporter, ctxInfo, only_allow_default) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 136 | auto ctx = ctxInfo.directContext(); |
Robert Phillips | ec32534 | 2017-10-30 18:02:48 +0000 | [diff] [blame] | 137 | |
| 138 | run_test(ctx, reporter); |
| 139 | } |