1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 
16 #include "loader/render_data_loader.h"
17 
18 #include <base/containers/string_view.h>
19 #include <core/io/intf_directory.h>
20 #include <core/io/intf_file_manager.h>
21 #include <core/namespace.h>
22 #include <render/datastore/intf_render_data_store_pod.h>
23 
24 #include "loader/render_data_configuration_loader.h"
25 #include "util/log.h"
26 
27 using namespace BASE_NS;
28 using namespace CORE_NS;
29 
30 RENDER_BEGIN_NAMESPACE()
31 namespace {
HasExtension(string_view ext,const string_view fileUri)32 bool HasExtension(string_view ext, const string_view fileUri)
33 {
34     if (auto const pos = fileUri.rfind(ext); pos != string_view::npos) {
35         return ext.size() == (fileUri.length() - pos);
36     }
37     return false;
38 }
39 } // namespace
40 
RenderDataLoader(IFileManager & fileManager)41 RenderDataLoader::RenderDataLoader(IFileManager& fileManager) : fileManager_(fileManager) {}
42 
Load(const BASE_NS::string_view pathPrefix,IRenderDataStorePod & renderDataStorePod)43 void RenderDataLoader::Load(const BASE_NS::string_view pathPrefix, IRenderDataStorePod& renderDataStorePod)
44 {
45     const string fullPath = pathPrefix + RENDER_CONFIG_PATH;
46     if (auto const path = fileManager_.OpenDirectory(fullPath); path) {
47         const string currentPath = fullPath + string_view(POST_PROCESS_PATH) + '/';
48         auto currentDirectory = fileManager_.OpenDirectory(currentPath);
49         if (currentDirectory) {
50             RecurseDirectory(currentPath, *currentDirectory, ConfigurationType::POST_PROCESS, renderDataStorePod);
51         } else {
52             PLUGIN_LOG_E("render configuration files path (%s) not found.", currentPath.c_str());
53         }
54     } else {
55         PLUGIN_LOG_E("render configuration files path (%s) not found.", fullPath.c_str());
56     }
57 }
58 
RecurseDirectory(const string_view currentPath,const IDirectory & directory,const ConfigurationType configurationType,IRenderDataStorePod & renderDataStorePod)59 void RenderDataLoader::RecurseDirectory(const string_view currentPath, const IDirectory& directory,
60     const ConfigurationType configurationType, IRenderDataStorePod& renderDataStorePod)
61 {
62     for (auto const& entry : directory.GetEntries()) {
63         switch (entry.type) {
64             default:
65             case IDirectory::Entry::Type::UNKNOWN:
66                 break;
67             case IDirectory::Entry::Type::FILE: {
68                 if (HasExtension(".json", entry.name)) {
69                     auto const file = currentPath + entry.name;
70                     IRenderDataConfigurationLoader::LoadResult result;
71                     if (configurationType == ConfigurationType::POST_PROCESS) {
72                         const auto loadedPP = RenderDataConfigurationLoader::LoadPostProcess(fileManager_, file);
73                         if (loadedPP.loadResult.success) {
74                             renderDataStorePod.CreatePod(
75                                 "PostProcess", loadedPP.name, arrayviewU8(loadedPP.postProcessConfiguration));
76                         }
77                         result = loadedPP.loadResult;
78                     }
79 
80                     if (!result.success) {
81                         PLUGIN_LOG_E("unable to load renderer data configuration json %s : %s", file.c_str(),
82                             result.error.c_str());
83                     }
84                 }
85             } break;
86             case IDirectory::Entry::Type::DIRECTORY: {
87                 PLUGIN_LOG_I("recursive renderer configuration directories not supported");
88             } break;
89         }
90     }
91 }
92 RENDER_END_NAMESPACE()
93