1/*
2 * Copyright 2011 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
9#include "src/gpu/GrPathRendererChain.h"
10
11#include "include/gpu/GrContext.h"
12#include "include/private/GrRecordingContext.h"
13#include "src/gpu/GrCaps.h"
14#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrRecordingContextPriv.h"
17#include "src/gpu/GrShaderCaps.h"
18#include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
19#include "src/gpu/ops/GrAAConvexPathRenderer.h"
20#include "src/gpu/ops/GrAAHairLinePathRenderer.h"
21#include "src/gpu/ops/GrAALinearizingConvexPathRenderer.h"
22#include "src/gpu/ops/GrDashLinePathRenderer.h"
23#include "src/gpu/ops/GrDefaultPathRenderer.h"
24#include "src/gpu/ops/GrSmallPathRenderer.h"
25#include "src/gpu/ops/GrStencilAndCoverPathRenderer.h"
26#include "src/gpu/ops/GrTriangulatingPathRenderer.h"
27#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
28
29GrPathRendererChain::GrPathRendererChain(GrRecordingContext* context, const Options& options) {
30 const GrCaps& caps = *context->priv().caps();
31 if (options.fGpuPathRenderers & GpuPathRenderers::kDashLine) {
32 fChain.push_back(sk_make_sp<GrDashLinePathRenderer>());
33 }
34 if (options.fGpuPathRenderers & GpuPathRenderers::kTessellation) {
35 if (caps.shaderCaps()->tessellationSupport()) {
36 auto tess = sk_make_sp<GrTessellationPathRenderer>(caps);
37 context->priv().addOnFlushCallbackObject(tess.get());
38 fChain.push_back(std::move(tess));
39 }
40 }
41 if (options.fGpuPathRenderers & GpuPathRenderers::kAAConvex) {
42 fChain.push_back(sk_make_sp<GrAAConvexPathRenderer>());
43 }
44 if (options.fGpuPathRenderers & GpuPathRenderers::kCoverageCounting) {
45 using AllowCaching = GrCoverageCountingPathRenderer::AllowCaching;
46 if (auto ccpr = GrCoverageCountingPathRenderer::CreateIfSupported(
47 caps, AllowCaching(options.fAllowPathMaskCaching),
48 context->priv().contextID())) {
49 fCoverageCountingPathRenderer = ccpr.get();
50 context->priv().addOnFlushCallbackObject(fCoverageCountingPathRenderer);
51 fChain.push_back(std::move(ccpr));
52 }
53 }
54 if (options.fGpuPathRenderers & GpuPathRenderers::kAAHairline) {
55 fChain.push_back(sk_make_sp<GrAAHairLinePathRenderer>());
56 }
57 if (options.fGpuPathRenderers & GpuPathRenderers::kAALinearizing) {
58 fChain.push_back(sk_make_sp<GrAALinearizingConvexPathRenderer>());
59 }
60 if (options.fGpuPathRenderers & GpuPathRenderers::kSmall) {
61 auto spr = sk_make_sp<GrSmallPathRenderer>();
62 context->priv().addOnFlushCallbackObject(spr.get());
63 fChain.push_back(std::move(spr));
64 }
65 if (options.fGpuPathRenderers & GpuPathRenderers::kStencilAndCover) {
66 auto direct = context->priv().asDirectContext();
67 if (direct) {
68 auto resourceProvider = direct->priv().resourceProvider();
69
70 sk_sp<GrPathRenderer> pr(
71 GrStencilAndCoverPathRenderer::Create(resourceProvider, caps));
72 if (pr) {
73 fChain.push_back(std::move(pr));
74 }
75 }
76 }
77 if (options.fGpuPathRenderers & GpuPathRenderers::kTriangulating) {
78 fChain.push_back(sk_make_sp<GrTriangulatingPathRenderer>());
79 }
80
81 // We always include the default path renderer (as well as SW), so we can draw any path
82 fChain.push_back(sk_make_sp<GrDefaultPathRenderer>());
83}
84
85GrPathRenderer* GrPathRendererChain::getPathRenderer(
86 const GrPathRenderer::CanDrawPathArgs& args,
87 DrawType drawType,
88 GrPathRenderer::StencilSupport* stencilSupport) {
89 static_assert(GrPathRenderer::kNoSupport_StencilSupport <
90 GrPathRenderer::kStencilOnly_StencilSupport);
91 static_assert(GrPathRenderer::kStencilOnly_StencilSupport <
92 GrPathRenderer::kNoRestriction_StencilSupport);
93 GrPathRenderer::StencilSupport minStencilSupport;
94 if (DrawType::kStencil == drawType) {
95 minStencilSupport = GrPathRenderer::kStencilOnly_StencilSupport;
96 } else if (DrawType::kStencilAndColor == drawType) {
97 minStencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
98 } else {
99 minStencilSupport = GrPathRenderer::kNoSupport_StencilSupport;
100 }
101 if (minStencilSupport != GrPathRenderer::kNoSupport_StencilSupport) {
102 // We don't support (and shouldn't need) stenciling of non-fill paths.
103 if (!args.fShape->style().isSimpleFill()) {
104 return nullptr;
105 }
106 }
107
108 GrPathRenderer* bestPathRenderer = nullptr;
109 for (const sk_sp<GrPathRenderer>& pr : fChain) {
110 GrPathRenderer::StencilSupport support = GrPathRenderer::kNoSupport_StencilSupport;
111 if (GrPathRenderer::kNoSupport_StencilSupport != minStencilSupport) {
112 support = pr->getStencilSupport(*args.fShape);
113 if (support < minStencilSupport) {
114 continue;
115 }
116 }
117 GrPathRenderer::CanDrawPath canDrawPath = pr->canDrawPath(args);
118 if (GrPathRenderer::CanDrawPath::kNo == canDrawPath) {
119 continue;
120 }
121 if (GrPathRenderer::CanDrawPath::kAsBackup == canDrawPath && bestPathRenderer) {
122 continue;
123 }
124 if (stencilSupport) {
125 *stencilSupport = support;
126 }
127 bestPathRenderer = pr.get();
128 if (GrPathRenderer::CanDrawPath::kYes == canDrawPath) {
129 break;
130 }
131 }
132 return bestPathRenderer;
133}
134