Flutter Engine
 
Loading...
Searching...
No Matches
flutter::FragmentProgram Class Reference

#include <fragment_program.h>

Inheritance diagram for flutter::FragmentProgram:
flutter::RefCountedDartWrappable< FragmentProgram > fml::RefCountedThreadSafe< T > tonic::DartWrappable fml::internal::RefCountedThreadSafeBase

Public Member Functions

 ~FragmentProgram () override
 
std::string initFromAsset (const std::string &asset_name)
 
fml::RefPtr< FragmentShader > shader (Dart_Handle shader, Dart_Handle uniforms_handle, Dart_Handle samplers)
 
std::shared_ptr< DlColorSourceMakeDlColorSource (std::shared_ptr< std::vector< uint8_t > > float_uniforms, const std::vector< std::shared_ptr< DlColorSource > > &children)
 
std::shared_ptr< DlImageFilterMakeDlImageFilter (std::shared_ptr< std::vector< uint8_t > > float_uniforms, const std::vector< std::shared_ptr< DlColorSource > > &children)
 
- Public Member Functions inherited from flutter::RefCountedDartWrappable< FragmentProgram >
virtual void RetainDartWrappableReference () const override
 
virtual void ReleaseDartWrappableReference () const override
 
- Public Member Functions inherited from fml::RefCountedThreadSafe< T >
void Release () const
 
- Public Member Functions inherited from fml::internal::RefCountedThreadSafeBase
void AddRef () const
 
bool HasOneRef () const
 
void AssertHasOneRef () const
 
- Public Member Functions inherited from tonic::DartWrappable
 DartWrappable ()
 
virtual const DartWrapperInfoGetDartWrapperInfo () const =0
 
Dart_Handle CreateDartWrapper (DartState *dart_state)
 
void AssociateWithDartWrapper (Dart_Handle wrappable)
 
void ClearDartWrapper ()
 
Dart_WeakPersistentHandle dart_wrapper () const
 

Static Public Member Functions

static void Create (Dart_Handle wrapper)
 

Additional Inherited Members

- Public Types inherited from tonic::DartWrappable
enum  DartNativeFields {
  kPeerIndex ,
  kNumberOfNativeFields
}
 
- Protected Member Functions inherited from fml::RefCountedThreadSafe< T >
 RefCountedThreadSafe ()
 
 ~RefCountedThreadSafe ()
 
- Protected Member Functions inherited from fml::internal::RefCountedThreadSafeBase
 RefCountedThreadSafeBase ()
 
 ~RefCountedThreadSafeBase ()
 
bool Release () const
 
void Adopt ()
 
- Protected Member Functions inherited from tonic::DartWrappable
virtual ~DartWrappable ()
 
- Static Protected Member Functions inherited from tonic::DartWrappable
static Dart_PersistentHandle GetTypeForWrapper (tonic::DartState *dart_state, const tonic::DartWrapperInfo &wrapper_info)
 

Detailed Description

Definition at line 24 of file fragment_program.h.

Constructor & Destructor Documentation

◆ ~FragmentProgram()

flutter::FragmentProgram::~FragmentProgram ( )
overridedefault

Member Function Documentation

◆ Create()

void flutter::FragmentProgram::Create ( Dart_Handle  wrapper)
static

Definition at line 235 of file fragment_program.cc.

235 {
236 auto res = fml::MakeRefCounted<FragmentProgram>();
237 res->AssociateWithDartWrapper(wrapper);
238}

◆ initFromAsset()

std::string flutter::FragmentProgram::initFromAsset ( const std::string &  asset_name)

Definition at line 100 of file fragment_program.cc.

