1 /*
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 "SkTypes.h"
9
10 #include "GrClip.h"
11 #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"
18 #include "GrRenderTargetContext.h"
19 #include "GrStyle.h"
20 #include "GrTypesPriv.h"
21 #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"
31 #include "effects/GrConstColorProcessor.h"
32
33 #include <utility>
34
only_allow_default(GrContextOptions * options)35 static void only_allow_default(GrContextOptions* options) {
36 options->fGpuPathRenderers = GpuPathRenderers::kNone;
37 }
38
read_back(GrRenderTargetContext * rtc,int width,int height)39 static 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
make_path(const SkRect & outer,int inset,SkPath::FillType fill)51 static 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
61 static const int kBigSize = 64; // This should be a power of 2
62 static 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
run_test(GrContext * ctx,skiatest::Reporter * reporter)74 static 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 GrBackendFormat format =
83 ctx->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
84 {
85 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
86 format,
87 SkBackingFit::kApprox,
88 kBigSize/2+1, kBigSize/2+1,
89 kRGBA_8888_GrPixelConfig, nullptr);
90
91 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
92
93 GrPaint paint;
94
95 const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
96 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
97 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 {
106 auto rtc = ctx->contextPriv().makeDeferredRenderTargetContext(
107 format, SkBackingFit::kExact, kBigSize,
108 kBigSize, kRGBA_8888_GrPixelConfig,
109 nullptr);
110
111 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
112
113 GrPaint paint;
114
115 const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
116 auto fp = GrConstColorProcessor::Make(color, GrConstColorProcessor::InputMode::kIgnore);
117 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
DEF_GPUTEST_FOR_CONTEXTS(GrDefaultPathRendererTest,sk_gpu_test::GrContextFactory::IsRenderingContext,reporter,ctxInfo,only_allow_default)135 DEF_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 }
142