From 7d25c9de47d04d1f647cccc61715ec06aead72d8 Mon Sep 17 00:00:00 2001 From: Natan Date: Thu, 11 Jul 2024 09:14:01 -0300 Subject: [PATCH 1/8] Small file storage improvement --- .../gdx/backends/teavm/filesystem/MemoryFileStorage.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/filesystem/MemoryFileStorage.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/filesystem/MemoryFileStorage.java index 32bd36bc..98d38f04 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/filesystem/MemoryFileStorage.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/filesystem/MemoryFileStorage.java @@ -25,6 +25,9 @@ public MemoryFileStorage() { public InputStream read(TeaFileHandle file) { String path = fixPath(file.path()); FileData data = getInternal(path); + if(data == null) { + return null; + } byte[] byteArray = data.getBytes(); try { return new ByteArrayInputStream(byteArray); @@ -32,7 +35,7 @@ public InputStream read(TeaFileHandle file) { catch(RuntimeException e) { // Something corrupted: we remove it & re-throw the error removeInternal(path); - throw e; + throw new GdxRuntimeException(getClass().getSimpleName() + " Error: " + path, e); } } @@ -299,6 +302,9 @@ private String[] list(FileHandle file, boolean equals) { String[] str = new String[tmpPaths.size]; for(int i = 0; i < tmpPaths.size; i++) { String s = tmpPaths.get(i); + if(s.startsWith("/")) { + s = s.substring(1); + } if(debug) { System.out.println(getClass().getSimpleName() + " LIST[" + i + "]: " + s); } From 5ce7233e4e2f61b999a62259ebb6a633e7e9adc8 Mon Sep 17 00:00:00 2001 From: Natan Date: Thu, 11 Jul 2024 09:29:56 -0300 Subject: [PATCH 2/8] Improve initializing properties --- build.gradle.kts | 6 ++ buildSrc/src/main/kotlin/Dependencies.kt | 80 ++++++++++++------- examples/gdx-tests/desktop/build.gradle.kts | 4 +- .../gdx/examples/teavm/BuildGdxTest.java | 4 +- settings.gradle.kts | 2 +- 5 files changed, 60 insertions(+), 36 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b615c6ed..08784e4c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,6 +4,8 @@ plugins { id("signing") } +LibExt.initProperties(rootDir) + subprojects { apply { plugin("java") @@ -44,6 +46,10 @@ configure(libProjects) { group = LibExt.groupId version = LibExt.libVersion + if(LibExt.libVersion.isEmpty()) { + throw RuntimeException("Version cannot be empty") + } + publishing { repositories { maven { diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 469faa54..2f908618 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -4,9 +4,7 @@ import java.util.* object LibExt { const val groupId = "com.github.xpenatan.gdx-teavm" - val properties = getProperties(); - - val libVersion: String = getVersion(properties) + var libVersion = "" const val gdxVersion = "1.12.1" const val teaVMVersion = "0.10.0" @@ -22,35 +20,55 @@ object LibExt { const val truthVersion = "1.4.2" + var gdxSourcePath = "" + var gdxTestsAssetsPath = "" + var teavmPath = "" + var includeLibgdxSource = false + var includeTeaVMSource = false + + fun initProperties(rootDir: File) { + val properties = getProperties(rootDir) + updateProperties(properties) + } + + private fun updateProperties(properties: Properties) { + val isVersionEmpty = libVersion.isEmpty() + libVersion = getVersion(properties) - // ######### Add libgdx tests to project - // ######### Need to have libgdx project source code tag 1.12.1. - // ######### Need to disable in libgdx settings :tests:gdx-tests-gwt because of some conflicts - // ######### Need to update gradle properties. - val gdxSourcePath = properties.getOrDefault("gdxSourcePath", "") as String - val gdxTestsAssetsPath = "${gdxSourcePath}/tests/gdx-tests-android/assets/" - val teavmPath = properties.getOrDefault("teavmPath", "") as String - val includeLibgdxSource = (properties.getOrDefault("includeLibgdxSource", "false") as String).toBoolean() - val includeTeaVMSource = (properties.getOrDefault("includeTeaVMSource", "false") as String).toBoolean() -} - -private fun getVersion(properties: Properties): String { - val isReleaseStr = System.getenv("RELEASE") - val isRelease = isReleaseStr != null && isReleaseStr.toBoolean() - var libVersion = "-SNAPSHOT" - val version = properties.getProperty("version") - if(isRelease) { - libVersion = version + if(isVersionEmpty) { + println("Lib Version: $libVersion") + } + + gdxSourcePath = properties.getOrDefault("gdxSourcePath", "") as String + gdxTestsAssetsPath = "${gdxSourcePath}/tests/gdx-tests-android/assets/" + teavmPath = properties.getOrDefault("teavmPath", "") as String + includeLibgdxSource = (properties.getOrDefault("includeLibgdxSource", "false") as String).toBoolean() + includeTeaVMSource = (properties.getOrDefault("includeTeaVMSource", "false") as String).toBoolean() + } + + private fun getProperties(rootDir: File? = null): Properties { + val propFile ="gradle.properties" + val file = if(rootDir != null) { + File(rootDir, propFile) + } else { + File(propFile) + } + val properties = Properties() + if(file.exists()) { + properties.load(file.inputStream()) + } + updateProperties(properties) + return properties } - println("Lib Version: $libVersion") - return libVersion -} - -private fun getProperties(): Properties { - val file = File("gradle.properties") - val properties = Properties() - if(file.exists()) { - properties.load(file.inputStream()) + + private fun getVersion(properties: Properties): String { + val isReleaseStr = System.getenv("RELEASE") + val isRelease = isReleaseStr != null && isReleaseStr.toBoolean() + var libVersion = "-SNAPSHOT" + val version = properties.getProperty("version", "") + if(isRelease) { + libVersion = version + } + return libVersion } - return properties } \ No newline at end of file diff --git a/examples/gdx-tests/desktop/build.gradle.kts b/examples/gdx-tests/desktop/build.gradle.kts index 01b4eff2..a81a2461 100644 --- a/examples/gdx-tests/desktop/build.gradle.kts +++ b/examples/gdx-tests/desktop/build.gradle.kts @@ -10,8 +10,6 @@ dependencies { val mainClassName = "com.github.xpenatan.imgui.example.tests.Main" -val assetsDir = File(LibExt.gdxTestsAssetsPath) - sourceSets["main"].resources.srcDirs(File("../../core/assets")) tasks.register("gdx-tests-run-desktop") { @@ -20,5 +18,5 @@ tasks.register("gdx-tests-run-desktop") { description = "Run gdx tests example" mainClass.set(mainClassName) classpath = sourceSets["main"].runtimeClasspath - workingDir = assetsDir + workingDir = File(LibExt.gdxTestsAssetsPath) } \ No newline at end of file diff --git a/examples/gdx-tests/teavm/src/main/java/com/github/xpenatan/gdx/examples/teavm/BuildGdxTest.java b/examples/gdx-tests/teavm/src/main/java/com/github/xpenatan/gdx/examples/teavm/BuildGdxTest.java index 83b8afd5..9d2c7921 100644 --- a/examples/gdx-tests/teavm/src/main/java/com/github/xpenatan/gdx/examples/teavm/BuildGdxTest.java +++ b/examples/gdx-tests/teavm/src/main/java/com/github/xpenatan/gdx/examples/teavm/BuildGdxTest.java @@ -19,7 +19,9 @@ public static void main(String[] args) throws IOException { TeaReflectionSupplier.addReflectionClass(reflectionPackage); TeaBuildConfiguration teaBuildConfiguration = new TeaBuildConfiguration(); - teaBuildConfiguration.assetsPath.add(new AssetFileHandle(args[0])); + String gdxAssetsPath = args[0]; + System.out.println("gdxAssetsPath: " + gdxAssetsPath); + teaBuildConfiguration.assetsPath.add(new AssetFileHandle(gdxAssetsPath)); teaBuildConfiguration.assetsPath.add(new AssetFileHandle("../../core/assets")); teaBuildConfiguration.webappPath = new File("build/dist").getCanonicalPath(); diff --git a/settings.gradle.kts b/settings.gradle.kts index 62e83173..99303872 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -17,7 +17,7 @@ include(":examples:freetype:core") include(":examples:freetype:desktop") include(":examples:freetype:teavm") -val file = File("gradle.properties") +val file = File(settingsDir, "gradle.properties") val properties = Properties() if(file.exists()) { From abb68f61a3b45184d1292b47c45ffcc5d96a86ce Mon Sep 17 00:00:00 2001 From: Natan Date: Thu, 11 Jul 2024 09:30:28 -0300 Subject: [PATCH 3/8] Remove Exception emulation --- .../gdx/utils/GdxRuntimeExceptionEmu.java | 40 ------------------- 1 file changed, 40 deletions(-) delete mode 100644 backends/backend-teavm/emu/com/badlogic/gdx/utils/GdxRuntimeExceptionEmu.java diff --git a/backends/backend-teavm/emu/com/badlogic/gdx/utils/GdxRuntimeExceptionEmu.java b/backends/backend-teavm/emu/com/badlogic/gdx/utils/GdxRuntimeExceptionEmu.java deleted file mode 100644 index 8b791680..00000000 --- a/backends/backend-teavm/emu/com/badlogic/gdx/utils/GdxRuntimeExceptionEmu.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.badlogic.gdx.utils; - -/** - * Emulated hack. - * throw a GdxRuntimeException does not work with teavm. We just rethrow it to make it work. - * - * @author xpenatan - */ - -import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; - -@Emulate(GdxRuntimeException.class) -public class GdxRuntimeExceptionEmu extends RuntimeException { - private static final long serialVersionUID = -7034897190745766939L; - - public GdxRuntimeExceptionEmu() { - super(); -// throw new RuntimeException(); - } - - public GdxRuntimeExceptionEmu(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - - public GdxRuntimeExceptionEmu(String message, Throwable cause) { - super(message, cause); -// throw new RuntimeException(message, cause); - } - - public GdxRuntimeExceptionEmu(String message) { - super(message); - -// throw new RuntimeException(message); - } - - public GdxRuntimeExceptionEmu(Throwable cause) { - super(cause); -// throw new RuntimeException(cause); - } -} From c77f1e93bc80fa727f90d5490f3ec24e557caca0 Mon Sep 17 00:00:00 2001 From: Natan Date: Thu, 11 Jul 2024 17:59:21 -0300 Subject: [PATCH 4/8] Use file type for checking if asset is loaded --- .../com/badlogic/gdx/assets/AssetLoadingTaskEmu.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backends/backend-teavm/emu/com/badlogic/gdx/assets/AssetLoadingTaskEmu.java b/backends/backend-teavm/emu/com/badlogic/gdx/assets/AssetLoadingTaskEmu.java index 0663e7b4..b7cd4261 100644 --- a/backends/backend-teavm/emu/com/badlogic/gdx/assets/AssetLoadingTaskEmu.java +++ b/backends/backend-teavm/emu/com/badlogic/gdx/assets/AssetLoadingTaskEmu.java @@ -80,17 +80,17 @@ public Void call() throws Exception { public boolean update() { // GTW: check if we have a file that was not preloaded and is not done loading yet AssetLoader.AssetLoad assetLoader = AssetLoader.getInstance(); - - String path = resolve(loader, assetDesc).path(); - - if(!assetLoader.isAssetLoaded(Files.FileType.Internal, path)) { + FileHandle fileHandle = resolve(loader, assetDesc); + String path = fileHandle.path(); + Files.FileType type = fileHandle.type(); + if(!assetLoader.isAssetLoaded(type, path)) { if(!assetLoader.isAssetInQueue(path)) { count++; if(count == 2) { cancel = true; } else { - assetLoader.loadAsset(true, path, AssetType.Binary, Files.FileType.Internal, null); + assetLoader.loadAsset(true, path, AssetType.Binary, type, null); } } } From 9becf12829492e315c77ed477cf905062525fb84 Mon Sep 17 00:00:00 2001 From: Natan <6472084+xpenatan@users.noreply.github.com> Date: Sat, 13 Jul 2024 11:49:46 -0300 Subject: [PATCH 5/8] Improve arraybuffer performance (#126) --- .../classlib/java/nio/ByteBufferImplEmu.java | 29 ++++- .../java/nio/FloatBufferOverArrayEmu.java | 47 ++++++++ .../nio/FloatBufferOverByteBufferEmu.java | 34 +++++- .../classlib/java/nio/HasArrayBufferView.java | 5 +- .../java/nio/IntBufferOverArrayEmu.java | 47 ++++++++ .../java/nio/IntBufferOverByteBufferEmu.java | 35 +++++- .../java/nio/ShortBufferOverArrayEmu.java | 47 ++++++++ .../nio/ShortBufferOverByteBufferEmu.java | 34 +++++- .../xpenatan/gdx/backends/teavm/TeaGL20.java | 103 ++++-------------- .../xpenatan/gdx/backends/teavm/TeaGL30.java | 30 ++--- .../teavm/dom/typedarray/TypedArrays.java | 77 +++++++------ 11 files changed, 339 insertions(+), 149 deletions(-) create mode 100644 backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverArrayEmu.java create mode 100644 backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverArrayEmu.java create mode 100644 backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverArrayEmu.java diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ByteBufferImplEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ByteBufferImplEmu.java index 1c19dfce..82414472 100644 --- a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ByteBufferImplEmu.java +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ByteBufferImplEmu.java @@ -1,5 +1,6 @@ package org.teavm.classlib.java.nio; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; @@ -8,9 +9,12 @@ @Emulate(valueStr = "java.nio.ByteBufferImpl", updateCode = true) public abstract class ByteBufferImplEmu extends TByteBufferImpl implements HasArrayBufferView { - public ByteBufferImplEmu(int capacity, boolean direct) { - super(capacity, direct); - } + @Emulate + Int8ArrayWrapper backupArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; public ByteBufferImplEmu(int start, int capacity, byte[] array, int position, int limit, boolean direct, boolean readOnly) { super(start, capacity, array, position, limit, direct, readOnly); @@ -18,9 +22,24 @@ public ByteBufferImplEmu(int start, int capacity, byte[] array, int position, in @Override @Emulate - public Int8ArrayWrapper getTypedArray() { + public ArrayBufferViewWrapper getArrayBufferView() { + Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView(); + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = int8Array.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { Object array = array(); - return TypedArrays.getArrayBufferView((JSObject)array); + Int8ArrayWrapper int8Array = TypedArrays.getArrayBufferView((JSObject)array); + return int8Array; } @Override diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverArrayEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverArrayEmu.java new file mode 100644 index 00000000..5418dc7b --- /dev/null +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverArrayEmu.java @@ -0,0 +1,47 @@ +package org.teavm.classlib.java.nio; + +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Float32ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; +import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; + +@Emulate(valueStr = "java.nio.TFloatBufferOverArray", updateCode = true) +public abstract class FloatBufferOverArrayEmu extends TFloatBufferOverArray implements HasArrayBufferView { + + @Emulate + Float32ArrayWrapper backupArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; + + public FloatBufferOverArrayEmu(int start, int capacity, float[] array, int position, int limit, boolean readOnly) { + super(start, capacity, array, position, limit, readOnly); + } + + @Override + @Emulate + public ArrayBufferViewWrapper getArrayBufferView() { + Float32ArrayWrapper originalBuffer = (Float32ArrayWrapper)getOriginalArrayBufferView(); + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = originalBuffer.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { + return TypedArrays.getTypedArray(array); + } + + @Override + @Emulate + public int getElementSize() { + return 4; + } +} \ No newline at end of file diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverByteBufferEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverByteBufferEmu.java index a24d80b3..f3d44665 100644 --- a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverByteBufferEmu.java +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/FloatBufferOverByteBufferEmu.java @@ -1,21 +1,51 @@ package org.teavm.classlib.java.nio; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Float32ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; @Emulate(valueStr = "java.nio.FloatBufferOverByteBuffer", updateCode = true) public abstract class FloatBufferOverByteBufferEmu extends TFloatBufferOverByteBuffer implements HasArrayBufferView { + @Emulate + Float32ArrayWrapper backupArray; + @Emulate + Float32ArrayWrapper floatArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; + public FloatBufferOverByteBufferEmu(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { super(start, capacity, byteBuffer, position, limit, readOnly); } @Override @Emulate - public Int8ArrayWrapper getTypedArray() { + public ArrayBufferViewWrapper getArrayBufferView() { + // Int8Array + Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView(); + if(floatArray == null) { + floatArray = TypedArrays.createFloat32Array(int8Array.getBuffer()); + } + + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = floatArray.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { HasArrayBufferView buff = (HasArrayBufferView)byteByffer; - return buff.getTypedArray(); + return buff.getOriginalArrayBufferView(); } @Override diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/HasArrayBufferView.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/HasArrayBufferView.java index 29ff50e6..a1eb3936 100644 --- a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/HasArrayBufferView.java +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/HasArrayBufferView.java @@ -1,8 +1,9 @@ package org.teavm.classlib.java.nio; -import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; public interface HasArrayBufferView { - Int8ArrayWrapper getTypedArray(); + ArrayBufferViewWrapper getArrayBufferView(); + ArrayBufferViewWrapper getOriginalArrayBufferView(); int getElementSize(); } \ No newline at end of file diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverArrayEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverArrayEmu.java new file mode 100644 index 00000000..778238c9 --- /dev/null +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverArrayEmu.java @@ -0,0 +1,47 @@ +package org.teavm.classlib.java.nio; + +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int32ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; +import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; + +@Emulate(valueStr = "java.nio.TIntBufferOverArray", updateCode = true) +public abstract class IntBufferOverArrayEmu extends TIntBufferOverArray implements HasArrayBufferView { + + @Emulate + Int32ArrayWrapper backupArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; + + public IntBufferOverArrayEmu(int start, int capacity, int[] array, int position, int limit, boolean readOnly) { + super(start, capacity, array, position, limit, readOnly); + } + + @Override + @Emulate + public ArrayBufferViewWrapper getArrayBufferView() { + Int32ArrayWrapper originalBuffer = (Int32ArrayWrapper)getOriginalArrayBufferView(); + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = originalBuffer.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { + return TypedArrays.getTypedArray(array); + } + + @Override + @Emulate + public int getElementSize() { + return 4; + } +} \ No newline at end of file diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverByteBufferEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverByteBufferEmu.java index fa954e58..92a4da2c 100644 --- a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverByteBufferEmu.java +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/IntBufferOverByteBufferEmu.java @@ -1,20 +1,51 @@ package org.teavm.classlib.java.nio; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int32ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; @Emulate(valueStr = "java.nio.IntBufferOverByteBuffer", updateCode = true) public abstract class IntBufferOverByteBufferEmu extends TIntBufferOverByteBuffer implements HasArrayBufferView { + @Emulate + Int32ArrayWrapper backupArray; + @Emulate + Int32ArrayWrapper intArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; + + public IntBufferOverByteBufferEmu(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { super(start, capacity, byteBuffer, position, limit, readOnly); } @Override @Emulate - public Int8ArrayWrapper getTypedArray() { + public ArrayBufferViewWrapper getArrayBufferView() { + // Int8Array + Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView(); + if(intArray == null) { + intArray = TypedArrays.createInt32Array(int8Array.getBuffer()); + } + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = intArray.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { HasArrayBufferView buff = (HasArrayBufferView)byteByffer; - return buff.getTypedArray(); + return buff.getOriginalArrayBufferView(); } @Override diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverArrayEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverArrayEmu.java new file mode 100644 index 00000000..379a37e9 --- /dev/null +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverArrayEmu.java @@ -0,0 +1,47 @@ +package org.teavm.classlib.java.nio; + +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int16ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; +import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; + +@Emulate(valueStr = "java.nio.TShortBufferOverArray", updateCode = true) +public abstract class ShortBufferOverArrayEmu extends TShortBufferOverArray implements HasArrayBufferView { + + @Emulate + Int16ArrayWrapper backupArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; + + public ShortBufferOverArrayEmu(int start, int capacity, short[] array, int position, int limit, boolean readOnly) { + super(start, capacity, array, position, limit, readOnly); + } + + @Override + @Emulate + public ArrayBufferViewWrapper getArrayBufferView() { + Int16ArrayWrapper originalBuffer = (Int16ArrayWrapper)getOriginalArrayBufferView(); + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = originalBuffer.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { + return TypedArrays.getTypedArray(array); + } + + @Override + @Emulate + public int getElementSize() { + return 2; + } +} \ No newline at end of file diff --git a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverByteBufferEmu.java b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverByteBufferEmu.java index 0175ba18..fc0cedb3 100644 --- a/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverByteBufferEmu.java +++ b/backends/backend-teavm/emu/org/teavm/classlib/java/nio/ShortBufferOverByteBufferEmu.java @@ -1,20 +1,50 @@ package org.teavm.classlib.java.nio; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int16ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper; +import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; import com.github.xpenatan.gdx.backends.teavm.gen.Emulate; @Emulate(valueStr = "java.nio.ShortBufferOverByteBuffer", updateCode = true) public abstract class ShortBufferOverByteBufferEmu extends TShortBufferOverByteBuffer implements HasArrayBufferView { + @Emulate + Int16ArrayWrapper backupArray; + @Emulate + Int16ArrayWrapper shortArray; + @Emulate + int positionCache; + @Emulate + int remainingCache; + public ShortBufferOverByteBufferEmu(int start, int capacity, TByteBufferImpl byteBuffer, int position, int limit, boolean readOnly) { super(start, capacity, byteBuffer, position, limit, readOnly); } @Override @Emulate - public Int8ArrayWrapper getTypedArray() { + public ArrayBufferViewWrapper getArrayBufferView() { + // Int8Array + Int8ArrayWrapper int8Array = (Int8ArrayWrapper)getOriginalArrayBufferView(); + if(shortArray == null) { + shortArray = TypedArrays.createInt16Array(int8Array.getBuffer()); + } + int position1 = position(); + int remaining1 = remaining(); + if(backupArray == null || positionCache != position1 || remaining1 != remainingCache) { + positionCache = position1; + remainingCache = remaining1; + backupArray = shortArray.subarray(position1, remaining1); + } + return backupArray; + } + + @Override + @Emulate + public ArrayBufferViewWrapper getOriginalArrayBufferView() { HasArrayBufferView buff = (HasArrayBufferView)byteByffer; - return buff.getTypedArray(); + return buff.getOriginalArrayBufferView(); } @Override diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java index 07db2c67..6be6eea3 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java @@ -72,16 +72,6 @@ public TeaGL20(WebGLRenderingContextWrapper gl) { this.gl.pixelStorei(WebGLRenderingContextWrapper.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0); } - public Float32ArrayWrapper copy(FloatBuffer buffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray(buffer); - return TypedArrays.createFloat32Array(typedArray.getBuffer(), buffer.position(), buffer.remaining()); - } - - public Int32ArrayWrapper copy(IntBuffer buffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray(buffer); - return TypedArrays.createInt32Array(typedArray.getBuffer(), buffer.position(), buffer.remaining()); - } - protected WebGLUniformLocationWrapper getUniformLocation(int location) { return uniforms.get(currProgram).get(location); } @@ -151,51 +141,19 @@ public void glBlendFuncSeparate(int srcRGB, int dstRGB, int srcAlpha, int dstAlp @Override public void glBufferData(int target, int size, Buffer data, int usage) { - if(data instanceof FloatBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((FloatBuffer)data); - gl.bufferData(target, typedArray, usage); - } - else if(data instanceof IntBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((IntBuffer)data); - gl.bufferData(target, typedArray, usage); - } - else if(data instanceof ShortBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((ShortBuffer)data); - gl.bufferData(target, typedArray, usage); - } - else if(data instanceof ByteBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((ByteBuffer)data); - gl.bufferData(target, typedArray, usage); - } - else if(data == null) { + if(data == null) { gl.bufferData(target, size, usage); } else { - throw new GdxRuntimeException("Can only cope with FloatBuffer and ShortBuffer at the moment"); + ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray(data); + gl.bufferData(target, typedArray, usage); } } @Override public void glBufferSubData(int target, int offset, int size, Buffer data) { - if(data instanceof FloatBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((FloatBuffer)data); - gl.bufferSubData(target, offset, typedArray); - } - else if(data instanceof IntBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((IntBuffer)data); - gl.bufferSubData(target, offset, typedArray); - } - else if(data instanceof ShortBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((ShortBuffer)data); - gl.bufferSubData(target, offset, typedArray); - } - else if(data instanceof ByteBuffer) { - ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray((ByteBuffer)data); - gl.bufferSubData(target, offset, typedArray); - } - else { - throw new GdxRuntimeException("Can only cope with FloatBuffer and ShortBuffer at the moment"); - } + ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray(data); + gl.bufferSubData(target, offset, typedArray); } @Override @@ -797,29 +755,10 @@ public void glReadPixels(int x, int y, int width, int height, int format, int ty throw new GdxRuntimeException( "Only format RGBA and type UNSIGNED_BYTE are currently supported for glReadPixels(...). Create an issue when you need other formats."); } - ArrayBufferViewWrapper typedArray = null; - if(pixels instanceof ByteBuffer) { - typedArray = TypedArrays.getTypedArray((ByteBuffer)pixels); - } - else if(pixels instanceof FloatBuffer) { - typedArray = TypedArrays.getTypedArray((FloatBuffer)pixels); - } - else if(pixels instanceof ShortBuffer) { - typedArray = TypedArrays.getTypedArray((ShortBuffer)pixels); - } - else if(pixels instanceof IntBuffer) { - typedArray = TypedArrays.getTypedArray((IntBuffer)pixels); - } - else { - throw new GdxRuntimeException("Inputed pixels buffer not supported."); - } - - // create new ArrayBufferView (4 bytes per pixel) + ArrayBufferViewWrapper typedArray = TypedArrays.getTypedArray(pixels); int size = 4 * width * height; Uint8ArrayWrapper buffer = TypedArrays.createUint8Array(typedArray.getBuffer(), typedArray.getByteOffset(), size); - gl.readPixels(x, y, width, height, format, type, buffer); - pixels.limit(size); } @@ -961,7 +900,7 @@ public void glUniform1f(int location, float x) { @Override public void glUniform1fv(int location, int count, FloatBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform1fv(loc, copy(v)); + gl.uniform1fv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -979,7 +918,7 @@ public void glUniform1i(int location, int x) { @Override public void glUniform1iv(int location, int count, IntBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform1iv(loc, copy(v)); + gl.uniform1iv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -997,7 +936,7 @@ public void glUniform2f(int location, float x, float y) { @Override public void glUniform2fv(int location, int count, FloatBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform2fv(loc, copy(v)); + gl.uniform2fv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -1015,7 +954,7 @@ public void glUniform2i(int location, int x, int y) { @Override public void glUniform2iv(int location, int count, IntBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform2iv(loc, copy(v)); + gl.uniform2iv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -1033,7 +972,7 @@ public void glUniform3f(int location, float x, float y, float z) { @Override public void glUniform3fv(int location, int count, FloatBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform3fv(loc, copy(v)); + gl.uniform3fv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -1051,7 +990,7 @@ public void glUniform3i(int location, int x, int y, int z) { @Override public void glUniform3iv(int location, int count, IntBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform3iv(loc, copy(v)); + gl.uniform3iv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -1069,7 +1008,7 @@ public void glUniform4f(int location, float x, float y, float z, float w) { @Override public void glUniform4fv(int location, int count, FloatBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform4fv(loc, copy(v)); + gl.uniform4fv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -1087,7 +1026,7 @@ public void glUniform4i(int location, int x, int y, int z, int w) { @Override public void glUniform4iv(int location, int count, IntBuffer v) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniform4iv(loc, copy(v)); + gl.uniform4iv(loc, TypedArrays.getTypedArray(v)); } @Override @@ -1099,7 +1038,7 @@ public void glUniform4iv(int location, int count, int[] v, int offset) { @Override public void glUniformMatrix2fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix2fv(loc, transpose, copy(value)); + gl.uniformMatrix2fv(loc, transpose, TypedArrays.getTypedArray(value)); } @Override @@ -1111,7 +1050,7 @@ public void glUniformMatrix2fv(int location, int count, boolean transpose, float @Override public void glUniformMatrix3fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix3fv(loc, transpose, copy(value)); + gl.uniformMatrix3fv(loc, transpose, TypedArrays.getTypedArray(value)); } @Override @@ -1123,7 +1062,7 @@ public void glUniformMatrix3fv(int location, int count, boolean transpose, float @Override public void glUniformMatrix4fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix4fv(loc, transpose, copy(value)); + gl.uniformMatrix4fv(loc, transpose, TypedArrays.getTypedArray(value)); } @Override @@ -1150,7 +1089,7 @@ public void glVertexAttrib1f(int indx, float x) { @Override public void glVertexAttrib1fv(int indx, FloatBuffer values) { - gl.vertexAttrib1fv(indx, copy(values)); + gl.vertexAttrib1fv(indx, TypedArrays.getTypedArray(values)); } @Override @@ -1160,7 +1099,7 @@ public void glVertexAttrib2f(int indx, float x, float y) { @Override public void glVertexAttrib2fv(int indx, FloatBuffer values) { - gl.vertexAttrib2fv(indx, copy(values)); + gl.vertexAttrib2fv(indx, TypedArrays.getTypedArray(values)); } @Override @@ -1170,7 +1109,7 @@ public void glVertexAttrib3f(int indx, float x, float y, float z) { @Override public void glVertexAttrib3fv(int indx, FloatBuffer values) { - gl.vertexAttrib3fv(indx, copy(values)); + gl.vertexAttrib3fv(indx, TypedArrays.getTypedArray(values)); } @Override @@ -1180,7 +1119,7 @@ public void glVertexAttrib4f(int indx, float x, float y, float z, float w) { @Override public void glVertexAttrib4fv(int indx, FloatBuffer values) { - gl.vertexAttrib4fv(indx, copy(values)); + gl.vertexAttrib4fv(indx, TypedArrays.getTypedArray(values)); } @Override diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java index 58cc0061..453690dd 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java @@ -111,17 +111,17 @@ public void glClearBufferfi(int buffer, int drawbuffer, float depth, int stencil @Override public void glClearBufferfv(int buffer, int drawbuffer, FloatBuffer value) { - gl.clearBufferfv(buffer, drawbuffer, copy(value)); + gl.clearBufferfv(buffer, drawbuffer, TypedArrays.getTypedArray(value)); } @Override public void glClearBufferiv(int buffer, int drawbuffer, IntBuffer value) { - gl.clearBufferiv(buffer, drawbuffer, copy(value)); + gl.clearBufferiv(buffer, drawbuffer, TypedArrays.getTypedArray(value)); } @Override public void glClearBufferuiv(int buffer, int drawbuffer, IntBuffer value) { - gl.clearBufferuiv(buffer, drawbuffer, copy(value)); + gl.clearBufferuiv(buffer, drawbuffer, TypedArrays.getTypedArray(value)); } @Override @@ -230,7 +230,7 @@ public void glDrawArraysInstanced(int mode, int first, int count, int instanceCo @Override public void glDrawBuffers(int n, IntBuffer bufs) { int startPosition = bufs.position(); - gl.drawBuffers(copy((IntBuffer)bufs).subarray(0, n)); + gl.drawBuffers(TypedArrays.getTypedArray(bufs).subarray(0, n)); bufs.position(startPosition); } @@ -389,15 +389,15 @@ public void glGetActiveUniformBlockName(int program, int uniformBlockIndex, Buff } @Override - public void glGetActiveUniformsiv(int program, int uniformCount, IntBuffer uniformIndices, int pname, IntBuffer params) { + public void glGetActiveUniformsiv(int program, int uniformCount, IntBuffer uniformIndices, int pname, IntBuffer params) { if(pname == GL30.GL_UNIFORM_IS_ROW_MAJOR) { - JSArray arr = gl.getActiveUniformsb(programs.get(program), copy(uniformIndices).subarray(0, uniformCount), pname); + JSArray arr = gl.getActiveUniformsb(programs.get(program), TypedArrays.getTypedArray(uniformIndices).subarray(0, uniformCount), pname); for(int i = 0; i < uniformCount; i++) { params.put(i, arr.get(i) ? GL20.GL_TRUE : GL20.GL_FALSE); } } else { - JSArray arr = gl.getActiveUniformsi(programs.get(program), copy(uniformIndices).subarray(0, uniformCount), pname); + JSArray arr = gl.getActiveUniformsi(programs.get(program), TypedArrays.getTypedArray(uniformIndices).subarray(0, uniformCount), pname); for(int i = 0; i < uniformCount; i++) { params.put(i, arr.get(i)); } @@ -638,7 +638,7 @@ public void glGetVertexAttribIuiv(int index, int pname, IntBuffer params) { @Override public void glInvalidateFramebuffer(int target, int numAttachments, IntBuffer attachments) { int startPosition = attachments.position(); - gl.invalidateFramebuffer(target, copy((IntBuffer)attachments).subarray(0, numAttachments)); + gl.invalidateFramebuffer(target, TypedArrays.getTypedArray(attachments).subarray(0, numAttachments)); attachments.position(startPosition); } @@ -646,7 +646,7 @@ public void glInvalidateFramebuffer(int target, int numAttachments, IntBuffer at public void glInvalidateSubFramebuffer(int target, int numAttachments, IntBuffer attachments, int x, int y, int width, int height) { int startPosition = attachments.position(); - gl.invalidateSubFramebuffer(target, copy((IntBuffer)attachments).subarray(0, numAttachments), x, y, width, height); + gl.invalidateSubFramebuffer(target, TypedArrays.getTypedArray(attachments).subarray(0, numAttachments), x, y, width, height); attachments.position(startPosition); } @@ -817,37 +817,37 @@ public void glUniformBlockBinding(int program, int uniformBlockIndex, int unifor @Override public void glUniformMatrix2x3fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix2x3fv(loc, transpose, copy(value)); + gl.uniformMatrix2x3fv(loc, transpose, TypedArrays.getTypedArray(value)); } @Override public void glUniformMatrix2x4fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix2x4fv(loc, transpose, copy(value), 0, count); + gl.uniformMatrix2x4fv(loc, transpose, TypedArrays.getTypedArray(value), 0, count); } @Override public void glUniformMatrix3x2fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix3x2fv(loc, transpose, copy(value), 0, count); + gl.uniformMatrix3x2fv(loc, transpose, TypedArrays.getTypedArray(value), 0, count); } @Override public void glUniformMatrix3x4fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix3x4fv(loc, transpose, copy(value), 0, count); + gl.uniformMatrix3x4fv(loc, transpose, TypedArrays.getTypedArray(value), 0, count); } @Override public void glUniformMatrix4x2fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix4x2fv(loc, transpose, copy(value), 0, count); + gl.uniformMatrix4x2fv(loc, transpose, TypedArrays.getTypedArray(value), 0, count); } @Override public void glUniformMatrix4x3fv(int location, int count, boolean transpose, FloatBuffer value) { WebGLUniformLocationWrapper loc = getUniformLocation(location); - gl.uniformMatrix4x3fv(loc, transpose, copy(value), 0, count); + gl.uniformMatrix4x3fv(loc, transpose, TypedArrays.getTypedArray(value), 0, count); } @Override diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java index 105d90d3..efa67b8f 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java @@ -1,5 +1,6 @@ package com.github.xpenatan.gdx.backends.teavm.dom.typedarray; +import com.badlogic.gdx.utils.GdxRuntimeException; import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.FloatBuffer; @@ -163,70 +164,68 @@ public static byte[] toByteArray(ArrayBufferViewWrapper array) { return byteArray; } - public static ArrayBufferViewWrapper getTypedArray(ByteBuffer buffer) { - ArrayBufferViewWrapper bufferView = getInt8Array(buffer); - if(bufferView != null) { - return bufferView; + public static ArrayBufferViewWrapper getTypedArray(Buffer buffer) { + if(buffer instanceof HasArrayBufferView) { + return ((HasArrayBufferView)buffer).getArrayBufferView(); + } + else { + throw new GdxRuntimeException("Buffer should have ArrayBufferView interface"); } - byte[] array = buffer.array(); - ArrayBufferViewWrapper arrayBuffer = getTypedArray(array); - return arrayBuffer; } - public static ArrayBufferViewWrapper getTypedArray(ShortBuffer buffer) { - ArrayBufferViewWrapper bufferView = getInt8Array(buffer); - if(bufferView != null) { - return bufferView; + public static Int8ArrayWrapper getTypedArray(ByteBuffer buffer) { + if(buffer instanceof HasArrayBufferView) { + return (Int8ArrayWrapper)((HasArrayBufferView)buffer).getArrayBufferView(); + } + else { + throw new GdxRuntimeException("Buffer should have ArrayBufferView interface"); } - short[] array = buffer.array(); - ArrayBufferViewWrapper arrayBuffer = getTypedArray(array); - return arrayBuffer; } - public static ArrayBufferViewWrapper getTypedArray(IntBuffer buffer) { - ArrayBufferViewWrapper bufferView = getInt8Array(buffer); - if(bufferView != null) { - return bufferView; + public static Int16ArrayWrapper getTypedArray(ShortBuffer buffer) { + if(buffer instanceof HasArrayBufferView) { + return (Int16ArrayWrapper)((HasArrayBufferView)buffer).getArrayBufferView(); + } + else { + throw new GdxRuntimeException("Buffer should have ArrayBufferView interface"); } - int[] array = buffer.array(); - ArrayBufferViewWrapper arrayBuffer = getTypedArray(array); - return arrayBuffer; } - public static ArrayBufferViewWrapper getTypedArray(FloatBuffer buffer) { - ArrayBufferViewWrapper bufferView = getInt8Array(buffer); - if(bufferView != null) { - return bufferView; + public static Int32ArrayWrapper getTypedArray(IntBuffer buffer) { + if(buffer instanceof HasArrayBufferView) { + return (Int32ArrayWrapper)((HasArrayBufferView)buffer).getArrayBufferView(); + } + else { + throw new GdxRuntimeException("Buffer should have ArrayBufferView interface"); + } + } + + public static Float32ArrayWrapper getTypedArray(FloatBuffer buffer) { + if(buffer instanceof HasArrayBufferView) { + return (Float32ArrayWrapper)((HasArrayBufferView)buffer).getArrayBufferView(); + } + else { + throw new GdxRuntimeException("Buffer should have ArrayBufferView interface"); } - float[] array = buffer.array(); - ArrayBufferViewWrapper arrayBuffer = getTypedArray(array); - return arrayBuffer; } @JSBody(params = {"buffer"}, script = "" + "return buffer;") - public static native ArrayBufferViewWrapper getTypedArray(@JSByRef() byte[] buffer); + public static native Int8ArrayWrapper getTypedArray(@JSByRef() byte[] buffer); @JSBody(params = {"buffer"}, script = "" + "return buffer;") - private static native ArrayBufferViewWrapper getTypedArray(@JSByRef() int[] buffer); + public static native Int32ArrayWrapper getTypedArray(@JSByRef() int[] buffer); @JSBody(params = {"buffer"}, script = "" + "return buffer;") - private static native ArrayBufferViewWrapper getTypedArray(@JSByRef() float[] buffer); + public static native Int16ArrayWrapper getTypedArray(@JSByRef() short[] buffer); @JSBody(params = {"buffer"}, script = "" + "return buffer;") - private static native ArrayBufferViewWrapper getTypedArray(@JSByRef() short[] buffer); + public static native Float32ArrayWrapper getTypedArray(@JSByRef() float[] buffer); @org.teavm.jso.JSBody(params = {"array"}, script = "" + "return array.data;") public static native Int8ArrayWrapper getArrayBufferView(JSObject array); - - public static Int8ArrayWrapper getInt8Array(Buffer buffer) { - if(buffer instanceof HasArrayBufferView) { - return ((HasArrayBufferView)buffer).getTypedArray(); - } - return null; - } } From dde6cae48a4a1b310ad2b01cf0dca2250bb25649 Mon Sep 17 00:00:00 2001 From: Natan Date: Sat, 13 Jul 2024 17:40:27 -0300 Subject: [PATCH 6/8] Improve texture opengl method --- .../xpenatan/gdx/backends/teavm/TeaGL20.java | 57 +++++++------------ .../xpenatan/gdx/backends/teavm/TeaGL30.java | 48 ++++++---------- .../teavm/dom/typedarray/TypedArrays.java | 6 ++ .../dom/typedarray/Uint16ArrayWrapper.java | 31 ++++++++++ .../gl/WebGLRenderingContextWrapper.java | 4 -- 5 files changed, 73 insertions(+), 73 deletions(-) create mode 100644 backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/Uint16ArrayWrapper.java diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java index 6be6eea3..1656d2ab 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL20.java @@ -3,7 +3,6 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.utils.GdxRuntimeException; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferViewWrapper; -import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Float32ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int32ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Uint8ArrayWrapper; @@ -18,14 +17,12 @@ import com.github.xpenatan.gdx.backends.teavm.gl.WebGLTextureWrapper; import com.github.xpenatan.gdx.backends.teavm.gl.WebGLUniformLocationWrapper; import java.nio.Buffer; -import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; -import java.nio.ShortBuffer; import org.teavm.jso.JSBody; import org.teavm.jso.JSClass; import org.teavm.jso.JSObject; -import org.teavm.jso.typedarrays.Uint8Array; +import org.teavm.jso.webgl.WebGLRenderingContext; /** * Port from GWT gdx 1.12.0 @@ -69,7 +66,7 @@ static class CustomIntMap implements JSObject { public TeaGL20(WebGLRenderingContextWrapper gl) { this.gl = gl; - this.gl.pixelStorei(WebGLRenderingContextWrapper.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0); + this.gl.pixelStorei(WebGLRenderingContext.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0); } protected WebGLUniformLocationWrapper getUniformLocation(int location) { @@ -751,7 +748,7 @@ public void glPolygonOffset(float factor, float units) { @Override public void glReadPixels(int x, int y, int width, int height, int format, int type, Buffer pixels) { // verify request - if((format != WebGLRenderingContextWrapper.RGBA) || (type != WebGLRenderingContextWrapper.UNSIGNED_BYTE)) { + if((format != WebGLRenderingContext.RGBA) || (type != WebGLRenderingContext.UNSIGNED_BYTE)) { throw new GdxRuntimeException( "Only format RGBA and type UNSIGNED_BYTE are currently supported for glReadPixels(...). Create an issue when you need other formats."); } @@ -829,46 +826,30 @@ public void glTexImage2D(int target, int level, int internalformat, int width, i return; } - ArrayBufferViewWrapper buffer; - if(pixels instanceof ByteBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((ByteBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createUint8Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); - gl.texImage2D(target, level, internalformat, width, height, border, format, type, buffer); + ArrayBufferViewWrapper arrayBuffer = TypedArrays.getTypedArray(pixels); + if(type == WebGLRenderingContext.UNSIGNED_BYTE) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint8Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } - else if(pixels instanceof FloatBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((FloatBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createFloat32Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); - gl.texImage2D(target, level, internalformat, width, height, border, format, type, buffer); - } - else { - throw new GdxRuntimeException("Not supported buffer"); + else if(type == WebGLRenderingContext.UNSIGNED_SHORT || type == GL_UNSIGNED_SHORT_5_6_5 || type == GL_UNSIGNED_SHORT_4_4_4_4) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint16Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } + gl.texImage2D(target, level, internalformat, width, height, border, format, type, arrayBuffer); } @Override public void glTexSubImage2D(int target, int level, int xoffset, int yoffset, int width, int height, int format, int type, Buffer pixels) { - ArrayBufferViewWrapper buffer; - if(pixels instanceof ByteBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((ByteBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createUint8Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); - gl.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, buffer); + ArrayBufferViewWrapper arrayBuffer = TypedArrays.getTypedArray(pixels); + if(type == WebGLRenderingContext.UNSIGNED_BYTE) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint8Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } - else if(pixels instanceof FloatBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((FloatBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createFloat32Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); - gl.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, buffer); - } - else { - throw new GdxRuntimeException("Not supported buffer"); + else if(type == WebGLRenderingContext.UNSIGNED_SHORT || type == GL_UNSIGNED_SHORT_5_6_5 || type == GL_UNSIGNED_SHORT_4_4_4_4) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint16Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } + gl.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, arrayBuffer); } @Override diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java index 453690dd..05a75fa4 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/TeaGL30.java @@ -16,11 +16,11 @@ import com.github.xpenatan.gdx.backends.teavm.gl.WebGLUniformLocationWrapper; import com.github.xpenatan.gdx.backends.teavm.gl.WebGLVertexArrayObjectWrapper; import java.nio.Buffer; -import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.LongBuffer; import org.teavm.jso.core.JSArray; +import org.teavm.jso.webgl.WebGLRenderingContext; /** * Port from GWT gdx 1.12.0 @@ -729,44 +729,30 @@ public void glTexImage3D(int target, int level, int internalformat, int width, i return; } - ArrayBufferViewWrapper buffer; - if(pixels instanceof ByteBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((ByteBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createUint8Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); + ArrayBufferViewWrapper arrayBuffer = TypedArrays.getTypedArray(pixels); + if(type == WebGLRenderingContext.UNSIGNED_BYTE) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint8Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } - else if(pixels instanceof FloatBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((FloatBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createFloat32Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); + else if(type == WebGLRenderingContext.UNSIGNED_SHORT || type == GL_UNSIGNED_SHORT_5_6_5 || type == GL_UNSIGNED_SHORT_4_4_4_4) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint16Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } - else { - throw new GdxRuntimeException("Not supported buffer"); - } - gl.texImage3D(target, level, internalformat, width, height, depth, border, format, type, buffer); + gl.texImage3D(target, level, internalformat, width, height, depth, border, format, type, arrayBuffer); } @Override public void glTexSubImage3D(int target, int level, int xoffset, int yoffset, int zoffset, int width, int height, int depth, int format, int type, Buffer pixels) { - ArrayBufferViewWrapper buffer; - if(pixels instanceof ByteBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((ByteBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createUint8Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); - } - else if(pixels instanceof FloatBuffer) { - ArrayBufferViewWrapper typedArrayBuffer = TypedArrays.getTypedArray((FloatBuffer)pixels); - int remainingBytes = pixels.remaining(); - int byteOffset = typedArrayBuffer.getByteOffset() + pixels.position(); - buffer = TypedArrays.createFloat32Array(typedArrayBuffer.getBuffer(), byteOffset, remainingBytes); + ArrayBufferViewWrapper arrayBuffer = TypedArrays.getTypedArray(pixels); + if(type == WebGLRenderingContext.UNSIGNED_BYTE) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint8Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } - else { - throw new GdxRuntimeException("Not supported buffer"); + else if(type == WebGLRenderingContext.UNSIGNED_SHORT || type == GL_UNSIGNED_SHORT_5_6_5 || type == GL_UNSIGNED_SHORT_4_4_4_4) { + int byteOffset = arrayBuffer.getByteOffset() + pixels.position(); + arrayBuffer = TypedArrays.createUint16Array(arrayBuffer.getBuffer(), byteOffset, pixels.remaining()); } - gl.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, buffer); + gl.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, arrayBuffer); } @Override diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java index efa67b8f..7c2e3281 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/TypedArrays.java @@ -15,6 +15,7 @@ import org.teavm.jso.typedarrays.Int16Array; import org.teavm.jso.typedarrays.Int32Array; import org.teavm.jso.typedarrays.Int8Array; +import org.teavm.jso.typedarrays.Uint16Array; import org.teavm.jso.typedarrays.Uint8Array; import org.teavm.jso.typedarrays.Uint8ClampedArray; @@ -145,6 +146,11 @@ public static Uint8ArrayWrapper createUint8Array(ArrayBufferWrapper buffer, int return (Uint8ArrayWrapper)Uint8Array.create(arrayBuffer, offset, length); } + public static Uint16ArrayWrapper createUint16Array(ArrayBufferWrapper buffer, int offset, int length) { + ArrayBuffer arrayBuffer = (ArrayBuffer)buffer; + return (Uint16ArrayWrapper)Uint16Array.create(arrayBuffer, offset, length); + } + public static Uint32ArrayWrapper createUint32Array(int length) { return Uint32ArrayWrapper.create(length); } diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/Uint16ArrayWrapper.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/Uint16ArrayWrapper.java new file mode 100644 index 00000000..7e80a673 --- /dev/null +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/dom/typedarray/Uint16ArrayWrapper.java @@ -0,0 +1,31 @@ +package com.github.xpenatan.gdx.backends.teavm.dom.typedarray; + +import org.teavm.jso.JSIndexer; +import org.teavm.jso.JSProperty; + +/** + * @author xpenatan + */ +public interface Uint16ArrayWrapper extends ArrayBufferViewWrapper { + // Uint8Array + static final int BYTES_PER_ELEMENT = 2; + + @JSProperty + int getLength(); + + @JSIndexer + byte get(int index); + + @JSIndexer + void set(int index, byte value); + + void set(Uint16ArrayWrapper array); + + void set(Uint16ArrayWrapper array, int offset); + +// void set(OctetArray array); + +// void set(OctetArray array, int offset); + + Uint16ArrayWrapper subarray(int start, int end); +} \ No newline at end of file diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/gl/WebGLRenderingContextWrapper.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/gl/WebGLRenderingContextWrapper.java index a9f57f9c..54778052 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/gl/WebGLRenderingContextWrapper.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/gl/WebGLRenderingContextWrapper.java @@ -19,10 +19,6 @@ * @author xpenatan */ public interface WebGLRenderingContextWrapper extends JSObject { - final int UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241; - final int RGBA = 0x1908; - final int UNSIGNED_BYTE = 0x1401; - int getDrawingBufferWidth(); int getDrawingBufferHeight(); From f30b594f4474513ac059898714cec407cdf1ab60 Mon Sep 17 00:00:00 2001 From: Natan Date: Sat, 13 Jul 2024 18:24:52 -0300 Subject: [PATCH 7/8] bump version and update changelogs --- CHANGES.md | 6 ++++++ gradle.properties | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 467d969e..8454bfa4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,11 @@ [-SNAPSHOT] +[1.0.2] +- Small file storage improvement +- Use file type for checking if asset is loaded +- Improve arraybuffer performance +- Improve texture opengl method + [1.0.1] - Fix an issue when creating database and trying to load assets. - Quick fix to filter out assets.txt file and to delete it before adding new files diff --git a/gradle.properties b/gradle.properties index 60b5940d..6a641b72 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version=1.0.1 +version=1.0.2 gdxSourcePath = E:/Dev/Projects/java/libgdx teavmPath = E:/Dev/Projects/java/teavm includeLibgdxSource = false From b5b0a7f2177786623db93a94e5e6e7adfe9f37f3 Mon Sep 17 00:00:00 2001 From: Natan Date: Sat, 13 Jul 2024 20:11:14 -0300 Subject: [PATCH 8/8] Fix sync loading --- CHANGES.md | 1 + .../teavm/assetloader/AssetDownloadImpl.java | 123 +++++++++++------- 2 files changed, 78 insertions(+), 46 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8454bfa4..4d940e7d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ - Use file type for checking if asset is loaded - Improve arraybuffer performance - Improve texture opengl method +- Fix sync loading (FreeType) [1.0.1] - Fix an issue when creating database and trying to load assets. diff --git a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/assetloader/AssetDownloadImpl.java b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/assetloader/AssetDownloadImpl.java index 81bf69ee..b2510c03 100644 --- a/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/assetloader/AssetDownloadImpl.java +++ b/backends/backend-teavm/src/main/java/com/github/xpenatan/gdx/backends/teavm/assetloader/AssetDownloadImpl.java @@ -7,6 +7,8 @@ import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.ArrayBufferWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.Int8ArrayWrapper; import com.github.xpenatan.gdx.backends.teavm.dom.typedarray.TypedArrays; +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; import org.teavm.jso.ajax.XMLHttpRequest; import org.teavm.jso.browser.Window; import org.teavm.jso.dom.html.HTMLDocument; @@ -95,7 +97,7 @@ public void onFailure(String url) { listener.onFailure(url); } } - }, 0, false); + }, 0, showLogs); } private void loadBinary(boolean async, final String url, final AssetLoaderListener listener, int count, boolean showLogs) { @@ -111,58 +113,81 @@ private void loadBinary(boolean async, final String url, final AssetLoaderListen // don't load on main thread addQueue(); - new Thread() { - public void run() { - XMLHttpRequest request = new XMLHttpRequest(); - request.setOnReadyStateChange(evt -> { - if(request.getReadyState() == XMLHttpRequest.DONE) { - int status = request.getStatus(); - if(status == 0) { - if(listener != null) { - listener.onFailure(url); - } + if(async) { + new Thread() { + public void run() { + loadBinaryInternally(true, url, listener, count, showLogs); + } + }.start(); + } + else { + loadBinaryInternally(false, url, listener, count, showLogs); + } + } + + private void loadBinaryInternally(boolean async, final String url, final AssetLoaderListener listener, int count, boolean showLogs) { + XMLHttpRequest request = new XMLHttpRequest(); + request.setOnReadyStateChange(evt -> { + if(request.getReadyState() == XMLHttpRequest.DONE) { + int status = request.getStatus(); + if(status == 0) { + if(listener != null) { + listener.onFailure(url); + } + } + else if(status != 200) { + if ((status != 404) && (status != 403)) { + // re-try: e.g. failure due to ERR_HTTP2_SERVER_REFUSED_STREAM (too many requests) + try { + Thread.sleep(100); } - else if(status != 200) { - if ((status != 404) && (status != 403)) { - // re-try: e.g. failure due to ERR_HTTP2_SERVER_REFUSED_STREAM (too many requests) - try { - Thread.sleep(100); - } - catch (Throwable e) { - // ignored - } - int newCount = count + 1; - loadBinary(async, url, listener, newCount, showLogs); - } - else { - if(listener != null) { - listener.onFailure(url); - } - } + catch (Throwable e) { + // ignored } - else { - if(showLogs) { - System.out.println("Asset loaded: " + url); - } - - ArrayBufferWrapper response = (ArrayBufferWrapper)request.getResponse(); - Int8Array data = (Int8Array)TypedArrays.createInt8Array(response); - if(listener != null) { - listener.onSuccess(url, new Blob((ArrayBuffer)response, data)); - } + int newCount = count + 1; + loadBinary(async, url, listener, newCount, showLogs); + } + else { + if(listener != null) { + listener.onFailure(url); } - subtractQueue(); } - }); + } + else { + if(showLogs) { + System.out.println("Asset loaded: " + url); + } + JSObject jsResponse = request.getResponse(); + + Int8Array data = null; + ArrayBuffer arrayBuffer = null; + if(isString(jsResponse)) { + // sync downloading is always string + String responseStr = toString(jsResponse); + Int8ArrayWrapper typedArray = TypedArrays.getTypedArray(responseStr.getBytes()); + data = (Int8Array)typedArray; + arrayBuffer = data.getBuffer(); + } + else { + ArrayBufferWrapper response = (ArrayBufferWrapper)jsResponse; + data = (Int8Array)TypedArrays.createInt8Array(response); + arrayBuffer = (ArrayBuffer)response; + } - setOnProgress(request, url, listener); - request.open("GET", url, async); - if(async) { - request.setResponseType("arraybuffer"); + if(listener != null) { + listener.onSuccess(url, new Blob(arrayBuffer, data)); + } } - request.send(); + subtractQueue(); } - }.start(); + }); + + setOnProgress(request, url, listener); + request.open("GET", url, async); + if(async) { + request.setResponseType("arraybuffer"); + } + request.send(); } private void setOnProgress(XMLHttpRequest req, String url, final AssetLoaderListener listener) { @@ -178,4 +203,10 @@ private void setOnProgress(XMLHttpRequest req, String url, final AssetLoaderList } }); } + + @JSBody(params = "jsObject", script = "return typeof jsObject == 'string';") + private static native boolean isString(JSObject jsObject); + + @JSBody(params = "jsObject", script = "return jsObject;") + private static native String toString(JSObject jsObject); } \ No newline at end of file