[WebGPU][Kotlin] Optimize GPUImportedAHB per-frame hot path - Query device.GetAdapterInfo() once when constructing AhbTextureWrapper and cache isVulkan instead of querying it on every beginAccess() and endAccess() call. - Pass null from GPUHardwareBufferWrapper.beginAccess() when fences is null or empty, and allocate IntArray(fences.size) directly otherwise. - Lazily reserve importedFences and move imported SharedFence handles in nativeBeginAccess(). Bug: b/582193550 Test: ./gradlew :webgpu:testDebugUnitTest, buildCMakeRelWithDebInfo Change-Id: I3afb9527a9c5f8cde3d0bf6894dbc6136a22535b Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/359935 Reviewed-by: Tarun Saini <sainitarun@google.com> Commit-Queue: Mridul Goyal <mridulgoyal@google.com>
diff --git a/tools/android/webgpu/src/main/cpp/GPUImportedAHB.cpp b/tools/android/webgpu/src/main/cpp/GPUImportedAHB.cpp index 425f17a..ea47dec 100644 --- a/tools/android/webgpu/src/main/cpp/GPUImportedAHB.cpp +++ b/tools/android/webgpu/src/main/cpp/GPUImportedAHB.cpp
@@ -61,6 +61,7 @@ wgpu::ExternalTexture externalTexture; AHardwareBuffer* ahb = nullptr; wgpu::Device device; + bool isVulkan = false; }; struct UniqueFd { @@ -139,6 +140,12 @@ } } +bool IsVulkanDevice(const wgpu::Device& device) { + wgpu::AdapterInfo adapterInfo = {}; + return device.GetAdapterInfo(&adapterInfo) == wgpu::Status::Success && + adapterInfo.backendType == wgpu::BackendType::Vulkan; +} + } // namespace extern "C" { @@ -268,6 +275,7 @@ wrapper->stm = std::move(stm); wrapper->texture = std::move(texture); wrapper->ahb = ahb; + wrapper->isVulkan = IsVulkanDevice(device); wrapper->device = std::move(device); jlong wrapPtr = reinterpret_cast<jlong>(wrapper.get()); @@ -477,6 +485,7 @@ wrapper->view = std::move(view); wrapper->externalTexture = std::move(externalTexture); wrapper->ahb = ahb; + wrapper->isVulkan = IsVulkanDevice(device); wrapper->device = std::move(device); jlong wrapPtr = reinterpret_cast<jlong>(wrapper.get()); @@ -537,7 +546,10 @@ return; } if (fence) { - importedFences.push_back(fence); + if (importedFences.empty()) { + importedFences.reserve(len); + } + importedFences.push_back(std::move(fence)); } } } @@ -554,12 +566,7 @@ beginDesc.initialized = true; wgpu::SharedTextureMemoryVkImageLayoutBeginState beginLayout = {}; - wgpu::AdapterInfo adapterInfo; - wrapper->device.GetAdapterInfo(&adapterInfo); - if (env->ExceptionCheck()) { - return; - } - if (adapterInfo.backendType == wgpu::BackendType::Vulkan) { + if (wrapper->isVulkan) { beginLayout.oldLayout = VK_IMAGE_LAYOUT_GENERAL; beginLayout.newLayout = VK_IMAGE_LAYOUT_GENERAL; beginDesc.nextInChain = &beginLayout; @@ -585,12 +592,7 @@ wgpu::SharedTextureMemoryEndAccessState endState = {}; wgpu::SharedTextureMemoryVkImageLayoutEndState endLayout = {}; - wgpu::AdapterInfo adapterInfo; - wrapper->device.GetAdapterInfo(&adapterInfo); - if (env->ExceptionCheck()) { - return -1; - } - if (adapterInfo.backendType == wgpu::BackendType::Vulkan) { + if (wrapper->isVulkan) { endLayout.oldLayout = VK_IMAGE_LAYOUT_GENERAL; endLayout.newLayout = VK_IMAGE_LAYOUT_GENERAL; endState.nextInChain = &endLayout;
diff --git a/tools/android/webgpu/src/main/java/androidx/webgpu/GPUHardwareBufferWrapper.kt b/tools/android/webgpu/src/main/java/androidx/webgpu/GPUHardwareBufferWrapper.kt index 4c94fc7..1bee5b3 100644 --- a/tools/android/webgpu/src/main/java/androidx/webgpu/GPUHardwareBufferWrapper.kt +++ b/tools/android/webgpu/src/main/java/androidx/webgpu/GPUHardwareBufferWrapper.kt
@@ -32,7 +32,11 @@ * @param fences Array of GPUSyncFence objects to wait on before accessing the buffer. */ public fun beginAccess(fences: Array<GPUSyncFence>?) { - val fds = fences?.mapNotNull { it.getParcelFileDescriptor().fd }?.toIntArray() + val fds = if (fences.isNullOrEmpty()) { + null + } else { + IntArray(fences.size) { i -> fences[i].getParcelFileDescriptor().fd } + } nativeBeginAccess(handle, fds) }