Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
fragment_program.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <memory>
6#include <sstream>
7
11
14#if IMPELLER_SUPPORTS_RENDERING
16#endif
20
22#include "third_party/skia/include/core/SkString.h"
23#include "third_party/skia/include/effects/SkRuntimeEffect.h"
25
26namespace flutter {
27
29
30static std::string RuntimeStageBackendToString(
32 switch (backend) {
34 return "SkSL";
36 return "Metal";
38 return "OpenGLES";
40 return "Vulkan";
42 return "OpenGLES3";
43 }
44}
45
46namespace {
47Dart_Handle ConvertUniformDescriptionToMap(
48 const impeller::RuntimeUniformDescription& uniform_description) {
49 // The number of KV pairs in the map we send to dart.
50 constexpr int num_entries = 5;
51 Dart_Handle keys = Dart_NewList(num_entries);
52 FML_DCHECK(!Dart_IsError(keys));
53 Dart_Handle values = Dart_NewList(num_entries);
54 FML_DCHECK(!Dart_IsError(values));
55 { // 0
56 Dart_Handle name =
57 Dart_NewStringFromCString(uniform_description.name.c_str());
58 FML_DCHECK(!Dart_IsError(name));
59 [[maybe_unused]] Dart_Handle result =
60 Dart_ListSetAt(keys, 0, Dart_NewStringFromCString("name"));
61 FML_DCHECK(!Dart_IsError(result));
62 result = Dart_ListSetAt(values, 0, name);
63 FML_DCHECK(!Dart_IsError(result));
64 }
65 { // 1
66 Dart_Handle type;
67 switch (uniform_description.type) {
69 type = Dart_NewStringFromCString("Float");
70 break;
72 type = Dart_NewStringFromCString("SampledImage");
73 break;
75 type = Dart_NewStringFromCString("Struct");
76 break;
77 }
78 FML_DCHECK(!Dart_IsError(type));
79 [[maybe_unused]] Dart_Handle result =
80 Dart_ListSetAt(keys, 1, Dart_NewStringFromCString("type"));
81 FML_DCHECK(!Dart_IsError(result));
82 result = Dart_ListSetAt(values, 1, type);
83 FML_DCHECK(!Dart_IsError(result));
84 }
85 { // 2
86 Dart_Handle size =
87 Dart_NewIntegerFromUint64(uniform_description.GetDartSize());
88 FML_DCHECK(!Dart_IsError(size));
89 [[maybe_unused]] Dart_Handle result =
90 Dart_ListSetAt(keys, 2, Dart_NewStringFromCString("size"));
91 FML_DCHECK(!Dart_IsError(result));
92 result = Dart_ListSetAt(values, 2, size);
93 FML_DCHECK(!Dart_IsError(result));
94 }
95 {
96 // 3
97 Dart_Handle struct_member_names =
98 Dart_NewList(uniform_description.struct_fields.size());
99 int i = 0;
100 for (const auto& elem : uniform_description.struct_fields) {
101 Dart_ListSetAt(struct_member_names, i++,
102 Dart_NewStringFromCString(elem.name.c_str()));
103 }
104 [[maybe_unused]] Dart_Handle result = Dart_ListSetAt(
105 keys, 3, Dart_NewStringFromCString("struct_field_names"));
106 FML_DCHECK(!Dart_IsError(result));
107 result = Dart_ListSetAt(values, 3, struct_member_names);
108 FML_DCHECK(!Dart_IsError(result));
109 }
110 {
111 // 4
112 Dart_Handle struct_member_bytes =
113 Dart_NewList(uniform_description.struct_fields.size());
114 int i = 0;
115 for (const auto& elem : uniform_description.struct_fields) {
116 Dart_ListSetAt(struct_member_bytes, i++, Dart_NewInteger(elem.byte_size));
117 }
118 [[maybe_unused]] Dart_Handle result = Dart_ListSetAt(
119 keys, 4, Dart_NewStringFromCString("struct_field_bytes"));
120 FML_DCHECK(!Dart_IsError(result));
121 result = Dart_ListSetAt(values, 4, struct_member_bytes);
122 FML_DCHECK(!Dart_IsError(result));
123 }
124 Dart_Handle map =
125 Dart_NewMap(Dart_TypeString(), keys, Dart_TypeObject(), values);
126
127 return map;
128}
129} // namespace
130
131std::string FragmentProgram::initFromAsset(const std::string& asset_name) {
132 FML_TRACE_EVENT("flutter", "FragmentProgram::initFromAsset", "asset",
133 asset_name);
134 UIDartState* ui_dart_state = UIDartState::Current();
135 std::shared_ptr<AssetManager> asset_manager =
136 ui_dart_state->platform_configuration()->client()->GetAssetManager();
137
138 std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(asset_name);
139 if (data == nullptr) {
140 return std::string("Asset '") + asset_name + std::string("' not found");
141 }
142
143 auto runtime_stages =
145
146 if (!runtime_stages.ok()) {
147 return std::string("Asset '") + asset_name +
148 std::string("' manifest could not be decoded: ") +
149 runtime_stages.status().ToString();
150 }
151
152 if (runtime_stages->empty()) {
153 return std::string("Asset '") + asset_name +
154 std::string("' does not contain any shader data.");
155 }
156
158 ui_dart_state->GetRuntimeStageBackend();
159 std::shared_ptr<impeller::RuntimeStage> runtime_stage =
160 (*runtime_stages)[backend];
161 if (runtime_stage) {
162 // Anchor the registry namespace to the asset path so hot reload of the
163 // same asset evicts the previous registration at the same scoped key,
164 // and so two different FragmentProgram assets cannot collide with each
165 // other or with engine-internal entrypoint names.
166 runtime_stage->SetLibraryId(asset_name);
167 }
168 if (!runtime_stage) {
169 std::ostringstream stream;
170 stream << "Asset '" << asset_name
171 << "' does not contain appropriate runtime stage data for current "
172 "backend ("
173 << RuntimeStageBackendToString(backend) << ")." << std::endl
174 << "Found stages: ";
175 for (const auto& kvp : *runtime_stages) {
176 if (kvp.second) {
177 stream << RuntimeStageBackendToString(kvp.first) << " ";
178 }
179 }
180 return stream.str();
181 }
182
183 int sampled_image_count = 0;
184 size_t other_uniforms_bytes = 0;
185 const std::vector<impeller::RuntimeUniformDescription>& uniforms =
186 runtime_stage->GetUniforms();
187 Dart_Handle uniform_info = Dart_NewList(uniforms.size());
188 FML_DCHECK(!Dart_IsError(uniform_info));
189 for (size_t i = 0; i < uniforms.size(); ++i) {
190 const impeller::RuntimeUniformDescription& uniform_description =
191 uniforms[i];
192
193 Dart_Handle map = ConvertUniformDescriptionToMap(uniform_description);
194 [[maybe_unused]] Dart_Handle dart_result =
195 Dart_ListSetAt(uniform_info, i, map);
196 FML_DCHECK(!Dart_IsError(dart_result));
197
198 if (uniform_description.type ==
200 sampled_image_count++;
201 } else {
202 other_uniforms_bytes += uniform_description.GetDartSize();
203 }
204 }
205
206 if (UIDartState::Current()->IsImpellerEnabled()) {
207 // Spawn (but do not block on) a task that will load the runtime stage and
208 // populate an initial shader variant.
209 auto snapshot_controller = UIDartState::Current()->GetSnapshotDelegate();
210 ui_dart_state->GetTaskRunners().GetRasterTaskRunner()->PostTask(
211 [runtime_stage, snapshot_controller]() {
212 if (!snapshot_controller) {
213 return;
214 }
215 snapshot_controller->CacheRuntimeStage(runtime_stage);
216 });
217#if IMPELLER_SUPPORTS_RENDERING
218 runtime_effect_ = DlRuntimeEffectImpeller::Make(std::move(runtime_stage));
219#endif
220 } else {
221 const auto& code_mapping = runtime_stage->GetCodeMapping();
222 auto code_size = code_mapping->GetSize();
223 const char* sksl =
224 reinterpret_cast<const char*>(code_mapping->GetMapping());
225 // SkString makes a copy.
226 SkRuntimeEffect::Result result =
227 SkRuntimeEffect::MakeForShader(SkString(sksl, code_size));
228 if (result.effect == nullptr) {
229 return std::string("Invalid SkSL:\n") + sksl +
230 std::string("\nSkSL Error:\n") + result.errorText.c_str();
231 }
232 runtime_effect_ = DlRuntimeEffectSkia::Make(result.effect);
233 }
234
235 Dart_Handle ths = Dart_HandleFromWeakPersistent(dart_wrapper());
236 if (Dart_IsError(ths)) {
237 Dart_PropagateError(ths);
238 }
239
240 Dart_Handle result = Dart_SetField(ths, tonic::ToDart("_samplerCount"),
241 Dart_NewInteger(sampled_image_count));
242 if (Dart_IsError(result)) {
243 return "Failed to set sampler count for fragment program.";
244 }
245
246 size_t rounded_uniform_bytes =
247 (other_uniforms_bytes + sizeof(float) - 1) & ~(sizeof(float) - 1);
248 size_t float_count = rounded_uniform_bytes / sizeof(float);
249
250 result = Dart_SetField(ths, tonic::ToDart("_uniformFloatCount"),
251 Dart_NewInteger(float_count));
252 if (Dart_IsError(result)) {
253 return "Failed to set uniform float count for fragment program.";
254 }
255
256 result = Dart_SetField(ths, tonic::ToDart("_uniformInfo"), uniform_info);
257 if (Dart_IsError(result)) {
258 FML_DLOG(ERROR) << Dart_GetError(result);
259 return "Failed to set uniform info for fragment program.";
260 }
261
262 return "";
263}
264
265std::shared_ptr<DlColorSource> FragmentProgram::MakeDlColorSource(
266 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
267 const std::vector<std::shared_ptr<DlColorSource>>& children) {
268 return DlColorSource::MakeRuntimeEffect(runtime_effect_, children,
269 std::move(float_uniforms));
270}
271
272std::shared_ptr<DlImageFilter> FragmentProgram::MakeDlImageFilter(
273 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
274 const std::vector<std::shared_ptr<DlColorSource>>& children) {
275 return DlImageFilter::MakeRuntimeEffect(runtime_effect_, children,
276 std::move(float_uniforms));
277}
278
279void FragmentProgram::Create(Dart_Handle wrapper) {
280 auto res = fml::MakeRefCounted<FragmentProgram>();
281 res->AssociateWithDartWrapper(wrapper);
282}
283
284FragmentProgram::FragmentProgram() = default;
285
287
288} // namespace flutter
static std::shared_ptr< DlColorSource > MakeRuntimeEffect(sk_sp< DlRuntimeEffect > runtime_effect, std::vector< std::shared_ptr< DlColorSource > > samplers, std::shared_ptr< std::vector< uint8_t > > uniform_data)
static std::shared_ptr< DlImageFilter > MakeRuntimeEffect(sk_sp< DlRuntimeEffect > runtime_effect, std::vector< std::shared_ptr< DlColorSource > > samplers, std::shared_ptr< std::vector< uint8_t > > uniform_data)
static sk_sp< DlRuntimeEffect > Make(std::shared_ptr< impeller::RuntimeStage > runtime_stage)
static sk_sp< DlRuntimeEffect > Make(const sk_sp< SkRuntimeEffect > &runtime_effect)
std::shared_ptr< DlImageFilter > MakeDlImageFilter(std::shared_ptr< std::vector< uint8_t > > float_uniforms, const std::vector< std::shared_ptr< DlColorSource > > &children)
std::shared_ptr< DlColorSource > MakeDlColorSource(std::shared_ptr< std::vector< uint8_t > > float_uniforms, const std::vector< std::shared_ptr< DlColorSource > > &children)
std::string initFromAsset(const std::string &asset_name)
static void Create(Dart_Handle wrapper)
virtual std::shared_ptr< AssetManager > GetAssetManager()=0
Returns the current collection of assets available on the platform.
PlatformConfigurationClient * client() const
Access to the platform configuration client (which typically is implemented by the RuntimeController)...
fml::RefPtr< fml::TaskRunner > GetRasterTaskRunner() const
PlatformConfiguration * platform_configuration() const
const TaskRunners & GetTaskRunners() const
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
impeller::RuntimeStageBackend GetRuntimeStageBackend() const
The runtime stage to use for fragment shaders.
static UIDartState * Current()
virtual void PostTask(const fml::closure &task) override
static absl::StatusOr< Map > DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
Dart_WeakPersistentHandle dart_wrapper() const
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
#define FML_DLOG(severity)
Definition logging.h:121
#define FML_DCHECK(condition)
Definition logging.h:122
it will be possible to load the file into Perfetto s trace viewer use test Running tests that layout and measure text will not yield consistent results across various platforms Enabling this option will make font resolution default to the Ahem test font on all disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switch_defs.h:36
static std::string RuntimeStageBackendToString(impeller::RuntimeStageBackend backend)
Dart_Handle ToDart(const T &object)
impeller::ShaderType type
size_t GetDartSize() const
Computes the total number of bytes that this uniform requires for representation in the Dart float bu...
std::vector< StructField > struct_fields
#define FML_TRACE_EVENT(category_group, name,...)