Flutter Engine
The Flutter Engine
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
9#include "flutter/lib/ui/painting/fragment_program.h"
10
11#include "flutter/assets/asset_manager.h"
12#include "flutter/fml/trace_event.h"
13#include "flutter/impeller/runtime_stage/runtime_stage.h"
14#include "flutter/lib/ui/ui_dart_state.h"
15#include "flutter/lib/ui/window/platform_configuration.h"
16
20
21namespace flutter {
22
24
25static std::string RuntimeStageBackendToString(
27 switch (backend) {
29 return "SkSL";
31 return "Metal";
33 return "OpenGLES";
35 return "Vulkan";
36 }
37}
38
39std::string FragmentProgram::initFromAsset(const std::string& asset_name) {
40 FML_TRACE_EVENT("flutter", "FragmentProgram::initFromAsset", "asset",
41 asset_name);
42 UIDartState* ui_dart_state = UIDartState::Current();
43 std::shared_ptr<AssetManager> asset_manager =
44 ui_dart_state->platform_configuration()->client()->GetAssetManager();
45
46 std::unique_ptr<fml::Mapping> data = asset_manager->GetAsMapping(asset_name);
47 if (data == nullptr) {
48 return std::string("Asset '") + asset_name + std::string("' not found");
49 }
50
51 auto runtime_stages =
53
54 if (runtime_stages.empty()) {
55 return std::string("Asset '") + asset_name +
56 std::string("' does not contain any shader data.");
57 }
58
60 ui_dart_state->GetRuntimeStageBackend();
61 std::shared_ptr<impeller::RuntimeStage> runtime_stage =
62 runtime_stages[backend];
63 if (!runtime_stage) {
64 std::ostringstream stream;
65 stream << "Asset '" << asset_name
66 << "' does not contain appropriate runtime stage data for current "
67 "backend ("
68 << RuntimeStageBackendToString(backend) << ")." << std::endl
69 << "Found stages: ";
70 for (const auto& kvp : runtime_stages) {
71 if (kvp.second) {
72 stream << RuntimeStageBackendToString(kvp.first) << " ";
73 }
74 }
75 return stream.str();
76 }
77
78 int sampled_image_count = 0;
79 size_t other_uniforms_bytes = 0;
80 for (const auto& uniform_description : runtime_stage->GetUniforms()) {
81 if (uniform_description.type ==
83 sampled_image_count++;
84 } else {
85 other_uniforms_bytes += uniform_description.GetSize();
86 }
87 }
88
90 // Spawn (but do not block on) a task that will load the runtime stage and
91 // populate an initial shader variant.
92 auto snapshot_controller = UIDartState::Current()->GetSnapshotDelegate();
94 [runtime_stage, snapshot_controller]() {
95 if (!snapshot_controller) {
96 return;
97 }
98 snapshot_controller->CacheRuntimeStage(runtime_stage);
99 });
100 runtime_effect_ = DlRuntimeEffect::MakeImpeller(std::move(runtime_stage));
101 } else {
102 const auto& code_mapping = runtime_stage->GetCodeMapping();
103 auto code_size = code_mapping->GetSize();
104 const char* sksl =
105 reinterpret_cast<const char*>(code_mapping->GetMapping());
106 // SkString makes a copy.
109 if (result.effect == nullptr) {
110 return std::string("Invalid SkSL:\n") + sksl +
111 std::string("\nSkSL Error:\n") + result.errorText.c_str();
112 }
113 runtime_effect_ = DlRuntimeEffect::MakeSkia(result.effect);
114 }
115
117 if (Dart_IsError(ths)) {
119 }
120
121 Dart_Handle result = Dart_SetField(ths, tonic::ToDart("_samplerCount"),
122 Dart_NewInteger(sampled_image_count));
123 if (Dart_IsError(result)) {
124 return "Failed to set sampler count for fragment program.";
125 }
126
127 size_t rounded_uniform_bytes =
128 (other_uniforms_bytes + sizeof(float) - 1) & ~(sizeof(float) - 1);
129 size_t float_count = rounded_uniform_bytes / sizeof(float);
130
131 result = Dart_SetField(ths, tonic::ToDart("_uniformFloatCount"),
132 Dart_NewInteger(float_count));
133 if (Dart_IsError(result)) {
134 return "Failed to set uniform float count for fragment program.";
135 }
136
137 return "";
138}
139
140std::shared_ptr<DlColorSource> FragmentProgram::MakeDlColorSource(
141 std::shared_ptr<std::vector<uint8_t>> float_uniforms,
142 const std::vector<std::shared_ptr<DlColorSource>>& children) {
143 return DlColorSource::MakeRuntimeEffect(runtime_effect_, children,
144 std::move(float_uniforms));
145}
146
148 auto res = fml::MakeRefCounted<FragmentProgram>();
149 res->AssociateWithDartWrapper(wrapper);
150}
151
152FragmentProgram::FragmentProgram() = default;
153
155
156} // namespace flutter
const char * backend
static Result MakeForShader(SkString sksl, const Options &)
static std::shared_ptr< DlRuntimeEffectColorSource > 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 > MakeSkia(const sk_sp< SkRuntimeEffect > &runtime_effect)
static sk_sp< DlRuntimeEffect > MakeImpeller(std::shared_ptr< impeller::RuntimeStage > runtime_stage)
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
bool IsImpellerEnabled() const
Whether Impeller is enabled for this application.
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
impeller::RuntimeStageBackend GetRuntimeStageBackend() const
The expected type for runtime stage 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
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_SetField(Dart_Handle container, Dart_Handle name, Dart_Handle value)
DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_HandleFromWeakPersistent(Dart_WeakPersistentHandle object)
#define IMPLEMENT_WRAPPERTYPEINFO(LibraryName, ClassName)
GAsyncResult * result
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 switches.h:41
static std::string RuntimeStageBackendToString(impeller::RuntimeStageBackend backend)
Dart_Handle ToDart(const T &object)
#define FML_TRACE_EVENT(category_group, name,...)