• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_component3d_bridge.h"
16 
17 #include "base/utils/utils.h"
18 #include "bridge/declarative_frontend/jsview/js_sceneview.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
21 
22 namespace OHOS::Ace::NG {
23 namespace {
24 constexpr int NUM_0 = 0;
25 constexpr int NUM_1 = 1;
26 } // namespace
27 
SetShaderInputBuffer(ArkUIRuntimeCallInfo * runtimeCallInfo)28 ArkUINativeModuleValue Component3DBridge::SetShaderInputBuffer(ArkUIRuntimeCallInfo* runtimeCallInfo)
29 {
30     EcmaVM* vm = runtimeCallInfo->GetVM();
31     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
32     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
33     if (firstArg.IsEmpty() || !firstArg->IsNativePointer(vm)) {
34         return panda::JSValueRef::Undefined(vm);
35     }
36     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
37     CHECK_NULL_RETURN(!secondArg.IsNull(), panda::JSValueRef::Undefined(vm));
38     if (!secondArg->IsArray(vm)) {
39         return panda::JSValueRef::Undefined(vm);
40     }
41     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
42     auto array = Local<panda::ArrayRef>(secondArg);
43     auto length = array->Length(vm);
44 
45     std::vector<ArkUI_Float32> bufferArray;
46     for (uint32_t index = 0; index < length; index++) {
47         Local<JSValueRef> value = panda::ArrayRef::GetValueAt(vm, array, index);
48         double param;
49         if (ArkTSUtils::ParseJsDouble(vm, value, param)) {
50             bufferArray.emplace_back(static_cast<ArkUI_Float32>(param));
51         } else {
52             bufferArray.clear();
53             break;
54         }
55     }
56 
57     auto component3DModifier = GetArkUINodeModifiers()->getComponent3DModifier();
58     if (component3DModifier) {
59         component3DModifier->setShaderInputBuffer(nativeNode, bufferArray.data(), bufferArray.size());
60     }
61 
62     return panda::JSValueRef::Undefined(vm);
63 }
64 
ResetShaderInputBuffer(ArkUIRuntimeCallInfo * runtimeCallInfo)65 ArkUINativeModuleValue Component3DBridge::ResetShaderInputBuffer(ArkUIRuntimeCallInfo* runtimeCallInfo)
66 {
67     EcmaVM* vm = runtimeCallInfo->GetVM();
68     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
69     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
70     if (firstArg.IsEmpty() || !firstArg->IsNativePointer(vm)) {
71         return panda::JSValueRef::Undefined(vm);
72     }
73     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
74     auto component3DModifier = GetArkUINodeModifiers()->getComponent3DModifier();
75     if (component3DModifier) {
76         component3DModifier->resetShaderInputBuffer(nativeNode);
77     }
78 
79     return panda::JSValueRef::Undefined(vm);
80 }
81 } // namespace OHOS::Ace::NG
82