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