pngWorker线程改为taskpool

Signed-off-by: 明月清风 <qiufeihu1@h-partners.com>
This commit is contained in:
明月清风 2024-02-02 18:32:59 +08:00
parent a486b36cee
commit 25b3722475
8 changed files with 207 additions and 219 deletions

View File

@ -1,5 +1,6 @@
## 2.1.2-rc.6
- 修复手机调节显示大小时图片消失
- pngWorker线程改为taskpool
## 2.1.2-rc.5
- moduleContext新增缓存策略缓存上限5缓存策略Lru

View File

@ -430,6 +430,7 @@ export default class EntryAbility extends UIAbility {
## 约束与限制
在下述版本验证通过:
DevEco Studio 4.14.1.3.520--SDK:API11 4.1.0.63
DevEco Studio 4.14.1.3.418--SDK:API11 4.1.0.56
DevEco Studio 4.14.1.3.322--SDK:API11 4.1.0.36
DevEco Studio 4.04.0.3.700--SDK:API10 4.0.10.15

View File

@ -12,15 +12,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import router from '@system.router';
import { Pngj } from '@ohos/libraryimageknife'
import resourceManager from '@ohos.resourceManager';
import { FileUtils,ImageKnifeGlobal } from '@ohos/libraryimageknife'
import featureability from '@ohos.ability.featureAbility'
import ArkWorker from '@ohos.worker'
import worker from '@ohos.worker';
import { BusinessError } from '@ohos.base'
import common from '@ohos.app.ability.common';
interface WorkerType {
type: string
name: string
}
@Entry
@Component
struct PngjTestCasePage {
@ -115,17 +115,16 @@ struct PngjTestCasePage {
if (!this.pngdecodeRun2) {
this.pngdecodeRun2 = true;
let pngj = new Pngj();
let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', {
let png_worker: WorkerType = {
type: 'classic',
name: 'readPngImageAsync'
})
pngj.readPngImageAsync(png_worker, this.pngSource2!, {pngCallback: (sender:ArrayBuffer, value:Record<string,Object>) => {
this.pngSource2 = sender
}
pngj.readPngImageAsync(png_worker, this.pngSource2!, (value:ESObject) => {
this.hint8 = '重新获取buffer才能测试'
this.hint2 = 'img with=' + value.width + ' img height=' + value.height
+ ' img depth=' + value.depth + ' img ctype=' + value.ctype
this.pngdecodeRun2 = false;
}})
})
} else {
this.hint8 = '已经在执行了,请稍等'
@ -161,12 +160,11 @@ struct PngjTestCasePage {
if (!this.pngdecodeRun3) {
this.pngdecodeRun3 = true;
let pngj = new Pngj();
let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', {
let png_worker: WorkerType = {
type: 'classic',
name: 'writePngWithStringAsync'
})
pngj.writePngWithStringAsync(png_worker, 'hello world', this.pngSource3, {pngCallback: (sender:ArrayBuffer, value:ArrayBuffer) => {
this.pngSource3 = sender
}
pngj.writePngWithStringAsync(png_worker, this.pngSource3, ( value:ESObject) => {
FileUtils.getInstance().createFileProcess(
this.rootFolder + '/pngj',
this.rootFolder + '/pngj/newPng.png',
@ -174,7 +172,7 @@ struct PngjTestCasePage {
let png1 = new Uint8Array(value)
this.hint3 = 'png写入后长度' + png1.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng.png' + '保存后使用2进制查看数据是否新增'
this.pngdecodeRun3 = false;
}})
},'hello world')
} else {
this.hint9 = '已经在执行了,请稍等'
}
@ -210,12 +208,11 @@ struct PngjTestCasePage {
if (!this.pngdecodeRun4) {
this.pngdecodeRun4 = true;
let pngj = new Pngj();
let png_worker = new worker.ThreadWorker('entry/ets/workers/upngWorkerTestCase.ets', {
let png_worker: WorkerType = {
type: 'classic',
name: 'writePngAsync'
})
pngj.writePngAsync(png_worker, this.pngSource4,{pngCallback: (sender:ArrayBuffer, value:ArrayBuffer) => {
this.pngSource4 = sender
}
pngj.writePngAsync(png_worker, this.pngSource4, ( value:ESObject) => {
FileUtils.getInstance().createFileProcess(
this.rootFolder + '/pngj',
this.rootFolder + '/pngj/newPng2.png',
@ -223,7 +220,7 @@ struct PngjTestCasePage {
let png2 = new Uint8Array(value)
this.hint4 = 'png2未写入长度' + png2.byteLength + '目录文件:' + this.rootFolder + '/pngj/newPng2.png' + '保存后使用2进制查看数据是否新增'
this.pngdecodeRun4 = false;
}})
})
} else {
this.hint10 = '已经在执行了,请稍等'
}

61
library/PngWork.ets Normal file
View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import taskpool from '@ohos.taskpool';
import { UPNG } from './src/main/ets/components/3rd_party/upng/UPNG'
import { WorkerType } from "./src/main/ets/components/imageknife/pngj/PngCallback"
export function handler(e: WorkerType, source: ArrayBuffer, pngCallback: (value:ESObject) => void, info?: string) {
let task: taskpool.Task | undefined = undefined
if (info == undefined) {
task = new taskpool.Task(printArgs, e)
} else {
task = new taskpool.Task(printArgs, e, source)
}
let val1: ESObject = taskpool.execute(task)
pngCallback(val1)
try {
taskpool.cancel(task)
} catch (e) {
console.error("taskpool.cancel occur error:" + e)
}
}
@Concurrent
function printArgs(e: WorkerType, pngSource: ArrayBuffer, info?: string): Object | ArrayBufferLike | void {
let a: Object | ArrayBufferLike | undefined = undefined
let png = UPNG.decode(pngSource)
switch (e.name) {
case 'readPngImageAsync':
let array: Uint8Array = png.data;
let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
png.data = arrayData;
a = png
break;
case 'writePngWithStringAsync':
let addInfo: string | undefined = info;
let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(png), png.width, png.height, 0)
a = newPng
break;
case 'writePngAsync':
let newPng3 = UPNG.encode(UPNG.toRGBA8(png), png.width, png.height, 0)
a = newPng3
break;
default:
break
}
if(a != undefined) { return a }
}

View File

@ -1,47 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import arkWorker from '@ohos.worker';
import {UPNG} from './src/main/ets/components/3rd_party/upng/UPNG'
export function handler (e) {
var data = e.data;
switch (data.type) {
case 'readPngImageAsync':
var png = UPNG.decode(data.data);
let array = png.data;
let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
png.data = arrayData;
let dataObj = { type: 'readPngImageAsync', data: png, receiver: data.data}
arkWorker.parentPort.postMessage(dataObj, [png.data, data.data]);
break;
case 'writePngWithStringAsync':
let addInfo = data.info;
let pngDecode = UPNG.decode(data.data);
let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
let dataObj2 = { type: 'writePngWithStringAsync', data: newPng, receiver: data.data}
arkWorker.parentPort.postMessage(dataObj2, [newPng, data.data]);
break;
case 'writePngAsync':
let pngDecode3 = UPNG.decode(data.data);
let newPng3 = UPNG.encode(UPNG.toRGBA8(pngDecode3), pngDecode3.width, pngDecode3.height, 0)
let dataObj3 = { type: 'writePngAsync', data: newPng3, receiver: data.data}
arkWorker.parentPort.postMessage(dataObj3, [newPng3, data.data]);
break;
default:
break
}
}

View File

@ -16,3 +16,8 @@
export interface PngCallback<R,T>{
pngCallback: (sender:R, receover:T)=>void
}
export interface WorkerType {
type: string
name: string
}

View File

@ -0,0 +1,123 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { UPNG } from '../../3rd_party/upng/UPNG';
import { PngCallback, WorkerType } from './PngCallback';
import image from '@ohos.multimedia.image';
import taskpool from '@ohos.taskpool';
import { BusinessError } from '@ohos.base'
export class Pngj {
readPngImageInfo(arraybuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, image.ImageInfo>) {
let imageSource: image.ImageSource = image.createImageSource(arraybuffer);
if (imageSource != undefined) {
imageSource.getImageInfo((err: BusinessError, value: image.ImageInfo) => {
if (err) {
return;
}
callback.pngCallback(arraybuffer, value);
});
}
}
/**
*
* @param pngBuffer ArrayBuffer containing the PNG file
* @param callback
* returns an image object with following properties:
* width: the width of the image
* height: the height of the image
* depth: number of bits per channel
* ctype: color type of the file (Truecolor, Grayscale, Palette ...)
* frames: additional info about frames (frame delays etc.)
* tabs: additional chunks of the PNG file
* data: pixel data of the image
*/
readPngImage(pngBuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, Object>) {
let png = UPNG.decode(pngBuffer);
callback.pngCallback(pngBuffer, png)
}
writePngWithString(addInfo: string, pngBuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, ArrayBufferLike>) {
let pngDecode = UPNG.decode(pngBuffer);
let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
callback.pngCallback(pngBuffer, newPng);
}
writePng(pngBuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, ArrayBufferLike>) {
let pngDecode = UPNG.decode(pngBuffer);
let newPng = UPNG.encode(UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
callback.pngCallback(pngBuffer, newPng);
}
async readPngImageAsync(e: WorkerType, source: ArrayBuffer, pngCallback: (value: ESObject) => void) {
let task = new taskpool.Task(taskPngImage, e, source)
let val1: ESObject = await taskpool.execute(task)
pngCallback(val1)
try {
taskpool.cancel(task)
} catch (e) {
console.error("taskpool.cancel occur error:" + e)
}
}
async writePngWithStringAsync(e: WorkerType, source: ArrayBuffer, pngCallback: (value: ESObject) => void, info: string) {
let task = new taskpool.Task(taskPngImage, e, source, info)
let val1: ESObject = await taskpool.execute(task)
pngCallback(val1)
try {
taskpool.cancel(task)
} catch (e) {
console.error("taskpool.cancel occur error:" + e)
}
}
async writePngAsync(e: WorkerType, source: ArrayBuffer, pngCallback: (value: ESObject) => void) {
let task = new taskpool.Task(taskPngImage, e, source)
let val1: ESObject = await taskpool.execute(task)
pngCallback(val1)
try {
taskpool.cancel(task)
} catch (e) {
console.error("taskpool.cancel occur error:" + e)
}
}
}
@Concurrent
function taskPngImage(e: WorkerType, pngSource: ArrayBuffer, info?: string): Object | ArrayBufferLike | void {
let a: Object | ArrayBufferLike | undefined = undefined
let png = UPNG.decode(pngSource)
switch (e.name) {
case 'readPngImageAsync':
let array: Uint8Array = png.data;
let arrayData = array.buffer.slice(array.byteOffset, array.byteLength + array.byteOffset)
png.data = arrayData;
a = png
break;
case 'writePngWithStringAsync':
let addInfo: string | undefined = info;
let newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(png), png.width, png.height, 0)
a = newPng
break;
case 'writePngAsync':
let newPng3 = UPNG.encode(UPNG.toRGBA8(png), png.width, png.height, 0)
a = newPng3
break;
default:
break
}
if(a != undefined) { return a }
}

View File

@ -1,153 +0,0 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {UPNG} from '../../3rd_party/upng/UPNG';
import {PngCallback} from './PngCallback';
import image from '@ohos.multimedia.image';
import resourceManager from '@ohos.resourceManager';
import ArkWorker from '@ohos.worker'
import { BusinessError } from '@ohos.base'
export class Pngj {
readPngImageInfo(arraybuffer: ArrayBuffer, callback:PngCallback<ArrayBuffer, image.ImageInfo>) {
let imageSource:image.ImageSource = image.createImageSource(arraybuffer);
if (imageSource != undefined){
imageSource.getImageInfo((err:BusinessError, value:image.ImageInfo) => {
if (err) {
return;
}
callback.pngCallback(arraybuffer, value);
});
}
}
/**
*
* @param pngBuffer ArrayBuffer containing the PNG file
* @param callback
* returns an image object with following properties:
* width: the width of the image
* height: the height of the image
* depth: number of bits per channel
* ctype: color type of the file (Truecolor, Grayscale, Palette ...)
* frames: additional info about frames (frame delays etc.)
* tabs: additional chunks of the PNG file
* data: pixel data of the image
*/
readPngImage(pngBuffer: ArrayBuffer, callback:PngCallback<ArrayBuffer, any>) {
var png = UPNG.decode(pngBuffer);
callback.pngCallback(pngBuffer, png)
}
writePngWithString(addInfo:string, pngBuffer: ArrayBuffer,callback:PngCallback<ArrayBuffer, any>) {
var pngDecode = UPNG.decode(pngBuffer);
var newPng = UPNG.encodeWithString(addInfo, UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
callback.pngCallback(pngBuffer, newPng);
}
writePng(pngBuffer: ArrayBuffer,callback:PngCallback<ArrayBuffer, any>) {
var pngDecode = UPNG.decode(pngBuffer);
var newPng = UPNG.encode(UPNG.toRGBA8(pngDecode), pngDecode.width, pngDecode.height, 0)
callback.pngCallback(pngBuffer, newPng);
}
readPngImageAsync(worker: any, pngBuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, any>) {
worker.onerror = function (data) {
}
worker.onmessageerror = function (e) {
}
worker.onexit = function () {
}
worker.onmessage = function(e) {
var data = e.data;
switch (data.type) {
case 'readPngImageAsync':
callback.pngCallback(data.receiver, data.data)
break;
default:
break
}
worker.terminate();
}
var obj = { type: 'readPngImageAsync', data: pngBuffer }
worker.postMessage(obj, [pngBuffer])
}
writePngWithStringAsync(worker: any, addInfo: string, pngBuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, any>) {
worker.onerror = function (data) {
}
worker.onmessageerror = function (e) {
}
worker.onexit = function () {
}
worker.onmessage = function(e) {
var data = e.data;
switch (data.type) {
case 'writePngWithStringAsync':
callback.pngCallback(data.receiver, data.data)
break;
default:
break
}
worker.terminate();
}
var obj = { type: 'writePngWithStringAsync', data:pngBuffer, info: addInfo}
worker.postMessage(obj, [pngBuffer])
}
writePngAsync(worker: any, pngBuffer: ArrayBuffer, callback: PngCallback<ArrayBuffer, any>) {
worker.onerror = function (data) {
}
worker.onmessageerror = function (e) {
}
worker.onexit = function () {
}
worker.onmessage = function(e) {
var data = e.data;
switch (data.type) {
case 'writePngAsync':
callback.pngCallback(data.receiver, data.data)
break;
default:
break
}
worker.terminate();
}
var obj = { type: 'writePngAsync', data:pngBuffer}
worker.postMessage(obj, [pngBuffer])
}
}