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 constexpr int num_entries = 3;
50 Dart_Handle keys = Dart_NewList(num_entries);
51 FML_DCHECK(!Dart_IsError(keys));
52 Dart_Handle values = Dart_NewList(num_entries);
53 FML_DCHECK(!Dart_IsError(values));
54 { // 0
55 Dart_Handle name =
56 Dart_NewStringFromCString(uniform_description.name.c_str());
57 FML_DCHECK(!Dart_IsError(name));
58 [[maybe_unused]] Dart_Handle result =
59 Dart_ListSetAt(keys, 0, Dart_NewStringFromCString("name"));
60 FML_DCHECK(!Dart_IsError(result));
61 result = Dart_ListSetAt(values, 0, name);
62 FML_DCHECK(!Dart_IsError(result));
63 }
64 { // 1
65 Dart_Handle type;
66 switch (uniform_description.type) {
68 type = Dart_NewStringFromCString("Float");
69 break;
71 type = Dart_NewStringFromCString("SampledImage");
72 break;
74 type = Dart_NewStringFromCString("Struct");
75 break;
76 }
77 FML_DCHECK(!Dart_IsError(type));
78 [[maybe_unused]] Dart_Handle result =
79 Dart_ListSetAt(keys, 1, Dart_NewStringFromCString("type"));
80 FML_DCHECK(!Dart_IsError(result));
81 result = Dart_ListSetAt(values, 1, type);
82 FML_DCHECK(!Dart_IsError(result));
83 }
84 { // 2
85 Dart_Handle size = Dart_NewIntegerFromUint64(uniform_description.GetSize());
86 FML_DCHECK(!Dart_IsError(size));
87 [[maybe_unused]] Dart_Handle result =
88 Dart_ListSetAt(keys, 2, Dart_NewStringFromCString("size"));
89 FML_DCHECK(!Dart_IsError(result));
90 result = Dart_ListSetAt(values, 2, size);
91 FML_DCHECK(!Dart_IsError(result));
92 }
93 Dart_Handle map =
94 Dart_NewMap(Dart_TypeString(), keys, Dart_TypeObject(), values);
95
96 return map;
97}
98} // namespace
99
100std::string FragmentProgram::initFromAsset(const std::string& asset_name) {
101 FML_TRACE_EVENT("flutter", "FragmentProgram::initFromAsset", "asset",
102 asset_name);
103 UIDartState* ui_dart_state = UIDartState::Current();
104 std::shared_ptr<AssetManager> asset_manager =
105 ui_dart_state->platform_configuration()->client()->GetAssetManager();
106
107 std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(asset_name);
108 if (data == nullptr) {
109 return std::string("Asset '") + asset_name + std::string("' not found");
110 }
111
112 auto runtime_stages =
114
115 if (!runtime_stages.ok()) {
116 return std::string("Asset '") + asset_name +
117 std::string("' manifest could not be decoded: ") +
118 runtime_stages.status().ToString();
119 }
120
121 if (runtime_stages->empty()) {
122 return std::string("Asset '") + asset_name +
123 std::string("' does not contain any shader data.");
124 }
125
127 ui_dart_state->GetRuntimeStageBackend();
128 std::shared_ptr<impeller::RuntimeStage> runtime_stage =
129 (*runtime_stages)[backend];
130 if (!runtime_stage) {
131 std::ostringstream stream;
132 stream << "Asset '" << asset_name
133 << "' does not contain appropriate runtime stage data for current "
134 "backend ("
135 << RuntimeStageBackendToString(backend) << ")." << std::endl
136 << "Found stages: ";
137 for (const auto& kvp : *runtime_stages) {
138 if (kvp.second) {
139 stream << RuntimeStageBackendToString(kvp.first) << " ";
140 }
141 }
142 return stream.str();
143 }
144
145 int sampled_image_count = 0;
146 size_t other_uniforms_bytes = 0;
147 const std::vector<impeller::RuntimeUniformDescription>& uniforms =
148 runtime_stage->GetUniforms();
149 Dart_Handle uniform_info = Dart_NewList(uniforms.size());
150 FML_DCHECK(!Dart_IsError(uniform_info));
151 for (size_t i = 0; i < uniforms.size(); ++i) {
152 const impeller::RuntimeUniformDescription& uniform_description =
153 uniforms[i];
154
155 Dart_Handle map = ConvertUniformDescriptionToMap(uniform_description);
156 [[maybe_unused]] Dart_Handle dart_result =
157 Dart_ListSetAt(uniform_info, i, map);
158 FML_DCHECK(!Dart_IsError(dart_result));
159
160 if (uniform_description.type ==
162 sampled_image_count++;
163 } else {
164 other_uniforms_bytes += uniform_description.GetSize();
165 }
166 }
167
168 if (UIDartState::Current()->IsImpellerEnabled()) {
169 // Spawn (but do not block on) a task that will load the runtime stage and
170 // populate an initial shader variant.
171 auto snapshot_controller = UIDartState::Current()->GetSnapshotDelegate();
172 ui_dart_state->GetTaskRunners().GetRasterTaskRunner()->PostTask(
173 [runtime_stage, snapshot_controller]() {
174 if (!snapshot_controller) {
175 return;
176 }
177 snapshot_controller->CacheRuntimeStage(runtime_stage);
178 });
179#if IMPELLER_SUPPORTS_RENDERING
180 runtime_effect_ = DlRuntimeEffectImpeller::Make(std::move(runtime_stage));
181#endif
182 } else {
183 const auto& code_mapping = runtime_stage->GetCodeMapping();
184 auto code_size = code_mapping->GetSize();
185 const char* sksl =
186 reinterpret_cast<const char*>(code_mapping->GetMapping());
187 // SkString makes a copy.
188 SkRuntimeEffect::Result result =
189 SkRuntimeEffect::MakeForShader(SkString(sksl, code_size));
190 if (result.effect == nullptr) {
191 return std::string("Invalid SkSL:\n") + sksl +
192 std::string("\nSkSL Error:\n") + result.errorText.c_str();
193 }
194 runtime_effect_ = DlRuntimeEffectSkia::Make(result.effect);
195 }
196
197 Dart_Handle ths = Dart_HandleFromWeakPersistent(dart_wrapper());
198 if (Dart_IsError(ths)) {
199 Dart_PropagateError(ths);
200 }
201
202 Dart_Handle result = Dart_SetField(ths, tonic::ToDart("_samplerCount"),
203 Dart_NewInteger(sampled_image_count));
204 if (Dart_IsError(result)) {
205 return "Failed to set sampler count for fragment program.";
206 }
207
208 size_t rounded_uniform_bytes =
209 (other_uniforms_bytes + sizeof(float) - 1) & ~(sizeof(float) - 1);
210 size_t float_count = rounded_uniform_bytes / sizeof(float);
211
212 result = Dart_SetField(ths, tonic::ToDart("_uniformFloatCount"),
213 Dart_NewInteger(float_count));
214 if (Dart_IsError(result)) {
215 return "Failed to set uniform float count for fragment program.";
216 }
217
218 result = Dart_SetField(ths, tonic::ToDart("_uniformInfo"), uniform_info);
219 if (Dart_IsError(result)) {
220 FML_DLOG(ERROR) << Dart_GetError(result);
221 return "Failed to set uniform info for fragment program.";
222 }
223
224 return "";
225}
226
227std::shared_ptr<DlColorSource> FragmentProgram::MakeDlColorSource(
228 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
229 const std::vector<std::shared_ptr<DlColorSource>>& children) {
230 return DlColorSource::MakeRuntimeEffect(runtime_effect_, children,
231 std::move(float_uniforms));
232}
233
234std::shared_ptr<DlImageFilter> FragmentProgram::MakeDlImageFilter(
235 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
236 const std::vector<std::shared_ptr<DlColorSource>>& children) {
237 return DlImageFilter::MakeRuntimeEffect(runtime_effect_, children,
238 std::move(float_uniforms));
239}
240
241void FragmentProgram::Create(Dart_Handle wrapper) {
242 auto res = fml::MakeRefCounted<FragmentProgram>();
243 res->AssociateWithDartWrapper(wrapper);
244}
245
246FragmentProgram::FragmentProgram() = default;
247
249
250} // namespace flutter
GLenum type
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)
size_t GetSize() const
Computes the total number of bytes that this uniform requires.
#define FML_TRACE_EVENT(category_group, name,...)