skia2/modules/canvaskit/rt_shader.js
Kevin Lubick 6aa3869f76 [canvaskit] Use scratch arrays for colors and matrices
At startup, we allocate a few scratch arrays and then use those
instead of having to malloc and free a bunch of arrays during
runtime.

The benchmark that was added is a bit noisy (probably because
of the garbage collection going on from the created Float32Arrays),
but a few percent faster.

We also don't set the paragraph background/foreground colors to
transparent because we check them being falsey before sending them
over the wire. I noticed that if foreground was transparent black,
no text shows up at all, which was unexpected.

Change-Id: I9f3a590a122d7de222cb5f58ea40e86b2d261c96
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292685
Reviewed-by: Nathaniel Nifong <nifong@google.com>
2020-06-01 15:47:07 +00:00

30 lines
1.7 KiB
JavaScript

CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
CanvasKit._extraInitializations.push(function() {
CanvasKit.SkRuntimeEffect.prototype.makeShader = function(floats, isOpaque, localMatrix) {
// We don't need to free these floats because they will become owned by the shader.
var fptr = copy1dArray(floats, CanvasKit.HEAPF32);
var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
// Our array has 4 bytes per float, so be sure to account for that before
// sending it over the wire.
return this._makeShader(fptr, floats.length * 4, !!isOpaque, localMatrixPtr);
}
// childrenWithShaders is an array of other shaders (e.g. SkImage.makeShader())
CanvasKit.SkRuntimeEffect.prototype.makeShaderWithChildren = function(floats, isOpaque, childrenShaders, localMatrix) {
// We don't need to free these floats because they will become owned by the shader.
var fptr = copy1dArray(floats, CanvasKit.HEAPF32);
var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
var barePointers = [];
for (var i = 0; i < childrenShaders.length; i++) {
// childrenShaders are emscriptens smart pointer type. We want to get the bare pointer
// and send that over the wire, so it can be re-wrapped as an sk_sp.
barePointers.push(childrenShaders[i].$$.ptr);
}
var childrenPointers = copy1dArray(barePointers, CanvasKit.HEAPU32);
// Our array has 4 bytes per float, so be sure to account for that before
// sending it over the wire.
return this._makeShaderWithChildren(fptr, floats.length * 4, !!isOpaque, childrenPointers,
barePointers.length, localMatrixPtr);
}
});