Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
14namespace impeller {
15
16constexpr ArchiveShaderType ToShaderType(fb::Stage stage) {
17 switch (stage) {
18 case fb::Stage::kVertex:
20 case fb::Stage::kFragment:
22 case fb::Stage::kCompute:
24 }
26}
27
28ShaderArchive::ShaderArchive(std::shared_ptr<const 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 archive 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
62ShaderArchive::ShaderArchive(ShaderArchive&&) = default;
63
64ShaderArchive::~ShaderArchive() = default;
65
66bool ShaderArchive::IsValid() const {
67 return is_valid_;
68}
69
70size_t ShaderArchive::GetShaderCount() const {
71 return shaders_.size();
72}
73
74std::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
84size_t ShaderArchive::IterateAllShaders(
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
int count
ShaderArchive(ShaderArchive &&)
glong glong end
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_UNREACHABLE()
Definition logging.h:109
const char * name
Definition fuchsia.cc:50
constexpr ArchiveShaderType ToShaderType(fb::Stage stage)
Definition ref_ptr.h:256
#define VALIDATION_LOG
Definition validation.h:73