blob: f67109103b5d12f95347323a0895ef925cb45102 [file] [log] [blame]
Kevin Lubickf3d6c362020-01-06 08:11:52 -05001CanvasKit._extraInitializations = CanvasKit._extraInitializations || [];
2CanvasKit._extraInitializations.push(function() {
Kevin Lubick70b67292021-02-03 11:26:12 -05003
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
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040018 CanvasKit.RuntimeEffect.prototype.makeShader = function(floats, isOpaque, localMatrix) {
Kevin Lubick6bffe392020-04-02 15:24:15 -040019 // We don't need to free these floats because they will become owned by the shader.
Kevin Lubick69e46da2020-06-05 07:13:48 -040020 var fptr = copy1dArray(floats, "HEAPF32");
Kevin Lubick6bffe392020-04-02 15:24:15 -040021 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Kevin Lubickf3d6c362020-01-06 08:11:52 -050022 // Our array has 4 bytes per float, so be sure to account for that before
23 // sending it over the wire.
Kevin Lubick6aa38692020-06-01 11:25:47 -040024 return this._makeShader(fptr, floats.length * 4, !!isOpaque, localMatrixPtr);
Kevin Lubickf3d6c362020-01-06 08:11:52 -050025 }
Kevin Lubickecd87622020-02-22 07:37:33 -050026
Kevin Lubick54c1b3d2020-10-07 16:09:22 -040027 // childrenWithShaders is an array of other shaders (e.g. Image.makeShader())
28 CanvasKit.RuntimeEffect.prototype.makeShaderWithChildren = function(floats, isOpaque, childrenShaders, localMatrix) {
Kevin Lubick6bffe392020-04-02 15:24:15 -040029 // We don't need to free these floats because they will become owned by the shader.
Kevin Lubick69e46da2020-06-05 07:13:48 -040030 var fptr = copy1dArray(floats, "HEAPF32");
Kevin Lubick6bffe392020-04-02 15:24:15 -040031 var localMatrixPtr = copy3x3MatrixToWasm(localMatrix);
Kevin Lubickecd87622020-02-22 07:37:33 -050032 var barePointers = [];
Kevin Lubick6bffe392020-04-02 15:24:15 -040033 for (var i = 0; i < childrenShaders.length; i++) {
Kevin Lubickecd87622020-02-22 07:37:33 -050034 // childrenShaders are emscriptens smart pointer type. We want to get the bare pointer
35 // and send that over the wire, so it can be re-wrapped as an sk_sp.
36 barePointers.push(childrenShaders[i].$$.ptr);
37 }
Kevin Lubick69e46da2020-06-05 07:13:48 -040038 var childrenPointers = copy1dArray(barePointers, "HEAPU32");
Kevin Lubickecd87622020-02-22 07:37:33 -050039 // Our array has 4 bytes per float, so be sure to account for that before
40 // sending it over the wire.
Kevin Lubick6aa38692020-06-01 11:25:47 -040041 return this._makeShaderWithChildren(fptr, floats.length * 4, !!isOpaque, childrenPointers,
42 barePointers.length, localMatrixPtr);
Kevin Lubickecd87622020-02-22 07:37:33 -050043 }
Kevin Lubick6aa38692020-06-01 11:25:47 -040044});