From f7b617f48f9376df5d92396e283fa97dfb4667c9 Mon Sep 17 00:00:00 2001 From: Jim Blackler Date: Tue, 27 Aug 2024 11:45:51 +0000 Subject: [PATCH] Kotlin: Enable explicitApi mode for the library This requires that visibility, and types, are explicitly stated. Bug: 337195271 Change-Id: Ia4d62872a208b2b4a2a0721d121d173af3334fd4 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/203074 Reviewed-by: dan sinclair Reviewed-by: Alex Benton Commit-Queue: Jim Blackler --- generator/templates/art/api_kotlin_async_helpers.kt | 6 +++--- generator/templates/art/api_kotlin_constants.kt | 13 ++++++++----- generator/templates/art/api_kotlin_enum.kt | 11 ++++++----- .../templates/art/api_kotlin_function_pointer.kt | 4 ++-- generator/templates/art/api_kotlin_functions.kt | 2 +- generator/templates/art/api_kotlin_object.kt | 6 +++--- generator/templates/art/api_kotlin_structure.kt | 8 ++++---- tools/android/webgpu/build.gradle | 3 +++ .../main/java/android/dawn/helper/DawnException.kt | 2 +- .../src/main/java/android/dawn/helper/Rounding.kt | 10 +++++----- .../src/main/java/android/dawn/helper/Streams.kt | 2 +- .../src/main/java/android/dawn/helper/Textures.kt | 4 ++-- .../src/main/java/android/dawn/helper/Util.kt | 4 ++-- 13 files changed, 41 insertions(+), 34 deletions(-) diff --git a/generator/templates/art/api_kotlin_async_helpers.kt b/generator/templates/art/api_kotlin_async_helpers.kt index 537b0384818..afa35293345 100644 --- a/generator/templates/art/api_kotlin_async_helpers.kt +++ b/generator/templates/art/api_kotlin_async_helpers.kt @@ -36,7 +36,7 @@ import kotlin.coroutines.suspendCoroutine in by_category['function pointer'] if len(function_pointer.name.chunks) > 1 %} //* Function pointers generally end in Callback which we replace with Return. {% set return_name = function_pointer.name.chunks[:-1] | map('title') | join + 'Return' %} - data class {{ return_name }}( + public data class {{ return_name }}( {% for arg in kotlin_record_members(function_pointer.arguments) %} val {{ as_varName(arg.name) }}: {{ kotlin_declaration(arg) }}, {% endfor %}) @@ -48,10 +48,10 @@ import kotlin.coroutines.suspendCoroutine {% for method in obj.methods if is_async_method(method) %} {% set function_pointer = method.arguments[-2].type %} {% set return_name = function_pointer.name.chunks[:-1] | map('title') | join + 'Return' %} - suspend fun {{ obj.name.CamelCase() }}.{{ method.name.camelCase() }}( + public suspend fun {{ obj.name.CamelCase() }}.{{ method.name.camelCase() }}( {%- for arg in method.arguments[:-2] %} {{- as_varName(arg.name) }}: {{ kotlin_definition(arg) }}, - {%- endfor %}) = suspendCoroutine { + {%- endfor %}): {{ return_name }} = suspendCoroutine { {{ method.name.camelCase() }}( {%- for arg in method.arguments[:-2] %} {{- as_varName(arg.name) }}, diff --git a/generator/templates/art/api_kotlin_constants.kt b/generator/templates/art/api_kotlin_constants.kt index 2eed7fce570..8216097e391 100644 --- a/generator/templates/art/api_kotlin_constants.kt +++ b/generator/templates/art/api_kotlin_constants.kt @@ -24,13 +24,16 @@ //* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, //* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE //* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +{% from 'art/api_kotlin_types.kt' import kotlin_declaration with context %} package {{ kotlin_package }} -object Constants { - val UINT32_MAX = Integer.parseUnsignedInt("4294967295") - val UINT64_MAX = java.lang.Long.parseUnsignedLong("18446744073709551615") - val SIZE_MAX = UINT64_MAX +public object Constants { + private val UINT32_MAX = Integer.parseUnsignedInt("4294967295") + private val UINT64_MAX = java.lang.Long.parseUnsignedLong("18446744073709551615") + private val SIZE_MAX = UINT64_MAX + {% for constant in by_category['constant'] %} - val {{ as_ktName(constant.name.SNAKE_CASE() ) }} = {{ constant.value }} + public val {{ as_ktName(constant.name.SNAKE_CASE() ) }}:{{ ' ' }} + {{- kotlin_declaration(constant) }} = {{ constant.value }} {% endfor %} } diff --git a/generator/templates/art/api_kotlin_enum.kt b/generator/templates/art/api_kotlin_enum.kt index 4a9c3b8acd1..ccb1efba7ea 100644 --- a/generator/templates/art/api_kotlin_enum.kt +++ b/generator/templates/art/api_kotlin_enum.kt @@ -27,16 +27,17 @@ package {{ kotlin_package }} @JvmInline -value class {{ enum.name.CamelCase() }}(@get:JvmName("getValue") val v: Int) { +public value class {{ enum.name.CamelCase() }}(@get:JvmName("getValue") public val v: Int) { {% if enum.category == 'bitmask' %} - infix fun or(b: {{ enum.name.CamelCase() }}) = {{ enum.name.CamelCase() }}(this.v or b.v) + public infix fun or(b: {{ enum.name.CamelCase() }}): {{ enum.name.CamelCase() }} ={{ ' ' }} + {{- enum.name.CamelCase() }}(this.v or b.v) {% endif %} - companion object { + public companion object { {% for value in enum.values %} - val {{ as_ktName(value.name.CamelCase()) }} ={{' '}} + public val {{ as_ktName(value.name.CamelCase()) }}: {{ enum.name.CamelCase() }} ={{' '}} {{- enum.name.CamelCase() }}({{ '{:#010x}'.format(value.value) }}) {% endfor %} - val names = mapOf( + internal val names: Map = mapOf( {% for value in enum.values %} {{ '{:#010x}'.format(value.value) }} to "{{ as_ktName(value.name.CamelCase()) }}", {% endfor %} diff --git a/generator/templates/art/api_kotlin_function_pointer.kt b/generator/templates/art/api_kotlin_function_pointer.kt index 9c9e261d1f4..4b417a4adc9 100644 --- a/generator/templates/art/api_kotlin_function_pointer.kt +++ b/generator/templates/art/api_kotlin_function_pointer.kt @@ -27,10 +27,10 @@ package {{ kotlin_package }} {% from 'art/api_kotlin_types.kt' import kotlin_declaration with context %} -fun interface {{ function_pointer.name.CamelCase() }} { +public fun interface {{ function_pointer.name.CamelCase() }} { @Suppress("INAPPLICABLE_JVM_NAME") //* Required for @JvmName on global function. @JvmName("callback") //* Required to access Inline Value Class parameters via JNI. - fun callback( + public fun callback( {%- for arg in kotlin_record_members(function_pointer.arguments) -%} {{ as_varName(arg.name) }}: {{ kotlin_declaration(arg) }},{{ ' ' }} {%- endfor -%}); diff --git a/generator/templates/art/api_kotlin_functions.kt b/generator/templates/art/api_kotlin_functions.kt index e5c690b24b6..061326b7fdf 100644 --- a/generator/templates/art/api_kotlin_functions.kt +++ b/generator/templates/art/api_kotlin_functions.kt @@ -32,7 +32,7 @@ import dalvik.annotation.optimization.FastNative {% for function in by_category['function'] if include_method(function) %} @FastNative - external fun {{ function.name.camelCase() }}( + public external fun {{ function.name.camelCase() }}( {%- for arg in function.arguments -%} {{- as_varName(arg.name) }}: {{ kotlin_definition(arg) }},{{' '}} {%- endfor %}): {{ kotlin_declaration(kotlin_return(function)) }} diff --git a/generator/templates/art/api_kotlin_object.kt b/generator/templates/art/api_kotlin_object.kt index cd1ad7966d5..f170bc6caef 100644 --- a/generator/templates/art/api_kotlin_object.kt +++ b/generator/templates/art/api_kotlin_object.kt @@ -31,11 +31,11 @@ import java.nio.ByteBuffer {% from 'art/api_kotlin_types.kt' import kotlin_declaration, kotlin_definition with context %} -class {{ obj.name.CamelCase() }}(val handle: Long): AutoCloseable { +public class {{ obj.name.CamelCase() }}(public val handle: Long): AutoCloseable { {% for method in obj.methods if include_method(method) %} @FastNative @JvmName("{{ method.name.camelCase() }}") - external fun {{ method.name.camelCase() }}( + public external fun {{ method.name.camelCase() }}( //* TODO(b/341923892): rework async methods to use futures. {%- for arg in kotlin_record_members(method.arguments) %} {{- as_varName(arg.name) }}: {{ kotlin_definition(arg) }},{{ ' ' }} @@ -46,7 +46,7 @@ class {{ obj.name.CamelCase() }}(val handle: Long): AutoCloseable { //* camelCase() (lower case first word). E.g. "get foo bar" translated to fooBar. {% set name = method.name.chunks[1] + method.name.chunks[2:] | map('title') | join %} @get:JvmName("{{ name }}") - val {{ name }} get() = {{ method.name.camelCase() }}() + public val {{ name }}: {{ kotlin_declaration(kotlin_return(method)) }} get() = {{ method.name.camelCase() }}() {% endif %} {% endfor %} diff --git a/generator/templates/art/api_kotlin_structure.kt b/generator/templates/art/api_kotlin_structure.kt index 71a943fdd50..7b277e2ba85 100644 --- a/generator/templates/art/api_kotlin_structure.kt +++ b/generator/templates/art/api_kotlin_structure.kt @@ -27,16 +27,16 @@ package {{ kotlin_package }} {% from 'art/api_kotlin_types.kt' import kotlin_definition with context %} -class {{ structure.name.CamelCase() }}( +public class {{ structure.name.CamelCase() }}( {% for member in kotlin_record_members(structure.members) %} - {# We supply a getter that is excluded from name manging to allow Inline Value Classed + {# We supply a getter that is excluded from name mangling to allow Inline Value Classed enums/bitmasks to be accessible as integers from the JVM adapter layer. #} {% if member.type.category in ['bitmask', 'enum'] %} {{' '}}@get:JvmName("get{{ member.name.CamelCase() }}") {% endif %} - var {{ member.name.camelCase() }}: {{ kotlin_definition(member) }}, + public var {{ member.name.camelCase() }}: {{ kotlin_definition(member) }}, {% endfor %} {% for structure in chain_children[structure.name.get()] %} - var {{ structure.name.camelCase() }}: {{ structure.name.CamelCase() }}? = null, + public var {{ structure.name.camelCase() }}: {{ structure.name.CamelCase() }}? = null, {% endfor %} ) diff --git a/tools/android/webgpu/build.gradle b/tools/android/webgpu/build.gradle index 0aca3e95674..6b9cfe3a78d 100644 --- a/tools/android/webgpu/build.gradle +++ b/tools/android/webgpu/build.gradle @@ -64,6 +64,9 @@ android { kotlinOptions { jvmTarget = '17' } + kotlin { + explicitApi() + } compileOptions { targetCompatibility JavaVersion.VERSION_17 } diff --git a/tools/android/webgpu/src/main/java/android/dawn/helper/DawnException.kt b/tools/android/webgpu/src/main/java/android/dawn/helper/DawnException.kt index 4126b64461e..285c32acb50 100644 --- a/tools/android/webgpu/src/main/java/android/dawn/helper/DawnException.kt +++ b/tools/android/webgpu/src/main/java/android/dawn/helper/DawnException.kt @@ -1,3 +1,3 @@ package android.dawn.helper -class DawnException(message:String): Exception(message) +public class DawnException(message:String): Exception(message) diff --git a/tools/android/webgpu/src/main/java/android/dawn/helper/Rounding.kt b/tools/android/webgpu/src/main/java/android/dawn/helper/Rounding.kt index 2900b7583f1..461569e4c90 100644 --- a/tools/android/webgpu/src/main/java/android/dawn/helper/Rounding.kt +++ b/tools/android/webgpu/src/main/java/android/dawn/helper/Rounding.kt @@ -5,22 +5,22 @@ package android.dawn.helper * or lower multiple of a value, e.g., "round -1,234 down to the nearest multiple of 1,000" * returns -2000. */ -fun Long.roundDownToNearestMultipleOf(boundary: Int): Long { +public fun Long.roundDownToNearestMultipleOf(boundary: Int): Long { val padding = if (this < 0 && (this % boundary != 0L)) boundary else 0 return ((this - padding) / boundary) * boundary } -fun Int.roundDownToNearestMultipleOf(boundary: Int): Int { +public fun Int.roundDownToNearestMultipleOf(boundary: Int): Int { val padding = if (this < 0 && (this % boundary != 0)) boundary else 0 return ((this - padding) / boundary) * boundary } -fun Long.roundUpToNearestMultipleOf(boundary: Int): Long { +public fun Long.roundUpToNearestMultipleOf(boundary: Int): Long { val padding = if (this > 0 && (this % boundary != 0L)) boundary else 0 return ((this + padding) / boundary) * boundary } -fun Int.roundUpToNearestMultipleOf(boundary: Int): Int { +public fun Int.roundUpToNearestMultipleOf(boundary: Int): Int { val padding = if (this > 0 && (this % boundary != 0)) boundary else 0 return ((this + padding) / boundary) * boundary -} \ No newline at end of file +} diff --git a/tools/android/webgpu/src/main/java/android/dawn/helper/Streams.kt b/tools/android/webgpu/src/main/java/android/dawn/helper/Streams.kt index 8cb01b002d6..da551ec5967 100644 --- a/tools/android/webgpu/src/main/java/android/dawn/helper/Streams.kt +++ b/tools/android/webgpu/src/main/java/android/dawn/helper/Streams.kt @@ -5,7 +5,7 @@ import java.io.InputStreamReader import java.nio.charset.StandardCharsets import java.util.Scanner -fun InputStream.asString(): String = +public fun InputStream.asString(): String = Scanner(InputStreamReader(this, StandardCharsets.UTF_8)).useDelimiter("\\A").run { if (hasNext()) next() else "" } diff --git a/tools/android/webgpu/src/main/java/android/dawn/helper/Textures.kt b/tools/android/webgpu/src/main/java/android/dawn/helper/Textures.kt index 5c9cc38303f..6176652a891 100644 --- a/tools/android/webgpu/src/main/java/android/dawn/helper/Textures.kt +++ b/tools/android/webgpu/src/main/java/android/dawn/helper/Textures.kt @@ -4,7 +4,7 @@ import android.dawn.* import android.graphics.Bitmap import java.nio.ByteBuffer -fun Bitmap.createGpuTexture(device: Device): Texture { +public fun Bitmap.createGpuTexture(device: Device): Texture { val size = Extent3D(width = width, height = height) return device.createTexture( TextureDescriptor( @@ -29,7 +29,7 @@ fun Bitmap.createGpuTexture(device: Device): Texture { } } -suspend fun Texture.createBitmap(device: Device): Bitmap { +public suspend fun Texture.createBitmap(device: Device): Bitmap { if (width % 64 > 0) { throw DawnException("Texture must be a multiple of 64. Was ${width}") } diff --git a/tools/android/webgpu/src/main/java/android/dawn/helper/Util.kt b/tools/android/webgpu/src/main/java/android/dawn/helper/Util.kt index f6cbc32d8a8..4d353c61361 100644 --- a/tools/android/webgpu/src/main/java/android/dawn/helper/Util.kt +++ b/tools/android/webgpu/src/main/java/android/dawn/helper/Util.kt @@ -2,10 +2,10 @@ package android.dawn.helper import android.view.Surface -object Util { +public object Util { init { System.loadLibrary("webgpu_c_bundled") } - external fun windowFromSurface(surface: Surface?): Long + public external fun windowFromSurface(surface: Surface?): Long } \ No newline at end of file