Flutter Engine
The Flutter Engine
shader_library_gles.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
6
7#include <sstream>
8
9#include "flutter/fml/closure.h"
14
15namespace impeller {
16
18 switch (type) {
25 }
27}
28
29static std::string GLESShaderNameToShaderKeyName(const std::string& name,
30 ShaderStage stage) {
31 std::stringstream stream;
32 stream << name;
33 switch (stage) {
35 stream << "_unknown_";
36 break;
38 stream << "_vertex_";
39 break;
41 stream << "_fragment_";
42 break;
44 stream << "_compute_";
45 break;
46 }
47 stream << "main";
48 return stream.str();
49}
50
51ShaderLibraryGLES::ShaderLibraryGLES(
52 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries) {
53 ShaderFunctionMap functions;
54 auto iterator = [&functions, library_id = library_id_](auto type, //
55 const auto& name, //
56 const auto& mapping //
57 ) -> bool {
58 const auto stage = ToShaderStage(type);
59 const auto key_name = GLESShaderNameToShaderKeyName(name, stage);
60
61 functions[ShaderKey{key_name, stage}] = std::shared_ptr<ShaderFunctionGLES>(
62 new ShaderFunctionGLES(library_id, //
63 stage, //
64 key_name, //
65 mapping //
66 ));
67
68 return true;
69 };
70 for (auto library : shader_libraries) {
71 auto blob_library = ShaderArchive{std::move(library)};
72 if (!blob_library.IsValid()) {
73 VALIDATION_LOG << "Could not construct blob library for shaders.";
74 return;
75 }
76 blob_library.IterateAllShaders(iterator);
77 }
78
79 functions_ = functions;
80 is_valid_ = true;
81}
82
83// |ShaderLibrary|
85
86// |ShaderLibrary|
88 return is_valid_;
89}
90
91// |ShaderLibrary|
92std::shared_ptr<const ShaderFunction> ShaderLibraryGLES::GetFunction(
93 std::string_view name,
94 ShaderStage stage) {
95 ReaderLock lock(functions_mutex_);
96 const auto key = ShaderKey{name, stage};
97 if (auto found = functions_.find(key); found != functions_.end()) {
98 return found->second;
99 }
100 return nullptr;
101}
102
103// |ShaderLibrary|
104void ShaderLibraryGLES::RegisterFunction(std::string name,
105 ShaderStage stage,
106 std::shared_ptr<fml::Mapping> code,
107 RegistrationCallback callback) {
108 if (!callback) {
109 callback = [](auto) {};
110 }
111 fml::ScopedCleanupClosure auto_fail([callback]() { callback(false); });
112 if (name.empty() || stage == ShaderStage::kUnknown || code == nullptr ||
113 code->GetMapping() == nullptr) {
114 VALIDATION_LOG << "Invalid runtime stage registration.";
115 return;
116 }
117 const auto key = ShaderKey{name, stage};
118 WriterLock lock(functions_mutex_);
119 if (functions_.count(key) != 0) {
120 VALIDATION_LOG << "Runtime stage named " << name
121 << " has already been registered.";
122 return;
123 }
124 functions_[key] = std::shared_ptr<ShaderFunctionGLES>(new ShaderFunctionGLES(
125 library_id_, //
126 stage, //
128 code //
129 ));
130 auto_fail.Release();
131 callback(true);
132}
133
134// |ShaderLibrary|
135void ShaderLibraryGLES::UnregisterFunction(std::string name,
136 ShaderStage stage) {
137 ReaderLock lock(functions_mutex_);
138
139 const auto key = ShaderKey{name, stage};
140
141 auto found = functions_.find(key);
142 if (found == functions_.end()) {
143 VALIDATION_LOG << "Library function named " << name
144 << " was not found, so it couldn't be unregistered.";
145 return;
146 }
147
148 functions_.erase(found);
149
150 return;
151}
152
153} // namespace impeller
GLenum type
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition: closure.h:32
bool IsValid() const override
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_UNREACHABLE()
Definition: logging.h:109
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
static std::string GLESShaderNameToShaderKeyName(const std::string &name, ShaderStage stage)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
std::unordered_map< ShaderKey, std::shared_ptr< const ShaderFunction >, ShaderKey::Hash, ShaderKey::Equal > ShaderFunctionMap
Definition: shader_key.h:43
#define VALIDATION_LOG
Definition: validation.h:73