Skip to content

Commit

Permalink
Kotlin: Enable explicitApi mode for the library
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Alex Benton <[email protected]>
Commit-Queue: Jim Blackler <[email protected]>
  • Loading branch information
jimblacklercorp authored and Dawn LUCI CQ committed Aug 27, 2024
1 parent fab658d commit f7b617f
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 34 deletions.
6 changes: 3 additions & 3 deletions generator/templates/art/api_kotlin_async_helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 %})
Expand All @@ -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) }},
Expand Down
13 changes: 8 additions & 5 deletions generator/templates/art/api_kotlin_constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
}
11 changes: 6 additions & 5 deletions generator/templates/art/api_kotlin_enum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, String> = mapOf(
{% for value in enum.values %}
{{ '{:#010x}'.format(value.value) }} to "{{ as_ktName(value.name.CamelCase()) }}",
{% endfor %}
Expand Down
4 changes: 2 additions & 2 deletions generator/templates/art/api_kotlin_function_pointer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%});
Expand Down
2 changes: 1 addition & 1 deletion generator/templates/art/api_kotlin_functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }}
Expand Down
6 changes: 3 additions & 3 deletions generator/templates/art/api_kotlin_object.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }},{{ ' ' }}
Expand All @@ -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 %}
Expand Down
8 changes: 4 additions & 4 deletions generator/templates/art/api_kotlin_structure.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
)
3 changes: 3 additions & 0 deletions tools/android/webgpu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ android {
kotlinOptions {
jvmTarget = '17'
}
kotlin {
explicitApi()
}
compileOptions {
targetCompatibility JavaVersion.VERSION_17
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package android.dawn.helper

class DawnException(message:String): Exception(message)
public class DawnException(message:String): Exception(message)
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f7b617f

Please sign in to comment.