• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkBitmap.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPathTypes.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkStrokeRec.h"
17 #include "include/core/SkSurfaceProps.h"
18 #include "include/core/SkTypes.h"
19 #include "include/gpu/GrContextOptions.h"
20 #include "include/gpu/GrDirectContext.h"
21 #include "include/private/SkColorData.h"
22 #include "include/private/gpu/ganesh/GrTypesPriv.h"
23 #include "src/gpu/SkBackingFit.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25 #include "src/gpu/ganesh/GrFragmentProcessor.h"
26 #include "src/gpu/ganesh/GrPaint.h"
27 #include "src/gpu/ganesh/GrStyle.h"
28 #include "src/gpu/ganesh/SurfaceDrawContext.h"
29 #include "tests/CtsEnforcement.h"
30 #include "tests/Test.h"
31 
32 #include <memory>
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(GrDirectContext * dContext,skgpu::v1::SurfaceDrawContext * sdc,int width,int height)39 static SkBitmap read_back(GrDirectContext* dContext,
40                           skgpu::v1::SurfaceDrawContext* sdc,
41                           int width, int height) {
42 
43     SkImageInfo dstII = SkImageInfo::MakeN32Premul(width, height);
44 
45     SkBitmap bm;
46     bm.allocPixels(dstII);
47 
48     sdc->readPixels(dContext, bm.pixmap(), {0, 0});
49 
50     return bm;
51 }
52 
make_path(const SkRect & outer,int inset,SkPathFillType fill)53 static SkPath make_path(const SkRect& outer, int inset, SkPathFillType fill) {
54     SkPath p;
55 
56     p.addRect(outer, SkPathDirection::kCW);
57     p.addRect(outer.makeInset(inset, inset), SkPathDirection::kCCW);
58     p.setFillType(fill);
59     return p;
60 }
61 
62 
63 static const int kBigSize = 64; // This should be a power of 2
64 static const int kPad = 3;
65 
66 // From crbug.com/769898:
67 //   create an approx fit render target context that will have extra space (i.e., npot)
68 //   draw an inverse wound concave path into it - forcing use of the stencil-using path renderer
69 //   throw the RTC away so the backing GrSurface/GrStencilBuffer can be reused
70 //   create a new render target context that will reuse the prior GrSurface
71 //   draw a normally wound concave path that touches outside of the approx fit RTC's content rect
72 //
73 // When the bug manifests the DefaultPathRenderer/GrMSAAPathRenderer is/was leaving the stencil
74 // buffer outside of the first content rect in a bad state and the second draw would be incorrect.
75 
run_test(GrDirectContext * dContext,skiatest::Reporter * reporter)76 static void run_test(GrDirectContext* dContext, skiatest::Reporter* reporter) {
77     SkPath invPath = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
78                                kBigSize/2-1, SkPathFillType::kInverseWinding);
79     SkPath path = make_path(SkRect::MakeXYWH(0, 0, kBigSize, kBigSize),
80                             kPad, SkPathFillType::kWinding);
81 
82     GrStyle style(SkStrokeRec::kFill_InitStyle);
83 
84     {
85         auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
86                                                        SkBackingFit::kApprox,
87                                                        {kBigSize/2 + 1, kBigSize/2 + 1},
88                                                        SkSurfaceProps(), /*label=*/{});
89 
90         sdc->clear(SK_PMColor4fBLACK);
91 
92         GrPaint paint;
93 
94         const SkPMColor4f color = { 1.0f, 0.0f, 0.0f, 1.0f };
95         auto fp = GrFragmentProcessor::MakeColor(color);
96         paint.setColorFragmentProcessor(std::move(fp));
97 
98         sdc->drawPath(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), invPath, style);
99 
100         dContext->priv().flushSurface(sdc->asSurfaceProxy());
101     }
102 
103     {
104         auto sdc = skgpu::v1::SurfaceDrawContext::Make(dContext, GrColorType::kRGBA_8888, nullptr,
105                                                        SkBackingFit::kExact, {kBigSize, kBigSize},
106                                                        SkSurfaceProps(), /*label=*/{});
107 
108         sdc->clear(SK_PMColor4fBLACK);
109 
110         GrPaint paint;
111 
112         const SkPMColor4f color = { 0.0f, 1.0f, 0.0f, 1.0f };
113         auto fp = GrFragmentProcessor::MakeColor(color);
114         paint.setColorFragmentProcessor(std::move(fp));
115 
116         sdc->drawPath(nullptr, std::move(paint), GrAA::kNo,
117                       SkMatrix::I(), path, style);
118 
119         SkBitmap bm = read_back(dContext, sdc.get(), kBigSize, kBigSize);
120 
121         bool correct = true;
122         for (int y = kBigSize/2+1; y < kBigSize-kPad-1 && correct; ++y) {
123             for (int x = kPad+1; x < kBigSize-kPad-1 && correct; ++x) {
124                 correct = bm.getColor(x, y) == SK_ColorBLACK;
125                 REPORTER_ASSERT(reporter, correct);
126             }
127         }
128     }
129 }
130 
DEF_GANESH_TEST_FOR_CONTEXTS(DefaultPathRendererTest,sk_gpu_test::GrContextFactory::IsRenderingContext,reporter,ctxInfo,only_allow_default,CtsEnforcement::kApiLevel_T)131 DEF_GANESH_TEST_FOR_CONTEXTS(DefaultPathRendererTest,
132                              sk_gpu_test::GrContextFactory::IsRenderingContext,
133                              reporter,
134                              ctxInfo,
135                              only_allow_default,
136                              CtsEnforcement::kApiLevel_T) {
137     auto ctx = ctxInfo.directContext();
138 
139     run_test(ctx, reporter);
140 }
141