• 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// [Start copy_arraybuffer_transfer]
17import { taskpool } from '@kit.ArkTS';
18import { BusinessError } from '@kit.BasicServicesKit';
19
20@Concurrent
21function adjustImageValue(arrayBuffer: ArrayBuffer): ArrayBuffer {
22  // 对arrayBuffer进行操作
23  return arrayBuffer; // 返回值默认转移
24}
25
26function createImageTask(arrayBuffer: ArrayBuffer, isParamsByTransfer: boolean): taskpool.Task {
27  let task: taskpool.Task = new taskpool.Task(adjustImageValue, arrayBuffer);
28  if (!isParamsByTransfer) { // 是否使用转移方式
29    // 传递空数组[],全部arrayBuffer参数传递均采用拷贝方式
30    task.setTransferList([]);
31  }
32  return task;
33}
34
35@Entry
36@Component
37struct Index {
38  @State message: string = 'Hello World';
39
40  build() {
41    RelativeContainer() {
42      Text(this.message)
43        .id('HelloWorld')
44        .fontSize(50)
45        .fontWeight(FontWeight.Bold)
46        .alignRules({
47          center: { anchor: '__container__', align: VerticalAlign.Center },
48          middle: { anchor: '__container__', align: HorizontalAlign.Center }
49        })
50        .onClick(() => {
51          let taskNum = 4;
52          let arrayBuffer = new ArrayBuffer(1024 * 1024);
53          let taskPoolGroup = new taskpool.TaskGroup();
54          // 创建taskNum个Task
55          for (let i: number = 0; i < taskNum; i++) {
56            let arrayBufferSlice: ArrayBuffer =
57              arrayBuffer.slice(arrayBuffer.byteLength / taskNum * i, arrayBuffer.byteLength / taskNum * (i + 1));
58            // 使用拷贝方式传入ArrayBuffer,所以isParamsByTransfer为false
59            taskPoolGroup.addTask(createImageTask(arrayBufferSlice, false));
60          }
61          // 执行Task
62          taskpool.execute(taskPoolGroup).then((data) => {
63            // 返回结果,对数组拼接,获得最终结果
64          }).catch((e: BusinessError) => {
65            console.error(e.message);
66          })
67          // [StartExclude copy_arraybuffer_transfer]
68          this.message = 'success';
69          // [EndExclude copy_arraybuffer_transfer]
70        })
71    }
72    .height('100%')
73    .width('100%')
74  }
75}
76// [End copy_arraybuffer_transfer]
77