xref: /aosp_15_r20/external/skia/modules/canvaskit/rt_shader.js (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
2CanvasKit._extraInitializations.push(function() {
3
4  // sksl is the shader code.
5  // errorCallback is a function that will be called with an error string if the
6  // effect cannot be made. If not provided, the error will be logged.
7  CanvasKit.RuntimeEffect.Make = function(sksl, errorCallback) {
8    // The easiest way to pass a function into C++ code is to wrap it in an object and
9    // treat it as an emscripten::val on the other side.
10    var callbackObj = {
11      'onError': errorCallback || function(err) {
12        console.log('RuntimeEffect error', err);
13      },
14    };
15    return CanvasKit.RuntimeEffect._Make(sksl, callbackObj);
16  };
17
18  // sksl is the blender code.
19  // errorCallback is a function that will be called with an error string if the
20  // effect cannot be made. If not provided, the error will be logged.
21  CanvasKit.RuntimeEffect.MakeForBlender = function(sksl, errorCallback) {
22    // The easiest way to pass a function into C++ code is to wrap it in an object and
23    // treat it as an emscripten::val on the other side.
24    var callbackObj = {
25      'onError': errorCallback || function(err) {
26        console.log('RuntimeEffect error', err);
27      },
28    };
29    return CanvasKit.RuntimeEffect._MakeForBlender(sksl, callbackObj);
30  };
31
32  CanvasKit.RuntimeEffect.prototype.makeShader = function(floats, localMatrix) {
33    // If the uniforms were set in a MallocObj, we don't want the shader to take ownership of
34    // them (and free the memory when the shader is freed).
35    var shouldOwnUniforms = !floats['_ck'];
36    var fptr = copy1dArray(floats, 'HEAPF32');
37    var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
38    // Our array has 4 bytes per float, so be sure to account for that before
39    // sending it over the wire.
40    return this._makeShader(fptr, floats.length * 4, shouldOwnUniforms, localMatrixPtr);
41  }
42
43  // childrenWithShaders is an array of other shaders (e.g. Image.makeShader())
44  CanvasKit.RuntimeEffect.prototype.makeShaderWithChildren = function(floats, childrenShaders, localMatrix) {
45    // If the uniforms were set in a MallocObj, we don't want the shader to take ownership of
46    // them (and free the memory when the shader is freed).
47    var shouldOwnUniforms = !floats['_ck'];
48    var fptr = copy1dArray(floats, 'HEAPF32');
49    var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
50    var barePointers = [];
51    for (var i = 0; i < childrenShaders.length; i++) {
52      // childrenShaders are emscriptens smart pointer type. We want to get the bare pointer
53      // and send that over the wire, so it can be re-wrapped as an sk_sp.
54      barePointers.push(childrenShaders[i].$$.ptr);
55    }
56    var childrenPointers = copy1dArray(barePointers, 'HEAPU32');
57    // Our array has 4 bytes per float, so be sure to account for that before
58    // sending it over the wire.
59    return this._makeShaderWithChildren(fptr, floats.length * 4, shouldOwnUniforms, childrenPointers,
60                                        barePointers.length, localMatrixPtr);
61  }
62
63  CanvasKit.RuntimeEffect.prototype.makeBlender = function(floats) {
64    // If the uniforms were set in a MallocObj, we don't want the shader to take ownership of
65    // them (and free the memory when the blender is freed).
66    var shouldOwnUniforms = !floats['_ck'];
67    var fptr = copy1dArray(floats, 'HEAPF32');
68    return this._makeBlender(fptr, floats.length * 4, shouldOwnUniforms);
69  }
70});
71