[Impeller] Use flatbuffers in blobcat. (flutter/engine#35443)

This commit is contained in:
Chinmay Garde
2022-08-16 17:20:50 -07:00
committed by GitHub
parent f237e36090
commit e93a4f24db
12 changed files with 139 additions and 185 deletions

View File

@@ -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

View File

@@ -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",
]

View File

@@ -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";

View File

@@ -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 <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#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<fml::Mapping> mapping;
};
} // namespace impeller

View File

@@ -6,66 +6,49 @@
#include <string>
#include "impeller/base/validation.h"
#include "impeller/blobcat/blob_flatbuffers.h"
namespace impeller {
BlobLibrary::BlobLibrary(std::shared_ptr<fml::Mapping> 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<fml::Mapping> payload)
: payload_(std::move(payload)) {
if (!payload_ || payload_->GetMapping() == nullptr) {
VALIDATION_LOG << "Blob mapping was absent.";
return;
}
BlobHeader header;
std::vector<Blob> 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<const char*>(blob.name)};
auto mapping = std::make_shared<fml::NonOwnedMapping>(
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<fml::NonOwnedMapping>(
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<fml::Mapping> BlobLibrary::GetMapping(Blob::ShaderType type,
std::shared_ptr<fml::Mapping> BlobLibrary::GetMapping(BlobShaderType type,
std::string name) const {
BlobKey key;
key.type = type;
@@ -94,7 +77,7 @@ std::shared_ptr<fml::Mapping> BlobLibrary::GetMapping(Blob::ShaderType type,
}
size_t BlobLibrary::IterateAllBlobs(
std::function<bool(Blob::ShaderType type,
std::function<bool(BlobShaderType type,
const std::string& name,
const std::shared_ptr<fml::Mapping>& mapping)> callback)
const {

View File

@@ -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<fml::Mapping> mapping);
explicit BlobLibrary(std::shared_ptr<fml::Mapping> payload);
BlobLibrary(BlobLibrary&&);
@@ -27,17 +27,17 @@ class BlobLibrary {
size_t GetShaderCount() const;
std::shared_ptr<fml::Mapping> GetMapping(Blob::ShaderType type,
std::shared_ptr<fml::Mapping> GetMapping(BlobShaderType type,
std::string name) const;
size_t IterateAllBlobs(
std::function<bool(Blob::ShaderType type,
std::function<bool(BlobShaderType type,
const std::string& name,
const std::shared_ptr<fml::Mapping>& 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<fml::Mapping> mapping_;
std::shared_ptr<fml::Mapping> payload_;
Blobs blobs_;
bool is_valid_ = false;

View File

@@ -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

View File

@@ -7,18 +7,20 @@
#include <filesystem>
#include <optional>
#include "impeller/blobcat/blob_flatbuffers.h"
namespace impeller {
BlobWriter::BlobWriter() = default;
BlobWriter::~BlobWriter() = default;
std::optional<Blob::ShaderType> InferShaderTypefromFileExtension(
std::optional<BlobShaderType> 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<fml::Mapping> 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<fml::Mapping> BlobWriter::CreateMapping() const {
BlobHeader header;
header.blob_count = blob_descriptions_.size();
uint64_t offset = sizeof(BlobHeader) + (sizeof(Blob) * header.blob_count);
std::vector<Blob> 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<void*>(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<fb::BlobT>();
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<std::vector<uint8_t>>();
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<fml::NonOwnedMapping>(
buffer->data(), buffer->size(),
[buffer](const uint8_t* data, size_t size) {});
}
return nullptr;
auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
builder->Finish(fb::BlobLibrary::Pack(*builder.get(), &blobs),
fb::BlobLibraryIdentifier());
return std::make_shared<fml::NonOwnedMapping>(builder->GetBufferPointer(),
builder->GetSize(),
[builder](auto, auto) {});
}
} // namespace impeller

View File

@@ -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<fml::Mapping> mapping);
std::shared_ptr<fml::Mapping> CreateMapping() const;
private:
struct BlobDescription {
BlobShaderType type;
std::string name;
std::shared_ptr<fml::Mapping> mapping;
};
std::vector<BlobDescription> blob_descriptions_;
FML_DISALLOW_COPY_AND_ASSIGN(BlobWriter);

View File

@@ -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");
}

View File

@@ -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();

View File

@@ -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();