| 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 <string>
6
7 #include "flutter/fml/mapping.h"
8 #include "flutter/testing/testing.h"
9 #include "impeller/base/validation.h"
10 #include "impeller/shader_archive/multi_arch_shader_archive.h"
11 #include "impeller/shader_archive/multi_arch_shader_archive_flatbuffers.h"
12 #include "impeller/shader_archive/multi_arch_shader_archive_writer.h"
13 #include "impeller/shader_archive/shader_archive.h"
14 #include "impeller/shader_archive/shader_archive_flatbuffers.h"
15 #include "impeller/shader_archive/shader_archive_writer.h"
16
17 namespace impeller {
18 namespace testing {
19
20 static std::shared_ptr<fml::Mapping> CreateMappingFromString(
21 std::string p_string) {
22 auto string = std::make_shared<std::string>(std::move(p_string));
23 return std::make_shared<fml::NonOwnedMapping>(
24 reinterpret_cast<const uint8_t*>(string->data()), string->size(),
25 [string](auto, auto) {});
26 }
27
28 const std::string CreateStringFromMapping(const fml::Mapping& mapping) {
29 return std::string{reinterpret_cast<const char*>(mapping.GetMapping()),
30 mapping.GetSize()};
31 }
32
33 TEST(ShaderArchiveTest, CanReadAndWriteBlobs) {
34 ShaderArchiveWriter writer;
35 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kVertex, "Hello",
36 CreateMappingFromString("World")));
37 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kFragment, "Foo",
38 CreateMappingFromString("Bar")));
39 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kVertex, "Baz",
40 CreateMappingFromString("Bang")));
41 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kVertex, "Ping",
42 CreateMappingFromString("Pong")));
43 ASSERT_TRUE(writer.AddShader(ArchiveShaderType::kFragment, "Pang",
44 CreateMappingFromString("World")));
45
46 auto mapping = writer.CreateMapping();
47 ASSERT_NE(mapping, nullptr);
48
49 MultiArchShaderArchiveWriter multi_writer;
50
51 ASSERT_TRUE(multi_writer.RegisterShaderArchive(
52 ArchiveRenderingBackend::kOpenGLES, mapping));
53
54 {
55 ScopedValidationDisable no_val;
56 // Can't add the same backend again.
57 ASSERT_FALSE(multi_writer.RegisterShaderArchive(
58 ArchiveRenderingBackend::kOpenGLES, mapping));
59 }
60
61 auto multi_mapping = multi_writer.CreateMapping();
62 ASSERT_TRUE(multi_mapping);
63
64 {
65 ScopedValidationDisable no_val;
66 auto no_library = MultiArchShaderArchive::CreateArchiveFromMapping(
67 multi_mapping, ArchiveRenderingBackend::kVulkan);
68 ASSERT_EQ(no_library, nullptr);
69 }
70
71 auto library = MultiArchShaderArchive::CreateArchiveFromMapping(
72 multi_mapping, ArchiveRenderingBackend::kOpenGLES);
73 ASSERT_EQ(library->GetShaderCount(), 5u);
74
75 // Wrong type.
76 ASSERT_EQ(library->GetMapping(ArchiveShaderType::kFragment, "Hello"),
77 nullptr);
78
79 auto hello_vtx = library->GetMapping(ArchiveShaderType::kVertex, "Hello");
80 ASSERT_NE(hello_vtx, nullptr);
81 ASSERT_EQ(CreateStringFromMapping(*hello_vtx), "World");
82 }
83
84 TEST(ShaderArchiveTest, ArchiveAndMultiArchiveHaveDifferentIdentifiers) {
85 // The unarchiving process depends on these identifiers to check to see if its
86 // a standalone archive or a multi-archive. Things will get nutty if these are
87 // ever the same.
88 auto archive_id = fb::ShaderArchiveIdentifier();
89 auto multi_archive_id = fb::MultiArchShaderArchiveIdentifier();
90 ASSERT_EQ(std::strlen(archive_id), std::strlen(multi_archive_id));
91 ASSERT_NE(std::strncmp(archive_id, multi_archive_id, std::strlen(archive_id)),
92 0);
93 }
94
95 } // namespace testing
96 } // namespace impeller
multi_arch_shader_archive_writer.h
impeller::ArchiveRenderingBackend::kVulkan
@ kVulkan
impeller::testing::CreateMappingFromString
static std::shared_ptr< fml::Mapping > CreateMappingFromString(std::string p_string)
Definition: shader_archive_unittests.cc:20
impeller::ShaderArchiveWriter::CreateMapping
std::shared_ptr< fml::Mapping > CreateMapping() const
Definition: shader_archive_writer.cc:98
impeller::MultiArchShaderArchiveWriter::CreateMapping
std::shared_ptr< fml::Mapping > CreateMapping() const
Definition: multi_arch_shader_archive_writer.cc:44
validation.h
impeller::MultiArchShaderArchive::CreateArchiveFromMapping
static std::shared_ptr< ShaderArchive > CreateArchiveFromMapping(const std::shared_ptr< const fml::Mapping > &mapping, ArchiveRenderingBackend backend)
Definition: multi_arch_shader_archive.cc:24
impeller::MultiArchShaderArchiveWriter::RegisterShaderArchive
bool RegisterShaderArchive(ArchiveRenderingBackend backend, std::shared_ptr< const fml::Mapping > mapping)
Definition: multi_arch_shader_archive_writer.cc:16
multi_arch_shader_archive.h
impeller::ArchiveRenderingBackend::kOpenGLES
@ kOpenGLES
shader_archive_writer.h
impeller::ArchiveShaderType::kVertex
@ kVertex
impeller::testing::TEST
TEST(CanvasRecorder, Save)
Definition: canvas_recorder_unittests.cc:65
impeller::ShaderArchiveWriter::AddShader
bool AddShader(ArchiveShaderType type, std::string name, std::shared_ptr< fml::Mapping > mapping)
Definition: shader_archive_writer.cc:74
shader_archive.h
impeller::testing::CreateStringFromMapping
const std::string CreateStringFromMapping(const fml::Mapping &mapping)
Definition: shader_archive_unittests.cc:28
impeller::MultiArchShaderArchiveWriter
Definition: multi_arch_shader_archive_writer.h:16
impeller::ArchiveShaderType::kFragment
@ kFragment
impeller::ShaderArchiveWriter
Definition: shader_archive_writer.h:18
impeller::ScopedValidationDisable
Definition: validation.h:38
impeller
Definition: aiks_blur_unittests.cc:20