From 0e1babb7d2dbb3c80cda61080e72eea5a5322721 Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Sat, 7 Aug 2021 22:01:26 +0200 Subject: [PATCH] Fixed RuntimeEffect::makeShader, expanded RuntimeEffectScene #120 #124 --- CHANGELOG.md | 6 +++ README.md | 22 ++++----- examples/scenes/images/triangle.png | Bin 0 -> 1927 bytes examples/scenes/src/RuntimeEffectScene.java | 50 +++++++++++++++----- examples/scenes/src/Scenes.java | 2 +- platform/cc/RuntimeEffect.cc | 23 ++++----- shared/java/RuntimeEffect.java | 4 +- 7 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 examples/scenes/images/triangle.png diff --git a/CHANGELOG.md b/CHANGELOG.md index 2697bd83..651bd434 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.92.13 - Aug 7, 2021 + +Added: + +- RuntimeEffect #120 #124 thx @Vechro + # 0.92.11 - July 27, 2021 Added: diff --git a/README.md b/README.md index 90c9e538..f9da09ae 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/examples/scenes/images/triangle.png b/examples/scenes/images/triangle.png new file mode 100644 index 0000000000000000000000000000000000000000..e94ad6ab7ebfcb54dde50f1277e6e3cd4ac33be6 GIT binary patch literal 1927 zcmaJ?e@s(X6n-sAYPDvOb~+}rR>`2g_O(?&UP=Wl+E$C~THxV^p{1`_q(5w*Qji&0 z1Qh@9$7mB&)Tm6V4C#z>jxhxXOTf*c!C?$K9L_OA6b;!Xo7;VDh01U@Iq#l#&-Z=z zocr#{+mn&Ln!}D{BM8D#tCRrFM%q|R`21%lGYn1)Cs>_|G<`pR2)=ZZgxaLlB0}i1 zkZ{Hv#1BCj5EqF|r{U1D7;lgJ8yPV{4Af*u5n7oC%~WMZrUmh3aHfX}8qN358DpTp1$yDGGHak02~so3Tyz@&+ZiI_x&$z&pk5IIY%l-?z>Iu`{*Ug{`GXTHO1 zr_44hpVrkIY+I=$fq>p3L_(PG z^l>e`i$*=ASL!KJK{tR%vRoud(9sPLhcA_EkYoRWO_EsYAR&1w0lI^N0wJKO=r)JW zW-C%m)y;tf{QMv)C?Ln^yBu=21C8Tfg`J~dL*~maRIrCfS|1?@^Aoi)MdxA+_g`|i zC(lAMFk8fLJ8RggUw4gFrtjX(im8f^nNAEpQ(V4KAW{7CL;aQO*^;s~i^#Ivb3b-J za9q9@v9@Q3l8D|N8ZAA2Gxn`n;SHV&r}XEMKSo@O`>k^yK7Z0EbeFbntR5XWX#~}y zDTT7yZTO?gyw;}@W&`NWz7ZSDNDu$gAVAA4{Fqc_Nw@vG<5>8Cngt zG{+z2yr5C6e$J4lJU0Wq0aAPoYbvf9_&nC$Djf@r2X}p+-x2lDs3(1#T6K9HhVB4g zHxrtYw)?^d@9g!}NJhA-{eHA%Im_=gAVcy%dk@h~AIHMf1bE+N?lyu~05bqf!4>7(|2`!suW=wA3akO>A_?mgbO>g2OO skBsH>TAOE-^;<6v*f(uI^?NmQg~iAg?C^|L(Ek#3TDr0+HSeQ;0bTutod5s; literal 0 HcmV?d00001 diff --git a/examples/scenes/src/RuntimeEffectScene.java b/examples/scenes/src/RuntimeEffectScene.java index 66366b6f..62cd78f8 100644 --- a/examples/scenes/src/RuntimeEffectScene.java +++ b/examples/scenes/src/RuntimeEffectScene.java @@ -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); + } } } diff --git a/examples/scenes/src/Scenes.java b/examples/scenes/src/Scenes.java index 4a504076..32ee174c 100644 --- a/examples/scenes/src/Scenes.java +++ b/examples/scenes/src/Scenes.java @@ -6,7 +6,7 @@ public class Scenes { public static TreeMap scenes; - public static String currentScene = "Bitmap"; + public static String currentScene = "Runtime Effect"; public static HUD hud = new HUD(); public static boolean stats = true; diff --git a/platform/cc/RuntimeEffect.cc b/platform/cc/RuntimeEffect.cc index 3f6ce6cf..85d6e340 100644 --- a/platform/cc/RuntimeEffect.cc +++ b/platform/cc/RuntimeEffect.cc @@ -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(ptr); - - // Uniform SkData* uniform = jlongToPtr(uniformPtr); - sk_sp uniformData = SkData::MakeFromMalloc(uniform, uniform->size()); - - // Matrix std::unique_ptr 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> children(childCount); for (size_t i = 0; i < childCount; i++) { - SkShader* si = jlongToPtr(c[i]); + SkShader* si = jlongToPtr(childrenPtrs[i]); children[i] = sk_ref_sp(si); } - env->ReleaseLongArrayElements(childrenPtrs, c, 0); + env->ReleaseLongArrayElements(childrenPtrsArr, childrenPtrs, 0); - sk_sp shader = runtimeEffect->makeShader(uniformData, children.data(), childCount, - localMatrix.get(), isOpaque); + sk_sp shader = runtimeEffect->makeShader(sk_ref_sp(uniform), + children.data(), + childCount, + localMatrix.get(), + isOpaque); return ptrToJlong(shader.release()); } diff --git a/shared/java/RuntimeEffect.java b/shared/java/RuntimeEffect.java index 95b66305..fb29a101 100644 --- a/shared/java/RuntimeEffect.java +++ b/shared/java/RuntimeEffect.java @@ -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) {