1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/jsview/js_canvas.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "core/common/container.h"
20 #include "core/components/custom_paint/custom_paint_component.h"
21 #include "core/components_ng/pattern/custom_paint/custom_paint_view.h"
22 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
23
24 namespace OHOS::Ace::Framework {
25
Create(const JSCallbackInfo & info)26 void JSCanvas::Create(const JSCallbackInfo& info)
27 {
28 if (Container::IsCurrentUseNewPipeline()) {
29 auto pattern = NG::CustomPaintView::Create();
30 if (info[0]->IsObject()) {
31 JSCanvasRenderer* jsContext = JSRef<JSObject>::Cast(info[0])->Unwrap<JSCanvasRenderer>();
32 if (jsContext) {
33 jsContext->SetCustomPaintPattern(pattern);
34 LOGI("SetCustomPaintPattern successfully");
35 jsContext->SetAntiAlias();
36 }
37 }
38 return;
39 }
40
41 RefPtr<OHOS::Ace::CustomPaintComponent> paintChild = AceType::MakeRefPtr<OHOS::Ace::CustomPaintComponent>();
42 if (info[0]->IsObject()) {
43 JSCanvasRenderer* jsContext = JSRef<JSObject>::Cast(info[0])->Unwrap<JSCanvasRenderer>();
44 if (jsContext) {
45 jsContext->SetComponent(paintChild->GetTaskPool());
46 LOGI("SetComponent successfully");
47 jsContext->SetAntiAlias();
48 }
49 }
50
51 ViewStackProcessor::GetInstance()->ClaimElementId(paintChild);
52 ViewStackProcessor::GetInstance()->Push(paintChild);
53 }
54
JSBind(BindingTarget globalObj)55 void JSCanvas::JSBind(BindingTarget globalObj)
56 {
57 JSClass<JSCanvas>::Declare("Canvas");
58 MethodOptions opt = MethodOptions::NONE;
59 JSClass<JSCanvas>::StaticMethod("create", &JSCanvas::Create, opt);
60 JSClass<JSCanvas>::StaticMethod("onReady", &JSCanvas::OnReady);
61 JSClass<JSCanvas>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
62 JSClass<JSCanvas>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
63 JSClass<JSCanvas>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
64 JSClass<JSCanvas>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
65 JSClass<JSCanvas>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
66 JSClass<JSCanvas>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
67 JSClass<JSCanvas>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
68 JSClass<JSCanvas>::Inherit<JSViewAbstract>();
69 JSClass<JSCanvas>::Bind<>(globalObj);
70 }
71
OnReady(const JSCallbackInfo & info)72 void JSCanvas::OnReady(const JSCallbackInfo& info)
73 {
74 if (!info[0]->IsFunction()) {
75 return;
76 }
77
78 if (Container::IsCurrentUseNewPipeline()) {
79 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
80 auto readyEvent_ = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)]() {
81 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
82 ACE_SCORING_EVENT("Canvas.onReady");
83 func->Execute();
84 };
85 NG::CustomPaintView::SetOnReady(std::move(readyEvent_));
86 return;
87 }
88
89 auto container = Container::Current();
90 if (!container) {
91 LOGE("No container");
92 return;
93 }
94 auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
95 if (!context) {
96 LOGE("No PipelineContext");
97 return;
98 }
99 auto component = AceType::DynamicCast<CustomPaintComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
100 if (!component) {
101 LOGE("No CustomPaintComponent");
102 return;
103 }
104
105 auto elmtId = component->GetElementId();
106 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
107 auto readyEvent_ =
108 Container::IsCurrentUsePartialUpdate()
109 ? EventMarker([execCtx = info.GetExecutionContext(), func = std::move(jsFunc),
110 accountableCanvasElement = elmtId]() {
111 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
112 ACE_SCORING_EVENT("Canvas.onReady");
113 LOGD("Canvas elmtId %{public}d executing JS onReady function - start", accountableCanvasElement);
114 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(accountableCanvasElement);
115 func->Execute();
116 ViewStackProcessor::GetInstance()->StopGetAccessRecording();
117 LOGD("Canvas elmtId %{public}d executing JS onReady function - end", accountableCanvasElement);
118 })
119 : EventMarker([execCtx = info.GetExecutionContext(), func = std::move(jsFunc)]() {
120 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
121 ACE_SCORING_EVENT("Canvas.onReady");
122 func->Execute();
123 });
124 component->SetOnReadyEvent(readyEvent_, context);
125 }
126 } // namespace OHOS::Ace::Framework
127