Flutter Impeller
scenec_main.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 <filesystem>
6 #include <memory>
7 
8 #include "flutter/fml/backtrace.h"
9 #include "flutter/fml/command_line.h"
10 #include "flutter/fml/file.h"
11 #include "flutter/fml/macros.h"
12 #include "flutter/fml/mapping.h"
13 #include "impeller/base/strings.h"
16 #include "impeller/scene/importer/scene_flatbuffers.h"
19 
20 #include "third_party/flatbuffers/include/flatbuffers/flatbuffer_builder.h"
21 
22 namespace impeller {
23 namespace scene {
24 namespace importer {
25 
26 // Sets the file access mode of the file at path 'p' to 0644.
27 static bool SetPermissiveAccess(const std::filesystem::path& p) {
28  auto permissions =
29  std::filesystem::perms::owner_read | std::filesystem::perms::owner_write |
30  std::filesystem::perms::group_read | std::filesystem::perms::others_read;
31  std::error_code error;
32  std::filesystem::permissions(p, permissions, error);
33  if (error) {
34  std::cerr << "Failed to set access on file '" << p
35  << "': " << error.message() << std::endl;
36  return false;
37  }
38  return true;
39 }
40 
41 bool Main(const fml::CommandLine& command_line) {
42  fml::InstallCrashHandler();
43  if (command_line.HasOption("help")) {
44  Switches::PrintHelp(std::cout);
45  return true;
46  }
47 
48  Switches switches(command_line);
49  if (!switches.AreValid(std::cerr)) {
50  std::cerr << "Invalid flags specified." << std::endl;
51  Switches::PrintHelp(std::cerr);
52  return false;
53  }
54 
55  auto source_file_mapping =
56  fml::FileMapping::CreateReadOnly(switches.source_file_name);
57  if (!source_file_mapping) {
58  std::cerr << "Could not open input file." << std::endl;
59  return false;
60  }
61 
62  fb::SceneT scene;
63  bool success = false;
64  switch (switches.input_type) {
65  case SourceType::kGLTF:
66  success = ParseGLTF(*source_file_mapping, scene);
67  break;
69  std::cerr << "Unknown input type." << std::endl;
70  return false;
71  }
72  if (!success) {
73  std::cerr << "Failed to parse input file." << std::endl;
74  return false;
75  }
76 
77  flatbuffers::FlatBufferBuilder builder;
78  builder.Finish(fb::Scene::Pack(builder, &scene), fb::SceneIdentifier());
79 
80  auto output_file_name = std::filesystem::absolute(
81  std::filesystem::current_path() / switches.output_file_name);
82  fml::NonOwnedMapping mapping(builder.GetCurrentBufferPointer(),
83  builder.GetSize());
84  if (!fml::WriteAtomically(*switches.working_directory,
85  compiler::Utf8FromPath(output_file_name).c_str(),
86  mapping)) {
87  std::cerr << "Could not write file to " << switches.output_file_name
88  << std::endl;
89  return false;
90  }
91 
92  // Tools that consume the geometry data expect the access mode to be 0644.
93  if (!SetPermissiveAccess(output_file_name)) {
94  return false;
95  }
96 
97  return true;
98 }
99 
100 } // namespace importer
101 } // namespace scene
102 } // namespace impeller
103 
104 int main(int argc, char const* argv[]) {
106  fml::CommandLineFromPlatformOrArgcArgv(argc, argv))
107  ? EXIT_SUCCESS
108  : EXIT_FAILURE;
109 }
impeller::scene::importer::Switches::input_type
SourceType input_type
Definition: switches.h:23
impeller::scene::importer::ParseGLTF
bool ParseGLTF(const fml::Mapping &source_mapping, fb::SceneT &out_scene)
Definition: importer_gltf.cc:450
importer.h
impeller::scene::importer::Switches
Definition: switches.h:20
impeller::scene::importer::Switches::AreValid
bool AreValid(std::ostream &explain) const
Definition: switches.cc:67
impeller::scene::importer::Switches::output_file_name
std::string output_file_name
Definition: switches.h:24
main
int main(int argc, char const *argv[])
Definition: scenec_main.cc:104
impeller::scene::importer::SetPermissiveAccess
static bool SetPermissiveAccess(const std::filesystem::path &p)
Definition: scenec_main.cc:27
impeller::scene::importer::Switches::source_file_name
std::string source_file_name
Definition: switches.h:22
impeller::scene::importer::Main
bool Main(const fml::CommandLine &command_line)
Definition: scenec_main.cc:41
utilities.h
strings.h
impeller::scene::importer::SourceType::kGLTF
@ kGLTF
impeller::scene::importer::SourceType::kUnknown
@ kUnknown
types.h
switches.h
impeller::compiler::Utf8FromPath
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition: utilities.cc:30
impeller::scene::importer::Switches::working_directory
std::shared_ptr< fml::UniqueFD > working_directory
Definition: switches.h:21
impeller
Definition: aiks_blur_unittests.cc:20
impeller::scene::importer::Switches::PrintHelp
static void PrintHelp(std::ostream &stream)
Definition: switches.cc:24