blob: 9596c00db6a665f224045e7be31a8591cff9ae8f [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
82 {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050083 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
84 SkBackingFit::kApprox,
Robert Phillipsec325342017-10-30 18:02:48 +000085 kBigSize/2+1, kBigSize/2+1,
86 kRGBA_8888_GrPixelConfig, nullptr);
87
Chris Dalton344e9032017-12-11 15:42:09 -070088 rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
89 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +000090
91 GrPaint paint;
92
Brian Osmanf28e55d2018-10-03 16:35:54 -040093 const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -050094 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +000095 paint.addColorFragmentProcessor(std::move(fp));
96
97 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
98 SkMatrix::I(), invPath, style);
99
100 rtc->prepareForExternalIO(0, nullptr);
101 }
102
103 {
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500104 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
105 SkBackingFit::kExact, kBigSize, kBigSize,
Robert Phillipsec325342017-10-30 18:02:48 +0000106 kRGBA_8888_GrPixelConfig, nullptr);
107
Chris Dalton344e9032017-12-11 15:42:09 -0700108 rtc->clear(nullptr, GrColorPackRGBA(0x0, 0x0, 0x0, 0xFF),
109 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillipsec325342017-10-30 18:02:48 +0000110
111 GrPaint paint;
112
Brian Osmanf28e55d2018-10-03 16:35:54 -0400113 const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500114 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
Robert Phillipsec325342017-10-30 18:02:48 +0000115 paint.addColorFragmentProcessor(std::move(fp));
116
117 rtc->drawPath(GrNoClip(), std::move(paint), GrAA::kNo,
118 SkMatrix::I(), path, style);
119
120 SkBitmap bm = read_back(rtc.get(), kBigSize, kBigSize);
121
122 bool correct = true;
123 for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
124 for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
125 correct = bm.getColor(x, y) == SK_ColorBLACK;
126 REPORTER_ASSERT(reporter, correct);
127 }
128 }
129 }
130
131}
132
133DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,
134 sk_gpu_test::GrContextFactory::IsRenderingContext,
135 reporter, ctxInfo, only_allow_default) {
136 GrContext* ctx = ctxInfo.grContext();
137
138 run_test(ctx, reporter);
139}