blob: 677c9241484bfd2d9cb07d5cab3ed333b0268eb7 [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
Ben Wagnerd90cd3b2018-05-22 10:48:08 -04008#include "SkTypes.h"
Robert Phillipsec325342017-10-30 18:02:48 +00009
Robert Phillipsec325342017-10-30 18:02:48 +000010#include "GrClip.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040011#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 Phillipsec325342017-10-30 18:02:48 +000018#include "GrRenderTargetContext.h"
19#include "GrStyle.h"
20#include "GrTypesPriv.h"
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040021#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"
Ethan Nicholas21a9b562019-04-10 12:06:19 -040031#include "effects/generated/GrConstColorProcessor.h"
Ben Wagnerb5f28972018-04-17 17:42:08 -040032
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040033#include <utility>
34
Robert Phillipsec325342017-10-30 18:02:48 +000035static void only_allow_default(GrContextOptions* options) {
36 options->fGpuPathRenderers = GpuPathRenderers::kNone;
37}
38
39static 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
51static 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
61static const int kBigSize = 64; // This should be a power of 2
62static 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
74static 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
Greg Daniel4065d452018-11-16 15:43:41 -050082 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -050083 ctx->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Robert Phillipsec325342017-10-30 18:02:48 +000084 {
Robert Phillips9da87e02019-02-04 13:26:26 -050085 auto rtc = ctx->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -050086 format,
Robert Phillips0c4b7b12018-03-06 08:20:37 -050087 SkBackingFit::kApprox,
Robert Phillipsec325342017-10-30 18:02:48 +000088 kBigSize/2+1, kBigSize/2+1,
89 kRGBA_8888_GrPixelConfig, nullptr);
90
Brian Osman9a9baae2018-11-05 15:06:26 -050091 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +000092
93 GrPaint paint;
94
Brian Osmanf28e55d2018-10-03 16:35:54 -040095 const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -050096 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +000097 paint.addColorFragmentProcessor(std::move(fp));
98
99 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
100 SkMatrix::I(), invPath, style);
101
Greg Daniel4aa13e72019-04-15 14:42:20 -0400102 rtc->flush(SkSurface::BackendSurfaceAccess::kNoAccess,
103 kNone_GrFlushFlags, 0, nullptr, nullptr, nullptr);
Robert Phillipsec325342017-10-30 18:02:48 +0000104 }
105
106 {
Robert Phillips9da87e02019-02-04 13:26:26 -0500107 auto rtc = ctx->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500108 format, SkBackingFit::kExact, kBigSize,
109 kBigSize, kRGBA_8888_GrPixelConfig,
110 nullptr);
Robert Phillipsec325342017-10-30 18:02:48 +0000111
Brian Osman9a9baae2018-11-05 15:06:26 -0500112 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +0000113
114 GrPaint paint;
115
Brian Osmanf28e55d2018-10-03 16:35:54 -0400116 const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500117 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +0000118 paint.addColorFragmentProcessor(std::move(fp));
119
120 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
121 SkMatrix::I(), path, style);
122
123 SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
124
125 bool correct = true;
126 for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
127 for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
128 correct = bm.getColor(x, y) == SK_ColorBLACK;
129 REPORTER_ASSERT(reporter, correct);
130 }
131 }
132 }
133
134}
135
136DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
137 sk_gpu_test::GrContextFactory::IsRenderingContext,
138 reporter, ctxInfo, only_allow_default) {
139 GrContext* ctx = ctxInfo.grContext();
140
141 run_test(ctx, reporter);
142}