(function(CanvasKit){
CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
CanvasKit._extraInitializations.push(function() {
CanvasKit.MakeGPUDeviceContext = function(device) {
if (!device) {
return null;
}
CanvasKit.preinitializedWebGPUDevice = device;
var context = this._MakeGrContext();
context._device = device;
return context;
};
CanvasKit.MakeGPUCanvasContext = function(devCtx, canvas, opts) {
var canvasCtx = canvas.getContext('webgpu');
if (!canvasCtx) {
return null;
}
let format = (opts && opts.format) ? opts.format : navigator.gpu.getPreferredCanvasFormat();
canvasCtx.configure({
device: devCtx._device,
format: format,
alphaMode: (opts && opts.alphaMode) ? opts.alphaMode : undefined,
});
var context = {
'_inner': canvasCtx,
'_deviceContext': devCtx,
'_textureFormat': format,
};
context['requestAnimationFrame'] = function(callback) {
requestAnimationFrame(function() {
const surface = CanvasKit.MakeGPUCanvasSurface(context);
if (!surface) {
console.error('Failed to initialize Surface for current canvas swapchain texture');
return;
}
callback(surface.getCanvas());
surface.flush();
surface.dispose();
});
};
return context;
};
CanvasKit.MakeGPUCanvasSurface = function(canvasCtx, colorSpace, width, height) {
let context = canvasCtx._inner;
if (!width) {
width = context.canvas.width;
}
if (!height) {
height = context.canvas.height;
}
let surface = this.MakeGPUTextureSurface(canvasCtx._deviceContext,
context.getCurrentTexture(),
canvasCtx._textureFormat,
width, height, colorSpace);
surface._canvasContext = canvasCtx;
return surface;
};
CanvasKit.MakeGPUTextureSurface = function (devCtx, texture, textureFormat, width, height, colorSpace) {
colorSpace = colorSpace || null;
return this._MakeGPUTextureSurface(
devCtx,
this.JsValStore.add(texture),
this.WebGPU.TextureFormat.indexOf(textureFormat),
width, height,
colorSpace);
};
CanvasKit.Surface.prototype.assignCurrentSwapChainTexture = function() {
if (!this._canvasContext) {
console.log('Surface is not bound to a canvas context');
return false;
}
let ctx = this._canvasContext._inner;
return this._replaceBackendTexture(
CanvasKit.JsValStore.add(ctx.getCurrentTexture()),
CanvasKit.WebGPU.TextureFormat.indexOf(this._canvasContext._textureFormat),
ctx.canvas.width, ctx.canvas.height);
};
CanvasKit.Surface.prototype.requestAnimationFrame = function(callback, dirtyRect) {
if (!this.reportBackendTypeIsGPU()) {
return this._requestAnimationFrameInternal(callback, dirtyRect);
}
return requestAnimationFrame(function() {
if (this._canvasContext && !this.assignCurrentSwapChainTexture()) {
console.log('failed to replace GPU backend texture');
return;
}
callback(this.getCanvas());
this.flush(dirtyRect);
}.bind(this));
};
CanvasKit.Surface.prototype.drawOnce = function(callback, dirtyRect) {
if (!this.reportBackendTypeIsGPU()) {
this._drawOnceInternal(callback, dirtyRect);
return;
}
requestAnimationFrame(function() {
if (this._canvasContext && !this.assignCurrentSwapChainTexture()) {
console.log('failed to replace GPU backend texture');
return;
}
callback(this.getCanvas());
this.flush(dirtyRect);
this.dispose();
}.bind(this));
};
});
}(Module));