Flutter Impeller
compiler_test.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 
6 
7 #include <algorithm>
8 
9 namespace impeller {
10 namespace compiler {
11 namespace testing {
12 
13 static fml::UniqueFD CreateIntermediatesDirectory() {
14  auto test_name = flutter::testing::GetCurrentTestName();
15  std::replace(test_name.begin(), test_name.end(), '/', '_');
16  std::replace(test_name.begin(), test_name.end(), '.', '_');
17  return fml::OpenDirectory(flutter::testing::OpenFixturesDirectory(),
18  test_name.c_str(),
19  true, // create if necessary
20  fml::FilePermission::kReadWrite);
21 }
22 
24  : intermediates_directory_(CreateIntermediatesDirectory()) {
25  FML_CHECK(intermediates_directory_.is_valid());
26 }
27 
28 CompilerTest::~CompilerTest() = default;
29 
30 static std::string ReflectionHeaderName(const char* fixture_name) {
31  std::stringstream stream;
32  stream << fixture_name << ".h";
33  return stream.str();
34 }
35 
36 static std::string ReflectionCCName(const char* fixture_name) {
37  std::stringstream stream;
38  stream << fixture_name << ".cc";
39  return stream.str();
40 }
41 
42 static std::string ReflectionJSONName(const char* fixture_name) {
43  std::stringstream stream;
44  stream << fixture_name << ".json";
45  return stream.str();
46 }
47 
48 static std::string SPIRVFileName(const char* fixture_name) {
49  std::stringstream stream;
50  stream << fixture_name << ".spv";
51  return stream.str();
52 }
53 
54 static std::string SLFileName(const char* fixture_name,
55  TargetPlatform platform) {
56  std::stringstream stream;
57  stream << fixture_name << "." << TargetPlatformSLExtension(platform);
58  return stream.str();
59 }
60 
61 std::unique_ptr<fml::FileMapping> CompilerTest::GetReflectionJson(
62  const char* fixture_name) const {
63  auto filename = ReflectionJSONName(fixture_name);
64  auto fd = fml::OpenFileReadOnly(intermediates_directory_, filename.c_str());
65  return fml::FileMapping::CreateReadOnly(fd);
66 }
67 
68 bool CompilerTest::CanCompileAndReflect(const char* fixture_name,
69  SourceType source_type,
70  SourceLanguage source_language,
71  const char* entry_point_name) const {
72  std::shared_ptr<fml::Mapping> fixture =
73  flutter::testing::OpenFixtureAsMapping(fixture_name);
74  if (!fixture || !fixture->GetMapping()) {
75  VALIDATION_LOG << "Could not find shader in fixtures: " << fixture_name;
76  return false;
77  }
78 
79  SourceOptions source_options(fixture_name, source_type);
80  source_options.target_platform = GetParam();
81  source_options.source_language = source_language;
82  source_options.working_directory = std::make_shared<fml::UniqueFD>(
83  flutter::testing::OpenFixturesDirectory());
85  fixture_name, SourceTypeFromFileName(fixture_name), source_language,
86  entry_point_name);
87 
88  Reflector::Options reflector_options;
89  reflector_options.header_file_name = ReflectionHeaderName(fixture_name);
90  reflector_options.shader_name = "shader_name";
91 
92  Compiler compiler(fixture, source_options, reflector_options);
93  if (!compiler.IsValid()) {
94  VALIDATION_LOG << "Compilation failed: " << compiler.GetErrorMessages();
95  return false;
96  }
97 
98  auto spirv_assembly = compiler.GetSPIRVAssembly();
99  if (!spirv_assembly) {
100  VALIDATION_LOG << "No spirv was generated.";
101  return false;
102  }
103 
104  if (!fml::WriteAtomically(intermediates_directory_,
105  SPIRVFileName(fixture_name).c_str(),
106  *spirv_assembly)) {
107  VALIDATION_LOG << "Could not write SPIRV intermediates.";
108  return false;
109  }
110 
111  auto sl_source = compiler.GetSLShaderSource();
112  if (!sl_source) {
113  VALIDATION_LOG << "No SL source was generated.";
114  return false;
115  }
116 
117  if (!fml::WriteAtomically(intermediates_directory_,
118  SLFileName(fixture_name, GetParam()).c_str(),
119  *sl_source)) {
120  VALIDATION_LOG << "Could not write SL intermediates.";
121  return false;
122  }
123 
124  if (TargetPlatformNeedsReflection(GetParam())) {
125  auto reflector = compiler.GetReflector();
126  if (!reflector) {
128  << "No reflector was found for target platform SL compiler.";
129  return false;
130  }
131 
132  auto reflection_json = reflector->GetReflectionJSON();
133  auto reflection_header = reflector->GetReflectionHeader();
134  auto reflection_source = reflector->GetReflectionCC();
135 
136  if (!reflection_json) {
137  VALIDATION_LOG << "Reflection JSON was not found.";
138  return false;
139  }
140 
141  if (!reflection_header) {
142  VALIDATION_LOG << "Reflection header was not found.";
143  return false;
144  }
145 
146  if (!reflection_source) {
147  VALIDATION_LOG << "Reflection source was not found.";
148  return false;
149  }
150 
151  if (!fml::WriteAtomically(intermediates_directory_,
152  ReflectionHeaderName(fixture_name).c_str(),
153  *reflection_header)) {
154  VALIDATION_LOG << "Could not write reflection header intermediates.";
155  return false;
156  }
157 
158  if (!fml::WriteAtomically(intermediates_directory_,
159  ReflectionCCName(fixture_name).c_str(),
160  *reflection_source)) {
161  VALIDATION_LOG << "Could not write reflection CC intermediates.";
162  return false;
163  }
164 
165  if (!fml::WriteAtomically(intermediates_directory_,
166  ReflectionJSONName(fixture_name).c_str(),
167  *reflection_json)) {
168  VALIDATION_LOG << "Could not write reflection json intermediates.";
169  return false;
170  }
171  }
172  return true;
173 }
174 
175 } // namespace testing
176 } // namespace compiler
177 } // namespace impeller
impeller::compiler::Compiler
Definition: compiler.h:24
impeller::compiler::testing::SLFileName
static std::string SLFileName(const char *fixture_name, TargetPlatform platform)
Definition: compiler_test.cc:54
impeller::compiler::testing::CompilerTest::~CompilerTest
~CompilerTest()
impeller::compiler::TargetPlatformSLExtension
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition: types.cc:243
impeller::compiler::testing::ReflectionCCName
static std::string ReflectionCCName(const char *fixture_name)
Definition: compiler_test.cc:36
impeller::compiler::SourceOptions
Definition: source_options.h:19
impeller::compiler::TargetPlatformNeedsReflection
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition: types.cc:126
impeller::compiler::testing::ReflectionHeaderName
static std::string ReflectionHeaderName(const char *fixture_name)
Definition: compiler_test.cc:30
impeller::compiler::testing::CompilerTest::GetReflectionJson
std::unique_ptr< fml::FileMapping > GetReflectionJson(const char *fixture_name) const
Definition: compiler_test.cc:61
impeller::compiler::Reflector::Options::header_file_name
std::string header_file_name
Definition: reflector.h:55
impeller::compiler::TargetPlatform
TargetPlatform
Definition: types.h:28
impeller::compiler::testing::SPIRVFileName
static std::string SPIRVFileName(const char *fixture_name)
Definition: compiler_test.cc:48
impeller::compiler::testing::ReflectionJSONName
static std::string ReflectionJSONName(const char *fixture_name)
Definition: compiler_test.cc:42
impeller::compiler::SourceOptions::source_language
SourceLanguage source_language
Definition: source_options.h:22
impeller::compiler::Reflector::Options::shader_name
std::string shader_name
Definition: reflector.h:54
impeller::compiler::SourceOptions::entry_point_name
std::string entry_point_name
Definition: source_options.h:26
impeller::compiler::Compiler::GetErrorMessages
std::string GetErrorMessages() const
Definition: compiler.cc:439
impeller::compiler::testing::CreateIntermediatesDirectory
static fml::UniqueFD CreateIntermediatesDirectory()
Definition: compiler_test.cc:13
impeller::compiler::Compiler::GetSPIRVAssembly
std::shared_ptr< fml::Mapping > GetSPIRVAssembly() const
Definition: compiler.cc:421
impeller::compiler::EntryPointFunctionNameFromSourceName
std::string EntryPointFunctionNameFromSourceName(const std::string &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition: types.cc:90
impeller::compiler::Compiler::GetReflector
const Reflector * GetReflector() const
Definition: compiler.cc:482
impeller::compiler::Reflector::Options
Definition: reflector.h:51
impeller::compiler::SourceType
SourceType
Definition: types.h:19
impeller::compiler::SourceOptions::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: source_options.h:23
compiler_test.h
impeller::compiler::SourceTypeFromFileName
SourceType SourceTypeFromFileName(const std::string &file_name)
Definition: types.cc:29
impeller::compiler::SourceOptions::target_platform
TargetPlatform target_platform
Definition: source_options.h:21
impeller::compiler::Compiler::IsValid
bool IsValid() const
Definition: compiler.cc:429
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::compiler::testing::CompilerTest::CanCompileAndReflect
bool CanCompileAndReflect(const char *fixture_name, SourceType source_type=SourceType::kUnknown, SourceLanguage source_language=SourceLanguage::kGLSL, const char *entry_point_name="main") const
Definition: compiler_test.cc:68
impeller::compiler::SourceLanguage
SourceLanguage
Definition: types.h:41
impeller::compiler::testing::CompilerTest::CompilerTest
CompilerTest()
Definition: compiler_test.cc:23
impeller::compiler::Compiler::GetSLShaderSource
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
Definition: compiler.cc:425
impeller
Definition: aiks_context.cc:10