|
| 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/blobcat/blob_library.h" |
| 6 | + |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace impeller { |
| 10 | + |
| 11 | +BlobLibrary::BlobLibrary(std::shared_ptr<fml::Mapping> mapping) |
| 12 | + : mapping_(std::move(mapping)) { |
| 13 | + if (!mapping_ || mapping_->GetMapping() == nullptr) { |
| 14 | + FML_LOG(ERROR) << "Invalid mapping."; |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + BlobHeader header; |
| 19 | + std::vector<Blob> blobs; |
| 20 | + |
| 21 | + size_t offset = 0u; |
| 22 | + |
| 23 | + // Read the header. |
| 24 | + { |
| 25 | + const size_t read_size = sizeof(BlobHeader); |
| 26 | + if (mapping_->GetSize() < offset + read_size) { |
| 27 | + return; |
| 28 | + } |
| 29 | + std::memcpy(&header, mapping_->GetMapping() + offset, read_size); |
| 30 | + offset += read_size; |
| 31 | + |
| 32 | + // Validate the header. |
| 33 | + if (header.magic != kBlobCatMagic) { |
| 34 | + FML_LOG(ERROR) << "Invalid blob magic."; |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + blobs.resize(header.blob_count); |
| 39 | + } |
| 40 | + |
| 41 | + // Read the blob descriptions. |
| 42 | + { |
| 43 | + const size_t read_size = sizeof(Blob) * header.blob_count; |
| 44 | + ::memcpy(blobs.data(), mapping_->GetMapping() + offset, read_size); |
| 45 | + offset += read_size; |
| 46 | + } |
| 47 | + |
| 48 | + // Read the blobs. |
| 49 | + { |
| 50 | + for (size_t i = 0; i < header.blob_count; i++) { |
| 51 | + const auto& blob = blobs[i]; |
| 52 | + |
| 53 | + BlobKey key; |
| 54 | + key.type = blob.type; |
| 55 | + key.name = std::string{reinterpret_cast<const char*>(blob.name)}; |
| 56 | + auto mapping = std::make_shared<fml::NonOwnedMapping>( |
| 57 | + mapping_->GetMapping() + blob.offset, // offset |
| 58 | + blob.length, // length |
| 59 | + [mapping = mapping_](const uint8_t* data, size_t size) {} |
| 60 | + // release proc |
| 61 | + ); |
| 62 | + |
| 63 | + auto inserted = blobs_.insert({key, mapping}); |
| 64 | + if (!inserted.second) { |
| 65 | + FML_LOG(ERROR) << "Shader library had duplicate shader named " |
| 66 | + << key.name; |
| 67 | + return; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + is_valid_ = true; |
| 73 | +} |
| 74 | + |
| 75 | +BlobLibrary::~BlobLibrary() = default; |
| 76 | + |
| 77 | +bool BlobLibrary::IsValid() const { |
| 78 | + return is_valid_; |
| 79 | +} |
| 80 | + |
| 81 | +size_t BlobLibrary::GetShaderCount() const { |
| 82 | + return blobs_.size(); |
| 83 | +} |
| 84 | + |
| 85 | +std::shared_ptr<fml::Mapping> BlobLibrary::GetMapping(Blob::ShaderType type, |
| 86 | + std::string name) const { |
| 87 | + BlobKey key; |
| 88 | + key.type = type; |
| 89 | + key.name = name; |
| 90 | + auto found = blobs_.find(key); |
| 91 | + return found == blobs_.end() ? nullptr : found->second; |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace impeller |
0 commit comments