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
| Member | Args | Returns | Notes |
|---|---|---|---|
delete | — | void | Free the WASM context. Required when tearing down. Surfaces sharing it must already be deleted. |
getResourceCacheLimitBytes | — | number | Max bytes Skia will hold in the GPU cache. |
getResourceCacheUsageBytes | — | number | Currently in use. Useful for diagnostics. |
setResourceCacheLimitBytes | bytes: number | void | Tighten or loosen the cache cap at runtime. |
releaseResourcesAndAbandonContext | — | void | Drop all cached resources and disconnect from the underlying GL/GPU device. |
See also
CanvasKit.MakeGrContext,MakeWebGLContext,MakeGPUDeviceContext— factories that produce one.Surface— every GPU-backed surface holds a reference to aGrDirectContext.- Bundler integration — earlier in the init flow.