Skip to content

Commit

Permalink
Add a release method to PAGPlayer and PAGSurface for the OHOS platfor…
Browse files Browse the repository at this point in the history
…m and fix the hardware decoding memory leak issue. (#2637)

(cherry picked from commit 560d4a7)
  • Loading branch information
kevingpqi123 committed Dec 26, 2024
1 parent 007ba3a commit 0634ff3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ohos/libpag/src/main/cpp/types/libpag/Index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ export declare class JPAGPlayer {

hitTestPoint(pagLayer: JPAGLayer, surfaceX: number,
surfaceY: number, pixelHitTest: boolean): boolean;

release();
}

export declare class JPAGSurface {
Expand All @@ -296,6 +298,8 @@ export declare class JPAGSurface {

freeCache(): void;

release(): void;

updateSize(): void;

makeSnapshot(): image.PixelMap | null;
Expand Down
8 changes: 8 additions & 0 deletions ohos/libpag/src/main/ets/PAGPlayer.ets
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions ohos/libpag/src/main/ets/PAGSurface.ets
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion src/platform/ohos/JPAGPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "");
Expand Down
15 changes: 15 additions & 0 deletions src/platform/ohos/JPAGSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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]),
Expand Down
14 changes: 6 additions & 8 deletions src/platform/ohos/OHOSVideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0634ff3

Please sign in to comment.