Skip to content

Commit

Permalink
RuntimeEffect::makeColorFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jan 5, 2022
1 parent 41d03f8 commit 4620bef
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 37 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# WIP
# 0.98.1 - Jan 5, 2021

Added:

- RuntimeEffect::makeColorFilter

Changed:

- types 0.1.0 -> 0.1.1
- Removed unnecessary LineMetrics UTF-8 to UTF-16 conversion #9


# 0.98.0 - Dec 20, 2021
Expand Down
47 changes: 37 additions & 10 deletions examples/scenes/src/RuntimeEffectScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import java.nio.file.Path;
import java.io.*;
import io.github.humbleui.skija.*;
import io.github.humbleui.types.*;

public class RuntimeEffectScene extends Scene {
public final Shader _texture;
public final RuntimeEffect _effect;
public final Image _texture;
public final RuntimeEffect _effectForShader;
public final RuntimeEffect _effectForColorFilter;

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

_effect = RuntimeEffect.makeForShader(
_effectForShader = RuntimeEffect.makeForShader(
"uniform float xScale;\n" +
"uniform float xBias;\n" +
"uniform float yScale;\n" +
Expand All @@ -28,6 +30,16 @@ public RuntimeEffectScene() {
" return half4((xy.x - xBias) / xScale / 2 + 0.5, (xy.y - yBias) / yScale / 2 + 0.5, tex.b, 1);\n" +
"}"
);

_effectForColorFilter = RuntimeEffect.makeForColorFilter(
"uniform float xScale;\n" +
"uniform float xBias;\n" +
"uniform float yScale;\n" +
"uniform float yBias;\n" +
"half4 main(half4 p) {\n" +
" return half4(xBias / xScale * p.r, yBias / yScale * p.g, p.b, p.a);\n" +
"}"
);
}

@Override
Expand All @@ -37,12 +49,27 @@ public void draw(Canvas canvas, int width, int height, float dpi, int xpos, int
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);

canvas.save();

try (var data = Data.makeFromBytes(bb.array())) {
canvas.translate(20, 20);

try (var child = _texture.makeShader();
var shader = _effectForShader.makeShader(data, new Shader[] { child }, null, true);
var paint = new Paint().setShader(shader);)
{
canvas.drawRect(Rect.makeXYWH(0, 0, 200, 200), paint);
canvas.drawString("makeForShader", 0, 220, inter13, blackFill);
canvas.translate(220, 0);
}

try (var filter = _effectForColorFilter.makeColorFilter(data, null);
var paint = new Paint().setColorFilter(filter);)
{
canvas.drawImageRect(_texture, Rect.makeXYWH(0, 0, 200, 200), paint);
canvas.drawString("makeForColorFilter", 0, 220, inter13, blackFill);
}
}
}
}
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 = "Paragraph Metrics";
public static String currentScene = "Runtime Effect";
public static HUD hud = new HUD();
public static boolean stats = true;

Expand Down
50 changes: 37 additions & 13 deletions platform/cc/RuntimeEffect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
#include "SkRuntimeEffect.h"
#include "interop.hh"

extern "C" JNIEXPORT jlong JNICALL
Java_io_github_humbleui_skija_RuntimeEffect__1nMakeForShader(JNIEnv* env, jclass jclass, jstring sksl) {
SkString skslProper = skString(env, sksl);
SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(skslProper);
if (result.errorText.isEmpty()) {
sk_sp<SkRuntimeEffect> effect = result.effect;
return ptrToJlong(effect.release());
} else {
env->ThrowNew(java::lang::RuntimeException::cls, result.errorText.c_str());
return 0;
}
}

