14#if IMPELLER_SUPPORTS_RENDERING
22#include "third_party/skia/include/core/SkString.h"
23#include "third_party/skia/include/effects/SkRuntimeEffect.h"
47Dart_Handle ConvertUniformDescriptionToMap(
49 constexpr int num_entries = 3;
50 Dart_Handle keys = Dart_NewList(num_entries);
52 Dart_Handle values = Dart_NewList(num_entries);
56 Dart_NewStringFromCString(uniform_description.
name.c_str());
58 [[maybe_unused]] Dart_Handle result =
59 Dart_ListSetAt(keys, 0, Dart_NewStringFromCString(
"name"));
61 result = Dart_ListSetAt(values, 0,
name);
66 switch (uniform_description.
type) {
68 type = Dart_NewStringFromCString(
"Float");
71 type = Dart_NewStringFromCString(
"SampledImage");
74 type = Dart_NewStringFromCString(
"Struct");
78 [[maybe_unused]] Dart_Handle result =
79 Dart_ListSetAt(keys, 1, Dart_NewStringFromCString(
"type"));
81 result = Dart_ListSetAt(values, 1,
type);
85 Dart_Handle
size = Dart_NewIntegerFromUint64(uniform_description.
GetSize());
87 [[maybe_unused]] Dart_Handle result =
88 Dart_ListSetAt(keys, 2, Dart_NewStringFromCString(
"size"));
90 result = Dart_ListSetAt(values, 2,
size);
94 Dart_NewMap(Dart_TypeString(), keys, Dart_TypeObject(), values);
104 std::shared_ptr<AssetManager> asset_manager =
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");
112 auto runtime_stages =
115 if (runtime_stages.empty()) {
116 return std::string(
"Asset '") + asset_name +
117 std::string(
"' does not contain any shader data.");
122 std::shared_ptr<impeller::RuntimeStage> runtime_stage =
123 runtime_stages[backend];
124 if (!runtime_stage) {
125 std::ostringstream stream;
126 stream <<
"Asset '" << asset_name
127 <<
"' does not contain appropriate runtime stage data for current "
131 for (
const auto& kvp : runtime_stages) {
139 int sampled_image_count = 0;
140 size_t other_uniforms_bytes = 0;
141 const std::vector<impeller::RuntimeUniformDescription>& uniforms =
142 runtime_stage->GetUniforms();
143 Dart_Handle uniform_info = Dart_NewList(uniforms.size());
145 for (
size_t i = 0;
i < uniforms.size(); ++
i) {
149 Dart_Handle map = ConvertUniformDescriptionToMap(uniform_description);
150 [[maybe_unused]] Dart_Handle dart_result =
151 Dart_ListSetAt(uniform_info,
i, map);
154 if (uniform_description.
type ==
156 sampled_image_count++;
158 other_uniforms_bytes += uniform_description.
GetSize();
167 [runtime_stage, snapshot_controller]() {
168 if (!snapshot_controller) {
171 snapshot_controller->CacheRuntimeStage(runtime_stage);
173#if IMPELLER_SUPPORTS_RENDERING
177 const auto& code_mapping = runtime_stage->GetCodeMapping();
178 auto code_size = code_mapping->GetSize();
180 reinterpret_cast<const char*
>(code_mapping->GetMapping());
182 SkRuntimeEffect::Result result =
183 SkRuntimeEffect::MakeForShader(SkString(sksl, code_size));
184 if (result.effect ==
nullptr) {
185 return std::string(
"Invalid SkSL:\n") + sksl +
186 std::string(
"\nSkSL Error:\n") + result.errorText.c_str();
191 Dart_Handle ths = Dart_HandleFromWeakPersistent(
dart_wrapper());
192 if (Dart_IsError(ths)) {
193 Dart_PropagateError(ths);
196 Dart_Handle result = Dart_SetField(ths,
tonic::ToDart(
"_samplerCount"),
197 Dart_NewInteger(sampled_image_count));
198 if (Dart_IsError(result)) {
199 return "Failed to set sampler count for fragment program.";
202 size_t rounded_uniform_bytes =
203 (other_uniforms_bytes +
sizeof(float) - 1) & ~(
sizeof(float) - 1);
204 size_t float_count = rounded_uniform_bytes /
sizeof(float);
206 result = Dart_SetField(ths,
tonic::ToDart(
"_uniformFloatCount"),
207 Dart_NewInteger(float_count));
208 if (Dart_IsError(result)) {
209 return "Failed to set uniform float count for fragment program.";
212 result = Dart_SetField(ths,
tonic::ToDart(
"_uniformInfo"), uniform_info);
213 if (Dart_IsError(result)) {
214 FML_DLOG(ERROR) << Dart_GetError(result);
215 return "Failed to set uniform info for fragment program.";
222 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
223 const std::vector<std::shared_ptr<DlColorSource>>& children) {
225 std::move(float_uniforms));
229 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
230 const std::vector<std::shared_ptr<DlColorSource>>& children) {
232 std::move(float_uniforms));
236 auto res = fml::MakeRefCounted<FragmentProgram>();
237 res->AssociateWithDartWrapper(wrapper);
240FragmentProgram::FragmentProgram() =
default;
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)
~FragmentProgram() override
std::string initFromAsset(const std::string &asset_name)
static void Create(Dart_Handle wrapper)
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 Map DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
Dart_WeakPersistentHandle dart_wrapper() const
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
#define FML_DLOG(severity)
#define FML_DCHECK(condition)
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
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
static std::string RuntimeStageBackendToString(impeller::RuntimeStageBackend backend)
Dart_Handle ToDart(const T &object)
#define FML_TRACE_EVENT(category_group, name,...)