blob: 688ecee3baee72fac6261241127e3930483a813c [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"
Mike Kleinedf84492018-05-22 12:23:12 +000031#include "effects/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 =
83 ctx->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Robert Phillipsec325342017-10-30 18:02:48 +000084 {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050085 auto rtc = ctx->contextPriv().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
102 rtc->prepareForExternalIO(0, nullptr);
103 }
104
105 {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500106 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500107 format, SkBackingFit::kExact, kBigSize,
108 kBigSize, kRGBA_8888_GrPixelConfig,
109 nullptr);
Robert Phillipsec325342017-10-30 18:02:48 +0000110
Brian Osman9a9baae2018-11-05 15:06:26 -0500111 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +0000112
113 GrPaint paint;
114
Brian Osmanf28e55d2018-10-03 16:35:54 -0400115 const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500116 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +0000117 paint.addColorFragmentProcessor(std::move(fp));
118
119 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
120 SkMatrix::I(), path, style);
121
122 SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
123
124 bool correct = true;
125 for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
126 for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
127 correct = bm.getColor(x, y) == SK_ColorBLACK;
128 REPORTER_ASSERT(reporter, correct);
129 }
130 }
131 }
132
133}
134
135DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
136 sk_gpu_test::GrContextFactory::IsRenderingContext,
137 reporter, ctxInfo, only_allow_default) {
138 GrContext* ctx = ctxInfo.grContext();
139
140 run_test(ctx, reporter);
141}