10#if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26
23#define VK_CALL(X) gpu->vkInterface()->fFunctions.f##X
25namespace GrAHardwareBufferUtils {
28 uint32_t bufferFormat,
bool requireKnownFormat) {
35 switch (bufferFormat) {
36 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: {
40 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: {
44 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: {
48 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: {
52 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: {
56 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: {
60#if __ANDROID_API__ >= 33
61 case AHARDWAREBUFFER_FORMAT_R8_UNORM: {
67 if (requireKnownFormat) {
98 if (!GetAHardwareBufferProperties(
104 GetYcbcrConversionInfoFromFormatProps(&ycbcrConversion, hwbFormatProps);
109class VulkanCleanupHelper {
111 VulkanCleanupHelper(
GrVkGpu* gpu, VkImage
image, VkDeviceMemory memory)
115 , fDestroyImage(gpu->vkInterface()->fFunctions.fDestroyImage)
116 , fFreeMemory(gpu->vkInterface()->fFunctions.fFreeMemory) {}
117 ~VulkanCleanupHelper() {
118 fDestroyImage(fDevice, fImage,
nullptr);
119 fFreeMemory(fDevice, fMemory,
nullptr);
124 VkDeviceMemory fMemory;
129void delete_vk_image(
void* context) {
130 VulkanCleanupHelper* cleanupHelper =
static_cast<VulkanCleanupHelper*
>(context);
131 delete cleanupHelper;
141 DeleteImageProc* deleteProc,
142 UpdateImageProc* updateProc,
143 TexImageCtx* imageCtx,
144 bool isProtectedContent,
147 bool fromAndroidWindow) {
163 SkDebugf(
"AsVkFormat failed (valid: %d, backend: %u)",
165 (
unsigned)grBackendFormat.
backend());
172 if (!skgpu::GetAHardwareBufferProperties(
184 if (!importAsExternalFormat && hwbVkFormat != grBackendVkFormat) {
185 SkDebugf(
"Queried format not consistent with expected format; got: %d, expected: %d",
193 externalFormat.
pNext =
nullptr;
198 if (!ycbcrConversion) {
206 if (isRenderable && (importAsExternalFormat ||
208 SkDebugf(
"Renderable texture requested from an AHardwareBuffer which uses a "
209 "VkFormat that Skia cannot render to (VkFormat: %d).\n", grBackendVkFormat);
213 if (importAsExternalFormat) {
214 if (!ycbcrConversion->
isValid()) {
215 SkDebugf(
"YCbCr conversion must be valid when importing an AHardwareBuffer with an "
238 if (!importAsExternalFormat) {
239 usageFlags = usageFlags |
251 &externalMemoryImageInfo,
276 phyDevMemProps.
pNext =
nullptr;
277 VK_CALL(GetPhysicalDeviceMemoryProperties2(physicalDevice, &phyDevMemProps));
280 if (!skgpu::AllocateAndBindImageMemory(&alloc,
image, phyDevMemProps, hwbProps, hardwareBuffer,
291 imageInfo.
fFormat = grBackendVkFormat;
297 imageInfo.
fProtected = isProtectedContent ? GrProtected::kYes : GrProtected::kNo;
300#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
301 imageInfo.fPartOfSwapchainOrAndroidWindow = fromAndroidWindow;
304 *deleteProc = delete_vk_image;
305 *updateProc = update_vk_image;
306 *imageCtx =
new VulkanCleanupHelper(gpu,
image, alloc.
fMemory);
319 DeleteImageProc* deleteProc,
320 UpdateImageProc* updateProc,
321 TexImageCtx* imageCtx,
322 bool isProtectedContent,
325 bool fromAndroidWindow) {
327 if (!dContext || dContext->
abandoned()) {
335 if (isProtectedContent && !can_import_protected_content(dContext)) {
339 return make_vk_backend_texture(dContext, hardwareBuffer,
width,
height, deleteProc,
340 updateProc, imageCtx, isProtectedContent, backendFormat,
341 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={})
GrVkYcbcrConversionInfo fYcbcrConversionInfo
VkSharingMode fSharingMode
skgpu::VulkanAlloc fAlloc
uint32_t fCurrentQueueFamily
skgpu::Protected fProtected
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