diff --git a/filament/backend/CMakeLists.txt b/filament/backend/CMakeLists.txt index e4cc81a472f..b987e18ecb6 100644 --- a/filament/backend/CMakeLists.txt +++ b/filament/backend/CMakeLists.txt @@ -246,6 +246,10 @@ if (FILAMENT_SUPPORTS_WEBGPU) ) endif() +if (ANDROID) + list(APPEND SRCS src/BackendUtilsAndroid.cpp) +endif() + # ================================================================================================== # Definitions # ================================================================================================== diff --git a/filament/backend/include/backend/DriverEnums.h b/filament/backend/include/backend/DriverEnums.h index 551a7f19ad5..f0561be7094 100644 --- a/filament/backend/include/backend/DriverEnums.h +++ b/filament/backend/include/backend/DriverEnums.h @@ -807,6 +807,33 @@ static constexpr bool isStencilFormat(TextureFormat format) noexcept { } } +inline constexpr bool isColorFormat(TextureFormat format) noexcept { + switch (format) { + // Standard color formats + case TextureFormat::R8: + case TextureFormat::RG8: + case TextureFormat::RGBA8: + case TextureFormat::R16F: + case TextureFormat::RG16F: + case TextureFormat::RGBA16F: + case TextureFormat::R32F: + case TextureFormat::RG32F: + case TextureFormat::RGBA32F: + case TextureFormat::RGB10_A2: + case TextureFormat::R11F_G11F_B10F: + case TextureFormat::SRGB8: + case TextureFormat::SRGB8_A8: + case TextureFormat::RGB8: + case TextureFormat::RGB565: + case TextureFormat::RGB5_A1: + case TextureFormat::RGBA4: + return true; + default: + break; + } + return false; +} + static constexpr bool isUnsignedIntFormat(TextureFormat format) { switch (format) { case TextureFormat::R8UI: diff --git a/filament/backend/include/private/backend/BackendUtilsAndroid.h b/filament/backend/include/private/backend/BackendUtilsAndroid.h new file mode 100644 index 00000000000..21ad61f48a4 --- /dev/null +++ b/filament/backend/include/private/backend/BackendUtilsAndroid.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2025 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. + */ + +#ifndef TNT_FILAMENT_BACKEND_PRIVATE_BACKENDUTILSANDROID_H +#define TNT_FILAMENT_BACKEND_PRIVATE_BACKENDUTILSANDROID_H + +#include + +namespace filament::backend { + +/** + * Maps AHardwareBuffer format to TextureFormat. + */ +TextureFormat mapToFilamentFormat(unsigned int format, bool isSrgbTransfer) noexcept; + +/** + * Maps AHardwareBuffer usage to TextureUsage. + */ +TextureUsage mapToFilamentUsage(unsigned int usage, TextureFormat format) noexcept; + +} // namespace filament + +#endif // TNT_FILAMENT_BACKEND_PRIVATE_BACKENDUTILSANDROID_H diff --git a/filament/backend/src/BackendUtilsAndroid.cpp b/filament/backend/src/BackendUtilsAndroid.cpp new file mode 100644 index 00000000000..3eb967650ba --- /dev/null +++ b/filament/backend/src/BackendUtilsAndroid.cpp @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2025 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. + */ + +#include "private/backend/BackendUtilsAndroid.h" + +#include + +namespace filament::backend { + +TextureFormat mapToFilamentFormat(unsigned int format, bool isSrgbTransfer) noexcept { + if (isSrgbTransfer) { + switch (format) { + case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: + return TextureFormat::SRGB8; + default: + return TextureFormat::SRGB8_A8; + } + } + switch (format) { + case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: + case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: + return TextureFormat::RGBA8; + case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: + case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420: + return TextureFormat::RGB8; + case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: + return TextureFormat::RGB565; + case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: + return TextureFormat::RGBA16F; + case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: + return TextureFormat::RGB10_A2; + case AHARDWAREBUFFER_FORMAT_D16_UNORM: + return TextureFormat::DEPTH16; + case AHARDWAREBUFFER_FORMAT_D24_UNORM: + return TextureFormat::DEPTH24; + case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT: + return TextureFormat::DEPTH24_STENCIL8; + case AHARDWAREBUFFER_FORMAT_D32_FLOAT: + return TextureFormat::DEPTH32F; + case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT: + return TextureFormat::DEPTH32F_STENCIL8; + case AHARDWAREBUFFER_FORMAT_S8_UINT: + return TextureFormat::STENCIL8; + case AHARDWAREBUFFER_FORMAT_R8_UNORM: + return TextureFormat::R8; + default: + return TextureFormat::UNUSED; + } +} + +TextureUsage mapToFilamentUsage(unsigned int usage, TextureFormat format) noexcept { + TextureUsage usageFlags = TextureUsage::NONE; + + if (usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) { + usageFlags |= TextureUsage::SAMPLEABLE; + } + + if (usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) { + if (isDepthFormat(format)) { + usageFlags |= TextureUsage::DEPTH_ATTACHMENT; + } + if (isStencilFormat(format)) { + usageFlags |= TextureUsage::STENCIL_ATTACHMENT; + } + if (isColorFormat(format)) { + usageFlags |= TextureUsage::COLOR_ATTACHMENT; + } + } + + if (usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER) { + usageFlags |= TextureUsage::UPLOADABLE; + } + + if (usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) { + usageFlags |= TextureUsage::PROTECTED; + } + + if (usageFlags == TextureUsage::NONE) { + usageFlags = TextureUsage::COLOR_ATTACHMENT | TextureUsage::SAMPLEABLE; + } + + return usageFlags; +} + +} // namespace backend::filament