From e93a4f24db0cd7bb16cb6f9c0982f6c2394e7832 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 16 Aug 2022 17:20:50 -0700 Subject: [PATCH] [Impeller] Use flatbuffers in blobcat. (flutter/engine#35443) --- .../ci/licenses_golden/licenses_flutter | 4 +- engine/src/flutter/impeller/blobcat/BUILD.gn | 17 +++- engine/src/flutter/impeller/blobcat/blob.fbs | 23 +++++ engine/src/flutter/impeller/blobcat/blob.h | 43 --------- .../flutter/impeller/blobcat/blob_library.cc | 87 +++++++---------- .../flutter/impeller/blobcat/blob_library.h | 12 +-- .../blobcat/{blob.cc => blob_types.h} | 7 +- .../flutter/impeller/blobcat/blob_writer.cc | 95 +++++++------------ .../flutter/impeller/blobcat/blob_writer.h | 10 +- .../impeller/blobcat/blobcat_unittests.cc | 14 +-- .../backend/gles/shader_library_gles.cc | 6 +- .../backend/vulkan/shader_library_vk.cc | 6 +- 12 files changed, 139 insertions(+), 185 deletions(-) create mode 100644 engine/src/flutter/impeller/blobcat/blob.fbs delete mode 100644 engine/src/flutter/impeller/blobcat/blob.h rename engine/src/flutter/impeller/blobcat/{blob.cc => blob_types.h} (75%) diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 35ee303be2..43992af779 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -470,10 +470,10 @@ FILE: ../../../flutter/impeller/base/work_queue.cc FILE: ../../../flutter/impeller/base/work_queue.h FILE: ../../../flutter/impeller/base/work_queue_common.cc FILE: ../../../flutter/impeller/base/work_queue_common.h -FILE: ../../../flutter/impeller/blobcat/blob.cc -FILE: ../../../flutter/impeller/blobcat/blob.h +FILE: ../../../flutter/impeller/blobcat/blob.fbs FILE: ../../../flutter/impeller/blobcat/blob_library.cc FILE: ../../../flutter/impeller/blobcat/blob_library.h +FILE: ../../../flutter/impeller/blobcat/blob_types.h FILE: ../../../flutter/impeller/blobcat/blob_writer.cc FILE: ../../../flutter/impeller/blobcat/blob_writer.h FILE: ../../../flutter/impeller/blobcat/blobcat_main.cc diff --git a/engine/src/flutter/impeller/blobcat/BUILD.gn b/engine/src/flutter/impeller/blobcat/BUILD.gn index b38d29bdcf..a70f4a4ec1 100644 --- a/engine/src/flutter/impeller/blobcat/BUILD.gn +++ b/engine/src/flutter/impeller/blobcat/BUILD.gn @@ -2,19 +2,30 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//third_party/flatbuffers/flatbuffers.gni") import("../tools/impeller.gni") +config("blobcat_config") { + configs = [ "//flutter/impeller:impeller_public_config" ] + include_dirs = [ "$root_gen_dir/flutter" ] +} + +flatbuffers("blobcat_flatbuffers") { + flatbuffers = [ "blob.fbs" ] + public_configs = [ ":blobcat_config" ] + public_deps = [ "//third_party/flatbuffers" ] +} + impeller_component("blobcat_lib") { sources = [ - "blob.cc", - "blob.h", "blob_library.cc", "blob_library.h", "blob_writer.cc", "blob_writer.h", ] - deps = [ + public_deps = [ + ":blobcat_flatbuffers", "../base", "//flutter/fml", ] diff --git a/engine/src/flutter/impeller/blobcat/blob.fbs b/engine/src/flutter/impeller/blobcat/blob.fbs new file mode 100644 index 0000000000..741c6bfca5 --- /dev/null +++ b/engine/src/flutter/impeller/blobcat/blob.fbs @@ -0,0 +1,23 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +namespace impeller.fb; + +enum Stage:byte { + kVertex, + kFragment, +} + +table Blob { + stage: Stage; + name: string; + mapping: [ubyte]; +} + +table BlobLibrary { + items: [Blob]; +} + +root_type BlobLibrary; +file_identifier "BCAT"; diff --git a/engine/src/flutter/impeller/blobcat/blob.h b/engine/src/flutter/impeller/blobcat/blob.h deleted file mode 100644 index dc8ec7e472..0000000000 --- a/engine/src/flutter/impeller/blobcat/blob.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#pragma once - -#include -#include -#include -#include - -#include "flutter/fml/macros.h" -#include "flutter/fml/mapping.h" - -namespace impeller { - -constexpr const uint32_t kBlobCatMagic = 0x0B10BCA7; -struct BlobHeader { - uint32_t magic = kBlobCatMagic; - uint32_t blob_count = 0u; -}; - -struct Blob { - enum class ShaderType : uint8_t { - kVertex, - kFragment, - }; - - static constexpr size_t kMaxNameLength = 32u; - - ShaderType type = ShaderType::kVertex; - uint64_t offset = 0; - uint64_t length = 0; - uint8_t name[kMaxNameLength] = {}; -}; - -struct BlobDescription { - Blob::ShaderType type; - std::string name; - std::shared_ptr mapping; -}; - -} // namespace impeller diff --git a/engine/src/flutter/impeller/blobcat/blob_library.cc b/engine/src/flutter/impeller/blobcat/blob_library.cc index 94af2799f9..12c0730721 100644 --- a/engine/src/flutter/impeller/blobcat/blob_library.cc +++ b/engine/src/flutter/impeller/blobcat/blob_library.cc @@ -6,66 +6,49 @@ #include +#include "impeller/base/validation.h" +#include "impeller/blobcat/blob_flatbuffers.h" + namespace impeller { -BlobLibrary::BlobLibrary(std::shared_ptr mapping) - : mapping_(std::move(mapping)) { - if (!mapping_ || mapping_->GetMapping() == nullptr) { - FML_LOG(ERROR) << "Invalid mapping."; +constexpr BlobShaderType ToShaderType(fb::Stage stage) { + switch (stage) { + case fb::Stage::kVertex: + return BlobShaderType::kVertex; + case fb::Stage::kFragment: + return BlobShaderType::kFragment; + } + FML_UNREACHABLE(); +} + +BlobLibrary::BlobLibrary(std::shared_ptr payload) + : payload_(std::move(payload)) { + if (!payload_ || payload_->GetMapping() == nullptr) { + VALIDATION_LOG << "Blob mapping was absent."; return; } - BlobHeader header; - std::vector blobs; - - size_t offset = 0u; - - // Read the header. - { - const size_t read_size = sizeof(BlobHeader); - if (mapping_->GetSize() < offset + read_size) { - return; - } - std::memcpy(&header, mapping_->GetMapping() + offset, read_size); - offset += read_size; - - // Validate the header. - if (header.magic != kBlobCatMagic) { - FML_LOG(ERROR) << "Invalid blob magic."; - return; - } - - blobs.resize(header.blob_count); + if (!fb::BlobLibraryBufferHasIdentifier(payload_->GetMapping())) { + VALIDATION_LOG << "Invalid blob magic."; + return; } - // Read the blob descriptions. - { - const size_t read_size = sizeof(Blob) * header.blob_count; - ::memcpy(blobs.data(), mapping_->GetMapping() + offset, read_size); - offset += read_size; // NOLINT(clang-analyzer-deadcode.DeadStores) + auto blob_library = fb::GetBlobLibrary(payload_->GetMapping()); + if (!blob_library) { + return; } - // Read the blobs. - { - for (size_t i = 0; i < header.blob_count; i++) { - const auto& blob = blobs[i]; - + if (auto items = blob_library->items()) { + for (auto i = items->begin(), end = items->end(); i != end; i++) { BlobKey key; - key.type = blob.type; - key.name = std::string{reinterpret_cast(blob.name)}; - auto mapping = std::make_shared( - mapping_->GetMapping() + blob.offset, // offset - blob.length, // length - [mapping = mapping_](const uint8_t* data, size_t size) {} - // release proc - ); - - auto inserted = blobs_.insert({key, mapping}); - if (!inserted.second) { - FML_LOG(ERROR) << "Shader library had duplicate shader named " - << key.name; - return; - } + key.name = i->name()->str(); + key.type = ToShaderType(i->stage()); + blobs_[key] = std::make_shared( + i->mapping()->Data(), i->mapping()->size(), + [payload = payload_](auto, auto) { + // The pointers are into the base payload. Instead of copying the + // data, just hold onto the payload. + }); } } @@ -84,7 +67,7 @@ size_t BlobLibrary::GetShaderCount() const { return blobs_.size(); } -std::shared_ptr BlobLibrary::GetMapping(Blob::ShaderType type, +std::shared_ptr BlobLibrary::GetMapping(BlobShaderType type, std::string name) const { BlobKey key; key.type = type; @@ -94,7 +77,7 @@ std::shared_ptr BlobLibrary::GetMapping(Blob::ShaderType type, } size_t BlobLibrary::IterateAllBlobs( - std::function& mapping)> callback) const { diff --git a/engine/src/flutter/impeller/blobcat/blob_library.h b/engine/src/flutter/impeller/blobcat/blob_library.h index c548ec9531..7d5a8b598f 100644 --- a/engine/src/flutter/impeller/blobcat/blob_library.h +++ b/engine/src/flutter/impeller/blobcat/blob_library.h @@ -11,13 +11,13 @@ #include "flutter/fml/hash_combine.h" #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "impeller/blobcat/blob.h" +#include "impeller/blobcat/blob_types.h" namespace impeller { class BlobLibrary { public: - BlobLibrary(std::shared_ptr mapping); + explicit BlobLibrary(std::shared_ptr payload); BlobLibrary(BlobLibrary&&); @@ -27,17 +27,17 @@ class BlobLibrary { size_t GetShaderCount() const; - std::shared_ptr GetMapping(Blob::ShaderType type, + std::shared_ptr GetMapping(BlobShaderType type, std::string name) const; size_t IterateAllBlobs( - std::function& mapping)>) const; private: struct BlobKey { - Blob::ShaderType type = Blob::ShaderType::kFragment; + BlobShaderType type = BlobShaderType::kFragment; std::string name; struct Hash { @@ -60,7 +60,7 @@ class BlobLibrary { BlobKey::Hash, BlobKey::Equal>; - std::shared_ptr mapping_; + std::shared_ptr payload_; Blobs blobs_; bool is_valid_ = false; diff --git a/engine/src/flutter/impeller/blobcat/blob.cc b/engine/src/flutter/impeller/blobcat/blob_types.h similarity index 75% rename from engine/src/flutter/impeller/blobcat/blob.cc rename to engine/src/flutter/impeller/blobcat/blob_types.h index e7f97177af..7c22d907d0 100644 --- a/engine/src/flutter/impeller/blobcat/blob.cc +++ b/engine/src/flutter/impeller/blobcat/blob_types.h @@ -2,10 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "impeller/blobcat/blob.h" +#pragma once namespace impeller { -// +enum class BlobShaderType { + kVertex, + kFragment, +}; } // namespace impeller diff --git a/engine/src/flutter/impeller/blobcat/blob_writer.cc b/engine/src/flutter/impeller/blobcat/blob_writer.cc index be500f5e9f..abba520b26 100644 --- a/engine/src/flutter/impeller/blobcat/blob_writer.cc +++ b/engine/src/flutter/impeller/blobcat/blob_writer.cc @@ -7,18 +7,20 @@ #include #include +#include "impeller/blobcat/blob_flatbuffers.h" + namespace impeller { BlobWriter::BlobWriter() = default; BlobWriter::~BlobWriter() = default; -std::optional InferShaderTypefromFileExtension( +std::optional InferShaderTypefromFileExtension( const std::filesystem::path& path) { if (path == ".vert") { - return Blob::ShaderType::kVertex; + return BlobShaderType::kVertex; } else if (path == ".frag") { - return Blob::ShaderType::kFragment; + return BlobShaderType::kFragment; } return std::nullopt; } @@ -67,79 +69,48 @@ bool BlobWriter::AddBlobAtPath(const std::string& std_path) { std::move(file_mapping)); } -bool BlobWriter::AddBlob(Blob::ShaderType type, +bool BlobWriter::AddBlob(BlobShaderType type, std::string name, std::shared_ptr mapping) { if (name.empty() || !mapping || mapping->GetMapping() == nullptr) { return false; } - if (name.length() >= Blob::kMaxNameLength) { - FML_LOG(ERROR) << "Blob name length was too long."; - return false; - } - blob_descriptions_.emplace_back( BlobDescription{type, std::move(name), std::move(mapping)}); return true; } +constexpr fb::Stage ToStage(BlobShaderType type) { + switch (type) { + case BlobShaderType::kVertex: + return fb::Stage::kVertex; + case BlobShaderType::kFragment: + return fb::Stage::kFragment; + } + FML_UNREACHABLE(); +} + std::shared_ptr BlobWriter::CreateMapping() const { - BlobHeader header; - header.blob_count = blob_descriptions_.size(); - - uint64_t offset = sizeof(BlobHeader) + (sizeof(Blob) * header.blob_count); - - std::vector blobs; - { - blobs.resize(header.blob_count); - for (size_t i = 0; i < header.blob_count; i++) { - const auto& desc = blob_descriptions_[i]; - blobs[i].type = desc.type; - blobs[i].offset = offset; - blobs[i].length = desc.mapping->GetSize(); - std::memcpy(reinterpret_cast(blobs[i].name), desc.name.data(), - desc.name.size()); - offset += blobs[i].length; + fb::BlobLibraryT blobs; + for (const auto& blob_description : blob_descriptions_) { + auto mapping = blob_description.mapping; + if (!mapping) { + return nullptr; } + auto desc = std::make_unique(); + desc->name = blob_description.name; + desc->stage = ToStage(blob_description.type); + desc->mapping = {mapping->GetMapping(), + mapping->GetMapping() + mapping->GetSize()}; + blobs.items.emplace_back(std::move(desc)); } - - { - auto buffer = std::make_shared>(); - buffer->resize(offset, 0); - - size_t write_offset = 0u; - - // Write the header. - { - const size_t write_length = sizeof(header); - std::memcpy(buffer->data() + write_offset, &header, write_length); - write_offset += write_length; - } - - // Write the blob descriptions. - { - const size_t write_length = blobs.size() * sizeof(Blob); - std::memcpy(buffer->data() + write_offset, blobs.data(), write_length); - write_offset += write_length; - } - - // Write the blobs themselves. - { - for (size_t i = 0; i < header.blob_count; i++) { - const auto& desc = blob_descriptions_[i]; - const size_t write_length = desc.mapping->GetSize(); - std::memcpy(buffer->data() + write_offset, desc.mapping->GetMapping(), - write_length); - write_offset += write_length; - } - } - FML_CHECK(write_offset == offset); - return std::make_shared( - buffer->data(), buffer->size(), - [buffer](const uint8_t* data, size_t size) {}); - } - return nullptr; + auto builder = std::make_shared(); + builder->Finish(fb::BlobLibrary::Pack(*builder.get(), &blobs), + fb::BlobLibraryIdentifier()); + return std::make_shared(builder->GetBufferPointer(), + builder->GetSize(), + [builder](auto, auto) {}); } } // namespace impeller diff --git a/engine/src/flutter/impeller/blobcat/blob_writer.h b/engine/src/flutter/impeller/blobcat/blob_writer.h index bbf60213ac..eb2f80c3e4 100644 --- a/engine/src/flutter/impeller/blobcat/blob_writer.h +++ b/engine/src/flutter/impeller/blobcat/blob_writer.h @@ -10,7 +10,7 @@ #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" -#include "impeller/blobcat/blob.h" +#include "impeller/blobcat/blob_types.h" namespace impeller { @@ -22,13 +22,19 @@ class BlobWriter { [[nodiscard]] bool AddBlobAtPath(const std::string& path); - [[nodiscard]] bool AddBlob(Blob::ShaderType type, + [[nodiscard]] bool AddBlob(BlobShaderType type, std::string name, std::shared_ptr mapping); std::shared_ptr CreateMapping() const; private: + struct BlobDescription { + BlobShaderType type; + std::string name; + std::shared_ptr mapping; + }; + std::vector blob_descriptions_; FML_DISALLOW_COPY_AND_ASSIGN(BlobWriter); diff --git a/engine/src/flutter/impeller/blobcat/blobcat_unittests.cc b/engine/src/flutter/impeller/blobcat/blobcat_unittests.cc index bc5bab227d..6fc8783f65 100644 --- a/engine/src/flutter/impeller/blobcat/blobcat_unittests.cc +++ b/engine/src/flutter/impeller/blobcat/blobcat_unittests.cc @@ -27,15 +27,15 @@ const std::string CreateStringFromMapping(const fml::Mapping& mapping) { TEST(BlobTest, CanReadAndWriteBlobs) { BlobWriter writer; - ASSERT_TRUE(writer.AddBlob(Blob::ShaderType::kVertex, "Hello", + ASSERT_TRUE(writer.AddBlob(BlobShaderType::kVertex, "Hello", CreateMappingFromString("World"))); - ASSERT_TRUE(writer.AddBlob(Blob::ShaderType::kFragment, "Foo", + ASSERT_TRUE(writer.AddBlob(BlobShaderType::kFragment, "Foo", CreateMappingFromString("Bar"))); - ASSERT_TRUE(writer.AddBlob(Blob::ShaderType::kVertex, "Baz", + ASSERT_TRUE(writer.AddBlob(BlobShaderType::kVertex, "Baz", CreateMappingFromString("Bang"))); - ASSERT_TRUE(writer.AddBlob(Blob::ShaderType::kVertex, "Ping", + ASSERT_TRUE(writer.AddBlob(BlobShaderType::kVertex, "Ping", CreateMappingFromString("Pong"))); - ASSERT_TRUE(writer.AddBlob(Blob::ShaderType::kFragment, "Pang", + ASSERT_TRUE(writer.AddBlob(BlobShaderType::kFragment, "Pang", CreateMappingFromString("World"))); auto mapping = writer.CreateMapping(); @@ -46,9 +46,9 @@ TEST(BlobTest, CanReadAndWriteBlobs) { ASSERT_EQ(library.GetShaderCount(), 5u); // Wrong type. - ASSERT_EQ(library.GetMapping(Blob::ShaderType::kFragment, "Hello"), nullptr); + ASSERT_EQ(library.GetMapping(BlobShaderType::kFragment, "Hello"), nullptr); - auto hello_vtx = library.GetMapping(Blob::ShaderType::kVertex, "Hello"); + auto hello_vtx = library.GetMapping(BlobShaderType::kVertex, "Hello"); ASSERT_NE(hello_vtx, nullptr); ASSERT_EQ(CreateStringFromMapping(*hello_vtx), "World"); } diff --git a/engine/src/flutter/impeller/renderer/backend/gles/shader_library_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/shader_library_gles.cc index bfe199aa06..b20e46d160 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/shader_library_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/shader_library_gles.cc @@ -14,11 +14,11 @@ namespace impeller { -static ShaderStage ToShaderStage(Blob::ShaderType type) { +static ShaderStage ToShaderStage(BlobShaderType type) { switch (type) { - case Blob::ShaderType::kVertex: + case BlobShaderType::kVertex: return ShaderStage::kVertex; - case Blob::ShaderType::kFragment: + case BlobShaderType::kFragment: return ShaderStage::kFragment; } FML_UNREACHABLE(); diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc index f7811e2c7e..e7bb8ec661 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc @@ -11,11 +11,11 @@ namespace impeller { -static ShaderStage ToShaderStage(Blob::ShaderType type) { +static ShaderStage ToShaderStage(BlobShaderType type) { switch (type) { - case Blob::ShaderType::kVertex: + case BlobShaderType::kVertex: return ShaderStage::kVertex; - case Blob::ShaderType::kFragment: + case BlobShaderType::kFragment: return ShaderStage::kFragment; } FML_UNREACHABLE();