blob: 054708d14f608ee15365d616544e0f8261b2a43f [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 Kleinedf84492018-05-22 12:23:12 +00008#include "Test.h"
9#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"
Robert Phillipsec325342017-10-30 18:02:48 +000016
Mike Kleinedf84492018-05-22 12:23:12 +000017#include "effects/GrConstColorProcessor.h"
Ben Wagnerb5f28972018-04-17 17:42:08 -040018
Robert Phillipsec325342017-10-30 18:02:48 +000019static void only_allow_default(GrContextOptions* options) {
20 options->fGpuPathRenderers = GpuPathRenderers::kNone;
21}
22
23static SkBitmap read_back(GrRenderTargetContext* rtc, int width, int height) {
24
25 SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
26
27 SkBitmap bm;
28 bm.allocPixels(dstII);
29
30 rtc->readPixels(dstII, bm.getAddr(0, 0), bm.rowBytes(), 0, 0, 0);
31
32 return bm;
33}
34
35static SkPath make_path(const SkRect& outer, int inset, SkPath::FillType fill) {
36 SkPath p;
37
38 p.addRect(outer, SkPath::kCW_Direction);
39 p.addRect(outer.makeInset(inset, inset), SkPath::kCCW_Direction);
40 p.setFillType(fill);
41 return p;
42}
43
44
45static const int kBigSize = 64; // This should be a power of 2
46static const int kPad = 3;
47
48// From crbug.com/769898:
49// create an approx fit render target context that will have extra space (i.e., npot)
50// draw an inverse wound concave path into it - forcing use of the stencil-using path renderer
51// throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused
52// create a new render target context that will reuse the prior GrSurface
53// draw a normally wound concave path that touches outside of the approx fit RTC's content rect
54//
55// When the bug manifests the GrDefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil
56// buffer outside of the first content rect in a bad state and the second draw would be incorrect.
57
58static void run_test(GrContext* ctx, skiatest::Reporter* reporter) {
59 SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
60 kBigSize/2-1, SkPath::kInverseWinding_FillType);
61 SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
62 kPad, SkPath::kWinding_FillType);
63
64 GrStyle style(SkStrokeRec::kFill_InitStyle);
65
66 {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050067 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
68 SkBackingFit::kApprox,
Robert Phillipsec325342017-10-30 18:02:48 +000069 kBigSize/2+1, kBigSize/2+1,
70 kRGBA_8888_GrPixelConfig, nullptr);
71
Chris Dalton344e9032017-12-11 15:42:09 -070072 rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
73 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +000074
75 GrPaint paint;
76
77 const GrColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -050078 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +000079 paint.addColorFragmentProcessor(std::move(fp));
80
81 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
82 SkMatrix::I(), invPath, style);
83
84 rtc->prepareForExternalIO(0, nullptr);
85 }
86
87 {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050088 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
89 SkBackingFit::kExact, kBigSize, kBigSize,
Robert Phillipsec325342017-10-30 18:02:48 +000090 kRGBA_8888_GrPixelConfig, nullptr);
91
Chris Dalton344e9032017-12-11 15:42:09 -070092 rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
93 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +000094
95 GrPaint paint;
96
97 const GrColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -050098 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +000099 paint.addColorFragmentProcessor(std::move(fp));
100
101 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
102 SkMatrix::I(), path, style);
103
104 SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
105
106 bool correct = true;
107 for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
108 for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
109 correct = bm.getColor(x, y) == SK_ColorBLACK;
110 REPORTER_ASSERT(reporter, correct);
111 }
112 }
113 }
114
115}
116
117DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
118 sk_gpu_test::GrContextFactory::IsRenderingContext,
119 reporter, ctxInfo, only_allow_default) {
120 GrContext* ctx = ctxInfo.grContext();
121
122 run_test(ctx, reporter);
123}
124
Robert Phillipsec325342017-10-30 18:02:48 +0000125#endif