Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/skia/src/renderer/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ export interface CanvasProps extends Omit<ViewProps, "onLayout"> {
onSize?: SharedValue<SkSize>;
colorSpace?: "p3" | "srgb";
ref?: React.Ref<CanvasRef>;
/**
* WebGL context attributes for web platform.
* Allows configuration of the WebGL rendering context.
* Only applicable when running on web platform.
*
* Note: Boolean values will be automatically converted to 0/1 for CanvasKit compatibility.
*/
webglContextAttributes?: {
alpha?: boolean;
depth?: boolean;
stencil?: boolean;
antialias?: boolean;
premultipliedAlpha?: boolean;
preserveDrawingBuffer?: boolean;
preferLowPowerToHighPerformance?: boolean;
failIfMajorPerformanceCaveat?: boolean;
enableExtensionsByDefault?: boolean;
explicitSwapControl?: boolean;
renderViaOffscreenBackBuffer?: boolean;
majorVersion?: number;
minorVersion?: number;
};
}

export const Canvas = ({
Expand Down
17 changes: 17 additions & 0 deletions packages/skia/src/specs/SkiaPictureViewNativeComponent.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,37 @@ export interface NativeProps extends ViewProps {
debug?: boolean;
opaque?: boolean;
nativeID: string;
webglContextAttributes?: {
alpha?: boolean;
depth?: boolean;
stencil?: boolean;
antialias?: boolean;
premultipliedAlpha?: boolean;
preserveDrawingBuffer?: boolean;
preferLowPowerToHighPerformance?: boolean;
failIfMajorPerformanceCaveat?: boolean;
enableExtensionsByDefault?: boolean;
explicitSwapControl?: boolean;
renderViaOffscreenBackBuffer?: boolean;
majorVersion?: number;
minorVersion?: number;
};
}

const SkiaPictureViewNativeComponent = ({
nativeID,
debug,
opaque,
onLayout,
webglContextAttributes,
...viewProps
}: NativeProps) => {
return createElement(SkiaPictureView, {
nativeID,
debug,
opaque,
onLayout,
webglContextAttributes,
...viewProps,
});
};
Expand Down
26 changes: 25 additions & 1 deletion packages/skia/src/views/SkiaBaseWebView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,31 @@ export abstract class SkiaBaseWebView<
this.height = canvas.clientHeight;
canvas.width = this.width * pd;
canvas.height = this.height * pd;
const surface = CanvasKit.MakeWebGLCanvasSurface(canvas);

// Convert WebGL context attributes from boolean to numeric for CanvasKit
let contextAttributes: Record<string, number> | undefined;
if (this.props.webglContextAttributes) {
contextAttributes = {};
const attrs = this.props.webglContextAttributes;

// Iterate through all attributes and convert booleans to 0/1
for (const [key, value] of Object.entries(attrs)) {
if (value !== undefined) {
// Convert boolean to number (0/1), pass through numbers as-is
if (typeof value === "boolean") {
contextAttributes[key] = value ? 1 : 0;
} else {
contextAttributes[key] = value;
}
}
}
}

const surface = CanvasKit.MakeWebGLCanvasSurface(
canvas,
undefined, // colorSpace - using undefined to maintain default
contextAttributes
);
const ctx = canvas.getContext("webgl2");
if (ctx) {
ctx.drawingBufferColorSpace = "display-p3";
Expand Down
23 changes: 23 additions & 0 deletions packages/skia/src/views/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ export interface SkiaBaseViewProps extends ViewProps {
onSize?: SharedValue<SkSize>;

opaque?: boolean;

/**
* WebGL context attributes for web platform.
* Allows configuration of the WebGL rendering context.
* Only applicable when running on web platform.
*
* Note: Boolean values will be automatically converted to 0/1 for CanvasKit compatibility.
*/
webglContextAttributes?: {
alpha?: boolean;
depth?: boolean;
stencil?: boolean;
antialias?: boolean;
premultipliedAlpha?: boolean;
preserveDrawingBuffer?: boolean;
preferLowPowerToHighPerformance?: boolean;
failIfMajorPerformanceCaveat?: boolean;
enableExtensionsByDefault?: boolean;
explicitSwapControl?: boolean;
renderViaOffscreenBackBuffer?: boolean;
majorVersion?: number;
minorVersion?: number;
};
}

export interface SkiaPictureViewNativeProps extends SkiaBaseViewProps {
Expand Down
Loading