| 1 |
/* |
| 2 |
* Copyright 2019 Google LLC |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#include "AndroidHardwareBuffer.h" |
| 6 |
#include "util/detect_os.h" |
| 7 |
|
| 8 |
#if DETECT_OS_ANDROID || DETECT_OS_LINUX |
| 9 |
#include <drm_fourcc.h> |
| 10 |
#define DRM_FORMAT_YVU420_ANDROID fourcc_code('9', '9', '9', '7') |
| 11 |
#define DRM_FORMAT_D16_UNORM fourcc_code('9', '9', '9', '6') |
| 12 |
#define DRM_FORMAT_D24_UNORM fourcc_code('9', '9', '9', '5') |
| 13 |
#define DRM_FORMAT_D24_UNORM_S8_UINT fourcc_code('9', '9', '9', '4') |
| 14 |
#define DRM_FORMAT_D32_FLOAT fourcc_code('9', '9', '9', '3') |
| 15 |
#define DRM_FORMAT_D32_FLOAT_S8_UINT fourcc_code('9', '9', '9', '2') |
| 16 |
#define DRM_FORMAT_S8_UINT fourcc_code('9', '9', '9', '1') |
| 17 |
#endif |
| 18 |
|
| 19 |
#if defined(ANDROID) |
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
|
| 23 |
#include "gfxstream/guest/GfxStreamGralloc.h" |
| 24 |
#include "vk_format_info.h" |
| 25 |
#include "vk_util.h" |
| 26 |
#include "util/log.h" |
| 27 |
|
| 28 |
namespace gfxstream { |
| 29 |
namespace vk { |
| 30 |
|
| 31 |
// From Intel ANV implementation. |
| 32 |
/* Construct ahw usage mask from image usage bits, see |
| 33 |
* 'AHardwareBuffer Usage Equivalence' in Vulkan spec. |
| 34 |
*/ |
| 35 |
uint64_t getAndroidHardwareBufferUsageFromVkUsage(const VkImageCreateFlags vk_create, |
| 36 |
const VkImageUsageFlags vk_usage) { |
| 37 |
uint64_t ahw_usage = 0; |
| 38 |
|
| 39 |
if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT) { |
| 40 |
ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE; |
| 41 |
} |
| 42 |
if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) { |
| 43 |
ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE; |
| 44 |
} |
| 45 |
if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) { |
| 46 |
ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT; |
| 47 |
} |
| 48 |
if (vk_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { |
| 49 |
ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT; |
| 50 |
} |
| 51 |
if (vk_create & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) { |
| 52 |
ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP; |
| 53 |
} |
| 54 |
if (vk_create & VK_IMAGE_CREATE_PROTECTED_BIT) { |
| 55 |
ahw_usage |= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT; |
| 56 |
} |
| 57 |
|
| 58 |
/* No usage bits set - set at least one GPU usage. */ |
| 59 |
if (ahw_usage == 0) { |
| 60 |
ahw_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE; |
| 61 |
} |
| 62 |
|
| 63 |
return ahw_usage; |
| 64 |
} |
| 65 |
|
| 66 |
VkResult getAndroidHardwareBufferPropertiesANDROID( |
| 67 |
gfxstream::Gralloc* grallocHelper, const AHardwareBuffer* buffer, |
| 68 |
VkAndroidHardwareBufferPropertiesANDROID* pProperties) { |
| 69 |
VkAndroidHardwareBufferFormatPropertiesANDROID* ahbFormatProps = |
| 70 |
vk_find_struct(pProperties, ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID); |
| 71 |
|
| 72 |
const auto format = grallocHelper->getFormat(buffer); |
| 73 |
if (ahbFormatProps) { |
| 74 |
switch (format) { |
| 75 |
case AHARDWAREBUFFER_FORMAT_B8G8R8A8_UNORM: |
| 76 |
ahbFormatProps->format = VK_FORMAT_B8G8R8A8_UNORM; |
| 77 |
ahbFormatProps->externalFormat = DRM_FORMAT_ARGB8888; |
| 78 |
break; |
| 79 |
case AHARDWAREBUFFER_FORMAT_R8_UNORM: |
| 80 |
ahbFormatProps->format = VK_FORMAT_R8_UNORM; |
| 81 |
ahbFormatProps->externalFormat = DRM_FORMAT_R8; |
| 82 |
break; |
| 83 |
case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 84 |
ahbFormatProps->format = VK_FORMAT_R8G8B8A8_UNORM; |
| 85 |
ahbFormatProps->externalFormat = DRM_FORMAT_ABGR8888; |
| 86 |
break; |
| 87 |
case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 88 |
ahbFormatProps->format = VK_FORMAT_R8G8B8A8_UNORM; |
| 89 |
ahbFormatProps->externalFormat = DRM_FORMAT_XBGR8888; |
| 90 |
break; |
| 91 |
case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 92 |
ahbFormatProps->format = VK_FORMAT_R8G8B8_UNORM; |
| 93 |
ahbFormatProps->externalFormat = DRM_FORMAT_BGR888; |
| 94 |
break; |
| 95 |
case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 96 |
ahbFormatProps->format = VK_FORMAT_R5G6B5_UNORM_PACK16; |
| 97 |
ahbFormatProps->externalFormat = DRM_FORMAT_RGB565; |
| 98 |
break; |
| 99 |
case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 100 |
ahbFormatProps->format = VK_FORMAT_R16G16B16A16_SFLOAT; |
| 101 |
ahbFormatProps->externalFormat = DRM_FORMAT_ABGR16161616F; |
| 102 |
break; |
| 103 |
case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 104 |
ahbFormatProps->format = VK_FORMAT_A2B10G10R10_UNORM_PACK32; |
| 105 |
ahbFormatProps->externalFormat = DRM_FORMAT_ABGR2101010; |
| 106 |
break; |
| 107 |
case AHARDWAREBUFFER_FORMAT_D16_UNORM: |
| 108 |
ahbFormatProps->format = VK_FORMAT_D16_UNORM; |
| 109 |
ahbFormatProps->externalFormat = DRM_FORMAT_D16_UNORM; |
| 110 |
break; |
| 111 |
case AHARDWAREBUFFER_FORMAT_D24_UNORM: |
| 112 |
ahbFormatProps->format = VK_FORMAT_X8_D24_UNORM_PACK32; |
| 113 |
ahbFormatProps->externalFormat = DRM_FORMAT_D24_UNORM; |
| 114 |
break; |
| 115 |
case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT: |
| 116 |
ahbFormatProps->format = VK_FORMAT_D24_UNORM_S8_UINT; |
| 117 |
ahbFormatProps->externalFormat = DRM_FORMAT_D24_UNORM_S8_UINT; |
| 118 |
break; |
| 119 |
case AHARDWAREBUFFER_FORMAT_D32_FLOAT: |
| 120 |
ahbFormatProps->format = VK_FORMAT_D32_SFLOAT; |
| 121 |
ahbFormatProps->externalFormat = DRM_FORMAT_D32_FLOAT; |
| 122 |
break; |
| 123 |
case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT: |
| 124 |
ahbFormatProps->format = VK_FORMAT_D32_SFLOAT_S8_UINT; |
| 125 |
ahbFormatProps->externalFormat = DRM_FORMAT_D32_FLOAT_S8_UINT; |
| 126 |
break; |
| 127 |
case AHARDWAREBUFFER_FORMAT_S8_UINT: |
| 128 |
ahbFormatProps->format = VK_FORMAT_S8_UINT; |
| 129 |
ahbFormatProps->externalFormat = DRM_FORMAT_S8_UINT; |
| 130 |
break; |
| 131 |
default: |
| 132 |
ahbFormatProps->format = VK_FORMAT_UNDEFINED; |
| 133 |
ahbFormatProps->externalFormat = DRM_FORMAT_INVALID; |
| 134 |
mesa_loge("Unhandled AHB format:%u", format); |
| 135 |
break; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
// The formatFeatures member must include |
| 140 |
// VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT and at least one of |
| 141 |
// VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT or |
| 142 |
// VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT, and should include |
| 143 |
// VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT and |
| 144 |
// VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT. |
| 145 |
|
| 146 |
// org.skia.skqp.SkQPRunner#UnitTest_VulkanHardwareBuffer* requires the following: |
| 147 |
// VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
| 148 |
// VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
| 149 |
// VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
| 150 |
ahbFormatProps->formatFeatures = |
| 151 |
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT | |
| 152 |
VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT | |
| 153 |
VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT; |
| 154 |
|
| 155 |
// "Implementations may not always be able to determine the color model, |
| 156 |
// numerical range, or chroma offsets of the image contents, so the values in |
| 157 |
// VkAndroidHardwareBufferFormatPropertiesANDROID are only suggestions. |
| 158 |
// Applications should treat these values as sensible defaults to use in the |
| 159 |
// absence of more reliable information obtained through some other means." |
| 160 |
|
| 161 |
ahbFormatProps->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_IDENTITY; |
| 162 |
ahbFormatProps->samplerYcbcrConversionComponents.g = VK_COMPONENT_SWIZZLE_IDENTITY; |
| 163 |
ahbFormatProps->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_IDENTITY; |
| 164 |
ahbFormatProps->samplerYcbcrConversionComponents.a = VK_COMPONENT_SWIZZLE_IDENTITY; |
| 165 |
|
| 166 |
ahbFormatProps->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; |
| 167 |
ahbFormatProps->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_NARROW; |
| 168 |
ahbFormatProps->suggestedXChromaOffset = VK_CHROMA_LOCATION_MIDPOINT; |
| 169 |
ahbFormatProps->suggestedYChromaOffset = VK_CHROMA_LOCATION_MIDPOINT; |
| 170 |
|
| 171 |
#if DETECT_OS_ANDROID || DETECT_OS_LINUX |
| 172 |
if (android_format_is_yuv(format)) { |
| 173 |
uint32_t drmFormat = grallocHelper->getFormatDrmFourcc(buffer); |
| 174 |
ahbFormatProps->externalFormat = static_cast<uint64_t>(drmFormat); |
| 175 |
if (drmFormat) { |
| 176 |
// The host renderer is not aware of the plane ordering for YUV formats used |
| 177 |
// in the guest and simply knows that the format "layout" is one of: |
| 178 |
// |
| 179 |
// * VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16 |
| 180 |
// * VK_FORMAT_G8_B8R8_2PLANE_420_UNORM |
| 181 |
// * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM |
| 182 |
// |
| 183 |
// With this, the guest needs to adjust the component swizzle based on plane |
| 184 |
// ordering to ensure that the channels are interpreted correctly. |
| 185 |
// |
| 186 |
// From the Vulkan spec's "Sampler Y'CBCR Conversion" section: |
| 187 |
// |
| 188 |
// * Y comes from the G-channel (after swizzle) |
| 189 |
// * U (CB) comes from the B-channel (after swizzle) |
| 190 |
// * V (CR) comes from the R-channel (after swizzle) |
| 191 |
// |
| 192 |
// See |
| 193 |
// https://docs.vulkan.org/spec/latest/chapters/textures.html#textures-sampler-YCbCr-conversion |
| 194 |
// |
| 195 |
// To match the above, the guest needs to swizzle such that: |
| 196 |
// |
| 197 |
// * Y ends up in the G-channel |
| 198 |
// * U (CB) ends up in the B-channel |
| 199 |
// * V (CB) ends up in the R-channel |
| 200 |
switch (drmFormat) { |
| 201 |
case DRM_FORMAT_NV12: |
| 202 |
// NV12 is a Y-plane followed by a interleaved UV-plane and is |
| 203 |
// VK_FORMAT_G8_B8R8_2PLANE_420_UNORM on the host. |
| 204 |
break; |
| 205 |
case DRM_FORMAT_P010: |
| 206 |
// P010 is a Y-plane followed by a interleaved UV-plane and is |
| 207 |
// VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16 on the host. |
| 208 |
break; |
| 209 |
case DRM_FORMAT_YUV420: |
| 210 |
// YUV420 is a Y-plane, then a U-plane, and then a V-plane and is |
| 211 |
// VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM on the host. |
| 212 |
break; |
| 213 |
case DRM_FORMAT_NV21: |
| 214 |
// NV21 is a Y-plane followed by a interleaved VU-plane and is |
| 215 |
// VK_FORMAT_G8_B8R8_2PLANE_420_UNORM on the host. |
| 216 |
case DRM_FORMAT_YVU420: |
| 217 |
// YVU420 is a Y-plane, then a V-plane, and then a U-plane and is |
| 218 |
// VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM on the host. |
| 219 |
case DRM_FORMAT_YVU420_ANDROID: |
| 220 |
// DRM_FORMAT_YVU420_ANDROID is the same as DRM_FORMAT_YVU420 with |
| 221 |
// Android's extra alignement requirements. |
| 222 |
ahbFormatProps->samplerYcbcrConversionComponents.r = VK_COMPONENT_SWIZZLE_B; |
| 223 |
ahbFormatProps->samplerYcbcrConversionComponents.b = VK_COMPONENT_SWIZZLE_R; |
| 224 |
break; |
| 225 |
|
| 226 |
default: |
| 227 |
mesa_loge("Unhandled YUV drm format:%u", drmFormat); |
| 228 |
break; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
int32_t dataspace = grallocHelper->getDataspace(buffer); |
| 233 |
|
| 234 |
// Some of the dataspace enums are not composites built from the bitwise-or of the model, |
| 235 |
// transfer, and range. Replace those enums with their corresponding composite enums: |
| 236 |
switch (dataspace) { |
| 237 |
case GFXSTREAM_AHB_DATASPACE_UNKNOWN: { |
| 238 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_JFIF; |
| 239 |
break; |
| 240 |
} |
| 241 |
case GFXSTREAM_AHB_DATASPACE_BT601_525: { |
| 242 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_BT601_525; |
| 243 |
break; |
| 244 |
} |
| 245 |
case GFXSTREAM_AHB_DATASPACE_BT601_625: { |
| 246 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_BT601_625; |
| 247 |
break; |
| 248 |
} |
| 249 |
case GFXSTREAM_AHB_DATASPACE_BT709: { |
| 250 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_BT709; |
| 251 |
break; |
| 252 |
} |
| 253 |
case GFXSTREAM_AHB_DATASPACE_JFIF: { |
| 254 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_JFIF; |
| 255 |
break; |
| 256 |
} |
| 257 |
case GFXSTREAM_AHB_DATASPACE_SRGB: { |
| 258 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_SRGB; |
| 259 |
break; |
| 260 |
} |
| 261 |
case GFXSTREAM_AHB_DATASPACE_SRGB_LINEAR: { |
| 262 |
dataspace = GFXSTREAM_AHB_DATASPACE_V0_SRGB_LINEAR; |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
const int32_t model = dataspace & GFXSTREAM_AHB_DATASPACE_STANDARD_MASK; |
| 268 |
switch (model) { |
| 269 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT601_525: |
| 270 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT601_525_UNADJUSTED: |
| 271 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT601_625: |
| 272 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT601_625_UNADJUSTED: { |
| 273 |
ahbFormatProps->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601; |
| 274 |
break; |
| 275 |
} |
| 276 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT709: { |
| 277 |
ahbFormatProps->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709; |
| 278 |
break; |
| 279 |
} |
| 280 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT2020: |
| 281 |
case GFXSTREAM_AHB_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE: { |
| 282 |
ahbFormatProps->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020; |
| 283 |
break; |
| 284 |
} |
| 285 |
default: { |
| 286 |
mesa_logw("Unhandled AHB dataspace model: %d. Assuming YCBCR_601", model); |
| 287 |
ahbFormatProps->suggestedYcbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601; |
| 288 |
break; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
const int32_t range = dataspace & GFXSTREAM_AHB_DATASPACE_RANGE_MASK; |
| 293 |
switch (range) { |
| 294 |
case GFXSTREAM_AHB_DATASPACE_RANGE_FULL: { |
| 295 |
ahbFormatProps->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; |
| 296 |
break; |
| 297 |
} |
| 298 |
case GFXSTREAM_AHB_DATASPACE_RANGE_LIMITED: { |
| 299 |
ahbFormatProps->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_NARROW; |
| 300 |
break; |
| 301 |
} |
| 302 |
default: { |
| 303 |
mesa_logw("Unhandled AHB dataspace range: %d. Assuming full.", range); |
| 304 |
ahbFormatProps->suggestedYcbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; |
| 305 |
break; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
#endif |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
uint32_t colorBufferHandle = grallocHelper->getHostHandle(buffer); |
| 314 |
if (!colorBufferHandle) { |
| 315 |
return VK_ERROR_INVALID_EXTERNAL_HANDLE; |
| 316 |
} |
| 317 |
|
| 318 |
pProperties->allocationSize = grallocHelper->getAllocatedSize(buffer); |
| 319 |
|
| 320 |
return VK_SUCCESS; |
| 321 |
} |
| 322 |
|
| 323 |
// Based on Intel ANV implementation. |
| 324 |
VkResult getMemoryAndroidHardwareBufferANDROID(gfxstream::Gralloc* gralloc, |
| 325 |
struct AHardwareBuffer** pBuffer) { |
| 326 |
/* Some quotes from Vulkan spec: |
| 327 |
* |
| 328 |
* "If the device memory was created by importing an Android hardware |
| 329 |
* buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same |
| 330 |
* Android hardware buffer object." |
| 331 |
* |
| 332 |
* "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must |
| 333 |
* have been included in VkExportMemoryAllocateInfo::handleTypes when |
| 334 |
* memory was created." |
| 335 |
*/ |
| 336 |
|
| 337 |
if (!pBuffer) return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 338 |
if (!(*pBuffer)) return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 339 |
|
| 340 |
gralloc->acquire(*pBuffer); |
| 341 |
return VK_SUCCESS; |
| 342 |
} |
| 343 |
|
| 344 |
VkResult importAndroidHardwareBuffer(gfxstream::Gralloc* grallocHelper, |
| 345 |
const VkImportAndroidHardwareBufferInfoANDROID* info, |
| 346 |
struct AHardwareBuffer** importOut) { |
| 347 |
if (!info || !info->buffer) { |
| 348 |
return VK_ERROR_INVALID_EXTERNAL_HANDLE; |
| 349 |
} |
| 350 |
|
| 351 |
auto ahb = info->buffer; |
| 352 |
|
| 353 |
uint32_t colorBufferHandle = grallocHelper->getHostHandle(ahb); |
| 354 |
if (!colorBufferHandle) { |
| 355 |
return VK_ERROR_INVALID_EXTERNAL_HANDLE; |
| 356 |
} |
| 357 |
|
| 358 |
grallocHelper->acquire(ahb); |
| 359 |
|
| 360 |
if (importOut) *importOut = ahb; |
| 361 |
|
| 362 |
return VK_SUCCESS; |
| 363 |
} |
| 364 |
|
| 365 |
VkResult createAndroidHardwareBuffer(gfxstream::Gralloc* gralloc, bool hasDedicatedImage, |
| 366 |
bool hasDedicatedBuffer, const VkExtent3D& imageExtent, |
| 367 |
uint32_t imageLayers, VkFormat imageFormat, |
| 368 |
VkImageUsageFlags imageUsage, |
| 369 |
VkImageCreateFlags imageCreateFlags, VkDeviceSize bufferSize, |
| 370 |
VkDeviceSize allocationInfoAllocSize, |
| 371 |
struct AHardwareBuffer** out) { |
| 372 |
uint32_t w = 0; |
| 373 |
uint32_t h = 1; |
| 374 |
uint32_t layers = 1; |
| 375 |
uint32_t format = 0; |
| 376 |
uint64_t usage = 0; |
| 377 |
|
| 378 |
/* If caller passed dedicated information. */ |
| 379 |
if (hasDedicatedImage) { |
| 380 |
w = imageExtent.width; |
| 381 |
h = imageExtent.height; |
| 382 |
layers = imageLayers; |
| 383 |
format = android_format_from_vk(imageFormat); |
| 384 |
usage = getAndroidHardwareBufferUsageFromVkUsage(imageCreateFlags, imageUsage); |
| 385 |
} else if (hasDedicatedBuffer) { |
| 386 |
w = bufferSize; |
| 387 |
format = AHARDWAREBUFFER_FORMAT_BLOB; |
| 388 |
usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | |
| 389 |
AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER; |
| 390 |
} else { |
| 391 |
w = allocationInfoAllocSize; |
| 392 |
format = AHARDWAREBUFFER_FORMAT_BLOB; |
| 393 |
usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | |
| 394 |
AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER; |
| 395 |
} |
| 396 |
|
| 397 |
struct AHardwareBuffer* ahb = NULL; |
| 398 |
|
| 399 |
if (gralloc->allocate(w, h, format, usage, &ahb) != 0) { |
| 400 |
return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 401 |
} |
| 402 |
|
| 403 |
*out = ahb; |
| 404 |
|
| 405 |
return VK_SUCCESS; |
| 406 |
} |
| 407 |
|
| 408 |
} // namespace vk |
| 409 |
} // namespace gfxstream |
| 410 |
|
| 411 |
#endif // ANDROID |