• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "ani_matrix.h"
17 
18 namespace OHOS::Rosen {
19 namespace Drawing {
20 const char* ANI_CLASS_MATRIX_NAME = "L@ohos/graphics/drawing/drawing/Matrix;";
21 constexpr int32_t NUMBER_ZREO = 0;
22 constexpr int32_t MATRIX_SIZE = 9;
23 
AniInit(ani_env * env)24 ani_status AniMatrix::AniInit(ani_env *env)
25 {
26     ani_class cls = nullptr;
27     ani_status ret = env->FindClass(ANI_CLASS_MATRIX_NAME, &cls);
28     if (ret != ANI_OK) {
29         ROSEN_LOGE("[ANI] can't find class: %{public}s", ANI_CLASS_MATRIX_NAME);
30         return ANI_NOT_FOUND;
31     }
32 
33     std::array methods = {
34         ani_native_function { "constructorNative", ":V", reinterpret_cast<void*>(Constructor) },
35         ani_native_function { "constructorNative", "L@ohos/graphics/drawing/drawing/Matrix;:V",
36             reinterpret_cast<void*>(ConstructorWithMatrix) },
37         ani_native_function { "getValue", "D:D", reinterpret_cast<void*>(GetValue) },
38         ani_native_function { "reset", ":V", reinterpret_cast<void*>(Reset) },
39         ani_native_function { "setTranslation", "DD:V", reinterpret_cast<void*>(SetTranslation) },
40     };
41 
42     ret = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
43     if (ret != ANI_OK) {
44         ROSEN_LOGE("[ANI] bind methods fail: %{public}s", ANI_CLASS_MATRIX_NAME);
45         return ANI_NOT_FOUND;
46     }
47 
48     return ANI_OK;
49 }
50 
Constructor(ani_env * env,ani_object obj)51 void AniMatrix::Constructor(ani_env* env, ani_object obj)
52 {
53     AniMatrix* aniMatrix = new AniMatrix();
54     if (ANI_OK != env->Object_SetFieldByName_Long(obj, NATIVE_OBJ, reinterpret_cast<ani_long>(aniMatrix))) {
55         ROSEN_LOGE("AniMatrix::Constructor failed create AniMatrix");
56         delete aniMatrix;
57         return;
58     }
59 }
60 
ConstructorWithMatrix(ani_env * env,ani_object obj,ani_object aniMatrixObj)61 void AniMatrix::ConstructorWithMatrix(ani_env* env, ani_object obj, ani_object aniMatrixObj)
62 {
63     auto aniMatrix = GetNativeFromObj<AniMatrix>(env, aniMatrixObj);
64     if (aniMatrix == nullptr) {
65         AniThrowError(env, "Invalid params. "); // message length must be a multiple of 4, for example 16, 20, etc
66         return;
67     }
68 
69     AniMatrix* newAniMatrix = new AniMatrix(aniMatrix->GetMatrix());
70     if (ANI_OK != env->Object_SetFieldByName_Long(obj, NATIVE_OBJ, reinterpret_cast<ani_long>(newAniMatrix))) {
71         ROSEN_LOGE("AniMatrix::Constructor failed create AniMatrix");
72         delete newAniMatrix;
73         return;
74     }
75 }
76 
Reset(ani_env * env,ani_object obj)77 void AniMatrix::Reset(ani_env* env, ani_object obj)
78 {
79     auto aniMatrix = GetNativeFromObj<AniMatrix>(env, obj);
80     if (aniMatrix == nullptr) {
81         AniThrowError(env, "Invalid params. ");
82         return;
83     }
84 
85     aniMatrix->GetMatrix().Reset();
86 }
87 
SetTranslation(ani_env * env,ani_object obj,ani_double dx,ani_double dy)88 void AniMatrix::SetTranslation(ani_env* env, ani_object obj, ani_double dx, ani_double dy)
89 {
90     auto aniMatrix = GetNativeFromObj<AniMatrix>(env, obj);
91     if (aniMatrix == nullptr) {
92         AniThrowError(env, "Invalid params. ");
93         return;
94     }
95     aniMatrix->GetMatrix().Translate(dx, dy);
96 }
97 
GetValue(ani_env * env,ani_object obj,ani_double index)98 ani_double AniMatrix::GetValue(ani_env* env, ani_object obj, ani_double index)
99 {
100     auto aniMatrix = GetNativeFromObj<AniMatrix>(env, obj);
101     if (aniMatrix == nullptr) {
102         AniThrowError(env, "Invalid params. ");
103         return 0;
104     }
105 
106     if (CheckInt32OutOfRange(static_cast<ani_int>(index), NUMBER_ZREO, MATRIX_SIZE)) {
107         AniThrowError(env, "matrix index out of range. ");
108         return 0;
109     }
110 
111     return aniMatrix->GetMatrix().Get(index);
112 }
113 
114 
GetMatrix()115 Matrix& AniMatrix::GetMatrix()
116 {
117     return matrix_;
118 }
119 } // namespace Drawing
120 } // namespace OHOS::Rosen