blob: 1e9598ee2f29c0e8116173bae15537b7221ffb45 [file] [log] [blame]
Robert Phillipsec325342017-10-30 18:02:48 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Wagner9707a7e2019-05-06 17:17:19 -040017#include "include/core/SkSurface.h"
18#include "include/core/SkTypes.h"
19#include "include/gpu/GrBackendSurface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/gpu/GrContextOptions.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040021#include "include/gpu/GrDirectContext.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040022#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/private/GrTypesPriv.h"
Ben Wagner9707a7e2019-05-06 17:17:19 -040024#include "include/private/SkColorData.h"
25#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrFragmentProcessor.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040028#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrStyle.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040031#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "tests/Test.h"
33#include "tools/gpu/GrContextFactory.h"
Ben Wagnerb5f28972018-04-17 17:42:08 -040034
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040035#include <utility>
36
Robert Phillipsec325342017-10-30 18:02:48 +000037static void only_allow_default(GrContextOptions* options) {
38 options->fGpuPathRenderers = GpuPathRenderers::kNone;
39}
40
Robert Phillips4dca8312021-07-28 15:13:20 -040041static SkBitmap read_back(GrDirectContext* dContext,
42 skgpu::v1::SurfaceDrawContext* sdc,
Adlai Hollerc95b5892020-08-11 12:02:22 -040043 int width, int height) {
Robert Phillipsec325342017-10-30 18:02:48 +000044
45 SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
46
47 SkBitmap bm;
48 bm.allocPixels(dstII);
49
Robert Phillips4dca8312021-07-28 15:13:20 -040050 sdc->readPixels(dContext, bm.pixmap(), {0, 0});
Robert Phillipsec325342017-10-30 18:02:48 +000051
52 return bm;
53}
54
Mike Reed7d34dc72019-11-26 12:17:17 -050055static SkPath make_path(const SkRect& outer, int inset, SkPathFillType fill) {
Robert Phillipsec325342017-10-30 18:02:48 +000056 SkPath p;
57
Mike Reed30bc5272019-11-22 18:34:02 +000058 p.addRect(outer, SkPathDirection::kCW);
59 p.addRect(outer.makeInset(inset, inset), SkPathDirection::kCCW);
Robert Phillipsec325342017-10-30 18:02:48 +000060 p.setFillType(fill);
61 return p;
62}
63
64
65static const int kBigSize = 64; // This should be a power of 2
66static 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 Phillips461c5392021-08-17 16:42:51 -040075// When the bug manifests the DefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil
Robert Phillipsec325342017-10-30 18:02:48 +000076// buffer outside of the first content rect in a bad state and the second draw would be incorrect.
77
Adlai Hollerc95b5892020-08-11 12:02:22 -040078static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter) {
Robert Phillipsec325342017-10-30 18:02:48 +000079 SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
Mike Reed7d34dc72019-11-26 12:17:17 -050080 kBigSize/2-1, SkPathFillType::kInverseWinding);
Robert Phillipsec325342017-10-30 18:02:48 +000081 SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
Mike Reed7d34dc72019-11-26 12:17:17 -050082 kPad, SkPathFillType::kWinding);
Robert Phillipsec325342017-10-30 18:02:48 +000083
84 GrStyle style(SkStrokeRec::kFill_InitStyle);
85
86 {
Robert Phillips4dca8312021-07-28 15:13:20 -040087 auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
88 SkBackingFit::kApprox,
89 {kBigSize/2 + 1, kBigSize/2 + 1},
90 SkSurfaceProps());
Robert Phillipsec325342017-10-30 18:02:48 +000091
Robert Phillips4dca8312021-07-28 15:13:20 -040092 sdc->clear(SK_PMColor4fBLACK);
Robert Phillipsec325342017-10-30 18:02:48 +000093
94 GrPaint paint;
95
Brian Osmanf28e55d2018-10-03 16:35:54 -040096 const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
Brian Salomon354147a2021-04-14 11:15:05 -040097 auto fp = GrFragmentProcessor::MakeColor(color);
John Stiles5933d7d2020-07-21 12:28:35 -040098 paint.setColorFragmentProcessor(std::move(fp));
Robert Phillipsec325342017-10-30 18:02:48 +000099
Robert Phillips4dca8312021-07-28 15:13:20 -0400100 sdc->drawPath(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), invPath, style);
Robert Phillipsec325342017-10-30 18:02:48 +0000101
Robert Phillips4dca8312021-07-28 15:13:20 -0400102 dContext->priv().flushSurface(sdc->asSurfaceProxy());
Robert Phillipsec325342017-10-30 18:02:48 +0000103 }
104
105 {
Robert Phillips4dca8312021-07-28 15:13:20 -0400106 auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
107 SkBackingFit::kExact, {kBigSize, kBigSize},
108 SkSurfaceProps());
Robert Phillipsec325342017-10-30 18:02:48 +0000109
Robert Phillips4dca8312021-07-28 15:13:20 -0400110 sdc->clear(SK_PMColor4fBLACK);
Robert Phillipsec325342017-10-30 18:02:48 +0000111
112 GrPaint paint;
113
Brian Osmanf28e55d2018-10-03 16:35:54 -0400114 const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
Brian Salomon354147a2021-04-14 11:15:05 -0400115 auto fp = GrFragmentProcessor::MakeColor(color);
John Stiles5933d7d2020-07-21 12:28:35 -0400116 paint.setColorFragmentProcessor(std::move(fp));
Robert Phillipsec325342017-10-30 18:02:48 +0000117
Robert Phillips4dca8312021-07-28 15:13:20 -0400118 sdc->drawPath(nullptr, std::move(paint), GrAA::kNo,
Robert Phillipsec325342017-10-30 18:02:48 +0000119 SkMatrix::I(), path, style);
120
Robert Phillips4dca8312021-07-28 15:13:20 -0400121 SkBitmap bm = read_back(dContext, sdc.get(), kBigSize, kBigSize);
Robert Phillipsec325342017-10-30 18:02:48 +0000122
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 Phillipsec325342017-10-30 18:02:48 +0000131}
132
Robert Phillips461c5392021-08-17 16:42:51 -0400133DEF_GPUTEST_FOR_CONTEXTS(DefaultPathRendererTest,
Robert Phillipsec325342017-10-30 18:02:48 +0000134 sk_gpu_test::GrContextFactory::IsRenderingContext,
135 reporter, ctxInfo, only_allow_default) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400136 auto ctx = ctxInfo.directContext();
Robert Phillipsec325342017-10-30 18:02:48 +0000137
138 run_test(ctx, reporter);
139}