Flutter Impeller
blob_library.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/blobcat/blob_flatbuffers.h"
13 
14 namespace impeller {
15 
16 constexpr BlobShaderType 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 BlobLibrary::BlobLibrary(std::shared_ptr<fml::Mapping> payload)
29  : payload_(std::move(payload)) {
30  if (!payload_ || payload_->GetMapping() == nullptr) {
31  VALIDATION_LOG << "Blob mapping was absent.";
32  return;
33  }
34 
35  if (!fb::BlobLibraryBufferHasIdentifier(payload_->GetMapping())) {
36  VALIDATION_LOG << "Invalid blob magic.";
37  return;
38  }
39 
40  auto blob_library = fb::GetBlobLibrary(payload_->GetMapping());
41  if (!blob_library) {
42  return;
43  }
44 
45  if (auto items = blob_library->items()) {
46  for (auto i = items->begin(), end = items->end(); i != end; i++) {
47  BlobKey key;
48  key.name = i->name()->str();
49  key.type = ToShaderType(i->stage());
50  blobs_[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 
64 BlobLibrary::~BlobLibrary() = default;
65 
66 bool BlobLibrary::IsValid() const {
67  return is_valid_;
68 }
69 
71  return blobs_.size();
72 }
73 
74 std::shared_ptr<fml::Mapping> BlobLibrary::GetMapping(BlobShaderType type,
75  std::string name) const {
76  BlobKey key;
77  key.type = type;
78  key.name = std::move(name);
79  auto found = blobs_.find(key);
80  return found == blobs_.end() ? nullptr : found->second;
81 }
82 
84  const std::function<bool(BlobShaderType type,
85  const std::string& name,
86  const std::shared_ptr<fml::Mapping>& mapping)>&
87  callback) const {
88  if (!IsValid() || !callback) {
89  return 0u;
90  }
91  size_t count = 0u;
92  for (const auto& blob : blobs_) {
93  count++;
94  if (!callback(blob.first.type, blob.first.name, blob.second)) {
95  break;
96  }
97  }
98  return count;
99 }
100 
101 } // namespace impeller
impeller::BlobLibrary
Definition: blob_library.h:18
blob_library.h
impeller::BlobLibrary::BlobLibrary
BlobLibrary(std::shared_ptr< fml::Mapping > payload)
Definition: blob_library.cc:28
impeller::BlobLibrary::~BlobLibrary
~BlobLibrary()
impeller::BlobLibrary::GetMapping
std::shared_ptr< fml::Mapping > GetMapping(BlobShaderType type, std::string name) const
Definition: blob_library.cc:74
validation.h
impeller::BlobShaderType::kVertex
@ kVertex
impeller::BlobLibrary::GetShaderCount
size_t GetShaderCount() const
Definition: blob_library.cc:70
impeller::BlobShaderType
BlobShaderType
Definition: blob_types.h:9
impeller::BlobShaderType::kFragment
@ kFragment
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::ToShaderType
constexpr BlobShaderType ToShaderType(fb::Stage stage)
Definition: blob_library.cc:16
std
Definition: comparable.h:98
impeller::BlobLibrary::IsValid
bool IsValid() const
Definition: blob_library.cc:66
impeller::BlobShaderType::kCompute
@ kCompute
impeller::BlobLibrary::IterateAllBlobs
size_t IterateAllBlobs(const std::function< bool(BlobShaderType type, const std::string &name, const std::shared_ptr< fml::Mapping > &mapping)> &) const
Definition: blob_library.cc:83
impeller
Definition: aiks_context.cc:10