Flutter Engine
The Flutter Engine
PublicPrecompile.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC
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
9
29
30namespace {
31
32using namespace skgpu::graphite;
33
34void compile(const RendererProvider* rendererProvider,
35 ResourceProvider* resourceProvider,
36 const KeyContext& keyContext,
37 UniquePaintParamsID uniqueID,
38 DrawTypeFlags drawTypes,
39 SkSpan<const RenderPassDesc> renderPassDescs,
40 bool withPrimitiveBlender,
42 for (const Renderer* r : rendererProvider->renderers()) {
43 if (!(r->drawTypes() & drawTypes)) {
44 continue;
45 }
46
47 if (r->emitsPrimitiveColor() != withPrimitiveBlender) {
48 // UniqueIDs are explicitly built either w/ or w/o primitiveBlending so must
49 // match what the Renderer requires
50 continue;
51 }
52
53 if (r->coverage() != coverage) {
54 // For now, UniqueIDs are explicitly built with a specific type of coverage so must
55 // match what the Renderer requires
56 continue;
57 }
58
59 for (auto&& s : r->steps()) {
60 SkASSERT(!s->performsShading() || s->emitsPrimitiveColor() == withPrimitiveBlender);
61
62 UniquePaintParamsID paintID = s->performsShading() ? uniqueID
63 : UniquePaintParamsID::InvalidID();
64 GraphicsPipelineDesc pipelineDesc(s, paintID);
65
66 for (const RenderPassDesc& renderPassDesc : renderPassDescs) {
67 sk_sp<GraphicsPipeline> pipeline = resourceProvider->findOrCreateGraphicsPipeline(
68 keyContext.rtEffectDict(),
69 pipelineDesc,
70 renderPassDesc);
71 if (!pipeline) {
72 SKGPU_LOG_W("Failed to create GraphicsPipeline in precompile!");
73 return;
74 }
75 }
76 }
77 }
78}
79
80} // anonymous namespace
81
82namespace skgpu::graphite {
83
84bool Precompile(Context* context,
86 const GraphicsPipelineDesc& pipelineDesc,
87 const RenderPassDesc& renderPassDesc) {
88 ResourceProvider* resourceProvider = context->priv().resourceProvider();
89
90 sk_sp<GraphicsPipeline> pipeline = resourceProvider->findOrCreateGraphicsPipeline(
91 rteDict,
92 pipelineDesc,
93 renderPassDesc);
94 if (!pipeline) {
95 SKGPU_LOG_W("Failed to create GraphicsPipeline in precompile!");
96 return false;
97 }
98
99 return true;
100}
101
102void Precompile(Context* context, const PaintOptions& options, DrawTypeFlags drawTypes) {
103
105 const Caps* caps = context->priv().caps();
106
107 auto rtEffectDict = std::make_unique<RuntimeEffectDictionary>();
108
110 KeyContext keyContext(
111 caps, dict, rtEffectDict.get(), ci, /* dstTexture= */ nullptr, /* dstOffset= */ {0, 0});
112
115 context, options, keyContext,
116 static_cast<DrawTypeFlags>(drawTypes & ~DrawTypeFlags::kDrawVertices),
117 /* withPrimitiveBlender= */ false,
118 coverage);
119 }
120
121 if (drawTypes & DrawTypeFlags::kDrawVertices) {
123 // drawVertices w/ colors use a primitiveBlender while those w/o don't
124 for (bool withPrimitiveBlender : { true, false }) {
125 PrecompileCombinations(context, options, keyContext,
127 withPrimitiveBlender,
128 coverage);
129 }
130 }
131 }
132}
133
135 const PaintOptions& options,
136 const KeyContext& keyContext,
137 DrawTypeFlags drawTypes,
138 bool withPrimitiveBlender,
140 const Caps* caps = keyContext.caps();
141 // Since the precompilation path's uniforms aren't used and don't change the key,
142 // the exact layout doesn't matter
144
145 SkColorType destCT = keyContext.dstColorInfo().colorType();
146 // TODO: we need iterate over a broader set of TextureInfos here. Perhaps, allow the client
147 // to pass in colorType, mipmapping and protection.
152
153 Swizzle writeSwizzle = caps->getWriteSwizzle(destCT, info);
154 // Note: at least on Metal, the LoadOp, StoreOp and clearColor fields don't influence the
155 // actual RenderPassDescKey.
156 // TODO: if all of the Renderers associated w/ the requested drawTypes require MSAA we
157 // do not need to generate the combinations w/ the non-MSAA RenderPassDescs.
158 const RenderPassDesc renderPassDescs[] = {
160 info,
164 /* clearColor= */ { .0f, .0f, .0f, .0f },
165 /* requiresMSAA= */ true,
166 writeSwizzle),
168 info,
172 /* clearColor= */ { .0f, .0f, .0f, .0f },
173 /* requiresMSAA= */ true,
174 writeSwizzle),
176 info,
180 /* clearColor= */ { .0f, .0f, .0f, .0f },
181 /* requiresMSAA= */ false,
182 writeSwizzle),
184 info,
188 /* clearColor= */ { .0f, .0f, .0f, .0f },
189 /* requiresMSAA= */ false,
190 writeSwizzle),
191 };
192
193 options.priv().buildCombinations(
194 keyContext,
195 &gatherer,
196 drawTypes,
197 withPrimitiveBlender,
198 coverage,
199 [context, &keyContext, &renderPassDescs](UniquePaintParamsID uniqueID,
200 DrawTypeFlags drawTypes,
201 bool withPrimitiveBlender,
203 compile(context->priv().rendererProvider(),
204 context->priv().resourceProvider(),
205 keyContext,
206 uniqueID,
207 drawTypes,
208 renderPassDescs,
209 withPrimitiveBlender,
210 coverage);
211 });
212}
213
214} // namespace skgpu::graphite
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SKGPU_LOG_W(fmt,...)
Definition: Log.h:40
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
SkColorType colorType() const
Definition: SkImageInfo.h:140
skgpu::Swizzle getWriteSwizzle(SkColorType, const TextureInfo &) const
Definition: Caps.cpp:142
virtual TextureInfo getDefaultSampledTextureInfo(SkColorType, Mipmapped mipmapped, Protected, Renderable) const =0
const RendererProvider * rendererProvider() const
Definition: ContextPriv.h:46
const Caps * caps() const
Definition: ContextPriv.h:32
ResourceProvider * resourceProvider() const
Definition: ContextPriv.h:49
const ShaderCodeDictionary * shaderCodeDictionary() const
Definition: ContextPriv.h:34
RuntimeEffectDictionary * rtEffectDict() const
Definition: KeyContext.h:65
const SkColorInfo & dstColorInfo() const
Definition: KeyContext.h:67
const Caps * caps() const
Definition: KeyContext.h:59
SkSpan< const Renderer *const > renderers() const
sk_sp< GraphicsPipeline > findOrCreateGraphicsPipeline(const RuntimeEffectDictionary *, const GraphicsPipelineDesc &, const RenderPassDesc &)
struct MyStruct s
void Precompile(Context *context, const PaintOptions &paintOptions, DrawTypeFlags drawTypes=kMostCommon)
void PrecompileCombinations(Context *context, const PaintOptions &options, const KeyContext &keyContext, DrawTypeFlags drawTypes, bool withPrimitiveBlender, Coverage coverage)
static RenderPassDesc Make(const Caps *caps, const TextureInfo &targetInfo, LoadOp loadOp, StoreOp storeOp, SkEnumBitMask< DepthStencilFlags > depthStencilFlags, const std::array< float, 4 > &clearColor, bool requiresMSAA, Swizzle writeSwizzle)