[canvaskit] Add _RTShaderFactory for demos

Change-Id: Ib6dc1ff861efe4040508fea09c8a66d8d06db597
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/258568
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
This commit is contained in:
Kevin Lubick 2019-12-06 13:55:58 -05:00
parent 274a766baf
commit 4b5b645049
5 changed files with 83 additions and 2 deletions

View File

@ -23,6 +23,10 @@
<canvas id=sk_webfont width=500 height=500
title='This shows loading of a custom font (e.g. WebFont)'></canvas>
<h2> RT Shader </h2>
<canvas id=rtshader width=300 height=300></canvas>
<h2> Particles </h2>
<canvas id=particles width=500 height=500></canvas>
@ -68,6 +72,8 @@
ParticlesAPI1(CanvasKit);
ParagraphAPI1(CanvasKit, robotoData);
RTShaderAPI1(CanvasKit);
});
fetch('https://storage.googleapis.com/skia-cdn/misc/lego_loader.json').then((resp) => {
@ -349,4 +355,38 @@ const curves = {
document.getElementById('para1').addEventListener('pointermove', interact);
return surface;
}
function RTShaderAPI1(CanvasKit) {
if (!CanvasKit) {
return;
}
const prog = `
layout(ctype=SkRect) uniform half4 gColor;
void main(float x, float y, inout half4 color) {
color = half4(half(x)*(1.0/255), half(y)*(1.0/255), gColor.b, 1);
}
`;
const surface = CanvasKit.MakeCanvasSurface('rtshader');
if (!surface) {
console.error('Could not make surface');
return;
}
const canvas = surface.getCanvas();
const fact = CanvasKit._RTShaderFactory.MakeFromProgram(prog, true);
const rot = CanvasKit.SkMatrix.rotated(90, 128, 128);
const shader = fact.make([1, 0, 0, 1], rot);
const paint = new CanvasKit.SkPaint();
paint.setShader(shader);
canvas.drawRect(CanvasKit.LTRBRect(0, 0, 256, 256), paint);
surface.flush();
shader.delete();
paint.delete();
fact.delete();
}
</script>

View File

@ -53,6 +53,7 @@
#include "src/core/SkFontMgrPriv.h"
#include "src/core/SkMakeUnique.h"
#include "src/core/SkResourceCache.h"
#include "src/shaders/SkRTShader.h"
#include <iostream>
#include <string>
@ -1334,6 +1335,24 @@ EMSCRIPTEN_BINDINGS(Skia) {
class_<SkShader>("SkShader")
.smart_ptr<sk_sp<SkShader>>("sk_sp<SkShader>");
class_<SkRuntimeShaderFactory>("_RTShaderFactory")
.class_function("MakeFromProgram", optional_override([](std::string sksl, bool isOpaque)->SkRuntimeShaderFactory {
SkString s(sksl.c_str(), sksl.length());
return SkRuntimeShaderFactory(s, isOpaque);
}))
.function("_make", optional_override([](SkRuntimeShaderFactory& self, uintptr_t fptr, size_t len)->sk_sp<SkShader> {
uint8_t* floatData = reinterpret_cast<uint8_t*>(fptr);
sk_sp<SkData> bytes = SkData::MakeFromMalloc(floatData, len);
return self.make(bytes, nullptr);
}))
.function("_make", optional_override([](SkRuntimeShaderFactory& self, uintptr_t fptr, size_t len,
SimpleMatrix sm)->sk_sp<SkShader> {
uint8_t* floatData = reinterpret_cast<uint8_t*>(fptr);
sk_sp<SkData> bytes = SkData::MakeFromMalloc(floatData, len);
auto m = toSkMatrix(sm);
return self.make(bytes, &m);
}));
class_<SkSurface>("SkSurface")
.smart_ptr<sk_sp<SkSurface>>("sk_sp<SkSurface>")
.function("_flush", select_overload<void()>(&SkSurface::flush))

View File

@ -124,6 +124,14 @@ var CanvasKit = {
_getRectsForRange: function() {},
},
_RTShaderFactory: {
// public API (from C++ bindings)
MakeFromProgram: function() {},
// private API
_make: function() {},
},
ParagraphStyle: function() {},
RSXFormBuilder: function() {},
SkColorBuilder: function() {},
@ -784,6 +792,8 @@ CanvasKit.SkColorBuilder.prototype.delete = function() {};
CanvasKit.SkColorBuilder.prototype.push = function() {};
CanvasKit.SkColorBuilder.prototype.set = function() {};
CanvasKit._RTShaderFactory.prototype.make = function() {};
// Define StrokeOpts object
var StrokeOpts = {};
StrokeOpts.prototype.width;

View File

@ -216,6 +216,16 @@ CanvasKit.onRuntimeInitialized = function() {
return m;
}
CanvasKit._RTShaderFactory.prototype.make = function(floats, matrix) {
var fptr = copy1dArray(floats, CanvasKit.HEAPF32);
// Our array has 4 bytes per float, so be sure to account for that before
// sending it over the wire.
if (!matrix) {
return this._make(fptr, floats.length * 4);
}
return this._make(fptr, floats.length * 4, matrix);
}
CanvasKit.SkPath.prototype.addArc = function(oval, startAngle, sweepAngle) {
// see arc() for the HTMLCanvas version
// note input angles are degrees.
@ -1193,4 +1203,4 @@ CanvasKit.MakeSkVertices = function(mode, positions, textureCoordinates, colors,
var idxCount = (indices && indices.length) || 0;
// Create the vertices, which owns the memory that the builder had allocated.
return builder.detach();
};
};

View File

@ -530,8 +530,10 @@ public:
operator bool() const { return !fFPStack.empty(); }
bool operator!=(const EndIter&) { return (bool)*this; }
// Hopefully this does not actually get called because of RVO.
IterBase(const IterBase&) = default;
// Because each iterator carries a stack we want to avoid copies.
IterBase(const IterBase&) = delete;
IterBase& operator=(const IterBase&) = delete;
protected: