Simplify utilities in the API with extensions.
Change-Id: I24b99efeb71fe18ac0bbabe4d76accde314d8368
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/193960
Commit-Queue: Jim Blackler <jimblackler@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/tools/android/webgpu/src/androidTest/java/net/android/dawn/ImageTest.kt b/tools/android/webgpu/src/androidTest/java/net/android/dawn/ImageTest.kt
index f7f8847..08233f4 100644
--- a/tools/android/webgpu/src/androidTest/java/net/android/dawn/ImageTest.kt
+++ b/tools/android/webgpu/src/androidTest/java/net/android/dawn/ImageTest.kt
@@ -3,8 +3,8 @@
import android.dawn.*
import android.dawn.helper.DawnException
import android.dawn.helper.Util
-import android.dawn.helper.createImageFromGpuTexture
-import android.dawn.helper.streamToString
+import android.dawn.helper.asString
+import android.dawn.helper.createBitmap
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.os.Environment
@@ -66,7 +66,7 @@
val shaderModule = device.createShaderModule(
ShaderModuleDescriptor(
shaderModuleWGSLDescriptor = ShaderModuleWGSLDescriptor(
- code = streamToString(appContext.assets.open("triangle/shader.wgsl"))
+ code = appContext.assets.open("triangle/shader.wgsl").asString()
)
)
)
@@ -121,7 +121,7 @@
})
}
- val bitmap = createImageFromGpuTexture(device, testTexture)
+ val bitmap = testTexture.createBitmap(device)
if (false) {
writeReferenceImage(bitmap)
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 4a52b54..8cb01b0 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,13 +5,8 @@
import java.nio.charset.StandardCharsets
import java.util.Scanner
-fun streamToString(inputStream: InputStream): String {
- val scanner = Scanner(
- InputStreamReader(
- inputStream,
- StandardCharsets.UTF_8
- )
- ).useDelimiter("\\A")
- return if (scanner.hasNext()) scanner.next() else ""
-}
+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 be518b5..5c9cc38 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,8 +4,8 @@
import android.graphics.Bitmap
import java.nio.ByteBuffer
-fun createGpuTextureFromBitmap(device: Device, bitmap: Bitmap): Texture {
- val size = Extent3D(width = bitmap.width, height = bitmap.height)
+fun Bitmap.createGpuTexture(device: Device): Texture {
+ val size = Extent3D(width = width, height = height)
return device.createTexture(
TextureDescriptor(
size = size,
@@ -14,13 +14,12 @@
TextureUsage.RenderAttachment
)
).also { texture ->
- ByteBuffer.allocateDirect(bitmap.height * bitmap.width * Int.SIZE_BYTES).let { pixels ->
- bitmap.copyPixelsToBuffer(pixels)
+ ByteBuffer.allocateDirect(height * width * Int.SIZE_BYTES).let { pixels ->
+ copyPixelsToBuffer(pixels)
device.queue.writeTexture(
dataLayout = TextureDataLayout(
-
- bytesPerRow = bitmap.width * Int.SIZE_BYTES,
- rowsPerImage = bitmap.height
+ bytesPerRow = width * Int.SIZE_BYTES,
+ rowsPerImage = height
),
data = pixels,
destination = ImageCopyTexture(texture = texture),
@@ -30,36 +29,36 @@
}
}
-suspend fun createImageFromGpuTexture(device: Device, texture: Texture): Bitmap {
- if (texture.width % 64 > 0) {
- throw DawnException("Texture must be a multiple of 64. Was ${texture.width}")
+suspend fun Texture.createBitmap(device: Device): Bitmap {
+ if (width % 64 > 0) {
+ throw DawnException("Texture must be a multiple of 64. Was ${width}")
}
- val size = texture.width * texture.height * Int.SIZE_BYTES
+ val size = width * height * Int.SIZE_BYTES
val readbackBuffer = device.createBuffer(
BufferDescriptor(
size = size.toLong(),
usage = BufferUsage.CopyDst or BufferUsage.MapRead
)
)
- device.queue.submit(arrayOf(device.createCommandEncoder().run {
- copyTextureToBuffer(
- source = ImageCopyTexture(texture = texture),
+ device.queue.submit(arrayOf(device.createCommandEncoder().let {
+ it.copyTextureToBuffer(
+ source = ImageCopyTexture(texture = this),
destination = ImageCopyBuffer(
layout = TextureDataLayout(
offset = 0,
- bytesPerRow = texture.width * Int.SIZE_BYTES,
- rowsPerImage = texture.height
+ bytesPerRow = width * Int.SIZE_BYTES,
+ rowsPerImage = height
), buffer = readbackBuffer
),
- copySize = Extent3D(width = texture.width, height = texture.height)
+ copySize = Extent3D(width = width, height = height)
)
- finish()
+ it.finish()
}))
readbackBuffer.mapAsync(MapMode.Read, 0, size.toLong())
- return Bitmap.createBitmap(texture.width, texture.height, Bitmap.Config.ARGB_8888).apply {
+ return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888).apply {
copyPixelsFromBuffer(readbackBuffer.getConstMappedRange(size = readbackBuffer.size))
readbackBuffer.unmap()
}