100 {
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.empty()) {
116 return std::string("Asset '") + asset_name +
117 std::string("' does not contain any shader data.");
118 }
119
121 ui_dart_state->GetRuntimeStageBackend();
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 "
128 "backend ("
129 << RuntimeStageBackendToString(backend) << ")." << std::endl
130 << "Found stages: ";
131 for (const auto& kvp : runtime_stages) {
132 if (kvp.second) {
133 stream << RuntimeStageBackendToString(kvp.first) << " ";
134 }
135 }
136 return stream.str();
137 }
138
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());
144 FML_DCHECK(!Dart_IsError(uniform_info));
145 for (size_t i = 0; i < uniforms.size(); ++i) {
146 const impeller::RuntimeUniformDescription& uniform_description =
147 uniforms[i];
148
149 Dart_Handle map = ConvertUniformDescriptionToMap(uniform_description);
150 [[maybe_unused]] Dart_Handle dart_result =
151 Dart_ListSetAt(uniform_info, i, map);
152 FML_DCHECK(!Dart_IsError(dart_result));
153
154 if (uniform_description.type ==
156 sampled_image_count++;
157 } else {
158 other_uniforms_bytes += uniform_description.GetSize();
159 }
160 }
161
162 if (UIDartState::Current()->IsImpellerEnabled()) {
163 // Spawn (but do not block on) a task that will load the runtime stage and
164 // populate an initial shader variant.
165 auto snapshot_controller = UIDartState::Current()->GetSnapshotDelegate();
166 ui_dart_state->GetTaskRunners().GetRasterTaskRunner()->PostTask(
167 [runtime_stage, snapshot_controller]() {
168 if (!snapshot_controller) {
169 return;
170 }
171 snapshot_controller->CacheRuntimeStage(runtime_stage);
172 });
173#if IMPELLER_SUPPORTS_RENDERING
174 runtime_effect_ = DlRuntimeEffectImpeller::Make(std::move(runtime_stage));
175#endif
176 } else {
177 const auto& code_mapping = runtime_stage->GetCodeMapping();
178 auto code_size = code_mapping->GetSize();
179 const char* sksl =
180 reinterpret_cast<const char*>(code_mapping->GetMapping());
181 // SkString makes a copy.
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();
187 }
188 runtime_effect_ = DlRuntimeEffectSkia::Make(result.effect);
189 }
190
191 Dart_Handle ths = Dart_HandleFromWeakPersistent(dart_wrapper());
192 if (Dart_IsError(ths)) {
193 Dart_PropagateError(ths);
194 }
195
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.";
200 }
201
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);
205
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.";
210 }
211
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.";
216 }
217
218 return "";
219}
static sk_sp< DlRuntimeEffect > Make(std::shared_ptr< impeller::RuntimeStage > runtime_stage)
static sk_sp< DlRuntimeEffect > Make(const sk_sp< SkRuntimeEffect > &runtime_effect)
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
static UIDartState * Current()
static Map DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
Dart_WeakPersistentHandle dart_wrapper() const
#define FML_DLOG(severity)
Definition logging.h:121
#define FML_DCHECK(condition)
Definition logging.h:122
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,...)

References flutter::PlatformConfiguration::client(), flutter::UIDartState::Current(), tonic::DartWrappable::dart_wrapper(), flutter::data, impeller::RuntimeStage::DecodeRuntimeStages(), FML_DCHECK, FML_DLOG, FML_TRACE_EVENT, flutter::PlatformConfigurationClient::GetAssetManager(), flutter::TaskRunners::GetRasterTaskRunner(), flutter::UIDartState::GetRuntimeStageBackend(), impeller::RuntimeUniformDescription::GetSize(), flutter::UIDartState::GetSnapshotDelegate(), flutter::UIDartState::GetTaskRunners(), i, impeller::kSampledImage, flutter::DlRuntimeEffectSkia::Make(), flutter::DlRuntimeEffectImpeller::Make(), flutter::UIDartState::platform_configuration(), fml::TaskRunner::PostTask(), flutter::RuntimeStageBackendToString(), tonic::ToDart(), and impeller::RuntimeUniformDescription::type.

◆ MakeDlColorSource()

std::shared_ptr< DlColorSource > flutter::FragmentProgram::MakeDlColorSource ( std::shared_ptr< std::vector< uint8_t > >  float_uniforms,
const std::vector< std::shared_ptr< DlColorSource > > &  children 
)

Definition at line 221 of file fragment_program.cc.

223 {
224 return DlColorSource::MakeRuntimeEffect(runtime_effect_, children,
225 std::move(float_uniforms));
226}
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)

References flutter::DlColorSource::MakeRuntimeEffect().

◆ MakeDlImageFilter()

std::shared_ptr< DlImageFilter > flutter::FragmentProgram::MakeDlImageFilter ( std::shared_ptr< std::vector< uint8_t > >  float_uniforms,
const std::vector< std::shared_ptr< DlColorSource > > &  children 
)

Definition at line 228 of file fragment_program.cc.

230 {
231 return DlImageFilter::MakeRuntimeEffect(runtime_effect_, children,
232 std::move(float_uniforms));
233}
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)

References flutter::DlImageFilter::MakeRuntimeEffect().

◆ shader()

fml::RefPtr< FragmentShader > flutter::FragmentProgram::shader ( Dart_Handle  shader,
Dart_Handle  uniforms_handle,
Dart_Handle  samplers 
)

The documentation for this class was generated from the following files: