10#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
24#define VK_CALL(X) gpu->vkInterface()->fFunctions.f##X
26namespace GrAHardwareBufferUtils {
29 uint32_t bufferFormat,
bool requireKnownFormat) {
36 switch (bufferFormat) {
37 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: {
41 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: {
45 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: {
49 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: {
53 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: {
57 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: {
61#if __ANDROID_API__ >= 33
62 case AHARDWAREBUFFER_FORMAT_R8_UNORM: {
68 if (requireKnownFormat) {
99 if (!GetAHardwareBufferProperties(
105 GetYcbcrConversionInfoFromFormatProps(&ycbcrConversion, hwbFormatProps);
110class VulkanCleanupHelper {
112 VulkanCleanupHelper(
GrVkGpu* gpu, VkImage
image, VkDeviceMemory memory)
116 , fDestroyImage(gpu->vkInterface()->fFunctions.fDestroyImage)
117 , fFreeMemory(gpu->vkInterface()->fFunctions.fFreeMemory) {}
118 ~VulkanCleanupHelper() {
119 fDestroyImage(fDevice, fImage,
nullptr);
120 fFreeMemory(fDevice, fMemory,
nullptr);
125 VkDeviceMemory fMemory;
130void delete_vk_image(
void* context) {
131 VulkanCleanupHelper* cleanupHelper =
static_cast<VulkanCleanupHelper*
>(context);
132 delete cleanupHelper;
142 DeleteImageProc* deleteProc,
143 UpdateImageProc* updateProc,
144 TexImageCtx* imageCtx,
145 bool isProtectedContent,
148 bool fromAndroidWindow) {
164 SkDebugf(
"AsVkFormat failed (valid: %d, backend: %u)",
166 (
unsigned)grBackendFormat.
backend());
173 if (!skgpu::GetAHardwareBufferProperties(
185 if (!importAsExternalFormat && hwbVkFormat != grBackendVkFormat) {
186 SkDebugf(
"Queried format not consistent with expected format; got: %d, expected: %d",
194 externalFormat.
pNext =
nullptr;
199 if (!ycbcrConversion) {
207 if (isRenderable && (importAsExternalFormat ||
209 SkDebugf(
"Renderable texture requested from an AHardwareBuffer which uses a "
210 "VkFormat that Skia cannot render to (VkFormat: %d).\n", grBackendVkFormat);
214 if (importAsExternalFormat) {
215 if (!ycbcrConversion->
isValid()) {
216 SkDebugf(
"YCbCr conversion must be valid when importing an AHardwareBuffer with an "
239 if (!importAsExternalFormat) {
240 usageFlags = usageFlags |
252 &externalMemoryImageInfo,
277 phyDevMemProps.
pNext =
nullptr;
278 VK_CALL(GetPhysicalDeviceMemoryProperties2(physicalDevice, &phyDevMemProps));
281 if (!skgpu::AllocateAndBindImageMemory(&alloc,
image, phyDevMemProps, hwbProps, hardwareBuffer,
292 imageInfo.
fFormat = grBackendVkFormat;
301#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
302 imageInfo.fPartOfSwapchainOrAndroidWindow = fromAndroidWindow;
305 *deleteProc = delete_vk_image;
306 *updateProc = update_vk_image;
307 *imageCtx =
new VulkanCleanupHelper(gpu,
image, alloc.
fMemory);
320 DeleteImageProc* deleteProc,
321 UpdateImageProc* updateProc,
322 TexImageCtx* imageCtx,
323 bool isProtectedContent,
326 bool fromAndroidWindow) {
328 if (!dContext || dContext->
abandoned()) {
336 if (isProtectedContent && !can_import_protected_content(dContext)) {
340 return make_vk_backend_texture(dContext, hardwareBuffer,
width,
height, deleteProc,
341 updateProc, imageCtx, isProtectedContent, backendFormat,
342 isRenderable, fromAndroidWindow);
struct AHardwareBuffer AHardwareBuffer
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static constexpr bool SkToBool(const T &x)
SK_API GrBackendApi backend() const
bool abandoned() override
GrDirectContextPriv priv()
bool isVkFormatTexturable(VkFormat) const
bool isFormatRenderable(const GrBackendFormat &format, int sampleCount) const override
bool supportsAndroidHWBExternalMemory() const
const GrVkCaps & vkCaps() const
const skgpu::VulkanInterface * vkInterface() const
VkPhysicalDevice physicalDevice() const
bool protectedContext() const
FlutterSemanticsFlag flags
SK_API GrBackendTexture MakeVk(int width, int height, const GrVkImageInfo &, std::string_view label={})
sk_sp< const SkImage > image
VkSharingMode fSharingMode
skgpu::VulkanAlloc fAlloc
uint32_t fCurrentQueueFamily
skgpu::Protected fProtected
skgpu::VulkanYcbcrConversionInfo fYcbcrConversionInfo
VkImageLayout fImageLayout
VkImageTiling fImageTiling
VkSharingMode sharingMode
@ VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
@ VK_IMAGE_LAYOUT_UNDEFINED
void(VKAPI_PTR * PFN_vkFreeMemory)(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *pAllocator)
@ VK_SHARING_MODE_EXCLUSIVE
VkFlags VkImageUsageFlags
@ VK_IMAGE_CREATE_PROTECTED_BIT
@ VK_IMAGE_TILING_OPTIMAL
void(VKAPI_PTR * PFN_vkDestroyImage)(VkDevice device, VkImage image, const VkAllocationCallbacks *pAllocator)
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
@ VK_IMAGE_USAGE_SAMPLED_BIT
@ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
@ VK_IMAGE_USAGE_TRANSFER_SRC_BIT
@ VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID
VkFlags VkImageCreateFlags
#define VK_QUEUE_FAMILY_EXTERNAL
@ VK_FORMAT_R5G6B5_UNORM_PACK16
@ VK_FORMAT_A2B10G10R10_UNORM_PACK32
@ VK_FORMAT_R8G8B8A8_UNORM
@ VK_FORMAT_R16G16B16A16_SFLOAT
@ VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
@ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2
@ VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID
@ VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO