Skip to content

Commit

Permalink
Fixed RuntimeEffect::makeShader, expanded RuntimeEffectScene #120 #124
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Aug 7, 2021
1 parent bcb861b commit 0e1babb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 38 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.92.13 - Aug 7, 2021

Added:

- RuntimeEffect #120 #124 thx @Vechro

# 0.92.11 - July 27, 2021

Added:
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ ColorSpace ▓▓▓▓░░░░░░ PictureRecorder ▓
Data ▓▓▓▓▓▓▓▓▓░ PixelRef ▓▓▓▓▓▓▓▓▓▓
Drawable ▓▓▓▓▓▓▓▓░░ Pixmap ▓▓▓▓▓▓▓▓▓▓
Flattenable ░░░░░░░░░░ Region ▓▓▓▓▓▓▓▓▓▓
Font ▓▓▓▓▓▓▓▓▓▓ ScalerContext ░░░░░░░░░░
FontData ░░░░░░░░░░ Shader ▓▓▓▓▓▓▓▓▓▓
FontManager ▓▓▓▓▓▓▓▓▓░ ShadowUtils ▓▓▓▓▓▓▓▓▓▓
FontStyle ▓▓▓▓▓▓▓▓▓▓ Stream ░░░░░░░░░░
FontStyleSet ▓▓▓▓▓▓▓▓▓▓ String ░░░░░░░░░
Image ▓▓░░░░░░░░ Surface ▓░░░░░░░░░
ImageFilters ▓▓▓▓▓▓▓▓▓▓ TextBlob ▓▓▓▓▓▓▓▓▓▓
ImageInfo ▓▓▓▓▓▓▓▓▓▓ TextBlobBuilder ▓▓▓▓▓▓▓▓▓▓
MaskFilter ▓▓▓▓▓▓▓▓▓▓ Typeface ▓▓▓▓▓▓▓▓░░
Matrix33 ▓▓▓░░░░░░░ WStream ▓▓░░░░░░░░
Matrix44 ▓▓▓░░░░░░░
Font ▓▓▓▓▓▓▓▓▓▓ RuntimeEffect ▓▓▓▓▓░░░░░
FontData ░░░░░░░░░░ ScalerContext ░░░░░░░░░░
FontManager ▓▓▓▓▓▓▓▓▓░ Shader ▓▓▓▓▓▓▓▓▓▓
FontStyle ▓▓▓▓▓▓▓▓▓▓ ShadowUtils ▓▓▓▓▓▓▓▓▓▓
FontStyleSet ▓▓▓▓▓▓▓▓▓▓ Stream ░░░░░░░░░
Image ▓▓░░░░░░░░ String ▓░░░░░░░░░
ImageFilters ▓▓▓▓▓▓▓▓▓▓ Surface ▓░░░░░░░░░
ImageInfo ▓▓▓▓▓▓▓▓▓▓ TextBlob ▓▓▓▓▓▓▓▓▓▓
MaskFilter ▓▓▓▓▓▓▓▓▓▓ TextBlobBuilder ▓▓▓▓▓▓▓▓▓▓
Matrix33 ▓▓▓░░░░░░░ Typeface ▓▓▓▓▓▓▓▓░░
Matrix44 ▓▓▓░░░░░░░ WStream ▓▓░░░░░░░░
Shaper: Paragraph:
Expand Down
Binary file added examples/scenes/images/triangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 39 additions & 11 deletions examples/scenes/src/RuntimeEffectScene.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package org.jetbrains.skija.examples.scenes;

import java.nio.*;
import java.nio.file.*;
import java.nio.file.Path;
import java.io.*;
import org.jetbrains.skija.*;

