| Flutter Impeller |

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 "impeller/toolkit/interop/fragment_program.h"
6
7 #include "impeller/base/validation.h"
8
9 namespace impeller::interop {
10
11 FragmentProgram::FragmentProgram(const std::shared_ptr<fml::Mapping>& data) {
12 if (data == nullptr || data->GetSize() == 0) {
13 VALIDATION_LOG << "No data provided to create fragment program.";
14 return;
15 }
16
17 auto stages = RuntimeStage::DecodeRuntimeStages(data);
18 if (!stages.ok()) {
19 VALIDATION_LOG << "Failed to decode runtime stages: " << stages.status();
20 return;
21 }
22
23 for (const auto& stage : stages.value()) {
24 if (auto data = stage.second) {
25 stages_[stage.first] = std::move(data);
26 }
27 }
28
29 if (stages_.empty()) {
30 VALIDATION_LOG << "No valid runtime stages present in fragment program.";
31 return;
32 }
33
34 is_valid_ = true;
35 }
36
37 FragmentProgram::~FragmentProgram() = default;
38
39 bool FragmentProgram::IsValid() const {
40 return is_valid_;
41 }
42
43 static std::string AvailableStagesAsString(
44 const std::set<RuntimeStageBackend>& stages) {
45 std::stringstream stream;
46 size_t count = 0;
47 for (const auto& stage : stages) {
48 stream << RuntimeStageBackendToString(stage);
49 count++;
50 if (count != stages.size()) {
51 stream << ", ";
52 }
53 }
54 return stream.str();
55 }
56
57 std::shared_ptr<RuntimeStage> FragmentProgram::FindRuntimeStage(
58 RuntimeStageBackend backend) const {
59 if (backend == RuntimeStageBackend::kOpenGLES3) {
60 return FindRuntimeStage(RuntimeStageBackend::kOpenGLES);
61 }
62 auto found = stages_.find(backend);
63 if (found == stages_.end()) {
64 VALIDATION_LOG << "Could not find runtime shader for backend: "
65 << RuntimeStageBackendToString(backend)
66 << ". Shaders were packaged for "
67 << AvailableStagesAsString(GetAvailableStages())
68 << ". Check your shader compiler options.";
69 return nullptr;
70 }
71 return found->second;
72 }
73
74 const char* RuntimeStageBackendToString(RuntimeStageBackend backend) {
75 switch (backend) {
76 case RuntimeStageBackend::kSkSL:
77 return "SKSL";
78 case RuntimeStageBackend::kMetal:
79 return "Metal";
80 case RuntimeStageBackend::kOpenGLES:
81 return "OpenGL ES2";
82 case RuntimeStageBackend::kOpenGLES3:
83 return "OpenGL ES3";
84 case RuntimeStageBackend::kVulkan:
85 return "Vulkan";
86 }
87 return "Unknown";
88 }
89
90 std::set<RuntimeStageBackend> FragmentProgram::GetAvailableStages() const {
91 std::set<RuntimeStageBackend> stages;
92 for (const auto& stage : stages_) {
93 stages.insert(stage.first);
94 }
95 return stages;
96 }
97
98 } // namespace impeller::interop
impeller::RuntimeStage::DecodeRuntimeStages
static absl::StatusOr< Map > DecodeRuntimeStages(const std::shared_ptr< fml::Mapping > &payload)
Definition: runtime_stage.cc:159
impeller::interop::FragmentProgram::~FragmentProgram
~FragmentProgram()
impeller::interop::FragmentProgram::IsValid
bool IsValid() const
Definition: fragment_program.cc:39
impeller::interop::FragmentProgram::FindRuntimeStage
std::shared_ptr< RuntimeStage > FindRuntimeStage(RuntimeStageBackend backend) const
Definition: fragment_program.cc:57
impeller::interop::FragmentProgram::FragmentProgram
FragmentProgram(const std::shared_ptr< fml::Mapping > &mapping)
Definition: fragment_program.cc:11
fragment_program.h
impeller::interop
Definition: context_gles.cc:12
impeller::interop::RuntimeStageBackendToString
const char * RuntimeStageBackendToString(RuntimeStageBackend backend)
Definition: fragment_program.cc:74
impeller::interop::AvailableStagesAsString
static std::string AvailableStagesAsString(const std::set< RuntimeStageBackend > &stages)
Definition: fragment_program.cc:43
impeller::RuntimeStageBackend
RuntimeStageBackend
Definition: runtime_types.h:16
impeller::RuntimeStageBackend::kOpenGLES
@ kOpenGLES
impeller::RuntimeStageBackend::kMetal
@ kMetal
impeller::RuntimeStageBackend::kVulkan
@ kVulkan
impeller::RuntimeStageBackend::kSkSL
@ kSkSL
impeller::RuntimeStageBackend::kOpenGLES3
@ kOpenGLES3
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:69
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91