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 "image_type.h"
17
18 #include <scene/ext/intf_render_resource.h>
19 #include <scene/ext/util.h>
20 #include <scene/interface/resource/image_info.h>
21 #include <scene/interface/resource/intf_render_resource_manager.h>
22
23 #include <core/image/intf_image_loader_manager.h>
24 #include <core/intf_engine.h>
25 #include <render/device/intf_gpu_resource_manager.h>
26
27 #include <meta/api/metadata_util.h>
28
SCENE_BEGIN_NAMESPACE()29 SCENE_BEGIN_NAMESPACE()
30
31 bool ImageResourceType::Build(const META_NS::IMetadata::Ptr& d)
32 {
33 bool res = Super::Build(d);
34 if (res) {
35 auto context = GetInterfaceBuildArg<IRenderContext>(d, "RenderContext");
36 if (!context) {
37 CORE_LOG_E("Invalid arguments to construct ImageResourceType");
38 return false;
39 }
40 context_ = context;
41 }
42 return res;
43 }
GetResourceName() const44 BASE_NS::string ImageResourceType::GetResourceName() const
45 {
46 return "ImageResource";
47 }
GetResourceType() const48 BASE_NS::Uid ImageResourceType::GetResourceType() const
49 {
50 return ClassId::ImageResource.Id().ToUid();
51 }
LoadResource(const StorageInfo & s) const52 CORE_NS::IResource::Ptr ImageResourceType::LoadResource(const StorageInfo& s) const
53 {
54 CORE_NS::IResource::Ptr res;
55 auto context = context_.lock();
56 if (s.payload && context) {
57 ImageLoadInfo flags = DEFAULT_IMAGE_LOAD_INFO;
58 META_NS::IObjectResourceOptions::Ptr opts;
59 if (s.options) {
60 opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
61 META_NS::ClassId::ObjectResourceOptions);
62 if (opts) {
63 opts->Load(*s.options, nullptr, nullptr);
64 if (auto p = opts->GetProperty<ImageLoadInfo>("ImageLoadInfo")) {
65 flags = p->GetValue();
66 }
67 }
68 }
69 if (auto rman = META_NS::GetObjectRegistry().Create<IRenderResourceManager>(
70 ClassId::RenderResourceManager, CreateRenderContextArg(context))) {
71 res = interface_pointer_cast<CORE_NS::IResource>(rman->LoadImage(s.path, flags).GetResult());
72 if (opts) {
73 if (auto i = interface_cast<META_NS::IAttach>(res)) {
74 i->Attach(opts);
75 }
76 }
77 }
78 }
79 return res;
80 }
SaveResource(const CORE_NS::IResource::ConstPtr & p,const StorageInfo & s) const81 bool ImageResourceType::SaveResource(const CORE_NS::IResource::ConstPtr& p, const StorageInfo& s) const
82 {
83 if (s.options) {
84 if (auto i = interface_cast<META_NS::IAttach>(p)) {
85 if (auto c = i->GetAttachmentContainer(false)) {
86 if (auto opts = c->FindAny<META_NS::IObjectResourceOptions>("", META_NS::TraversalType::NO_HIERARCHY)) {
87 opts->Save(*s.options, nullptr);
88 }
89 }
90 }
91 }
92 return true;
93 }
ReloadResource(const StorageInfo & s,const CORE_NS::IResource::Ptr & res) const94 bool ImageResourceType::ReloadResource(const StorageInfo& s, const CORE_NS::IResource::Ptr& res) const
95 {
96 if (auto context = context_.lock()) {
97 ImageLoadInfo flags = DEFAULT_IMAGE_LOAD_INFO;
98 if (auto att = interface_cast<META_NS::IAttach>(res)) {
99 if (auto cont = att->GetAttachmentContainer(false)) {
100 if (auto opts =
101 cont->FindAny<META_NS::IObjectResourceOptions>("", META_NS::TraversalType::NO_HIERARCHY)) {
102 if (auto p = opts->GetProperty<ImageLoadInfo>("ImageLoadInfo")) {
103 flags = p->GetValue();
104 }
105 }
106 }
107 }
108 if (auto rman = META_NS::GetObjectRegistry().Create<IRenderResourceManager>(
109 ClassId::RenderResourceManager, CreateRenderContextArg(context))) {
110 auto reloaded = interface_pointer_cast<IRenderResource>(rman->LoadImage(s.path, flags).GetResult());
111 if (reloaded) {
112 if (auto i = interface_cast<IRenderResource>(res)) {
113 i->SetRenderHandle(reloaded->GetRenderHandle());
114 return true;
115 }
116 }
117 }
118 }
119 return false;
120 }
121
122 SCENE_END_NAMESPACE()