Flutter Impeller
shader_archive.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 <array>
8 #include <string>
9 #include <utility>
10 
12 #include "impeller/shader_archive/shader_archive_flatbuffers.h"
13 
14 namespace impeller {
15 
16 constexpr ArchiveShaderType ToShaderType(fb::Stage stage) {
17  switch (stage) {
18  case fb::Stage::kVertex:
20  case fb::Stage::kFragment:
22  case fb::Stage::kCompute:
24  }
25  FML_UNREACHABLE();
26 }
27 
28 ShaderArchive::ShaderArchive(std::shared_ptr<fml::Mapping> payload)
29  : payload_(std::move(payload)) {
30  if (!payload_ || payload_->GetMapping() == nullptr) {
31  VALIDATION_LOG << "Shader mapping was absent.";
32  return;
33  }
34 
35  if (!fb::ShaderArchiveBufferHasIdentifier(payload_->GetMapping())) {
36  VALIDATION_LOG << "Invalid shader magic.";
37  return;
38  }
39 
40  auto shader_archive = fb::GetShaderArchive(payload_->GetMapping());
41  if (!shader_archive) {
42  return;
43  }
44 
45  if (auto items = shader_archive->items()) {
46  for (auto i = items->begin(), end = items->end(); i != end; i++) {
47  ShaderKey key;
48  key.name = i->name()->str();
49  key.type = ToShaderType(i->stage());
50  shaders_[key] = std::make_shared<fml::NonOwnedMapping>(
51  i->mapping()->Data(), i->mapping()->size(),
52  [payload = payload_](auto, auto) {
53  // The pointers are into the base payload. Instead of copying the
54  // data, just hold onto the payload.
55  });
56  }
57  }
58 
59  is_valid_ = true;
60 }
61 
63 
65 
66 bool ShaderArchive::IsValid() const {
67  return is_valid_;
68 }
69 
71  return shaders_.size();
72 }
73 
74 std::shared_ptr<fml::Mapping> ShaderArchive::GetMapping(
76  std::string name) const {
77  ShaderKey key;
78  key.type = type;
79  key.name = std::move(name);
80  auto found = shaders_.find(key);
81  return found == shaders_.end() ? nullptr : found->second;
82 }
83 
85  const std::function<bool(ArchiveShaderType type,
86  const std::string& name,
87  const std::shared_ptr<fml::Mapping>& mapping)>&
88  callback) const {
89  if (!IsValid() || !callback) {
90  return 0u;
91  }
92  size_t count = 0u;
93  for (const auto& shader : shaders_) {
94  count++;
95  if (!callback(shader.first.type, shader.first.name, shader.second)) {
96  break;
97  }
98  }
99  return count;
100 }
101 
102 } // namespace impeller
GLenum type
size_t IterateAllShaders(const std::function< bool(ArchiveShaderType type, const std::string &name, const std::shared_ptr< fml::Mapping > &mapping)> &) const
ShaderArchive(std::shared_ptr< fml::Mapping > payload)
size_t GetShaderCount() const
std::shared_ptr< fml::Mapping > GetMapping(ArchiveShaderType type, std::string name) const
constexpr ArchiveShaderType ToShaderType(fb::Stage stage)
Definition: comparable.h:95
const size_t end
#define VALIDATION_LOG
Definition: validation.h:91