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