• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RENDER_JSON_SERIALIZE_ENUM(TonemapConfiguration::TonemapType,
31     {
32         { 0x7FFFFFFF, nullptr },
33         { TonemapConfiguration::TonemapType::TONEMAP_ACES, "aces" },
34         { TonemapConfiguration::TonemapType::TONEMAP_ACES_2020, "aces_2020" },
35         { TonemapConfiguration::TonemapType::TONEMAP_FILMIC, "filmic" },
36     })
37 RENDER_JSON_SERIALIZE_ENUM(ColorConversionConfiguration::ConversionFunctionType,
38     {
39         { 0x7FFFFFFF, nullptr },
40         { ColorConversionConfiguration::ConversionFunctionType::CONVERSION_LINEAR, "linear" },
41         { ColorConversionConfiguration::ConversionFunctionType::CONVERSION_LINEAR_TO_SRGB, "linear_to_srgb" },
42     })
43 RENDER_JSON_SERIALIZE_ENUM(DitherConfiguration::DitherType,
44     {
45         { 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 RENDER_JSON_SERIALIZE_ENUM(BlurConfiguration::BlurQualityType,
51     {
52         { 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 RENDER_JSON_SERIALIZE_ENUM(BlurConfiguration::BlurType,
58     {
59         { 0x7FFFFFFF, nullptr },
60         { BlurConfiguration::BlurType::TYPE_NORMAL, "normal" },
61         { BlurConfiguration::BlurType::TYPE_HORIZONTAL, "horizontal" },
62         { BlurConfiguration::BlurType::TYPE_VERTICAL, "vertical" },
63     })
64 RENDER_JSON_SERIALIZE_ENUM(BloomConfiguration::BloomQualityType,
65     {
66         { 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 RENDER_JSON_SERIALIZE_ENUM(BloomConfiguration::BloomType,
72     {
73         { 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 RENDER_JSON_SERIALIZE_ENUM(FxaaConfiguration::Sharpness,
80     {
81         { 0x7FFFFFFF, nullptr },
82         { FxaaConfiguration::Sharpness::SOFT, "soft" },
83         { FxaaConfiguration::Sharpness::MEDIUM, "medium" },
84         { FxaaConfiguration::Sharpness::SHARP, "sharp" },
85     })
86 RENDER_JSON_SERIALIZE_ENUM(FxaaConfiguration::Quality,
87     {
88         { 0x7FFFFFFF, nullptr },
89         { FxaaConfiguration::Quality::LOW, "low" },
90         { FxaaConfiguration::Quality::MEDIUM, "medium" },
91         { FxaaConfiguration::Quality::HIGH, "high" },
92     })
93 RENDER_JSON_SERIALIZE_ENUM(PostProcessConfiguration::PostProcessEnableFlagBits,
94     {
95         { 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, "scatter", err, ppConfig.bloomConfiguration.scatter);
136                 SafeGetJsonValue(*cIter, "scaleFactor", err, ppConfig.bloomConfiguration.scaleFactor);
137                 SafeGetJsonValue(*cIter, "useCompute", err, ppConfig.bloomConfiguration.useCompute);
138                 // NOTE: dirt mask name should be added
139             }
140             if (const auto cIter = iter->find("vignetteConfiguration"); cIter) {
141                 SafeGetJsonValue(*cIter, "coefficient", err, ppConfig.vignetteConfiguration.coefficient);
142                 SafeGetJsonValue(*cIter, "power", err, ppConfig.vignetteConfiguration.power);
143             }
144             if (const auto cIter = iter->find("colorFringeConfiguration"); cIter) {
145                 SafeGetJsonValue(
146                     *cIter, "distanceCoefficient", err, ppConfig.colorFringeConfiguration.distanceCoefficient);
147                 SafeGetJsonValue(*cIter, "coefficient", err, ppConfig.colorFringeConfiguration.coefficient);
148             }
149             if (const auto cIter = iter->find("tonemapConfiguration"); cIter) {
150                 SafeGetJsonEnum(*cIter, "tonemapType", err, ppConfig.tonemapConfiguration.tonemapType);
151                 SafeGetJsonValue(*cIter, "exposure", err, ppConfig.tonemapConfiguration.exposure);
152             }
153             if (const auto cIter = iter->find("ditherConfiguration"); cIter) {
154                 SafeGetJsonEnum(*cIter, "ditherType", err, ppConfig.ditherConfiguration.ditherType);
155             }
156             if (const auto cIter = iter->find("blurConfiguration"); cIter) {
157                 SafeGetJsonEnum(*cIter, "blurType", err, ppConfig.blurConfiguration.blurType);
158                 SafeGetJsonEnum(*cIter, "blurQualityType", err, ppConfig.blurConfiguration.blurQualityType);
159                 SafeGetJsonValue(*cIter, "filterSize", err, ppConfig.blurConfiguration.filterSize);
160                 SafeGetJsonValue(*cIter, "maxMipLevel", err, ppConfig.blurConfiguration.maxMipLevel);
161             }
162             if (const auto cIter = iter->find("colorConversionConfiguration"); cIter) {
163                 SafeGetJsonEnum(*cIter, "conversionFunctionType", err,
164                     ppConfig.colorConversionConfiguration.conversionFunctionType);
165             }
166             if (const auto cIter = iter->find("FxaaConfiguration"); cIter) {
167                 SafeGetJsonEnum(*cIter, "sharpness", err, ppConfig.fxaaConfiguration.sharpness);
168                 SafeGetJsonEnum(*cIter, "quality", err, ppConfig.fxaaConfiguration.quality);
169             }
170         } else {
171             err += "postProcessConfiguration not found\n";
172         }
173     }
174     result.loadResult.success = err.empty();
175 
176     return result;
177 }
LoadFromNullTerminated(const string_view jsonString)178 IRenderDataConfigurationLoader::LoadedPostProcess LoadFromNullTerminated(const string_view jsonString)
179 {
180     IRenderDataConfigurationLoader::LoadedPostProcess result;
181     const auto json = json::parse(jsonString.data());
182     if (json) {
183         result = RENDER_NS::LoadPostProcess(json);
184     } else {
185         result.loadResult.success = false;
186         result.loadResult.error = "Invalid json file.";
187     }
188 
189     return result;
190 }
191 } // namespace
192 
LoadPostProcess(const string_view jsonString)193 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoader::LoadPostProcess(
194     const string_view jsonString)
195 {
196     // make sure the input is zero terminated before parsing.
197     const auto asString = string(jsonString);
198     return LoadFromNullTerminated(asString);
199 }
200 
LoadPostProcess(IFileManager & fileManager,const string_view uri)201 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoader::LoadPostProcess(
202     IFileManager& fileManager, const string_view uri)
203 {
204     IRenderDataConfigurationLoader::LoadedPostProcess result;
205 
206     IFile::Ptr file = fileManager.OpenFile(uri);
207     if (!file) {
208         PLUGIN_LOG_E("Error loading '%s'", string(uri).c_str());
209         result.loadResult = IRenderDataConfigurationLoader::LoadResult("Failed to open file.");
210         return result;
211     }
212 
213     const uint64_t byteLength = file->GetLength();
214 
215     string raw;
216     raw.resize(static_cast<size_t>(byteLength));
217 
218     if (file->Read(raw.data(), byteLength) != byteLength) {
219         PLUGIN_LOG_E("Error loading '%s'", string(uri).c_str());
220         result.loadResult = IRenderDataConfigurationLoader::LoadResult("Failed to read file.");
221         return result;
222     }
223     result = LoadFromNullTerminated(raw);
224     return result;
225 }
226 
LoadPostProcess(const string_view jsonString)227 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoaderImpl::LoadPostProcess(
228     const string_view jsonString)
229 {
230     return RenderDataConfigurationLoader::LoadPostProcess(jsonString);
231 }
232 
LoadPostProcess(IFileManager & fileManager,const string_view uri)233 IRenderDataConfigurationLoader::LoadedPostProcess RenderDataConfigurationLoaderImpl::LoadPostProcess(
234     IFileManager& fileManager, const string_view uri)
235 {
236     return RenderDataConfigurationLoader::LoadPostProcess(fileManager, uri);
237 }
238 
GetInterface(const BASE_NS::Uid & uid) const239 const IInterface* RenderDataConfigurationLoaderImpl::GetInterface(const BASE_NS::Uid& uid) const
240 {
241     if ((uid == IRenderDataConfigurationLoader::UID) || (uid == IInterface::UID)) {
242         return this;
243     }
244     return nullptr;
245 }
246 
GetInterface(const BASE_NS::Uid & uid)247 IInterface* RenderDataConfigurationLoaderImpl::GetInterface(const BASE_NS::Uid& uid)
248 {
249     if ((uid == IRenderDataConfigurationLoader::UID) || (uid == IInterface::UID)) {
250         return this;
251     }
252     return nullptr;
253 }
254 
Ref()255 void RenderDataConfigurationLoaderImpl::Ref() {}
256 
Unref()257 void RenderDataConfigurationLoaderImpl::Unref() {}
258 RENDER_END_NAMESPACE()
259