• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ANDROID_AMBER_SCRIPT_H_
16 #define ANDROID_AMBER_SCRIPT_H_
17 
18 #include <android_native_app_glue.h>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "amber/amber.h"
24 #include "amber/result.h"
25 
26 namespace amber {
27 namespace android {
28 
29 struct AmberScriptInfo {
30   std::string asset_name;      // Script asset name. Note it is not a
31                                // path and just the name of script file.
32   std::string script_content;  // Script itself from the script file.
33   amber::ShaderMap shader_map;
34 };
35 
36 // A class to load scripts for Amber under assets/amber/ into
37 // |script_info_|. We assume that file extension of those scripts
38 // is ".amber" and all files with the extension are scripts for
39 // Amber.
40 class AmberScriptLoader {
41  public:
42   explicit AmberScriptLoader(android_app* app);
43   ~AmberScriptLoader();
44 
45   Result LoadAllScriptsFromAsset();
GetScripts()46   const std::vector<AmberScriptInfo>& GetScripts() const {
47     return script_info_;
48   }
49 
50  private:
51   // Find all files with ".amber" extension and set |asset_name| of
52   // |script_info_| as their names. In addition, return all shader
53   // file names that have ".spv" extensions.
54   std::vector<std::string> FindAllScriptsAndReturnShaderNames();
55 
56   // Return content of script named |script_name| under
57   // assets/amber/ as a std::string.
58   std::string ReadScript(const std::string& script_name);
59 
60   // Return SPIRV binary of script named |shader_name| under
61   // assets/amber/ as a std::vector<uint32_t>.
62   std::vector<uint32_t> ReadSpvShader(const std::string& shader_name);
63 
64   // Return content of asset named |asset_name| under assets/amber/
65   // as a std::vector<uint8_t>.
66   std::vector<uint8_t> ReadContent(const std::string& asset_name);
67 
68   android_app* app_context_ = nullptr;
69   std::vector<AmberScriptInfo> script_info_;
70 };
71 
72 }  // namespace android
73 }  // namespace amber
74 
75 #endif  // ANDROID_AMBER_SCRIPT_H_
76