canvaskit-wasm 0.39 build 2026-04-29

GrDirectContext

Skia's GPU resource cache, scoped to one rendering backend (a WebGL handle or a WebGPU device). Returned by CK.MakeGrContext, MakeWebGLContext, and MakeGPUDeviceContext. All GPU-backed surfaces created against the same context share its cached textures, programs, and buffers — which is why you create the context once and reuse it for the lifetime of the page.

GrDirectContext is a WASM object — .delete() it once you're tearing the rendering surface down. In practice that's on page unload.

const glHandle = CK.GetWebGLContext(canvasEl);
const grCtx    = CK.MakeGrContext(glHandle);

// Many surfaces share the same context:
const surfaceA = CK.MakeOnScreenGLSurface(grCtx, 800, 600, CK.ColorSpace.SRGB);

// Cap the cache (default is generous; 64 MB shown here).
grCtx.setResourceCacheLimitBytes(64 * 1024 * 1024);

// Later, on teardown:
surfaceA.delete();
grCtx.delete();

When to call releaseResourcesAndAbandonContext

If the GL/GPU device underneath has been lost — context-loss event, GPU reset, tab moved to a different adapter — call releaseResourcesAndAbandonContext() so Skia stops trying to issue commands against the dead device. The context object remains, but every cached resource is dropped and further draws no-op. You'll typically delete and rebuild the context after this.

Members

MemberArgsReturnsNotes
deletevoidFree the WASM context. Required when tearing down. Surfaces sharing it must already be deleted.
getResourceCacheLimitBytesnumberMax bytes Skia will hold in the GPU cache.
getResourceCacheUsageBytesnumberCurrently in use. Useful for diagnostics.
setResourceCacheLimitBytesbytes: numbervoidTighten or loosen the cache cap at runtime.
releaseResourcesAndAbandonContextvoidDrop all cached resources and disconnect from the underlying GL/GPU device.

See also