diff --git a/android/filament-android/src/main/java/com/google/android/filament/IndirectLight.java b/android/filament-android/src/main/java/com/google/android/filament/IndirectLight.java index 4e3d0466a1..cee8ec9d4e 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/IndirectLight.java +++ b/android/filament-android/src/main/java/com/google/android/filament/IndirectLight.java @@ -86,8 +86,7 @@ import com.google.android.filament.proguard.UsedByReflection; public class IndirectLight { long mNativeObject; - @UsedByReflection("KtxLoader.java") - IndirectLight(long indirectLight) { + public IndirectLight(long indirectLight) { mNativeObject = indirectLight; } diff --git a/android/filament-android/src/main/java/com/google/android/filament/Skybox.java b/android/filament-android/src/main/java/com/google/android/filament/Skybox.java index e6c757d6f1..d2753869f0 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/Skybox.java +++ b/android/filament-android/src/main/java/com/google/android/filament/Skybox.java @@ -49,8 +49,7 @@ import com.google.android.filament.proguard.UsedByReflection; public class Skybox { private long mNativeObject; - @UsedByReflection("KtxLoader.java") - Skybox(long nativeSkybox) { + public Skybox(long nativeSkybox) { mNativeObject = nativeSkybox; } diff --git a/android/filament-android/src/main/java/com/google/android/filament/Texture.java b/android/filament-android/src/main/java/com/google/android/filament/Texture.java index d74949d3b8..14df780926 100644 --- a/android/filament-android/src/main/java/com/google/android/filament/Texture.java +++ b/android/filament-android/src/main/java/com/google/android/filament/Texture.java @@ -73,8 +73,7 @@ import static com.google.android.filament.Texture.Type.COMPRESSED; public class Texture { private long mNativeObject; - @UsedByReflection("KtxLoader.java") - Texture(long nativeTexture) { + public Texture(long nativeTexture) { mNativeObject = nativeTexture; } diff --git a/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KtxLoader.java b/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KtxLoader.java deleted file mode 100644 index e52a1434c8..0000000000 --- a/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KtxLoader.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.filament.utils; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.google.android.filament.Engine; -import com.google.android.filament.IndirectLight; -import com.google.android.filament.Skybox; -import com.google.android.filament.Texture; - -import java.lang.reflect.Constructor; -import java.nio.Buffer; - -/** - * Utilities for consuming KTX files and producing Filament textures, IBLs, and sky boxes. - * - *

KTX is a simple container format that makes it easy to bundle miplevels and cubemap faces - * into a single file.

- */ -public class KtxLoader { - private static Constructor sTextureConstructor; - private static Constructor sIndirectLightConstructor; - private static Constructor sSkyboxConstructor; - - static { - try { - sTextureConstructor = Texture.class.getDeclaredConstructor(long.class); - sTextureConstructor.setAccessible(true); - - sIndirectLightConstructor = IndirectLight.class.getDeclaredConstructor(long.class); - sIndirectLightConstructor.setAccessible(true); - - sSkyboxConstructor = Skybox.class.getDeclaredConstructor(long.class); - sSkyboxConstructor.setAccessible(true); - } catch (NoSuchMethodException e) { - // Cannot happen - } - } - - public static class Options { - public boolean srgb; - } - - /** - * Consumes the content of a KTX file and produces a {@link Texture} object. - * - * @param engine Gets passed to the builder. - * @param buffer The content of the KTX File. - * @param options Loader options. - * @return The resulting Filament texture, or null on failure. - */ - @Nullable - public static Texture createTexture(@NonNull Engine engine, @NonNull Buffer buffer, @NonNull Options options) { - try { - long nativeEngine = engine.getNativeObject(); - long nativeTexture = nCreateTexture(nativeEngine, buffer, buffer.remaining(), options.srgb); - return sTextureConstructor.newInstance(nativeTexture); - } catch (Exception e) { - return null; - } - } - - /** - * Consumes the content of a KTX file and produces an {@link IndirectLight} object. - * - * @param engine Gets passed to the builder. - * @param buffer The content of the KTX File. - * @param options Loader options. - * @return The resulting Filament texture, or null on failure. - */ - @Nullable - public static IndirectLight createIndirectLight(@NonNull Engine engine, @NonNull Buffer buffer, @NonNull Options options) { - try { - long nativeEngine = engine.getNativeObject(); - long nativeIndirectLight = nCreateIndirectLight(nativeEngine, buffer, buffer.remaining(), options.srgb); - return sIndirectLightConstructor.newInstance(nativeIndirectLight); - } catch (Exception e) { - return null; - } - } - - /** - * Consumes the content of a KTX file and produces a {@link Skybox} object. - * - * @param engine Gets passed to the builder. - * @param buffer The content of the KTX File. - * @param options Loader options. - * @return The resulting Filament texture, or null on failure. - */ - @Nullable - public static Skybox createSkybox(@NonNull Engine engine, @NonNull Buffer buffer, @NonNull Options options) { - try { - long nativeEngine = engine.getNativeObject(); - long nativeSkybox = nCreateSkybox(nativeEngine, buffer, buffer.remaining(), options.srgb); - return sSkyboxConstructor.newInstance(nativeSkybox); - } catch (Exception e) { - return null; - } - } - - private static native long nCreateTexture(long nativeEngine, Buffer buffer, int remaining, boolean srgb); - private static native long nCreateIndirectLight(long nativeEngine, Buffer buffer, int remaining, boolean srgb); - private static native long nCreateSkybox(long nativeEngine, Buffer buffer, int remaining, boolean srgb); -} diff --git a/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KtxLoader.kt b/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KtxLoader.kt new file mode 100644 index 0000000000..3efd9e6fa3 --- /dev/null +++ b/android/filament-utils-android/src/main/java/com/google/android/filament/utils/KtxLoader.kt @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.filament.utils + +import com.google.android.filament.Engine +import com.google.android.filament.IndirectLight +import com.google.android.filament.Skybox +import com.google.android.filament.Texture + +import java.nio.Buffer + +/** + * Utilities for consuming KTX files and producing Filament textures, IBLs, and sky boxes. + * + * KTX is a simple container format that makes it easy to bundle miplevels and cubemap faces + * into a single file. + */ +object KtxLoader { + class Options { + var srgb = false + } + + /** + * Consumes the content of a KTX file and produces a [Texture] object. + * + * @param engine Gets passed to the builder. + * @param buffer The content of the KTX File. + * @param options Loader options. + * @return The resulting Filament texture, or null on failure. + */ + fun createTexture(engine: Engine, buffer: Buffer, options: Options): Texture { + val nativeEngine = engine.nativeObject + val nativeTexture = nCreateTexture(nativeEngine, buffer, buffer.remaining(), options.srgb) + return Texture(nativeTexture) + } + + /** + * Consumes the content of a KTX file and produces an [IndirectLight] object. + * + * @param engine Gets passed to the builder. + * @param buffer The content of the KTX File. + * @param options Loader options. + * @return The resulting Filament texture, or null on failure. + */ + fun createIndirectLight(engine: Engine, buffer: Buffer, options: Options): IndirectLight { + val nativeEngine = engine.nativeObject + val nativeIndirectLight = nCreateIndirectLight(nativeEngine, buffer, buffer.remaining(), options.srgb) + return IndirectLight(nativeIndirectLight) + } + + /** + * Consumes the content of a KTX file and produces a [Skybox] object. + * + * @param engine Gets passed to the builder. + * @param buffer The content of the KTX File. + * @param options Loader options. + * @return The resulting Filament texture, or null on failure. + */ + fun createSkybox(engine: Engine, buffer: Buffer, options: Options): Skybox { + val nativeEngine = engine.nativeObject + val nativeSkybox = nCreateSkybox(nativeEngine, buffer, buffer.remaining(), options.srgb) + return Skybox(nativeSkybox) + } + + private external fun nCreateTexture(nativeEngine: Long, buffer: Buffer, remaining: Int, srgb: Boolean): Long + private external fun nCreateIndirectLight(nativeEngine: Long, buffer: Buffer, remaining: Int, srgb: Boolean): Long + private external fun nCreateSkybox(nativeEngine: Long, buffer: Buffer, remaining: Int, srgb: Boolean): Long +} \ No newline at end of file