• 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 "postprocess_type.h"
17 
18 #include <scene/ext/intf_render_resource.h>
19 #include <scene/ext/util.h>
20 #include <scene/interface/intf_scene.h>
21 
22 #include <meta/api/metadata_util.h>
23 
24 #include "serialization/util.h"
25 
SCENE_BEGIN_NAMESPACE()26 SCENE_BEGIN_NAMESPACE()
27 
28 bool PostProcessResourceType::Build(const META_NS::IMetadata::Ptr& d)
29 {
30     bool res = Super::Build(d);
31     if (res) {
32         auto context = GetInterfaceBuildArg<IRenderContext>(d, "RenderContext");
33         if (!context) {
34             CORE_LOG_E("Invalid arguments to construct PostProcessResourceType");
35             return false;
36         }
37         context_ = context;
38     }
39     return res;
40 }
GetResourceName() const41 BASE_NS::string PostProcessResourceType::GetResourceName() const
42 {
43     return "PostProcess";
44 }
GetResourceType() const45 BASE_NS::Uid PostProcessResourceType::GetResourceType() const
46 {
47     return ClassId::PostProcessResource.Id().ToUid();
48 }
49 
LoadResource(const StorageInfo & s) const50 CORE_NS::IResource::Ptr PostProcessResourceType::LoadResource(const StorageInfo& s) const
51 {
52     auto scene = interface_pointer_cast<IScene>(s.context);
53     if (!scene) {
54         CORE_LOG_W("missing context, cannot load postprocess resource");
55         return nullptr;
56     }
57     CORE_NS::IResource::Ptr res = scene->CreateObject<CORE_NS::IResource>(ClassId::PostProcess).GetResult();
58     if (res && s.options) {
59         if (auto opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
60                 META_NS::ClassId::ObjectResourceOptions)) {
61             opts->Load(*s.options, s.self, s.context);
62             if (auto i = interface_cast<META_NS::IDerivedFromResource>(res)) {
63                 auto base = opts->GetBaseResource();
64                 if (base.IsValid()) {
65                     auto r = s.self->GetResource(base, s.context);
66                     if (!r) {
67                         CORE_LOG_W("Could not load base resource for postprocess %s", s.id.ToString().c_str());
68                     }
69                     i->SetResource(r);
70                 }
71             }
72             auto in = interface_cast<META_NS::IMetadata>(opts);
73             auto out = interface_cast<META_NS::IMetadata>(res);
74             if (in && out) {
75                 SerCopy(*in, *out);
76             }
77         }
78     }
79     return res;
80 }
SaveResource(const CORE_NS::IResource::ConstPtr & p,const StorageInfo & s) const81 bool PostProcessResourceType::SaveResource(const CORE_NS::IResource::ConstPtr& p, const StorageInfo& s) const
82 {
83     if (s.options) {
84         if (auto opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
85                 META_NS::ClassId::ObjectResourceOptions)) {
86             auto in = interface_cast<META_NS::IMetadata>(p);
87             auto out = interface_cast<META_NS::IMetadata>(opts);
88             if (auto i = interface_cast<META_NS::IDerivedFromResource>(p)) {
89                 opts->SetBaseResource(i->GetResource());
90             }
91             if (in && out) {
92                 SerCloneAllToDefaultIfSet(*in, *out);
93                 opts->Save(*s.options, s.self);
94             }
95         }
96     }
97     return true;
98 }
ReloadResource(const StorageInfo &,const CORE_NS::IResource::Ptr &) const99 bool PostProcessResourceType::ReloadResource(const StorageInfo&, const CORE_NS::IResource::Ptr&) const
100 {
101     return false;
102 }
103 
104 SCENE_END_NAMESPACE()