1 /* Copyright (c) 2018-2019 The Khronos Group Inc.
2 * Copyright (c) 2018-2019 Valve Corporation
3 * Copyright (c) 2018-2019 LunarG, Inc.
4 * Copyright (C) 2018-2019 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Dave Houlton <daveh@lunarg.com>
19 */
20
21 #ifndef ANDROID_NDK_TYPES_H_
22 #define ANDROID_NDK_TYPES_H_
23
24 #ifdef VK_USE_PLATFORM_ANDROID_KHR
25
26 // All eums referenced by VK_ANDROID_external_memory_android_hardware_buffer are present in
27 // the platform-28 (Android P) versions of the header files. A partial set exists in the
28 // platform-26 (O) headers, where hardware_buffer.h first appears in the NDK.
29 //
30 // Building Vulkan validation with NDK header files prior to platform-26 is not supported.
31 //
32 // Decoder ring for Android compile symbols found here: https://github.com/android-ndk/ndk/issues/407
33
34 #ifdef __ANDROID__ // Compiling for Android
35 #include <android/api-level.h>
36 #include <android/hardware_buffer.h> // First appearance in Android O (platform-26)
37
38 // If NDK is O (platform-26 or -27), supplement the missing enums with pre-processor defined literals
39 // If Android P or later, then all required enums are already defined
40 #if defined(__ANDROID_API_O__) && !defined(__ANDROID_API_P__)
41 // Formats
42 #define AHARDWAREBUFFER_FORMAT_D16_UNORM 0x30
43 #define AHARDWAREBUFFER_FORMAT_D24_UNORM 0x31
44 #define AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT 0x32
45 #define AHARDWAREBUFFER_FORMAT_D32_FLOAT 0x33
46 #define AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT 0x34
47 #define AHARDWAREBUFFER_FORMAT_S8_UINT 0x35
48 // Usage bits
49 #define AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP 0x2000000
50 #define AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE 0x4000000
51 #endif // __ANDROID_API_O__ && !_P__
52
53 #else // Not __ANDROID__, but VK_USE_PLATFORM_ANDROID_KHR
54 // This combination should not be seen in the wild, but can be used to allow testing
55 // of the AHB extension validation on other platforms using MockICD
56 //
57 // Define the minimal set of NDK enums and structs needed to compile
58 // VK_ANDROID_external_memory_android_hardware_buffer validation without an NDK present
59 struct AHardwareBuffer {};
60
61 // Enumerations of format and usage flags for Android opaque external memory blobs
62 typedef enum AHardwareBufferFormat {
63 AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM = 1,
64 AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM = 2,
65 AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM = 3,
66 AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM = 4,
67 AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT = 0x16,
68 AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM = 0x2b,
69 AHARDWAREBUFFER_FORMAT_D16_UNORM = 0x30,
70 AHARDWAREBUFFER_FORMAT_D24_UNORM = 0x31,
71 AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT = 0x32,
72 AHARDWAREBUFFER_FORMAT_D32_FLOAT = 0x33,
73 AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT = 0x34,
74 AHARDWAREBUFFER_FORMAT_S8_UINT = 0x35,
75 AHARDWAREBUFFER_FORMAT_BLOB = 0x21
76 } AHardwareBufferFormat;
77
78 typedef enum AHardwareBufferUsage {
79 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE = 0x100,
80 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT = 0x200,
81 AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP = 0x2000000,
82 AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE = 0x4000000,
83 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT = 0x4000,
84 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER = 0x1000000
85 } AHardwareBufferUsage;
86
87 typedef struct AHardwareBuffer_Desc {
88 uint32_t format; // One of AHARDWAREBUFFER_FORMAT_*.
89 uint32_t height; // Height in pixels.
90 uint32_t layers; // Number of images in an image array.
91 uint32_t rfu0; // Initialize to zero, reserved for future use.
92 uint64_t rfu1; // Initialize to zero, reserved for future use.
93 uint32_t stride; // Row stride in pixels, ignored for AHardwareBuffer_allocate()
94 uint64_t usage; // Combination of AHARDWAREBUFFER_USAGE_*.
95 uint32_t width; // Width in pixels.
96 } AHardwareBuffer_Desc;
97
98 // Minimal NDK fxn stubs to allow testing on ndk-less platform
AHardwareBuffer_allocate(const AHardwareBuffer_Desc * ahbDesc,AHardwareBuffer ** buffer)99 static inline int AHardwareBuffer_allocate(const AHardwareBuffer_Desc *ahbDesc, AHardwareBuffer **buffer) {
100 size_t size = ahbDesc->height * ahbDesc->width * 8; // Alloc for largest (64 bpp) format
101 if (size < sizeof(AHardwareBuffer_Desc)) size = sizeof(AHardwareBuffer_Desc);
102 *buffer = (AHardwareBuffer *)malloc(size);
103 memcpy((void *)(*buffer), (void *)ahbDesc, sizeof(AHardwareBuffer_Desc));
104 return 0;
105 }
106
AHardwareBuffer_release(AHardwareBuffer * buffer)107 static inline void AHardwareBuffer_release(AHardwareBuffer *buffer) {
108 if (buffer) free(buffer);
109 }
110
AHardwareBuffer_describe(const AHardwareBuffer * buffer,AHardwareBuffer_Desc * outDesc)111 static inline void AHardwareBuffer_describe(const AHardwareBuffer *buffer, AHardwareBuffer_Desc *outDesc) {
112 if (buffer && outDesc) {
113 memcpy((void *)outDesc, (void *)buffer, sizeof(AHardwareBuffer_Desc));
114 }
115 return;
116 }
117
118 #endif // __ANDROID__
119
120 #endif // VK_USE_PLATFORM_ANDROID_KHR
121
122 #endif // ANDROID_NDK_TYPES_H_
123