|
| 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/shader_archive/multi_arch_shader_archive.h" |
| 6 | + |
| 7 | +#include "impeller/shader_archive/multi_arch_shader_archive_flatbuffers.h" |
| 8 | + |
| 9 | +namespace impeller { |
| 10 | + |
| 11 | +constexpr ArchiveRenderingBackend ToArchiveRenderingBackend( |
| 12 | + fb::RenderingBackend backend) { |
| 13 | + switch (backend) { |
| 14 | + case fb::RenderingBackend::kOpenGLES: |
| 15 | + return ArchiveRenderingBackend::kOpenGLES; |
| 16 | + case fb::RenderingBackend::kVulkan: |
| 17 | + return ArchiveRenderingBackend::kVulkan; |
| 18 | + case fb::RenderingBackend::kMetal: |
| 19 | + return ArchiveRenderingBackend::kMetal; |
| 20 | + } |
| 21 | + FML_UNREACHABLE(); |
| 22 | +} |
| 23 | + |
| 24 | +std::shared_ptr<ShaderArchive> MultiArchShaderArchive::CreateArchiveFromMapping( |
| 25 | + const std::shared_ptr<const fml::Mapping>& mapping, |
| 26 | + ArchiveRenderingBackend backend) { |
| 27 | + { |
| 28 | + auto multi_archive = std::make_shared<MultiArchShaderArchive>(mapping); |
| 29 | + if (multi_archive->IsValid()) { |
| 30 | + return multi_archive->GetShaderArchive(backend); |
| 31 | + } |
| 32 | + } |
| 33 | + { |
| 34 | + auto single_archive = |
| 35 | + std::shared_ptr<ShaderArchive>(new ShaderArchive(mapping)); |
| 36 | + if (single_archive->IsValid()) { |
| 37 | + return single_archive; |
| 38 | + } |
| 39 | + } |
| 40 | + return nullptr; |
| 41 | +} |
| 42 | + |
| 43 | +MultiArchShaderArchive::MultiArchShaderArchive( |
| 44 | + const std::shared_ptr<const fml::Mapping>& mapping) { |
| 45 | + if (!mapping) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (!fb::MultiArchShaderArchiveBufferHasIdentifier(mapping->GetMapping())) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const auto* multi_arch = fb::GetMultiArchShaderArchive(mapping->GetMapping()); |
| 54 | + |
| 55 | + if (!multi_arch) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (auto archives = multi_arch->items()) { |
| 60 | + for (auto i = archives->begin(), end = archives->end(); i != end; i++) { |
| 61 | + // This implementation is unable to handle multiple archives for the same |
| 62 | + // backend. |
| 63 | + backend_mappings_[ToArchiveRenderingBackend(i->rendering_backend())] = |
| 64 | + std::make_shared<fml::NonOwnedMapping>(i->mapping()->Data(), |
| 65 | + i->mapping()->size(), |
| 66 | + [mapping](auto, auto) { |
| 67 | + // Just hold the mapping. |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + is_valid_ = true; |
| 73 | +} |
| 74 | + |
| 75 | +MultiArchShaderArchive::~MultiArchShaderArchive() = default; |
| 76 | + |
| 77 | +bool MultiArchShaderArchive::IsValid() const { |
| 78 | + return is_valid_; |
| 79 | +} |
| 80 | + |
| 81 | +std::shared_ptr<const fml::Mapping> MultiArchShaderArchive::GetArchive( |
| 82 | + ArchiveRenderingBackend backend) const { |
| 83 | + auto found = backend_mappings_.find(backend); |
| 84 | + if (found == backend_mappings_.end()) { |
| 85 | + return nullptr; |
| 86 | + } |
| 87 | + return found->second; |
| 88 | +} |
| 89 | + |
| 90 | +std::shared_ptr<ShaderArchive> MultiArchShaderArchive::GetShaderArchive( |
| 91 | + ArchiveRenderingBackend backend) const { |
| 92 | + auto archive = GetArchive(backend); |
| 93 | + if (!archive) { |
| 94 | + return nullptr; |
| 95 | + } |
| 96 | + auto shader_archive = |
| 97 | + std::shared_ptr<ShaderArchive>(new ShaderArchive(std::move(archive))); |
| 98 | + if (!shader_archive->IsValid()) { |
| 99 | + return nullptr; |
| 100 | + } |
| 101 | + return shader_archive; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace impeller |
0 commit comments