-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
178 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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 { | ||
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); | ||
} | ||
|
||
_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" + | ||
"}" | ||
); | ||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <jni.h> | ||
|
||
#include "SkRuntimeEffect.h" | ||
#include "interop.hh" | ||
|
||
extern "C" JNIEXPORT jlong JNICALL | ||
Java_org_jetbrains_skija_RuntimeEffect__1nMakeShader(JNIEnv* env, | ||
jclass jclass, | ||
jlong ptr, | ||
jlong uniformPtr, | ||
jlongArray childrenPtrsArr, | ||
jfloatArray localMatrixArr, | ||
jboolean isOpaque) { | ||
SkRuntimeEffect* runtimeEffect = jlongToPtr<SkRuntimeEffect*>(ptr); | ||
SkData* uniform = jlongToPtr<SkData*>(uniformPtr); | ||
std::unique_ptr<SkMatrix> localMatrix = skMatrix(env, localMatrixArr); | ||
|
||
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*>(childrenPtrs[i]); | ||
children[i] = sk_ref_sp(si); | ||
} | ||
env->ReleaseLongArrayElements(childrenPtrsArr, childrenPtrs, 0); | ||
|
||
sk_sp<SkShader> shader = runtimeEffect->makeShader(sk_ref_sp<SkData>(uniform), | ||
children.data(), | ||
childCount, | ||
localMatrix.get(), | ||
isOpaque); | ||
return ptrToJlong(shader.release()); | ||
} | ||
|
||
extern "C" JNIEXPORT jlong JNICALL | ||
Java_org_jetbrains_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_org_jetbrains_skija_RuntimeEffect__1nMakeForColorFilter(JNIEnv* env, | ||
jclass jclass, | ||
jstring sksl) { | ||
SkString skslProper = skString(env, sksl); | ||
SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForColorFilter(skslProper); | ||
if (result.errorText.isEmpty()) { | ||
return ptrToJlong(result.effect.release()); | ||
} else { | ||
env->ThrowNew(java::lang::RuntimeException::cls, result.errorText.c_str()); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.jetbrains.skija; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.skija.impl.*; | ||
|
||
public class RuntimeEffect extends RefCnt { | ||
static { | ||
Library.staticLoad(); | ||
} | ||
|
||
public Shader makeShader(@Nullable Data uniforms, @Nullable Shader[] children, @Nullable Matrix33 localMatrix, | ||
boolean isOpaque) { | ||
Stats.onNativeCall(); | ||
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]); | ||
} | ||
float[] matrix = localMatrix == null ? null : localMatrix._mat; | ||
return new Shader(_nMakeShader(_ptr, Native.getPtr(uniforms), childrenPtrs, matrix, isOpaque)); | ||
} | ||
|
||
public static RuntimeEffect makeForShader(String sksl) { | ||
Stats.onNativeCall(); | ||
return new RuntimeEffect(_nMakeForShader(sksl)); | ||
} | ||
|
||
public static RuntimeEffect makeForColorFilter(String sksl) { | ||
Stats.onNativeCall(); | ||
return new RuntimeEffect(_nMakeForColorFilter(sksl)); | ||
} | ||
|
||
@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); | ||
} |