public class RuntimeEffectScene extends Scene {
@Override
public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int ypos) {
var sksl = "const half3 iColor = half3(0, 0.5, 0.75);\n"
+ "half4 main(float2 coord) {\n"
+ " return iColor.rgb1;\n"
+ "}";
public final Shader _texture;
public final RuntimeEffect _effect;

public RuntimeEffectScene() {
try {
_texture = Image.makeFromEncoded(Files.readAllBytes(Path.of(file("images/triangle.png")))).makeShader();
} catch (IOException e) {
throw new RuntimeException(e);
}

var effect = RuntimeEffect.makeForShader(sksl);
var myShader = effect.makeShader(null, null, null, false);
_effect = RuntimeEffect.makeForShader(
"uniform float xScale;\n" +
"uniform float xBias;\n" +
"uniform float yScale;\n" +
"uniform float yBias;\n" +
"uniform shader input;\n" +
"half4 main(float2 xy) {\n" +
" half4 tex = sample(input, mod(xy, 100));\n" +
" return half4((xy.x - xBias) / xScale / 2 + 0.5, (xy.y - yBias) / yScale / 2 + 0.5, tex.b, 1);\n" +
"}"
);
}

var p = new Paint();
p.setShader(myShader);
canvas.drawPaint(p);
@Override
public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int ypos) {
var bb = ByteBuffer.allocate(4 * 4).order(ByteOrder.nativeOrder());
bb.putFloat((float) width);
bb.putFloat((float) xpos);
bb.putFloat((float) height);
bb.putFloat((float) ypos);
try (var data = Data.makeFromBytes(bb.array());
var paint = new Paint();
var shader = _effect.makeShader(data, new Shader[] { _texture }, null, true);)
{
paint.setShader(shader);
canvas.drawPaint(paint);
}
}
}
2 changes: 1 addition & 1 deletion examples/scenes/src/Scenes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class Scenes {
public static TreeMap<String, Scene> scenes;
public static String currentScene = "Bitmap";
public static String currentScene = "Runtime Effect";
public static HUD hud = new HUD();
public static boolean stats = true;

Expand Down
23 changes: 10 additions & 13 deletions platform/cc/RuntimeEffect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,27 @@ Java_org_jetbrains_skija_RuntimeEffect__1nMakeShader(JNIEnv* env,
jclass jclass,
jlong ptr,
jlong uniformPtr,
jlongArray childrenPtrs,
jlongArray childrenPtrsArr,
jfloatArray localMatrixArr,
jboolean isOpaque) {
SkRuntimeEffect* runtimeEffect = jlongToPtr<SkRuntimeEffect*>(ptr);

// Uniform
SkData* uniform = jlongToPtr<SkData*>(uniformPtr);
sk_sp<SkData> uniformData = SkData::MakeFromMalloc(uniform, uniform->size());

// Matrix
std::unique_ptr<SkMatrix> localMatrix = skMatrix(env, localMatrixArr);

// Children
jsize childCount = env->GetArrayLength(childrenPtrs);
jlong* c = env->GetLongArrayElements(childrenPtrs, 0);
jsize childCount = env->GetArrayLength(childrenPtrsArr);
jlong* childrenPtrs = env->GetLongArrayElements(childrenPtrsArr, 0);
std::vector<sk_sp<SkShader>> children(childCount);
for (size_t i = 0; i < childCount; i++) {
SkShader* si = jlongToPtr<SkShader*>(c[i]);
SkShader* si = jlongToPtr<SkShader*>(childrenPtrs[i]);
children[i] = sk_ref_sp(si);
}
env->ReleaseLongArrayElements(childrenPtrs, c, 0);
env->ReleaseLongArrayElements(childrenPtrsArr, childrenPtrs, 0);

sk_sp<SkShader> shader = runtimeEffect->makeShader(uniformData, children.data(), childCount,
localMatrix.get(), isOpaque);
sk_sp<SkShader> shader = runtimeEffect->makeShader(sk_ref_sp<SkData>(uniform),
children.data(),
childCount,
localMatrix.get(),
isOpaque);
return ptrToJlong(shader.release());
}

Expand Down
4 changes: 2 additions & 2 deletions shared/java/RuntimeEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public class RuntimeEffect extends RefCnt {
public Shader makeShader(@Nullable Data uniforms, @Nullable Shader[] children, @Nullable Matrix33 localMatrix,
boolean isOpaque) {
Stats.onNativeCall();
float[] arr = localMatrix == null ? null : localMatrix._mat;
int childCount = children == null ? 0 : children.length;
long[] childrenPtrs = new long[childCount];
for (int i = 0; i < childCount; i++) {
childrenPtrs[i] = Native.getPtr(children[i]);
}
return new Shader(_nMakeShader(_ptr, Native.getPtr(uniforms), childrenPtrs, arr, isOpaque));
float[] matrix = localMatrix == null ? null : localMatrix._mat;
return new Shader(_nMakeShader(_ptr, Native.getPtr(uniforms), childrenPtrs, matrix, isOpaque));
}

public static RuntimeEffect makeForShader(String sksl) {
Expand Down

0 comments on commit 0e1babb

Please sign in to comment.