Flutter Engine
The Flutter Engine
includer.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 <cstring>
8
9#include "flutter/fml/paths.h"
10
11namespace impeller {
12namespace compiler {
13
14Includer::Includer(std::shared_ptr<fml::UniqueFD> working_directory,
15 std::vector<IncludeDir> include_dirs,
16 std::function<void(std::string)> on_file_included)
17 : working_directory_(std::move(working_directory)),
18 include_dirs_(std::move(include_dirs)),
19 on_file_included_(std::move(on_file_included)) {}
20
21// |shaderc::CompileOptions::IncluderInterface|
22Includer::~Includer() = default;
23
24std::unique_ptr<fml::FileMapping> Includer::TryOpenMapping(
25 const IncludeDir& dir,
26 const char* requested_source) {
27 if (!dir.dir || !dir.dir->is_valid()) {
28 return nullptr;
29 }
30
31 if (requested_source == nullptr) {
32 return nullptr;
33 }
34
35 std::string source(requested_source);
36 if (source.empty()) {
37 return nullptr;
38 }
39
40 auto mapping = fml::FileMapping::CreateReadOnly(*dir.dir, requested_source);
41 if (!mapping || !mapping->IsValid()) {
42 return nullptr;
43 }
44
45 on_file_included_(fml::paths::JoinPaths({dir.name, requested_source}));
46
47 return mapping;
48}
49
50std::unique_ptr<fml::FileMapping> Includer::FindFirstMapping(
51 const char* requested_source) {
52 // Always try the working directory first no matter what the include
53 // directories are.
54 {
55 IncludeDir dir;
56 dir.name = ".";
57 dir.dir = working_directory_;
58 if (auto mapping = TryOpenMapping(dir, requested_source)) {
59 return mapping;
60 }
61 }
62
63 for (const auto& include_dir : include_dirs_) {
64 if (auto mapping = TryOpenMapping(include_dir, requested_source)) {
65 return mapping;
66 }
67 }
68 return nullptr;
69}
70
71shaderc_include_result* Includer::GetInclude(const char* requested_source,
72 shaderc_include_type type,
73 const char* requesting_source,
74 size_t include_depth) {
75 auto result = std::make_unique<shaderc_include_result>();
76
77 // Default initialize to failed inclusion.
78 result->source_name = "";
79 result->source_name_length = 0;
80
81 constexpr const char* kFileNotFoundMessage = "Included file not found.";
82 result->content = kFileNotFoundMessage;
83 result->content_length = ::strlen(kFileNotFoundMessage);
84 result->user_data = nullptr;
85
86 if (!working_directory_ || !working_directory_->is_valid()) {
87 return result.release();
88 }
89
90 if (requested_source == nullptr) {
91 return result.release();
92 }
93
94 auto file = FindFirstMapping(requested_source);
95
96 if (!file || file->GetMapping() == nullptr) {
97 return result.release();
98 }
99
100 auto includer_data =
101 std::make_unique<IncluderData>(requested_source, std::move(file));
102
103 result->source_name = includer_data->file_name.c_str();
104 result->source_name_length = includer_data->file_name.length();
105 result->content = reinterpret_cast<decltype(result->content)>(
106 includer_data->mapping->GetMapping());
107 result->content_length = includer_data->mapping->GetSize();
108 result->user_data = includer_data.release();
109
110 return result.release();
111}
112
113void Includer::ReleaseInclude(shaderc_include_result* data) {
114 delete reinterpret_cast<IncluderData*>(data->user_data);
115 delete data;
116}
117
118} // namespace compiler
119} // namespace impeller
GLenum type
bool empty() const
Definition: SkBitmap.h:210
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:20
Includer(std::shared_ptr< fml::UniqueFD > working_directory, std::vector< IncludeDir > include_dirs, std::function< void(std::string)> on_file_included)
Definition: includer.cc:14
void ReleaseInclude(shaderc_include_result *data) override
Definition: includer.cc:113
shaderc_include_result * GetInclude(const char *requested_source, shaderc_include_type type, const char *requesting_source, size_t include_depth) override
Definition: includer.cc:71
SkBitmap source
Definition: examples.cpp:28
GAsyncResult * result
Dart_NativeFunction function
Definition: fuchsia.cc:51
union flutter::testing::@2836::KeyboardChange::@76 content
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets dir
Definition: switches.h:145
std::string JoinPaths(std::initializer_list< std::string > components)
Definition: paths.cc:14
Definition: ref_ptr.h:256
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63