• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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     })
106 // clang-format on
107 namespace {
LoadPostProcess(const json::value & jsonData)108 IRenderDataConfigurationLoader::LoadedPostProcess LoadPostProcess(const json::value& jsonData)
109 {
110     IRenderDataConfigurationLoader::LoadedPostProcess result;
111     auto& ppConfig = result.postProcessConfiguration;
112     auto& err = result.loadResult.error;
113 
114     SafeGetJsonValue(jsonData, "name", err, result.name);
115     float version = 0.0f;
116     SafeGetJsonValue(jsonData, "version", err, version);
117     if (version < 1.0f) {
118         PLUGIN_LOG_W("version number should be 1.0 or higher for post process configuration json (version=%f name=%s)",
119             version, result.name.c_str());
120     }
121     if (err.empty()) {
122         if (const auto iter = jsonData.find("postProcessConfiguration"); iter) {
123             SafeGetJsonBitfield<PostProcessConfiguration::PostProcessEnableFlagbits>(
124                 *iter, "enableFlags", err, ppConfig.enableFlags);
125 
126             if (const auto cIter = iter->find("bloomConfiguration"); cIter) {
127                 SafeGetJsonEnum(*cIter, "bloomType", err, ppConfig.bloomConfiguration.bloomType);
128                 SafeGetJsonEnum(*cIter, "bloomQualityType", err, ppConfig.bloomConfiguration.bloomQualityType);
129                 SafeGetJsonValue(*cIter, "thresholdHard", err, ppConfig.bloomConfiguration.thresholdHard);
130                 SafeGetJsonValue(*cIter, "thresholdSoft", err, ppConfig.bloomConfiguration.thresholdSoft);
131                 SafeGetJsonValue(*cIter, "amountCoefficient", err, ppConfig.bloomConfiguration.amountCoefficient);
132                 SafeGetJsonValue(*cIter, "dirtMaskCoefficient", err, ppConfig.bloomConfiguration.dirtMaskCoefficient);
133                 SafeGetJsonValue(*cIter, "useCompute", err, ppConfig.bloomConfiguration.useCompute);
134                 // NOTE: dirt mask name should be added
135             }
136             if (const auto cIter = iter->find("vignetteConfiguration"); cIter) {
137                 SafeGetJsonValue(*cIter, "coefficient", err, ppConfig.vignetteConfiguration.coefficient);
138                 SafeGetJsonValue(*cIter, "power", err, ppConfig.vignetteConfiguration.power);
139             }
140             if (const auto cIter = iter->find("colorFringeConfiguration"); cIter) {
141                 SafeGetJsonValue(
142                     *cIter, "distanceCoefficient", err, ppConfig.colorFringeConfiguration.distanceCoefficient);
143                 SafeGetJsonValue(*cIter, "coefficient", err, ppConfig.colorFringeConfiguration.coefficient);
144             }
145             if (const auto cIter = iter->find("tonemapConfiguration"); cIter) {
146                 SafeGetJsonEnum(*cIter, "tonemapType", err, ppConfig.tonemapConfiguration.tonemapType);
147                 SafeGetJsonValue(*cIter, "exposure", err, ppConfig.tonemapConfiguration.exposure);
148             }
149             if (const auto cIter = iter->find("ditherConfiguration"); cIter) {
150                 SafeGetJsonEnum(*cIter, "ditherType", err, ppConfig.ditherConfiguration.ditherType);
151             }
152             if (const auto cIter = iter->find("blurConfiguration"); cIter) {
153                 SafeGetJsonEnum(*cIter, "blurType", err, ppConfig.blurConfiguration.blurType);
154                 SafeGetJsonEnum(*cIter, "blurQualityType", err, ppConfig.blurConfiguration.blurQualityType);
155                 SafeGetJsonValue(*cIter, "filterSize", err, ppConfig.blurConfiguration.filterSize);
156                 SafeGetJsonValue(*cIter, "maxMipLevel", err, ppConfig.blurConfiguration.maxMipLevel);
157             }
158             if (const auto cIter = iter->find("colorConversionConfiguration"); cIter) {
159                 SafeGetJsonEnum(*cIter, "conversionFunctionType", err,
160                     ppConfig.colorConversionConfiguration.conversionFunctionType);
161             }
162             if (const auto cIter = iter->find("FxaaConfiguration"); cIter) {
163                 SafeGetJsonEnum(*cIter, "sharpness", err, ppConfig.fxaaConfiguration.sharpness);
164                 SafeGetJsonEnum(*cIter, "quality", err, ppConfig.fxaaConfiguration.quality);
165             }
166         } else {
167             err += "postProcessConfiguration not found\n";
168         }
169     }
170     result.loadResult.success = err.empty();
171 
172     return result;
173 }
174 } // namespace
175 
LoadPostProcess(const string_view jsonString)176 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoader::LoadPostProcess(
177     const string_view jsonString)
178 {
179     IRenderDataConfigurationLoader::LoadedPostProcess result;
180     auto json = json::parse(jsonString.data());
181     if (json) {
182         result = RENDER_NS::LoadPostProcess(json);
183     } else {
184         result.loadResult.success = false;
185         result.loadResult.error = "Invalid json file.";
186     }
187 
188     return result;
189 }
190 
LoadPostProcess(IFileManager & fileManager,const string_view uri)191 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoader::LoadPostProcess(
192     IFileManager& fileManager, const string_view uri)
193 {
194     IRenderDataConfigurationLoader::LoadedPostProcess result;
195 
196     IFile::Ptr file = fileManager.OpenFile(uri);
197     if (!file) {
198         PLUGIN_LOG_E("Error loading '%s'", string(uri).c_str());
199         result.loadResult = IRenderDataConfigurationLoader::LoadResult("Failed to open file.");
200         return result;
201     }
202 
203     const uint64_t byteLength = file->GetLength();
204 
205     string raw;
206     raw.resize(static_cast<size_t>(byteLength));
207 
208     if (file->Read(raw.data(), byteLength) != byteLength) {
209         PLUGIN_LOG_E("Error loading '%s'", string(uri).c_str());
210         result.loadResult = IRenderDataConfigurationLoader::LoadResult("Failed to read file.");
211         return result;
212     }
213 
214     return LoadPostProcess(raw);
215 }
216 
LoadPostProcess(const string_view jsonString)217 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoaderImpl::LoadPostProcess(
218     const string_view jsonString)
219 {
220     return RenderDataConfigurationLoader::LoadPostProcess(jsonString);
221 }
222 
LoadPostProcess(IFileManager & fileManager,const string_view uri)223 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoaderImpl::LoadPostProcess(
224     IFileManager& fileManager, const string_view uri)
225 {
226     return RenderDataConfigurationLoader::LoadPostProcess(fileManager, uri);
227 }
228 
GetInterface(const BASE_NS::Uid & uid) const229 const IInterface* RenderDataConfigurationLoaderImpl::GetInterface(const BASE_NS::Uid& uid) const
230 {
231     if (uid == IRenderDataConfigurationLoader::UID) {
232         return this;
233     }
234     return nullptr;
235 }
236 
GetInterface(const BASE_NS::Uid & uid)237 IInterface* RenderDataConfigurationLoaderImpl::GetInterface(const BASE_NS::Uid& uid)
238 {
239     if (uid == IRenderDataConfigurationLoader::UID) {
240         return this;
241     }
242     return nullptr;
243 }
244 
Ref()245 void RenderDataConfigurationLoaderImpl::Ref() {}
246 
Unref()247 void RenderDataConfigurationLoaderImpl::Unref() {}
248 RENDER_END_NAMESPACE()
249