Add "close()" method to MP Web Tasks

PiperOrigin-RevId: 527726737
This commit is contained in:
Sebastian Schmidt 2023-04-27 17:14:20 -07:00 committed by Copybara-Service
parent b7e46ec528
commit 5e41d47f3a
3 changed files with 18 additions and 1 deletions

View File

@ -256,6 +256,11 @@ export abstract class TaskRunner {
this.baseOptions.setAcceleration(acceleration); this.baseOptions.setAcceleration(acceleration);
} }
/** Closes and cleans up the resources held by this task. */
close(): void {
this.graphRunner.closeGraph();
}
} }

View File

@ -36,7 +36,8 @@ export function createSpyWasmModule(): SpyWasmModule {
'_setAutoRenderToScreen', 'stringToNewUTF8', '_attachProtoListener', '_setAutoRenderToScreen', 'stringToNewUTF8', '_attachProtoListener',
'_attachProtoVectorListener', '_free', '_waitUntilIdle', '_attachProtoVectorListener', '_free', '_waitUntilIdle',
'_addStringToInputStream', '_registerModelResourcesGraphService', '_addStringToInputStream', '_registerModelResourcesGraphService',
'_configureAudio', '_malloc', '_addProtoToInputStream', '_getGraphConfig' '_configureAudio', '_malloc', '_addProtoToInputStream', '_getGraphConfig',
'_closeGraph'
]); ]);
spyWasmModule._getGraphConfig.and.callFake(() => { spyWasmModule._getGraphConfig.and.callFake(() => {
(spyWasmModule.simpleListeners![CALCULATOR_GRAPH_CONFIG_LISTENER_NAME] as (spyWasmModule.simpleListeners![CALCULATOR_GRAPH_CONFIG_LISTENER_NAME] as

View File

@ -63,6 +63,7 @@ export declare interface WasmModule {
_bindTextureToCanvas: () => boolean; _bindTextureToCanvas: () => boolean;
_changeBinaryGraph: (size: number, dataPtr: number) => void; _changeBinaryGraph: (size: number, dataPtr: number) => void;
_changeTextGraph: (size: number, dataPtr: number) => void; _changeTextGraph: (size: number, dataPtr: number) => void;
_closeGraph: () => void;
_free: (ptr: number) => void; _free: (ptr: number) => void;
_malloc: (size: number) => number; _malloc: (size: number) => number;
_processFrame: (width: number, height: number, timestamp: number) => void; _processFrame: (width: number, height: number, timestamp: number) => void;
@ -1148,6 +1149,16 @@ export class GraphRunner {
finishProcessing(): void { finishProcessing(): void {
this.wasmModule._waitUntilIdle(); this.wasmModule._waitUntilIdle();
} }
/**
* Closes the input streams and all calculators for this graph and frees up
* any C++ resources. The graph will not be usable once closed.
*/
closeGraph(): void {
this.wasmModule._closeGraph();
this.wasmModule.simpleListeners = undefined;
this.wasmModule.emptyPacketListeners = undefined;
}
} }
// Quick private helper to run the given script safely // Quick private helper to run the given script safely