extern "C" JNIEXPORT jlong JNICALL
Java_io_github_humbleui_skija_RuntimeEffect__1nMakeShader(JNIEnv* env,
jclass jclass,
Expand Down Expand Up @@ -32,19 +45,6 @@ Java_io_github_humbleui_skija_RuntimeEffect__1nMakeShader(JNIEnv* env,
return ptrToJlong(shader.release());
}

extern "C" JNIEXPORT jlong JNICALL
Java_io_github_humbleui_skija_RuntimeEffect__1nMakeForShader(JNIEnv* env, jclass jclass, jstring sksl) {
SkString skslProper = skString(env, sksl);
SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(skslProper);
if (result.errorText.isEmpty()) {
sk_sp<SkRuntimeEffect> effect = result.effect;
return ptrToJlong(effect.release());
} else {
env->ThrowNew(java::lang::RuntimeException::cls, result.errorText.c_str());
return 0;
}
}

extern "C" JNIEXPORT jlong JNICALL
Java_io_github_humbleui_skija_RuntimeEffect__1nMakeForColorFilter(JNIEnv* env,
jclass jclass,
Expand All @@ -57,4 +57,28 @@ Java_io_github_humbleui_skija_RuntimeEffect__1nMakeForColorFilter(JNIEnv* env,
env->ThrowNew(java::lang::RuntimeException::cls, result.errorText.c_str());
return 0;
}
}

extern "C" JNIEXPORT jlong JNICALL
Java_io_github_humbleui_skija_RuntimeEffect__1nMakeColorFilter(JNIEnv* env,
jclass jclass,
jlong ptr,
jlong uniformPtr,
jlongArray childrenPtrsArr) {
SkRuntimeEffect* runtimeEffect = jlongToPtr<SkRuntimeEffect*>(ptr);
SkData* uniform = jlongToPtr<SkData*>(uniformPtr);

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

sk_sp<SkColorFilter> filter = runtimeEffect->makeColorFilter(sk_ref_sp<SkData>(uniform),
children.data(),
childCount);
return ptrToJlong(filter.release());
}
33 changes: 21 additions & 12 deletions shared/java/RuntimeEffect.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package io.github.humbleui.skija;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.*;
import io.github.humbleui.skija.impl.*;

public class RuntimeEffect extends RefCnt {
static {
Library.staticLoad();
}

public static RuntimeEffect makeForShader(String sksl) {
Stats.onNativeCall();
return new RuntimeEffect(_nMakeForShader(sksl));
}

@NotNull
public Shader makeShader(@Nullable Data uniforms, @Nullable Shader[] children, @Nullable Matrix33 localMatrix,
boolean isOpaque) {
Stats.onNativeCall();
Expand All @@ -20,25 +25,29 @@ public Shader makeShader(@Nullable Data uniforms, @Nullable Shader[] children, @
return new Shader(_nMakeShader(_ptr, Native.getPtr(uniforms), childrenPtrs, matrix, isOpaque));
}

public static RuntimeEffect makeForShader(String sksl) {
@NotNull
public static RuntimeEffect makeForColorFilter(String sksl) {
Stats.onNativeCall();
return new RuntimeEffect(_nMakeForShader(sksl));
return new RuntimeEffect(_nMakeForColorFilter(sksl));
}

public static RuntimeEffect makeForColorFilter(String sksl) {
@NotNull
public ColorFilter makeColorFilter(@Nullable Data uniforms, @Nullable ColorFilter[] children) {
Stats.onNativeCall();
return new RuntimeEffect(_nMakeForColorFilter(sksl));
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 ColorFilter(_nMakeColorFilter(_ptr, Native.getPtr(uniforms), childrenPtrs));
}

@ApiStatus.Internal
public RuntimeEffect(long ptr) {
super(ptr);
}

public static native long _nMakeShader(long runtimeEffectPtr, long uniformPtr, long[] childrenPtrs,
float[] localMatrix, boolean isOpaque);

public static native long _nMakeForShader(String sksl);

public static native long _nMakeForColorFilter(String sksl);
@ApiStatus.Internal public static native long _nMakeForShader(String sksl);
@ApiStatus.Internal public static native long _nMakeShader(long runtimeEffectPtr, long uniformPtr, long[] childrenPtrs, float[] localMatrix, boolean isOpaque);
@ApiStatus.Internal public static native long _nMakeForColorFilter(String sksl);
@ApiStatus.Internal public static native long _nMakeColorFilter(long runtimeEffectPtr, long uniformPtr, long[] childrenPtrs);
}

0 comments on commit 4620bef

Please sign in to comment.