Skip to content

Commit ee68872

Browse files
authored
Add ahb mapping util functions to map ahb usage and format to TextureUsage and TextureFormat (#8491)
1 parent 2f4e48b commit ee68872

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

filament/backend/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ if (FILAMENT_SUPPORTS_WEBGPU)
246246
)
247247
endif()
248248

249+
if (ANDROID)
250+
list(APPEND SRCS src/BackendUtilsAndroid.cpp)
251+
endif()
252+
249253
# ==================================================================================================
250254
# Definitions
251255
# ==================================================================================================

filament/backend/include/backend/DriverEnums.h

+27
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,33 @@ static constexpr bool isStencilFormat(TextureFormat format) noexcept {
807807
}
808808
}
809809

810+
inline constexpr bool isColorFormat(TextureFormat format) noexcept {
811+
switch (format) {
812+
// Standard color formats
813+
case TextureFormat::R8:
814+
case TextureFormat::RG8:
815+
case TextureFormat::RGBA8:
816+
case TextureFormat::R16F:
817+
case TextureFormat::RG16F:
818+
case TextureFormat::RGBA16F:
819+
case TextureFormat::R32F:
820+
case TextureFormat::RG32F:
821+
case TextureFormat::RGBA32F:
822+
case TextureFormat::RGB10_A2:
823+
case TextureFormat::R11F_G11F_B10F:
824+
case TextureFormat::SRGB8:
825+
case TextureFormat::SRGB8_A8:
826+
case TextureFormat::RGB8:
827+
case TextureFormat::RGB565:
828+
case TextureFormat::RGB5_A1:
829+
case TextureFormat::RGBA4:
830+
return true;
831+
default:
832+
break;
833+
}
834+
return false;
835+
}
836+
810837
static constexpr bool isUnsignedIntFormat(TextureFormat format) {
811838
switch (format) {
812839
case TextureFormat::R8UI:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TNT_FILAMENT_BACKEND_PRIVATE_BACKENDUTILSANDROID_H
18+
#define TNT_FILAMENT_BACKEND_PRIVATE_BACKENDUTILSANDROID_H
19+
20+
#include <backend/DriverEnums.h>
21+
22+
namespace filament::backend {
23+
24+
/**
25+
* Maps AHardwareBuffer format to TextureFormat.
26+
*/
27+
TextureFormat mapToFilamentFormat(unsigned int format, bool isSrgbTransfer) noexcept;
28+
29+
/**
30+
* Maps AHardwareBuffer usage to TextureUsage.
31+
*/
32+
TextureUsage mapToFilamentUsage(unsigned int usage, TextureFormat format) noexcept;
33+
34+
} // namespace filament
35+
36+
#endif // TNT_FILAMENT_BACKEND_PRIVATE_BACKENDUTILSANDROID_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (C) 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "private/backend/BackendUtilsAndroid.h"
18+
19+
#include <android/hardware_buffer.h>
20+
21+
namespace filament::backend {
22+
23+
TextureFormat mapToFilamentFormat(unsigned int format, bool isSrgbTransfer) noexcept {
24+
if (isSrgbTransfer) {
25+
switch (format) {
26+
case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
27+
return TextureFormat::SRGB8;
28+
default:
29+
return TextureFormat::SRGB8_A8;
30+
}
31+
}
32+
switch (format) {
33+
case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
34+
case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
35+
return TextureFormat::RGBA8;
36+
case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
37+
case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
38+
return TextureFormat::RGB8;
39+
case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
40+
return TextureFormat::RGB565;
41+
case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
42+
return TextureFormat::RGBA16F;
43+
case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
44+
return TextureFormat::RGB10_A2;
45+
case AHARDWAREBUFFER_FORMAT_D16_UNORM:
46+
return TextureFormat::DEPTH16;
47+
case AHARDWAREBUFFER_FORMAT_D24_UNORM:
48+
return TextureFormat::DEPTH24;
49+
case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
50+
return TextureFormat::DEPTH24_STENCIL8;
51+
case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
52+
return TextureFormat::DEPTH32F;
53+
case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
54+
return TextureFormat::DEPTH32F_STENCIL8;
55+
case AHARDWAREBUFFER_FORMAT_S8_UINT:
56+
return TextureFormat::STENCIL8;
57+
case AHARDWAREBUFFER_FORMAT_R8_UNORM:
58+
return TextureFormat::R8;
59+
default:
60+
return TextureFormat::UNUSED;
61+
}
62+
}
63+
64+
TextureUsage mapToFilamentUsage(unsigned int usage, TextureFormat format) noexcept {
65+
TextureUsage usageFlags = TextureUsage::NONE;
66+
67+
if (usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) {
68+
usageFlags |= TextureUsage::SAMPLEABLE;
69+
}
70+
71+
if (usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) {
72+
if (isDepthFormat(format)) {
73+
usageFlags |= TextureUsage::DEPTH_ATTACHMENT;
74+
}
75+
if (isStencilFormat(format)) {
76+
usageFlags |= TextureUsage::STENCIL_ATTACHMENT;
77+
}
78+
if (isColorFormat(format)) {
79+
usageFlags |= TextureUsage::COLOR_ATTACHMENT;
80+
}
81+
}
82+
83+
if (usage & AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER) {
84+
usageFlags |= TextureUsage::UPLOADABLE;
85+
}
86+
87+
if (usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) {
88+
usageFlags |= TextureUsage::PROTECTED;
89+
}
90+
91+
if (usageFlags == TextureUsage::NONE) {
92+
usageFlags = TextureUsage::COLOR_ATTACHMENT | TextureUsage::SAMPLEABLE;
93+
}
94+
95+
return usageFlags;
96+
}
97+
98+
} // namespace backend::filament

0 commit comments

Comments
 (0)