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_pattern.h"
17
18 #include "core/components_ng/event/event_hub.h"
19 #include "core/components_ng/render/adapter/rosen_render_context.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21
22 namespace OHOS::Ace::NG {
OnRebuildFrame()23 void ModelPattern::OnRebuildFrame()
24 {
25 auto host = GetHost();
26 CHECK_NULL_VOID(host);
27 auto context = host->GetRenderContext();
28 modelAdapter_->OnRebuildFrame(context);
29 }
30
ModelPattern(uint32_t key,Render3D::SurfaceType surfaceType,const std::string & bundleName,const std::string & moduleName)31 ModelPattern::ModelPattern(uint32_t key, Render3D::SurfaceType surfaceType, const std::string& bundleName,
32 const std::string& moduleName) : key_(key)
33 {
34 modelAdapter_ = MakeRefPtr<ModelAdapterWrapper>(key_, surfaceType, bundleName, moduleName);
35 modelAdapter_->SetPaintFinishCallback([weak = WeakClaim(this)]() {
36 auto model = weak.Upgrade();
37 if (model) {
38 if (model->NeedsRepaint()) {
39 model->MarkDirtyNode(PROPERTY_UPDATE_RENDER);
40 }
41 model->GetPaintProperty<ModelPaintProperty>()->ResetFlagProperties();
42 }
43 });
44 }
45
OnModifyDone()46 void ModelPattern::OnModifyDone()
47 {
48 Pattern::OnModifyDone();
49 auto host = GetHost();
50 CHECK_NULL_VOID(host);
51 auto hub = host->GetEventHub<EventHub>();
52 CHECK_NULL_VOID(hub);
53 auto gestureHub = hub->GetOrCreateGestureEventHub();
54 CHECK_NULL_VOID(gestureHub);
55
56 if (touchListener_) {
57 return;
58 }
59 auto touchTask = [weak = WeakClaim(this)](const TouchEventInfo& info) {
60 auto pattern = weak.Upgrade();
61 if (pattern) {
62 pattern->HandleTouchEvent(info);
63 }
64 };
65
66 if (touchListener_) {
67 gestureHub->RemoveTouchEvent(touchListener_);
68 }
69 touchListener_ = MakeRefPtr<TouchEventImpl>(std::move(touchTask));
70 gestureHub->AddTouchEvent(touchListener_);
71 }
72
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)73 bool ModelPattern::OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config)
74 {
75 CHECK_NULL_RETURN(modelAdapter_, false);
76 auto host = GetHost();
77 CHECK_NULL_RETURN(dirty, false);
78 CHECK_NULL_RETURN(host, false);
79 auto geometryNode = dirty->GetGeometryNode();
80 CHECK_NULL_RETURN(geometryNode, false);
81
82 auto mainProperty = DynamicCast<ModelPaintProperty>(host->GetPaintProperty<ModelPaintProperty>());
83 auto widthScale = mainProperty->GetRenderWidth().value_or(1.0);
84 auto heightScale = mainProperty->GetRenderHeight().value_or(1.0);
85
86 auto contentSize = geometryNode->GetContentSize();
87 auto contentOffset = geometryNode->GetContentOffset();
88
89 bool measure = (config.skipMeasure || dirty->SkipMeasureContent()) ? false : true;
90 float width = contentSize.Width();
91 float height = contentSize.Height();
92 float scale = PipelineContext::GetCurrentContext()->GetViewScale();
93 Render3D::WindowChangeInfo windowChangeInfo {
94 contentOffset.GetX(), contentOffset.GetY(),
95 width, height,
96 scale, widthScale, heightScale,
97 config.contentSizeChange, modelAdapter_->GetSurfaceType()
98 };
99 modelAdapter_->OnDirtyLayoutWrapperSwap(windowChangeInfo);
100 host->MarkNeedSyncRenderTree();
101
102 return measure;
103 }
104
OnAttachToFrameNode()105 void ModelPattern::OnAttachToFrameNode()
106 {
107 auto host = GetHost();
108 CHECK_NULL_VOID(host);
109 CHECK_NULL_VOID(modelAdapter_);
110 modelAdapter_->OnAttachToFrameNode(host->GetRenderContext());
111 }
112
OnDetachFromFrameNode(FrameNode * node)113 void ModelPattern::OnDetachFromFrameNode(FrameNode* node) {}
114
HandleTouchEvent(const TouchEventInfo & info)115 void ModelPattern::HandleTouchEvent(const TouchEventInfo& info)
116 {
117 CHECK_NULL_VOID(modelAdapter_);
118 auto mainProperty = DynamicCast<ModelPaintProperty>(GetHost()->GetPaintProperty<ModelPaintProperty>());
119 bool repaint = modelAdapter_->HandleTouchEvent(info, mainProperty);
120 if (repaint) {
121 MarkDirtyNode(PROPERTY_UPDATE_RENDER);
122 }
123 }
124
NeedsRepaint()125 bool ModelPattern::NeedsRepaint()
126 {
127 CHECK_NULL_RETURN(modelAdapter_, false);
128 return modelAdapter_->NeedsRepaint();
129 }
130
MarkDirtyNode(const PropertyChangeFlag flag)131 void ModelPattern::MarkDirtyNode(const PropertyChangeFlag flag)
132 {
133 auto host = GetHost();
134 CHECK_NULL_VOID(host);
135 host->MarkDirtyNode(flag);
136 }
137
~ModelPattern()138 ModelPattern::~ModelPattern()
139 {
140 CHECK_NULL_VOID(modelAdapter_);
141 modelAdapter_->Deinit();
142 }
143 } // namespace OHOS::Ace::NG
144