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 "render_data_configuration_loader.h"
17 
18 #include <core/io/intf_file_manager.h>
19 #include <render/datastore/render_data_store_render_pods.h>
20 #include <render/namespace.h>
21 
22 #include "json_util.h"
23 #include "util/log.h"
24 
25 using namespace BASE_NS;
26 using namespace CORE_NS;
27 
28 RENDER_BEGIN_NAMESPACE()
29 // clang-format off
30 CORE_JSON_SERIALIZE_ENUM(TonemapConfiguration::TonemapType,
31     {
32         { static_cast<TonemapConfiguration::TonemapType>(0x7FFFFFFF), nullptr },
33         { TonemapConfiguration::TonemapType::TONEMAP_ACES, "aces" },
34         { TonemapConfiguration::TonemapType::TONEMAP_ACES_2020, "aces_2020" },
35         { TonemapConfiguration::TonemapType::TONEMAP_FILMIC, "filmic" },
36     })
37 CORE_JSON_SERIALIZE_ENUM(ColorConversionConfiguration::ConversionFunctionType,
38     {
39         { static_cast<ColorConversionConfiguration::ConversionFunctionType>(0x7FFFFFFF), nullptr },
40         { ColorConversionConfiguration::ConversionFunctionType::CONVERSION_LINEAR, "linear" },
41         { ColorConversionConfiguration::ConversionFunctionType::CONVERSION_LINEAR_TO_SRGB, "linear_to_srgb" },
42     })
43 CORE_JSON_SERIALIZE_ENUM(DitherConfiguration::DitherType,
44     {
45         { static_cast<DitherConfiguration::DitherType>(0x7FFFFFFF), nullptr },
46         { DitherConfiguration::DitherType::INTERLEAVED_NOISE, "interleaved_noise" },
47         { DitherConfiguration::DitherType::TRIANGLE_NOISE, "triangle_noise" },
48         { DitherConfiguration::DitherType::TRIANGLE_NOISE_RGB, "triangle_noise_rgb" },
49     })
50 CORE_JSON_SERIALIZE_ENUM(BlurConfiguration::BlurQualityType,
51     {
52         { static_cast<BlurConfiguration::BlurQualityType>(0x7FFFFFFF), nullptr },
53         { BlurConfiguration::BlurQualityType::QUALITY_TYPE_LOW, "low" },
54         { BlurConfiguration::BlurQualityType::QUALITY_TYPE_NORMAL, "normal" },
55         { BlurConfiguration::BlurQualityType::QUALITY_TYPE_HIGH, "high" },
56     })
57 CORE_JSON_SERIALIZE_ENUM(BlurConfiguration::BlurType,
58     {
59         { static_cast<BlurConfiguration::BlurType>(0x7FFFFFFF), nullptr },
60         { BlurConfiguration::BlurType::TYPE_NORMAL, "normal" },
61         { BlurConfiguration::BlurType::TYPE_HORIZONTAL, "horizontal" },
62         { BlurConfiguration::BlurType::TYPE_VERTICAL, "vertical" },
63     })
64 CORE_JSON_SERIALIZE_ENUM(BloomConfiguration::BloomQualityType,
65     {
66         { static_cast<BloomConfiguration::BloomQualityType>(0x7FFFFFFF), nullptr },
67         { BloomConfiguration::BloomQualityType::QUALITY_TYPE_LOW, "low" },
68         { BloomConfiguration::BloomQualityType::QUALITY_TYPE_NORMAL, "normal" },
69         { BloomConfiguration::BloomQualityType::QUALITY_TYPE_HIGH, "high" },
70     })
71 CORE_JSON_SERIALIZE_ENUM(BloomConfiguration::BloomType,
72     {
73         { static_cast<BloomConfiguration::BloomType>(0x7FFFFFFF), nullptr },
74         { BloomConfiguration::BloomType::TYPE_NORMAL, "normal" },
75         { BloomConfiguration::BloomType::TYPE_HORIZONTAL, "horizontal" },
76         { BloomConfiguration::BloomType::TYPE_VERTICAL, "vertical" },
77         { BloomConfiguration::BloomType::TYPE_BILATERAL, "bilateral" },
78     })
79 CORE_JSON_SERIALIZE_ENUM(FxaaConfiguration::Sharpness,
80     {
81         { static_cast<FxaaConfiguration::Sharpness>(0x7FFFFFFF), nullptr },
82         { FxaaConfiguration::Sharpness::SOFT, "soft" },
83         { FxaaConfiguration::Sharpness::MEDIUM, "medium" },
84         { FxaaConfiguration::Sharpness::SHARP, "sharp" },
85     })
86 CORE_JSON_SERIALIZE_ENUM(FxaaConfiguration::Quality,
87     {
88         { static_cast<FxaaConfiguration::Quality>(0x7FFFFFFF), nullptr },
89         { FxaaConfiguration::Quality::LOW, "low" },
90         { FxaaConfiguration::Quality::MEDIUM, "medium" },
91         { FxaaConfiguration::Quality::HIGH, "high" },
92     })
93 CORE_JSON_SERIALIZE_ENUM(PostProcessConfiguration::PostProcessEnableFlagBits,
94     {
95         { static_cast<PostProcessConfiguration::PostProcessEnableFlagBits>(0x7FFFFFFF), nullptr },
96         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_TONEMAP_BIT, "tonemap" },
97         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_VIGNETTE_BIT, "vignette" },
98         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_DITHER_BIT, "dither" },
99         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_COLOR_CONVERSION_BIT, "color_conversion" },
100         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_COLOR_FRINGE_BIT, "color_fringe" },
101         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_BLUR_BIT, "blur" },
102         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_BLOOM_BIT, "bloom" },
103         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_FXAA_BIT, "fxaa" },
104         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_TAA_BIT, "taa" },
105         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_DOF_BIT, "dof" },
106         { PostProcessConfiguration::PostProcessEnableFlagBits::ENABLE_MOTION_BLUR_BIT, "motion_blur" },
107     })
108 // clang-format on
109 namespace {
LoadPostProcess(const json::value & jsonData)110 IRenderDataConfigurationLoader::LoadedPostProcess LoadPostProcess(const json::value& jsonData)
111 {
112     IRenderDataConfigurationLoader::LoadedPostProcess result;
113     auto& ppConfig = result.postProcessConfiguration;
114     auto& err = result.loadResult.error;
115 
116     SafeGetJsonValue(jsonData, "name", err, result.name);
117     float version = 0.0f;
118     SafeGetJsonValue(jsonData, "version", err, version);
119     if (version < 1.0f) {
120         PLUGIN_LOG_W("version number should be 1.0 or higher for post process configuration json (version=%f name=%s)",
121             version, result.name.c_str());
122     }
123     if (err.empty()) {
124         if (const auto iter = jsonData.find("postProcessConfiguration"); iter) {
125             SafeGetJsonBitfield<PostProcessConfiguration::PostProcessEnableFlagBits>(
126                 *iter, "enableFlags", err, ppConfig.enableFlags);
127 
128             if (const auto cIter = iter->find("bloomConfiguration"); cIter) {
129                 SafeGetJsonEnum(*cIter, "bloomType", err, ppConfig.bloomConfiguration.bloomType);
130                 SafeGetJsonEnum(*cIter, "bloomQualityType", err, ppConfig.bloomConfiguration.bloomQualityType);
131                 SafeGetJsonValue(*cIter, "thresholdHard", err, ppConfig.bloomConfiguration.thresholdHard);
132                 SafeGetJsonValue(*cIter, "thresholdSoft", err, ppConfig.bloomConfiguration.thresholdSoft);
133                 SafeGetJsonValue(*cIter, "amountCoefficient", err, ppConfig.bloomConfiguration.amountCoefficient);
134                 SafeGetJsonValue(*cIter, "dirtMaskCoefficient", err, ppConfig.bloomConfiguration.dirtMaskCoefficient);
135                 SafeGetJsonValue(*cIter, "useCompute", err, ppConfig.bloomConfiguration.useCompute);
136                 // NOTE: dirt mask name should be added
137             }
138             if (const auto cIter = iter->find("vignetteConfiguration"); cIter) {
139                 SafeGetJsonValue(*cIter, "coefficient", err, ppConfig.vignetteConfiguration.coefficient);
140                 SafeGetJsonValue(*cIter, "power", err, ppConfig.vignetteConfiguration.power);
141             }
142             if (const auto cIter = iter->find("colorFringeConfiguration"); cIter) {
143                 SafeGetJsonValue(
144                     *cIter, "distanceCoefficient", err, ppConfig.colorFringeConfiguration.distanceCoefficient);
145                 SafeGetJsonValue(*cIter, "coefficient", err, ppConfig.colorFringeConfiguration.coefficient);
146             }
147             if (const auto cIter = iter->find("tonemapConfiguration"); cIter) {
148                 SafeGetJsonEnum(*cIter, "tonemapType", err, ppConfig.tonemapConfiguration.tonemapType);
149                 SafeGetJsonValue(*cIter, "exposure", err, ppConfig.tonemapConfiguration.exposure);
150             }
151             if (const auto cIter = iter->find("ditherConfiguration"); cIter) {
152                 SafeGetJsonEnum(*cIter, "ditherType", err, ppConfig.ditherConfiguration.ditherType);
153             }
154             if (const auto cIter = iter->find("blurConfiguration"); cIter) {
155                 SafeGetJsonEnum(*cIter, "blurType", err, ppConfig.blurConfiguration.blurType);
156                 SafeGetJsonEnum(*cIter, "blurQualityType", err, ppConfig.blurConfiguration.blurQualityType);
157                 SafeGetJsonValue(*cIter, "filterSize", err, ppConfig.blurConfiguration.filterSize);
158                 SafeGetJsonValue(*cIter, "maxMipLevel", err, ppConfig.blurConfiguration.maxMipLevel);
159             }
160             if (const auto cIter = iter->find("colorConversionConfiguration"); cIter) {
161                 SafeGetJsonEnum(*cIter, "conversionFunctionType", err,
162                     ppConfig.colorConversionConfiguration.conversionFunctionType);
163             }
164             if (const auto cIter = iter->find("FxaaConfiguration"); cIter) {
165                 SafeGetJsonEnum(*cIter, "sharpness", err, ppConfig.fxaaConfiguration.sharpness);
166                 SafeGetJsonEnum(*cIter, "quality", err, ppConfig.fxaaConfiguration.quality);
167             }
168         } else {
169             err += "postProcessConfiguration not found\n";
170         }
171     }
172     result.loadResult.success = err.empty();
173 
174     return result;
175 }
176 } // namespace
177 
LoadPostProcess(const string_view jsonString)178 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoader::LoadPostProcess(
179     const string_view jsonString)
180 {
181     IRenderDataConfigurationLoader::LoadedPostProcess result;
182     auto json = json::parse(jsonString.data());
183     if (json) {
184         result = RENDER_NS::LoadPostProcess(json);
185     } else {
186         result.loadResult.success = false;
187         result.loadResult.error = "Invalid json file.";
188     }
189 
190     return result;
191 }
192 
LoadPostProcess(IFileManager & fileManager,const string_view uri)193 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoader::LoadPostProcess(
194     IFileManager& fileManager, const string_view uri)
195 {
196     IRenderDataConfigurationLoader::LoadedPostProcess result;
197 
198     IFile::Ptr file = fileManager.OpenFile(uri);
199     if (!file) {
200         PLUGIN_LOG_E("Error loading '%s'", string(uri).c_str());
201         result.loadResult = IRenderDataConfigurationLoader::LoadResult("Failed to open file.");
202         return result;
203     }
204 
205     const uint64_t byteLength = file->GetLength();
206 
207     string raw;
208     raw.resize(static_cast<size_t>(byteLength));
209 
210     if (file->Read(raw.data(), byteLength) != byteLength) {
211         PLUGIN_LOG_E("Error loading '%s'", string(uri).c_str());
212         result.loadResult = IRenderDataConfigurationLoader::LoadResult("Failed to read file.");
213         return result;
214     }
215 
216     return LoadPostProcess(raw);
217 }
218 
LoadPostProcess(const string_view jsonString)219 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoaderImpl::LoadPostProcess(
220     const string_view jsonString)
221 {
222     return RenderDataConfigurationLoader::LoadPostProcess(jsonString);
223 }
224 
LoadPostProcess(IFileManager & fileManager,const string_view uri)225 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoaderImpl::LoadPostProcess(
226     IFileManager& fileManager, const string_view uri)
227 {
228     return RenderDataConfigurationLoader::LoadPostProcess(fileManager, uri);
229 }
230 
GetInterface(const BASE_NS::Uid & uid) const231 const IInterface* RenderDataConfigurationLoaderImpl::GetInterface(const BASE_NS::Uid& uid) const
232 {
233     if ((uid == IRenderDataConfigurationLoader::UID) || (uid == IInterface::UID)) {
234         return this;
235     }
236     return nullptr;
237 }
238 
GetInterface(const BASE_NS::Uid & uid)239 IInterface* RenderDataConfigurationLoaderImpl::GetInterface(const BASE_NS::Uid& uid)
240 {
241     if ((uid == IRenderDataConfigurationLoader::UID) || (uid == IInterface::UID)) {
242         return this;
243     }
244     return nullptr;
245 }
246 
Ref()247 void RenderDataConfigurationLoaderImpl::Ref() {}
248 
Unref()249 void RenderDataConfigurationLoaderImpl::Unref() {}
250 RENDER_END_NAMESPACE()
251