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.h>
17 #include <array>
18 #include <iostream>
19 #include "base/memory/referenced.h"
20 #include "frameworks/core/common/container.h"
21 #include "frameworks/core/pipeline/pipeline_base.h"
22
setImageCacheCount(ani_env * env,ani_object object,ani_double value)23 static void setImageCacheCount([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object, ani_double value)
24 {
25 size_t count = static_cast<size_t>(value);
26 auto container = OHOS::Ace::Container::Current();
27 if (!container) {
28 return;
29 }
30 auto pipelineContext = container->GetPipelineContext();
31 if (!pipelineContext) {
32 return;
33 }
34 auto taskExecutor = pipelineContext->GetTaskExecutor();
35 if (!taskExecutor) {
36 return;
37 }
38 OHOS::Ace::WeakPtr<OHOS::Ace::PipelineBase> pipelineContextWeak(pipelineContext);
39 taskExecutor->PostTask(
40 [pipelineContextWeak, count]() mutable {
41 auto pipelineContext = pipelineContextWeak.Upgrade();
42 if (pipelineContext) {
43 auto imageCache = pipelineContext->GetImageCache();
44 imageCache->SetCapacity(count);
45 }
46 },
47 OHOS::Ace::TaskExecutor::TaskType::UI, "ArkUISetImageCacheCount");
48 }
49
setImageRawDataCacheSize(ani_env * env,ani_object object,ani_double value)50 static void setImageRawDataCacheSize([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object,
51 ani_double value)
52 {
53 size_t cacheSize = static_cast<size_t>(value);
54 auto container = OHOS::Ace::Container::Current();
55 if (!container) {
56 return;
57 }
58 auto pipelineContext = container->GetPipelineContext();
59 if (!pipelineContext) {
60 return;
61 }
62 auto taskExecutor = pipelineContext->GetTaskExecutor();
63 if (!taskExecutor) {
64 return;
65 }
66 OHOS::Ace::WeakPtr<OHOS::Ace::PipelineBase> pipelineContextWeak(pipelineContext);
67 taskExecutor->PostTask(
68 [pipelineContextWeak, cacheSize]() mutable {
69 auto pipelineContext = pipelineContextWeak.Upgrade();
70 if (pipelineContext) {
71 auto imageCache = pipelineContext->GetImageCache();
72 imageCache->SetDataCacheLimit(cacheSize);
73 }
74 },
75 OHOS::Ace::TaskExecutor::TaskType::UI, "ArkUISetImageDataCacheSize");
76 }
77
ANI_Constructor(ani_vm * vm,uint32_t * result)78 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
79 {
80 ani_env *env;
81 if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
82 std::cerr << "Unsupported ANI_VERSION_1" << std::endl;
83 return ANI_ERROR;
84 }
85
86 static const char *className = "L@system/app/App;";
87 ani_class cls;
88 if (ANI_OK != env->FindClass(className, &cls)) {
89 std::cerr << "Not found '" << className << "'" << std::endl;
90 return ANI_ERROR;
91 }
92
93 std::array methods = {
94 ani_native_function {"setImageCacheCount", nullptr, reinterpret_cast<void *>(setImageCacheCount)},
95 ani_native_function {"setImageRawDataCacheSize", nullptr, reinterpret_cast<void *>(setImageRawDataCacheSize)},
96 };
97
98 if (ANI_OK != env->Class_BindNativeMethods(cls, methods.data(), methods.size())) {
99 std::cerr << "Cannot bind native methods to '" << className << "'" << std::endl;
100 return ANI_ERROR;
101 };
102
103 *result = ANI_VERSION_1;
104 return ANI_OK;
105 }
106