blob: 176461106f63737666b8517819c863618d92bf8f [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
8#include "Test.h"
Mike Reed75ae4212018-01-23 11:24:08 -05009#include "SkBitmap.h"
Robert Phillipsec325342017-10-30 18:02:48 +000010
11#if SK_SUPPORT_GPU
12#include "GrClip.h"
13#include "GrRenderTargetContext.h"
14#include "GrStyle.h"
15#include "GrTypesPriv.h"
16
17#include "effects/GrConstColorProcessor.h"
18
19static void allow_default_and_msaa(GrContextOptions* options) {
20 options->fGpuPathRenderers = GpuPathRenderers::kMSAA;
21}
22
23static void only_allow_default(GrContextOptions* options) {
24 options->fGpuPathRenderers = GpuPathRenderers::kNone;
25}
26
27static SkBitmap read_back(GrRenderTargetContext* rtc, int width, int height) {
28
29 SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
30
31 SkBitmap bm;
32 bm.allocPixels(dstII);
33
34 rtc->readPixels(dstII, bm.getAddr(0, 0), bm.rowBytes(), 0, 0, 0);
35
36 return bm;
37}
38
39static SkPath make_path(const SkRect& outer, int inset, SkPath::FillType fill) {
40 SkPath p;
41
42 p.addRect(outer, SkPath::kCW_Direction);
43 p.addRect(outer.makeInset(inset, inset), SkPath::kCCW_Direction);
44 p.setFillType(fill);
45 return p;
46}
47
48
49static const int kBigSize = 64; // This should be a power of 2
50static const int kPad = 3;
51
52// From crbug.com/769898:
53// create an approx fit render target context that will have extra space (i.e., npot)
54// draw an inverse wound concave path into it - forcing use of the stencil-using path renderer
55// throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused
56// create a new render target context that will reuse the prior GrSurface
57// draw a normally wound concave path that touches outside of the approx fit RTC's content rect
58//
59// When the bug manifests the GrDefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil
60// buffer outside of the first content rect in a bad state and the second draw would be incorrect.
61
62static void run_test(GrContext* ctx, skiatest::Reporter* reporter) {
63 SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
64 kBigSize/2-1, SkPath::kInverseWinding_FillType);
65 SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
66 kPad, SkPath::kWinding_FillType);
67
68 GrStyle style(SkStrokeRec::kFill_InitStyle);
69
70 {
71 auto rtc = ctx->makeDeferredRenderTargetContext(SkBackingFit::kApprox,
72 kBigSize/2+1, kBigSize/2+1,
73 kRGBA_8888_GrPixelConfig, nullptr);
74
Chris Dalton344e9032017-12-11 15:42:09 -070075 rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
76 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +000077
78 GrPaint paint;
79
80 const GrColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -050081 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +000082 paint.addColorFragmentProcessor(std::move(fp));
83
84 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
85 SkMatrix::I(), invPath, style);
86
87 rtc->prepareForExternalIO(0, nullptr);
88 }
89
90 {
91 auto rtc = ctx->makeDeferredRenderTargetContext(SkBackingFit::kExact, kBigSize, kBigSize,
92 kRGBA_8888_GrPixelConfig, nullptr);
93
Chris Dalton344e9032017-12-11 15:42:09 -070094 rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
95 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +000096
97 GrPaint paint;
98
99 const GrColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500100 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +0000101 paint.addColorFragmentProcessor(std::move(fp));
102
103 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
104 SkMatrix::I(), path, style);
105
106 SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
107
108 bool correct = true;
109 for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
110 for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
111 correct = bm.getColor(x, y) == SK_ColorBLACK;
112 REPORTER_ASSERT(reporter, correct);
113 }
114 }
115 }
116
117}
118
119DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
120 sk_gpu_test::GrContextFactory::IsRenderingContext,
121 reporter, ctxInfo, only_allow_default) {
122 GrContext* ctx = ctxInfo.grContext();
123
124 run_test(ctx, reporter);
125}
126
127DEF_GPUTEST_FOR_CONTEXTS(GrMSAAPathRendererTest,
128 sk_gpu_test::GrContextFactory::IsRenderingContext,
129 reporter, ctxInfo, allow_default_and_msaa) {
130 GrContext* ctx = ctxInfo.grContext();
131
132 if (!ctx->caps()->sampleShadingSupport()) { // The MSAAPathRenderer requires this
133 return;
134 }
135
136 run_test(ctx, reporter);
137}
138
139#endif