Skip to content

Commit

Permalink
Allow OffscreenCanvas to be used by DrawingUtils
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 579021013
  • Loading branch information
schmidt-sebastian authored and copybara-github committed Nov 3, 2023
1 parent 1cc7900 commit 8f564c4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions mediapipe/tasks/web/vision/core/drawing_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export {RGBAColor, CategoryToColorMap};
export class DrawingUtils {
private categoryMaskShaderContext?: CategoryMaskShaderContext;
private convertToWebGLTextureShaderContext?: MPImageShaderContext;
private readonly context2d?: CanvasRenderingContext2D;
private readonly context2d?: CanvasRenderingContext2D|
OffscreenCanvasRenderingContext2D;
private readonly contextWebGL?: WebGL2RenderingContext;

/**
Expand All @@ -139,12 +140,14 @@ export class DrawingUtils {
* `setOptions({ canvas: .. })`).
*/
constructor(
cpuContext: CanvasRenderingContext2D,
cpuContext: CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D,
gpuContext?: WebGL2RenderingContext);
constructor(
cpuOrGpuGontext: CanvasRenderingContext2D|WebGL2RenderingContext,
cpuOrGpuGontext: CanvasRenderingContext2D|
OffscreenCanvasRenderingContext2D|WebGL2RenderingContext,
gpuContext?: WebGL2RenderingContext) {
if (cpuOrGpuGontext instanceof CanvasRenderingContext2D) {
if (cpuOrGpuGontext instanceof CanvasRenderingContext2D ||
cpuOrGpuGontext instanceof OffscreenCanvasRenderingContext2D) {
this.context2d = cpuOrGpuGontext;
this.contextWebGL = gpuContext;
} else {
Expand Down Expand Up @@ -186,7 +189,8 @@ export class DrawingUtils {
return DrawingUtils.clamp(out, y0, y1);
}

private getCanvasRenderingContext(): CanvasRenderingContext2D {
private getCanvasRenderingContext(): CanvasRenderingContext2D
|OffscreenCanvasRenderingContext2D {
if (!this.context2d) {
throw new Error(
'CPU rendering requested but CanvasRenderingContext2D not provided.');
Expand Down

0 comments on commit 8f564c4

Please sign in to comment.