Skip to content

Commit

Permalink
Add ahb mapping util functions to map ahb usage and format to Texture…
Browse files Browse the repository at this point in the history
…Usage and TextureFormat (#8491)
  • Loading branch information
alizahlalani authored Mar 7, 2025
1 parent 2f4e48b commit ee68872
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
4 changes: 4 additions & 0 deletions filament/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ if (FILAMENT_SUPPORTS_WEBGPU)
)
endif()

if (ANDROID)
list(APPEND SRCS src/BackendUtilsAndroid.cpp)
endif()

# ==================================================================================================
# Definitions
# ==================================================================================================
Expand Down
27 changes: 27 additions & 0 deletions filament/backend/include/backend/DriverEnums.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions filament/backend/include/private/backend/BackendUtilsAndroid.h
Original file line number Diff line number Diff line change
@@ -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 <backend/DriverEnums.h>

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
98 changes: 98 additions & 0 deletions filament/backend/src/BackendUtilsAndroid.cpp
Original file line number Diff line number Diff line change
@@ -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 <android/hardware_buffer.h>

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

0 comments on commit ee68872

Please sign in to comment.