| Flutter Impeller |

shader_archive_unittests.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
5 #include <sstream>
6 #include <string>
7
8 #include "flutter/fml/mapping.h"
9 #include "flutter/testing/testing.h"
10 #include "impeller/shader_archive/shader_archive.h"
11 #include "impeller/shader_archive/shader_archive_flatbuffers.h"
12 #include "impeller/shader_archive/shader_archive_writer.h"
13
14 namespace impeller {
15 namespace testing {
16
17 static std::shared_ptr<fml::Mapping> CreateMappingFromString(
18 std::string p_string) {
19 auto string = std::make_shared<std::string>(std::move(p_string));
20 return std::make_shared<fml::NonOwnedMapping>(
21 reinterpret_cast<const uint8_t*>(string->data()), string->size(),
22 [string](auto, auto) {});
23 }
24
25 const std::string CreateStringFromMapping(const fml::Mapping& mapping) {
26 return std::string{reinterpret_cast<const char*>(mapping.GetMapping()),
27 mapping.GetSize()};
28 }
29
30 TEST(ShaderArchiveTest, CanReadAndWriteBlobs) {
31 ShaderArchiveWriter writer;
32 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kVertex, "Hello",
33 CreateMappingFromString("World")));
34 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kFragment, "Foo",
35 CreateMappingFromString("Bar")));
36 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kVertex, "Baz",
37 CreateMappingFromString("Bang")));
38 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kVertex, "Ping",
39 CreateMappingFromString("Pong")));
40 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kFragment, "Pang",
41 CreateMappingFromString("World")));
42
43 auto mapping = writer.CreateMapping();
44 ASSERT_NE(mapping, nullptr);
45
46 auto library = ShaderArchive::Create(mapping);
47 ASSERT_TRUE(library.ok());
48 ASSERT_EQ(library->GetShaderCount(), 5u);
49
50 // Wrong type.
51 ASSERT_EQ(library->GetMapping(ArchiveShaderType::kFragment, "Hello"),
52 nullptr);
53
54 auto hello_vtx = library->GetMapping(ArchiveShaderType::kVertex, "Hello");
55 ASSERT_NE(hello_vtx, nullptr);
56 ASSERT_EQ(CreateStringFromMapping(*hello_vtx), "World");
57 }
58
59 TEST(ShaderArchiveTest, ReturnsErrorOnInvalidVersion) {
60 fb::ShaderArchiveT shader_archive;
61 shader_archive.format_version = -1;
62
63 auto builder = std::make_shared<flatbuffers::FlatBufferBuilder>();
64 builder->Finish(fb::ShaderArchive::Pack(*builder.get(), &shader_archive),
65 fb::ShaderArchiveIdentifier());
66 auto mapping = std::make_shared<fml::NonOwnedMapping>(
67 builder->GetBufferPointer(), builder->GetSize(),
68 [builder](auto, auto) {});
69
70 auto library = ShaderArchive::Create(mapping);
71 ASSERT_FALSE(library.ok());
72 ASSERT_EQ(library.status().code(), absl::StatusCode::kInvalidArgument);
73 std::stringstream stream;
74 stream << "Unsupported shader archive format version. Expected: "
75 << static_cast<uint32_t>(fb::ShaderArchiveFormatVersion::kVersion)
76 << ", Got: 4294967295";
77 ASSERT_EQ(library.status().message(), stream.str());
78 }
79
80 } // namespace testing
81 } // namespace impeller
impeller::ShaderArchive::Create
static absl::StatusOr< ShaderArchive > Create(std::shared_ptr< fml::Mapping > payload)
Definition: shader_archive.cc:29
impeller::ShaderArchiveWriter
Definition: shader_archive_writer.h:17
impeller::ShaderArchiveWriter::AddShader
bool AddShader(ArchiveShaderType type, std::string name, std::shared_ptr< fml::Mapping > mapping)
Definition: shader_archive_writer.cc:87
impeller::ShaderArchiveWriter::CreateMapping
std::shared_ptr< fml::Mapping > CreateMapping() const
Definition: shader_archive_writer.cc:111
impeller::testing::CreateStringFromMapping
const std::string CreateStringFromMapping(const fml::Mapping &mapping)
Definition: shader_archive_unittests.cc:25
impeller::testing::TEST
TEST(AllocationSizeTest, CanCreateTypedAllocations)
Definition: allocation_size_unittests.cc:10
impeller::testing::CreateMappingFromString
static std::shared_ptr< fml::Mapping > CreateMappingFromString(std::string p_string)
Definition: shader_archive_unittests.cc:17
impeller
Definition: allocation.cc:12
impeller::ArchiveShaderType::kFragment
@ kFragment
impeller::ArchiveShaderType::kVertex
@ kVertex
shader_archive.h
shader_archive_writer.h