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