From 4375c18a63d8244f4cc11f6cbcae64a5394355c5 Mon Sep 17 00:00:00 2001 From: kevingpqi Date: Wed, 25 Dec 2024 20:04:43 +0800 Subject: [PATCH] Add a release method to PAGPlayer and PAGSurface for the HarmonyOS platform and fix the hardware decoding memory leak issue. --- ohos/libpag/src/main/cpp/types/libpag/Index.d.ts | 4 ++++ ohos/libpag/src/main/ets/PAGPlayer.ets | 8 ++++++++ ohos/libpag/src/main/ets/PAGSurface.ets | 8 ++++++++ src/platform/ohos/JPAGPlayer.cpp | 13 ++++++++++++- src/platform/ohos/JPAGSurface.cpp | 15 +++++++++++++++ src/platform/ohos/OHOSVideoDecoder.cpp | 14 ++++++-------- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/ohos/libpag/src/main/cpp/types/libpag/Index.d.ts b/ohos/libpag/src/main/cpp/types/libpag/Index.d.ts index d1029ebb81..c5e7545dfe 100644 --- a/ohos/libpag/src/main/cpp/types/libpag/Index.d.ts +++ b/ohos/libpag/src/main/cpp/types/libpag/Index.d.ts @@ -281,6 +281,8 @@ export declare class JPAGPlayer { hitTestPoint(pagLayer: JPAGLayer, surfaceX: number, surfaceY: number, pixelHitTest: boolean): boolean; + + release(); } export declare class JPAGSurface { @@ -296,6 +298,8 @@ export declare class JPAGSurface { freeCache(): void; + release(): void; + updateSize(): void; makeSnapshot(): image.PixelMap | null; diff --git a/ohos/libpag/src/main/ets/PAGPlayer.ets b/ohos/libpag/src/main/ets/PAGPlayer.ets index 0b899d2498..ead0f22a44 100644 --- a/ohos/libpag/src/main/ets/PAGPlayer.ets +++ b/ohos/libpag/src/main/ets/PAGPlayer.ets @@ -253,6 +253,14 @@ export class PAGPlayer { return this.nativePlayer.hitTestPoint(pagLayer.nativeLayer, surfaceX, surfaceY, pixelHitTest); } + /** + * Free up resources used by the PAGPlayer instance immediately instead of relying on the + * garbage collector to do this for you at some point in the future. + */ + release(): void { + this.nativePlayer.release(); + } + readonly nativePlayer: JPAGPlayer; private composition: PAGComposition | null = null; private surface: PAGSurface | null = null; diff --git a/ohos/libpag/src/main/ets/PAGSurface.ets b/ohos/libpag/src/main/ets/PAGSurface.ets index ffd64d1b1d..4e1777695a 100644 --- a/ohos/libpag/src/main/ets/PAGSurface.ets +++ b/ohos/libpag/src/main/ets/PAGSurface.ets @@ -77,6 +77,14 @@ export class PAGSurface { this.nativeSurface.updateSize(); } + /** + * Free up resources used by the PAGSurface instance immediately instead of relying on the + * garbage collector to do this for you at some point in the future. + */ + release(): void { + this.nativeSurface.release(); + } + /** * Returns a bitmap capturing the contents of the PAGSurface. Subsequent rendering of the * PAGSurface will not be captured. diff --git a/src/platform/ohos/JPAGPlayer.cpp b/src/platform/ohos/JPAGPlayer.cpp index 48695dab05..1650e42791 100644 --- a/src/platform/ohos/JPAGPlayer.cpp +++ b/src/platform/ohos/JPAGPlayer.cpp @@ -508,6 +508,16 @@ static napi_value HitTestPoint(napi_env env, napi_callback_info info) { return jsResult; } +static napi_value Release(napi_env env, napi_callback_info info) { + napi_value jsPlayer = nullptr; + size_t argc = 0; + napi_value args[1] = {0}; + napi_get_cb_info(env, info, &argc, args, &jsPlayer, nullptr); + void* data = nullptr; + napi_remove_wrap(env, jsPlayer, &data); + return nullptr; +} + napi_value JPAGPlayer::Constructor(napi_env env, napi_callback_info info) { napi_value jsPlayer = nullptr; size_t argc = 1; @@ -561,7 +571,8 @@ bool JPAGPlayer::Init(napi_env env, napi_value exports) { PAG_DEFAULT_METHOD_ENTRY(flush, Flush), PAG_DEFAULT_METHOD_ENTRY(getBounds, GetBounds), PAG_DEFAULT_METHOD_ENTRY(getLayersUnderPoint, GetLayersUnderPoint), - PAG_DEFAULT_METHOD_ENTRY(hitTestPoint, HitTestPoint)}; + PAG_DEFAULT_METHOD_ENTRY(hitTestPoint, HitTestPoint), + PAG_DEFAULT_METHOD_ENTRY(release, Release)}; auto status = DefineClass(env, exports, ClassName(), sizeof(classProp) / sizeof(classProp[0]), classProp, Constructor, ""); diff --git a/src/platform/ohos/JPAGSurface.cpp b/src/platform/ohos/JPAGSurface.cpp index be6ab7ff2d..ef6f5a083e 100644 --- a/src/platform/ohos/JPAGSurface.cpp +++ b/src/platform/ohos/JPAGSurface.cpp @@ -94,6 +94,20 @@ static napi_value FreeCache(napi_env env, napi_callback_info info) { return nullptr; } +static napi_value Release(napi_env env, napi_callback_info info) { + napi_value jSurface = nullptr; + size_t argc = 0; + napi_value args[1]; + napi_get_cb_info(env, info, &argc, args, &jSurface, nullptr); + auto surface = JPAGSurface::FromJs(env, jSurface); + if (surface) { + surface->freeCache(); + } + void* data = nullptr; + napi_remove_wrap(env, jSurface, &data); + return nullptr; +} + static napi_value MakeOffscreen(napi_env env, napi_callback_info info) { size_t argc = 2; napi_value args[2]; @@ -159,6 +173,7 @@ bool JPAGSurface::Init(napi_env env, napi_value exports) { PAG_DEFAULT_METHOD_ENTRY(updateSize, UpdateSize), PAG_DEFAULT_METHOD_ENTRY(clearAll, ClearAll), PAG_DEFAULT_METHOD_ENTRY(freeCache, FreeCache), + PAG_DEFAULT_METHOD_ENTRY(release, Release), PAG_DEFAULT_METHOD_ENTRY(makeSnapshot, MakeSnapshot)}; auto status = DefineClass(env, exports, ClassName(), sizeof(classProp) / sizeof(classProp[0]), diff --git a/src/platform/ohos/OHOSVideoDecoder.cpp b/src/platform/ohos/OHOSVideoDecoder.cpp index 9a95b1b075..e68c62bc37 100644 --- a/src/platform/ohos/OHOSVideoDecoder.cpp +++ b/src/platform/ohos/OHOSVideoDecoder.cpp @@ -91,14 +91,12 @@ OHOSVideoDecoder::OHOSVideoDecoder(const VideoFormat& format, bool hardware) { } OHOSVideoDecoder::~OHOSVideoDecoder() { - if (codecCategory == SOFTWARE) { - if (yuvBuffer) { - if (yuvBuffer->data[0]) { - delete[] yuvBuffer->data[0]; - } - if (yuvBuffer->data[1]) { - delete[] yuvBuffer->data[1]; - } + if (yuvBuffer) { + if (yuvBuffer->data[0]) { + delete[] yuvBuffer->data[0]; + } + if (yuvBuffer->data[1]) { + delete[] yuvBuffer->data[1]; } } if (videoCodec != nullptr) {