1 /*
2 * Copyright (c) 2022 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 "core/components_ng/pattern/model/model_view_ng.h"
17
18 #include "core/components_ng/base/view_stack_processor.h"
19 #include "core/components_ng/pattern/model/model_pattern.h"
20
21 namespace OHOS::Ace::NG {
Create(const ModelViewContext & context)22 void ModelViewNG::Create(const ModelViewContext& context)
23 {
24 auto* stack = ViewStackProcessor::GetInstance();
25 auto nodeId = stack->ClaimNodeId();
26 static int staticKey = 0;
27
28 ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::MODEL_ETS_TAG, nodeId);
29 auto frameNode = FrameNode::GetOrCreateFrameNode(
30 V2::MODEL_ETS_TAG, nodeId, [&context]() {
31 return AceType::MakeRefPtr<ModelPattern>(staticKey++, context);
32 });
33
34 stack->Push(frameNode);
35 frameNode_ = AceType::WeakClaim(AceType::RawPtr(frameNode));
36 }
37
SetBackground(const std::string & value)38 void ModelViewNG::SetBackground(const std::string& value)
39 {
40 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ModelBackground, value);
41 }
42
SetHandleCameraMove(bool value)43 void ModelViewNG::SetHandleCameraMove(bool value)
44 {
45 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ModelCameraMove, value);
46 }
47
SetModelSource(const std::string & value)48 void ModelViewNG::SetModelSource(const std::string& value)
49 {
50 LOGD("MODEL_NG: Model source: %s", value.c_str());
51 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ModelSource, value);
52 }
53
AddCustomRender(const std::shared_ptr<Render3D::CustomRenderDescriptor> & customRender)54 void ModelViewNG::AddCustomRender(const std::shared_ptr<Render3D::CustomRenderDescriptor>& customRender)
55 {
56 if (!customRender) {
57 return;
58 }
59 auto frameNode = frameNode_.Upgrade();
60 if (!frameNode) {
61 LOGE("frameNode is null!");
62 return;
63 }
64
65 auto paintProperty = frameNode->GetPaintProperty<ModelPaintProperty>();
66 if (!paintProperty) {
67 LOGE("model paint property is null!");
68 return;
69 }
70
71 const auto curCustomRender = paintProperty->GetModelCustomRenderValue({ });
72 if (!curCustomRender || (curCustomRender->GetUri() != customRender->GetUri())) {
73 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ModelCustomRender, customRender);
74 }
75 }
76
SetRenderHeight(Dimension & height)77 void ModelViewNG::SetRenderHeight(Dimension& height)
78 {
79 if (LessNotEqual(height.Value(), 0.0)) {
80 height.SetValue(1.0);
81 LOGE("MODEL_NG: ModelViewNG::SetRenderHeight() heigtScale set default 1.0");
82 }
83 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, RenderHeight, height.Value());
84 }
85
SetRenderWidth(Dimension & width)86 void ModelViewNG::SetRenderWidth(Dimension& width)
87 {
88 if (LessNotEqual(width.Value(), 0.0)) {
89 width.SetValue(1.0);
90 LOGE("MODEL_NG: ModelViewNG::SetRenderHeight() heigtScale set default 1.0");
91 }
92 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, RenderWidth, width.Value());
93 }
94
SetRenderFrameRate(float rate)95 void ModelViewNG::SetRenderFrameRate(float rate)
96 {
97 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, RenderFrameRate, rate);
98 }
99
SetShader(const std::string & path)100 void ModelViewNG::SetShader(const std::string& path)
101 {
102 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ShaderPath, path);
103 }
104
AddShaderImageTexture(const std::string & path)105 void ModelViewNG::AddShaderImageTexture(const std::string& path)
106 {
107 auto frameNode = frameNode_.Upgrade();
108 if (!frameNode) {
109 LOGE("frameNode is null!");
110 return;
111 }
112
113 auto paintProperty = frameNode->GetPaintProperty<ModelPaintProperty>();
114 if (!paintProperty) {
115 LOGE("model paint property is null!");
116 return;
117 }
118
119 const auto& images = paintProperty->GetModelImageTexturePathsValue({ });
120 for (auto& image : images) {
121 if (image == path) {
122 return;
123 }
124 }
125
126 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ModelSingleImageTexturePath, path);
127 }
128
AddShaderInputBuffer(const std::shared_ptr<Render3D::ShaderInputBuffer> & buffer)129 void ModelViewNG::AddShaderInputBuffer(const std::shared_ptr<Render3D::ShaderInputBuffer>& buffer)
130 {
131 ACE_UPDATE_PAINT_PROPERTY(ModelPaintProperty, ModelShaderInputBuffer, buffer);
132 }
133
AddShaderInputBuffer(FrameNode * frameNode,const std::shared_ptr<Render3D::ShaderInputBuffer> & buffer)134 void ModelViewNG::AddShaderInputBuffer(FrameNode* frameNode, const std::shared_ptr<Render3D::ShaderInputBuffer>& buffer)
135 {
136 ACE_UPDATE_NODE_PAINT_PROPERTY(ModelPaintProperty, ModelShaderInputBuffer, buffer, frameNode);
137 }
138
GetShaderInputBuffer()139 std::optional<std::shared_ptr<Render3D::ShaderInputBuffer>> ModelViewNG::GetShaderInputBuffer()
140 {
141 auto frameNode = frameNode_.Upgrade();
142 if (!frameNode) {
143 LOGE("frameNode is null!");
144 return {};
145 }
146
147 auto paintProperty = frameNode->GetPaintProperty<ModelPaintProperty>();
148 if (!paintProperty) {
149 LOGE("ModelPaintProperty is null");
150 return {};
151 }
152
153 return paintProperty->CloneModelShaderInputBuffer();
154 }
155
156 } // namespace OHOS::Ace::NG
157