blob: f08f56c226e7c135b9a5f6a5618f6581be123933 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrContextPriv.h"
27#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"
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 Wagnerb5f28972018-04-17 17:42:08 -040035
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040036#include <utility>
37
Robert Phillipsec325342017-10-30 18:02:48 +000038static void only_allow_default(GrContextOptions* options) {
39 options->fGpuPathRenderers = GpuPathRenderers::kNone;
40}
41
42static 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
Brian Salomon1d435302019-07-01 13:05:28 -040049 rtc->readPixels(dstII, bm.getAddr(0, 0), bm.rowBytes(), {0, 0});
Robert Phillipsec325342017-10-30 18:02:48 +000050
51 return bm;
52}
53
Mike Reed7d34dc72019-11-26 12:17:17 -050054static SkPath make_path(const SkRect& outer, int inset, SkPathFillType fill) {
Robert Phillipsec325342017-10-30 18:02:48 +000055 SkPath p;
56
Mike Reed30bc5272019-11-22 18:34:02 +000057 p.addRect(outer, SkPathDirection::kCW);
58 p.addRect(outer.makeInset(inset, inset), SkPathDirection::kCCW);
Robert Phillipsec325342017-10-30 18:02:48 +000059 p.setFillType(fill);
60 return p;
61}
62
63
64static const int kBigSize = 64; // This should be a power of 2
65static 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
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040077static void run_test(GrRecordingContext* rContext, skiatest::Reporter* reporter) {
Robert Phillipsec325342017-10-30 18:02:48 +000078 SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
Mike Reed7d34dc72019-11-26 12:17:17 -050079 kBigSize/2-1, SkPathFillType::kInverseWinding);
Robert Phillipsec325342017-10-30 18:02:48 +000080 SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
Mike Reed7d34dc72019-11-26 12:17:17 -050081 kPad, SkPathFillType::kWinding);
Robert Phillipsec325342017-10-30 18:02:48 +000082
83 GrStyle style(SkStrokeRec::kFill_InitStyle);
84
85 {
Greg Daniele20fcad2020-01-08 11:52:34 -050086 auto rtc = GrRenderTargetContext::Make(
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040087 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox,
Greg Daniele20fcad2020-01-08 11:52:34 -050088 {kBigSize/2 + 1, kBigSize/2 + 1});
Robert Phillipsec325342017-10-30 18:02:48 +000089
Michael Ludwig81d41722020-05-26 16:57:38 -040090 rtc->clear(SK_PMColor4fBLACK);
Robert Phillipsec325342017-10-30 18:02:48 +000091
92 GrPaint paint;
93
Brian Osmanf28e55d2018-10-03 16:35:54 -040094 const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
John Stiles7c196772020-07-13 10:00:16 -040095 auto fp = GrConstColorProcessor::Make(color);
Robert Phillipsec325342017-10-30 18:02:48 +000096 paint.addColorFragmentProcessor(std::move(fp));
97
Michael Ludwig7c12e282020-05-29 09:54:07 -040098 rtc->drawPath(nullptr, std::move(paint), GrAA::kNo,
Robert Phillipsec325342017-10-30 18:02:48 +000099 SkMatrix::I(), invPath, style);
100
Greg Daniel9efe3862020-06-11 11:51:06 -0400101 rtc->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr);
Robert Phillipsec325342017-10-30 18:02:48 +0000102 }
103
104 {
Greg Daniele20fcad2020-01-08 11:52:34 -0500105 auto rtc = GrRenderTargetContext::Make(
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400106 rContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {kBigSize, kBigSize});
Robert Phillipsec325342017-10-30 18:02:48 +0000107
Michael Ludwig81d41722020-05-26 16:57:38 -0400108 rtc->clear(SK_PMColor4fBLACK);
Robert Phillipsec325342017-10-30 18:02:48 +0000109
110 GrPaint paint;
111
Brian Osmanf28e55d2018-10-03 16:35:54 -0400112 const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
John Stiles7c196772020-07-13 10:00:16 -0400113 auto fp = GrConstColorProcessor::Make(color);
Robert Phillipsec325342017-10-30 18:02:48 +0000114 paint.addColorFragmentProcessor(std::move(fp));
115
Michael Ludwig7c12e282020-05-29 09:54:07 -0400116 rtc->drawPath(nullptr, std::move(paint), GrAA::kNo,
Robert Phillipsec325342017-10-30 18:02:48 +0000117 SkMatrix::I(), path, style);
118
119 SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
120
121 bool correct = true;
122 for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
123 for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
124 correct = bm.getColor(x, y) == SK_ColorBLACK;
125 REPORTER_ASSERT(reporter, correct);
126 }
127 }
128 }
Robert Phillipsec325342017-10-30 18:02:48 +0000129}
130
131DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
132 sk_gpu_test::GrContextFactory::IsRenderingContext,
133 reporter, ctxInfo, only_allow_default) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400134 auto ctx = ctxInfo.directContext();
Robert Phillipsec325342017-10-30 18:02:48 +0000135
136 run_test(ctx, reporter);
137}