Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 7 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 8 | #include "SkTypes.h" |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 9 | |
| 10 | #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 11 | #define GL_GLEXT_PROTOTYPES |
| 12 | #define EGL_EGLEXT_PROTOTYPES |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 13 | |
| 14 | #include "vk/GrVkVulkan.h" |
| 15 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 16 | #include "GrAHardwareBufferImageGenerator.h" |
| 17 | |
Derek Sollenberger | 7a86987 | 2017-06-27 15:37:25 -0400 | [diff] [blame] | 18 | #include <android/hardware_buffer.h> |
| 19 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 20 | #include "GrBackendSurface.h" |
| 21 | #include "GrContext.h" |
| 22 | #include "GrContextPriv.h" |
Robert Phillips | adbe132 | 2018-01-17 13:35:46 -0500 | [diff] [blame] | 23 | #include "GrProxyProvider.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 24 | #include "GrResourceCache.h" |
Robert Phillips | 0001828 | 2017-06-15 15:35:16 -0400 | [diff] [blame] | 25 | #include "GrResourceProvider.h" |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 26 | #include "GrResourceProviderPriv.h" |
Robert Phillips | 847d4c5 | 2017-06-13 18:21:44 -0400 | [diff] [blame] | 27 | #include "GrTexture.h" |
Robert Phillips | ade9f61 | 2017-06-16 07:32:43 -0400 | [diff] [blame] | 28 | #include "GrTextureProxy.h" |
Stan Iliev | dbba55d | 2017-06-28 13:24:41 -0400 | [diff] [blame] | 29 | #include "SkMessageBus.h" |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 30 | #include "gl/GrGLDefines.h" |
| 31 | #include "gl/GrGLTypes.h" |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 32 | |
| 33 | #include <EGL/egl.h> |
| 34 | #include <EGL/eglext.h> |
| 35 | #include <GLES/gl.h> |
| 36 | #include <GLES/glext.h> |
| 37 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 38 | #ifdef SK_VULKAN |
| 39 | #include "vk/GrVkExtensions.h" |
| 40 | #include "vk/GrVkGpu.h" |
| 41 | #endif |
| 42 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 43 | #define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content" |
| 44 | #define EGL_PROTECTED_CONTENT_EXT 0x32C0 |
| 45 | |
| 46 | static bool can_import_protected_content_eglimpl() { |
| 47 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 48 | const char* exts = eglQueryString(dpy, EGL_EXTENSIONS); |
| 49 | size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR); |
| 50 | size_t extsLen = strlen(exts); |
| 51 | bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts); |
| 52 | bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen+1); |
| 53 | bool atEnd = (cropExtLen+1) < extsLen |
| 54 | && !strcmp(" " PROT_CONTENT_EXT_STR, |
| 55 | exts + extsLen - (cropExtLen+1)); |
| 56 | bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " "); |
| 57 | return equal || atStart || atEnd || inMiddle; |
| 58 | } |
| 59 | |
| 60 | static bool can_import_protected_content(GrContext* context) { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 61 | if (GrBackendApi::kOpenGL == context->contextPriv().getBackend()) { |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 62 | // Only compute whether the extension is present once the first time this |
| 63 | // function is called. |
| 64 | static bool hasIt = can_import_protected_content_eglimpl(); |
| 65 | return hasIt; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 70 | std::unique_ptr<SkImageGenerator> GrAHardwareBufferImageGenerator::Make( |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 71 | AHardwareBuffer* graphicBuffer, SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace, |
| 72 | GrSurfaceOrigin surfaceOrigin) { |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 73 | AHardwareBuffer_Desc bufferDesc; |
| 74 | AHardwareBuffer_describe(graphicBuffer, &bufferDesc); |
| 75 | SkColorType colorType; |
| 76 | switch (bufferDesc.format) { |
| 77 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
Stan Iliev | 733b265 | 2018-10-08 16:15:37 -0400 | [diff] [blame] | 78 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 79 | colorType = kRGBA_8888_SkColorType; |
| 80 | break; |
| 81 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 82 | colorType = kRGBA_F16_SkColorType; |
| 83 | break; |
| 84 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 85 | colorType = kRGB_565_SkColorType; |
| 86 | break; |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 87 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 88 | colorType = kRGB_888x_SkColorType; |
| 89 | break; |
| 90 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 91 | colorType = kRGBA_1010102_SkColorType; |
| 92 | break; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 93 | default: |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 94 | // Given that we only use this texture as a source, colorType will not impact how Skia uses |
| 95 | // the texture. The only potential affect this is anticipated to have is that for some |
| 96 | // format types if we are not bound as an OES texture we may get invalid results for SKP |
| 97 | // capture if we read back the texture. |
| 98 | colorType = kRGBA_8888_SkColorType; |
| 99 | break; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 100 | } |
| 101 | SkImageInfo info = SkImageInfo::Make(bufferDesc.width, bufferDesc.height, colorType, |
| 102 | alphaType, std::move(colorSpace)); |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 103 | bool createProtectedImage = 0 != (bufferDesc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT); |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 104 | return std::unique_ptr<SkImageGenerator>(new GrAHardwareBufferImageGenerator( |
| 105 | info, graphicBuffer, alphaType, createProtectedImage, |
| 106 | bufferDesc.format, surfaceOrigin)); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | GrAHardwareBufferImageGenerator::GrAHardwareBufferImageGenerator(const SkImageInfo& info, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 110 | AHardwareBuffer* hardwareBuffer, SkAlphaType alphaType, bool isProtectedContent, |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 111 | uint32_t bufferFormat, GrSurfaceOrigin surfaceOrigin) |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 112 | : INHERITED(info) |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 113 | , fHardwareBuffer(hardwareBuffer) |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 114 | , fBufferFormat(bufferFormat) |
Stan Iliev | 505dd57 | 2018-09-13 14:20:03 -0400 | [diff] [blame] | 115 | , fIsProtectedContent(isProtectedContent) |
| 116 | , fSurfaceOrigin(surfaceOrigin) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 117 | AHardwareBuffer_acquire(fHardwareBuffer); |
| 118 | } |
| 119 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 120 | GrAHardwareBufferImageGenerator::~GrAHardwareBufferImageGenerator() { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 121 | AHardwareBuffer_release(fHardwareBuffer); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 124 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 125 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 126 | #ifdef SK_VULKAN |
| 127 | |
| 128 | class VulkanCleanupHelper { |
| 129 | public: |
| 130 | VulkanCleanupHelper(GrVkGpu* gpu, VkImage image, VkDeviceMemory memory) |
| 131 | : fDevice(gpu->device()) |
| 132 | , fImage(image) |
| 133 | , fMemory(memory) |
| 134 | , fDestroyImage(gpu->vkInterface()->fFunctions.fDestroyImage) |
| 135 | , fFreeMemory(gpu->vkInterface()->fFunctions.fFreeMemory) {} |
| 136 | ~VulkanCleanupHelper() { |
| 137 | fDestroyImage(fDevice, fImage, nullptr); |
| 138 | fFreeMemory(fDevice, fMemory, nullptr); |
| 139 | } |
| 140 | private: |
| 141 | VkDevice fDevice; |
| 142 | VkImage fImage; |
| 143 | VkDeviceMemory fMemory; |
| 144 | PFN_vkDestroyImage fDestroyImage; |
| 145 | PFN_vkFreeMemory fFreeMemory; |
| 146 | }; |
| 147 | |
| 148 | void GrAHardwareBufferImageGenerator::DeleteVkImage(void* context) { |
| 149 | VulkanCleanupHelper* cleanupHelper = static_cast<VulkanCleanupHelper*>(context); |
| 150 | delete cleanupHelper; |
| 151 | } |
| 152 | |
| 153 | #define VK_CALL(X) gpu->vkInterface()->fFunctions.f##X; |
| 154 | |
| 155 | static GrBackendTexture make_vk_backend_texture( |
| 156 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
| 157 | int width, int height, GrPixelConfig config, |
| 158 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
| 159 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
| 160 | bool isProtectedContent, |
| 161 | const GrBackendFormat& backendFormat) { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 162 | SkASSERT(context->contextPriv().getBackend() == GrBackendApi::kVulkan); |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 163 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu()); |
| 164 | |
| 165 | VkPhysicalDevice physicalDevice = gpu->physicalDevice(); |
| 166 | VkDevice device = gpu->device(); |
| 167 | |
| 168 | SkASSERT(gpu); |
| 169 | |
| 170 | if (!gpu->vkCaps().supportsAndroidHWBExternalMemory()) { |
| 171 | return GrBackendTexture(); |
| 172 | } |
| 173 | |
| 174 | SkASSERT(backendFormat.getVkFormat()); |
| 175 | VkFormat format = *backendFormat.getVkFormat(); |
| 176 | |
| 177 | VkResult err; |
| 178 | |
| 179 | VkAndroidHardwareBufferFormatPropertiesANDROID hwbFormatProps; |
| 180 | hwbFormatProps.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID; |
| 181 | hwbFormatProps.pNext = nullptr; |
| 182 | |
| 183 | VkAndroidHardwareBufferPropertiesANDROID hwbProps; |
| 184 | hwbProps.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID; |
| 185 | hwbProps.pNext = &hwbFormatProps; |
| 186 | |
| 187 | err = VK_CALL(GetAndroidHardwareBufferProperties(device, hardwareBuffer, &hwbProps)); |
| 188 | if (VK_SUCCESS != err) { |
| 189 | return GrBackendTexture(); |
| 190 | } |
| 191 | |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 192 | VkExternalFormatANDROID externalFormat; |
| 193 | externalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID; |
| 194 | externalFormat.pNext = nullptr; |
| 195 | externalFormat.externalFormat = 0; // If this is zero it is as if we aren't using this struct. |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 196 | |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 197 | const GrVkYcbcrConversionInfo* ycbcrConversion = backendFormat.getVkYcbcrConversionInfo(); |
| 198 | if (!ycbcrConversion) { |
| 199 | return GrBackendTexture(); |
| 200 | } |
| 201 | |
| 202 | if (hwbFormatProps.format != VK_FORMAT_UNDEFINED) { |
| 203 | // TODO: We should not assume the transfer features here and instead should have a way for |
| 204 | // Ganesh's tracking of intenral images to report whether or not they support transfers. |
| 205 | SkASSERT(SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & hwbFormatProps.formatFeatures) && |
| 206 | SkToBool(VK_FORMAT_FEATURE_TRANSFER_SRC_BIT & hwbFormatProps.formatFeatures) && |
| 207 | SkToBool(VK_FORMAT_FEATURE_TRANSFER_DST_BIT & hwbFormatProps.formatFeatures)); |
| 208 | SkASSERT(!ycbcrConversion->isValid()); |
| 209 | } else { |
| 210 | SkASSERT(ycbcrConversion->isValid()); |
| 211 | // We have an external only format |
| 212 | SkASSERT(SkToBool(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT & hwbFormatProps.formatFeatures)); |
| 213 | SkASSERT(format == VK_FORMAT_UNDEFINED); |
| 214 | SkASSERT(hwbFormatProps.externalFormat == ycbcrConversion->fExternalFormat); |
| 215 | externalFormat.externalFormat = hwbFormatProps.externalFormat; |
| 216 | } |
| 217 | SkASSERT(format == hwbFormatProps.format); |
| 218 | |
| 219 | const VkExternalMemoryImageCreateInfo externalMemoryImageInfo{ |
| 220 | VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, // sType |
| 221 | &externalFormat, // pNext |
| 222 | VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID, // handleTypes |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 223 | }; |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 224 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 225 | if (format != VK_FORMAT_UNDEFINED) { |
| 226 | usageFlags = usageFlags | |
| 227 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 228 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 229 | } |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 230 | |
| 231 | // TODO: Check the supported tilings vkGetPhysicalDeviceImageFormatProperties2 to see if we have |
| 232 | // to use linear. Add better linear support throughout Ganesh. |
| 233 | VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL; |
| 234 | |
| 235 | const VkImageCreateInfo imageCreateInfo = { |
| 236 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 237 | &externalMemoryImageInfo, // pNext |
| 238 | 0, // VkImageCreateFlags |
| 239 | VK_IMAGE_TYPE_2D, // VkImageType |
| 240 | format, // VkFormat |
| 241 | { (uint32_t)width, (uint32_t)height, 1 }, // VkExtent3D |
| 242 | 1, // mipLevels |
| 243 | 1, // arrayLayers |
| 244 | VK_SAMPLE_COUNT_1_BIT, // samples |
| 245 | tiling, // VkImageTiling |
| 246 | usageFlags, // VkImageUsageFlags |
| 247 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 248 | 0, // queueFamilyCount |
| 249 | 0, // pQueueFamilyIndices |
| 250 | VK_IMAGE_LAYOUT_UNDEFINED, // initialLayout |
| 251 | }; |
| 252 | |
| 253 | VkImage image; |
| 254 | err = VK_CALL(CreateImage(device, &imageCreateInfo, nullptr, &image)); |
| 255 | if (VK_SUCCESS != err) { |
| 256 | return GrBackendTexture(); |
| 257 | } |
| 258 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 259 | VkPhysicalDeviceMemoryProperties2 phyDevMemProps; |
| 260 | phyDevMemProps.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2; |
| 261 | phyDevMemProps.pNext = nullptr; |
| 262 | |
| 263 | uint32_t typeIndex = 0; |
| 264 | uint32_t heapIndex = 0; |
| 265 | bool foundHeap = false; |
| 266 | VK_CALL(GetPhysicalDeviceMemoryProperties2(physicalDevice, &phyDevMemProps)); |
| 267 | uint32_t memTypeCnt = phyDevMemProps.memoryProperties.memoryTypeCount; |
| 268 | for (uint32_t i = 0; i < memTypeCnt && !foundHeap; ++i) { |
| 269 | if (hwbProps.memoryTypeBits & (1 << i)) { |
| 270 | const VkPhysicalDeviceMemoryProperties& pdmp = phyDevMemProps.memoryProperties; |
| 271 | uint32_t supportedFlags = pdmp.memoryTypes[i].propertyFlags & |
| 272 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 273 | if (supportedFlags == VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { |
| 274 | typeIndex = i; |
| 275 | heapIndex = pdmp.memoryTypes[i].heapIndex; |
| 276 | foundHeap = true; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | if (!foundHeap) { |
| 281 | VK_CALL(DestroyImage(device, image, nullptr)); |
| 282 | return GrBackendTexture(); |
| 283 | } |
| 284 | |
| 285 | VkImportAndroidHardwareBufferInfoANDROID hwbImportInfo; |
| 286 | hwbImportInfo.sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID; |
| 287 | hwbImportInfo.pNext = nullptr; |
| 288 | hwbImportInfo.buffer = hardwareBuffer; |
| 289 | |
| 290 | VkMemoryDedicatedAllocateInfo dedicatedAllocInfo; |
| 291 | dedicatedAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO; |
| 292 | dedicatedAllocInfo.pNext = &hwbImportInfo; |
| 293 | dedicatedAllocInfo.image = image; |
| 294 | dedicatedAllocInfo.buffer = VK_NULL_HANDLE; |
| 295 | |
| 296 | VkMemoryAllocateInfo allocInfo = { |
| 297 | VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // sType |
| 298 | &dedicatedAllocInfo, // pNext |
| 299 | hwbProps.allocationSize, // allocationSize |
| 300 | typeIndex, // memoryTypeIndex |
| 301 | }; |
| 302 | |
| 303 | VkDeviceMemory memory; |
| 304 | |
| 305 | err = VK_CALL(AllocateMemory(device, &allocInfo, nullptr, &memory)); |
| 306 | if (VK_SUCCESS != err) { |
| 307 | VK_CALL(DestroyImage(device, image, nullptr)); |
| 308 | return GrBackendTexture(); |
| 309 | } |
| 310 | |
| 311 | VkBindImageMemoryInfo bindImageInfo; |
| 312 | bindImageInfo.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO; |
| 313 | bindImageInfo.pNext = nullptr; |
| 314 | bindImageInfo.image = image; |
| 315 | bindImageInfo.memory = memory; |
| 316 | bindImageInfo.memoryOffset = 0; |
| 317 | |
| 318 | err = VK_CALL(BindImageMemory2(device, 1, &bindImageInfo)); |
| 319 | if (VK_SUCCESS != err) { |
| 320 | VK_CALL(DestroyImage(device, image, nullptr)); |
| 321 | VK_CALL(FreeMemory(device, memory, nullptr)); |
| 322 | return GrBackendTexture(); |
| 323 | } |
| 324 | |
| 325 | GrVkImageInfo imageInfo; |
| 326 | |
| 327 | imageInfo.fImage = image; |
| 328 | imageInfo.fAlloc = GrVkAlloc(memory, 0, hwbProps.allocationSize, 0); |
| 329 | imageInfo.fImageTiling = tiling; |
| 330 | imageInfo.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 331 | imageInfo.fFormat = format; |
| 332 | imageInfo.fLevelCount = 1; |
| 333 | // TODO: This should possibly be VK_QUEUE_FAMILY_FOREIGN_EXT but current Adreno devices do not |
| 334 | // support that extension. Or if we know the source of the AHardwareBuffer is not from a |
| 335 | // "foreign" device we can leave them as external. |
| 336 | imageInfo.fCurrentQueueFamily = VK_QUEUE_FAMILY_EXTERNAL; |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 337 | imageInfo.fYcbcrConversionInfo = *ycbcrConversion; |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 338 | |
| 339 | *deleteProc = GrAHardwareBufferImageGenerator::DeleteVkImage; |
| 340 | *deleteCtx = new VulkanCleanupHelper(gpu, image, memory); |
| 341 | |
| 342 | return GrBackendTexture(width, height, imageInfo); |
| 343 | } |
| 344 | #endif |
| 345 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 346 | class GLCleanupHelper { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 347 | public: |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 348 | GLCleanupHelper(GrGLuint texID, EGLImageKHR image, EGLDisplay display) |
| 349 | : fTexID(texID) |
| 350 | , fImage(image) |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 351 | , fDisplay(display) { } |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 352 | ~GLCleanupHelper() { |
| 353 | glDeleteTextures(1, &fTexID); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 354 | // eglDestroyImageKHR will remove a ref from the AHardwareBuffer |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 355 | eglDestroyImageKHR(fDisplay, fImage); |
| 356 | } |
| 357 | private: |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 358 | GrGLuint fTexID; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 359 | EGLImageKHR fImage; |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 360 | EGLDisplay fDisplay; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 361 | }; |
| 362 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 363 | void GrAHardwareBufferImageGenerator::DeleteGLTexture(void* context) { |
| 364 | GLCleanupHelper* cleanupHelper = static_cast<GLCleanupHelper*>(context); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 365 | delete cleanupHelper; |
| 366 | } |
| 367 | |
| 368 | static GrBackendTexture make_gl_backend_texture( |
| 369 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 370 | int width, int height, GrPixelConfig config, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 371 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 372 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 373 | bool isProtectedContent, |
| 374 | const GrBackendFormat& backendFormat) { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 375 | while (GL_NO_ERROR != glGetError()) {} //clear GL errors |
| 376 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 377 | EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hardwareBuffer); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 378 | EGLint attribs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 379 | isProtectedContent ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, |
| 380 | isProtectedContent ? EGL_TRUE : EGL_NONE, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 381 | EGL_NONE }; |
| 382 | EGLDisplay display = eglGetCurrentDisplay(); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 383 | // eglCreateImageKHR will add a ref to the AHardwareBuffer |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 384 | EGLImageKHR image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, |
| 385 | clientBuffer, attribs); |
| 386 | if (EGL_NO_IMAGE_KHR == image) { |
| 387 | SkDebugf("Could not create EGL image, err = (%#x)", (int) eglGetError() ); |
| 388 | return GrBackendTexture(); |
| 389 | } |
| 390 | |
| 391 | GrGLuint texID; |
| 392 | glGenTextures(1, &texID); |
| 393 | if (!texID) { |
| 394 | eglDestroyImageKHR(display, image); |
| 395 | return GrBackendTexture(); |
| 396 | } |
| 397 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, texID); |
| 398 | GLenum status = GL_NO_ERROR; |
| 399 | if ((status = glGetError()) != GL_NO_ERROR) { |
| 400 | SkDebugf("glBindTexture failed (%#x)", (int) status); |
| 401 | glDeleteTextures(1, &texID); |
| 402 | eglDestroyImageKHR(display, image); |
| 403 | return GrBackendTexture(); |
| 404 | } |
| 405 | glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image); |
| 406 | if ((status = glGetError()) != GL_NO_ERROR) { |
| 407 | SkDebugf("glEGLImageTargetTexture2DOES failed (%#x)", (int) status); |
| 408 | glDeleteTextures(1, &texID); |
| 409 | eglDestroyImageKHR(display, image); |
| 410 | return GrBackendTexture(); |
| 411 | } |
| 412 | context->resetContext(kTextureBinding_GrGLBackendState); |
| 413 | |
| 414 | GrGLTextureInfo textureInfo; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 415 | textureInfo.fID = texID; |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 416 | SkASSERT(backendFormat.isValid()); |
| 417 | textureInfo.fTarget = *backendFormat.getGLTarget(); |
| 418 | textureInfo.fFormat = *backendFormat.getGLFormat(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 419 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 420 | *deleteProc = GrAHardwareBufferImageGenerator::DeleteGLTexture; |
| 421 | *deleteCtx = new GLCleanupHelper(texID, image, display); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 422 | |
| 423 | return GrBackendTexture(width, height, GrMipMapped::kNo, textureInfo); |
| 424 | } |
| 425 | |
| 426 | static GrBackendTexture make_backend_texture( |
| 427 | GrContext* context, AHardwareBuffer* hardwareBuffer, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 428 | int width, int height, GrPixelConfig config, |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 429 | GrAHardwareBufferImageGenerator::DeleteImageProc* deleteProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 430 | GrAHardwareBufferImageGenerator::DeleteImageCtx* deleteCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 431 | bool isProtectedContent, |
| 432 | const GrBackendFormat& backendFormat) { |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 433 | if (context->abandoned()) { |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 434 | return GrBackendTexture(); |
| 435 | } |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 436 | bool createProtectedImage = isProtectedContent && can_import_protected_content(context); |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 437 | |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 438 | if (GrBackendApi::kOpenGL == context->contextPriv().getBackend()) { |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 439 | return make_gl_backend_texture(context, hardwareBuffer, width, height, config, deleteProc, |
| 440 | deleteCtx, createProtectedImage, backendFormat); |
| 441 | } else { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 442 | SkASSERT(GrBackendApi::kVulkan == context->contextPriv().getBackend()); |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 443 | #ifdef SK_VULKAN |
| 444 | // Currently we don't support protected images on vulkan |
| 445 | SkASSERT(!createProtectedImage); |
| 446 | return make_vk_backend_texture(context, hardwareBuffer, width, height, config, deleteProc, |
| 447 | deleteCtx, createProtectedImage, backendFormat); |
| 448 | #else |
| 449 | return GrBackendTexture(); |
| 450 | #endif |
| 451 | } |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 454 | GrBackendFormat get_backend_format(GrContext* context, AHardwareBuffer* hardwareBuffer, |
| 455 | GrBackendApi backend, uint32_t bufferFormat) { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 456 | if (backend == GrBackendApi::kOpenGL) { |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 457 | switch (bufferFormat) { |
| 458 | //TODO: find out if we can detect, which graphic buffers support GR_GL_TEXTURE_2D |
| 459 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
Stan Iliev | 733b265 | 2018-10-08 16:15:37 -0400 | [diff] [blame] | 460 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 461 | return GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL); |
| 462 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 463 | return GrBackendFormat::MakeGL(GR_GL_RGBA16F, GR_GL_TEXTURE_EXTERNAL); |
| 464 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 465 | return GrBackendFormat::MakeGL(GR_GL_RGB565, GR_GL_TEXTURE_EXTERNAL); |
| 466 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 467 | return GrBackendFormat::MakeGL(GR_GL_RGB10_A2, GR_GL_TEXTURE_EXTERNAL); |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 468 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 469 | return GrBackendFormat::MakeGL(GR_GL_RGB8, GR_GL_TEXTURE_EXTERNAL); |
| 470 | default: |
| 471 | return GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_EXTERNAL); |
| 472 | } |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 473 | } else if (backend == GrBackendApi::kVulkan) { |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 474 | switch (bufferFormat) { |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 475 | case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM: |
| 476 | return GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM); |
| 477 | case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT: |
| 478 | return GrBackendFormat::MakeVk(VK_FORMAT_R16G16B16A16_SFLOAT); |
| 479 | case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM: |
| 480 | return GrBackendFormat::MakeVk(VK_FORMAT_R5G6B5_UNORM_PACK16); |
| 481 | case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM: |
| 482 | return GrBackendFormat::MakeVk(VK_FORMAT_A2B10G10R10_UNORM_PACK32); |
| 483 | case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM: |
| 484 | return GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8A8_UNORM); |
| 485 | case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM: |
| 486 | return GrBackendFormat::MakeVk(VK_FORMAT_R8G8B8_UNORM); |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 487 | default: { |
| 488 | GrVkGpu* gpu = static_cast<GrVkGpu*>(context->contextPriv().getGpu()); |
| 489 | SkASSERT(gpu); |
| 490 | VkDevice device = gpu->device(); |
| 491 | |
| 492 | if (!gpu->vkCaps().supportsAndroidHWBExternalMemory()) { |
| 493 | return GrBackendFormat(); |
| 494 | } |
| 495 | VkAndroidHardwareBufferFormatPropertiesANDROID hwbFormatProps; |
| 496 | hwbFormatProps.sType = |
| 497 | VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID; |
| 498 | hwbFormatProps.pNext = nullptr; |
| 499 | |
| 500 | VkAndroidHardwareBufferPropertiesANDROID hwbProps; |
| 501 | hwbProps.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID; |
| 502 | hwbProps.pNext = &hwbFormatProps; |
| 503 | |
| 504 | VkResult err = VK_CALL(GetAndroidHardwareBufferProperties(device, hardwareBuffer, |
| 505 | &hwbProps)); |
| 506 | if (VK_SUCCESS != err) { |
| 507 | return GrBackendFormat(); |
| 508 | } |
| 509 | |
| 510 | if (hwbFormatProps.format != VK_FORMAT_UNDEFINED) { |
| 511 | return GrBackendFormat(); |
| 512 | } |
| 513 | |
| 514 | GrVkYcbcrConversionInfo ycbcrConversion; |
| 515 | ycbcrConversion.fYcbcrModel = hwbFormatProps.suggestedYcbcrModel; |
| 516 | ycbcrConversion.fYcbcrRange = hwbFormatProps.suggestedYcbcrRange; |
| 517 | ycbcrConversion.fXChromaOffset = hwbFormatProps.suggestedXChromaOffset; |
| 518 | ycbcrConversion.fYChromaOffset = hwbFormatProps.suggestedYChromaOffset; |
| 519 | ycbcrConversion.fForceExplicitReconstruction = VK_FALSE; |
| 520 | ycbcrConversion.fExternalFormat = hwbFormatProps.externalFormat; |
| 521 | ycbcrConversion.fExternalFormatFeatures = hwbFormatProps.formatFeatures; |
| 522 | if (VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT & |
| 523 | hwbFormatProps.formatFeatures) { |
| 524 | ycbcrConversion.fChromaFilter = VK_FILTER_LINEAR; |
| 525 | } else { |
| 526 | ycbcrConversion.fChromaFilter = VK_FILTER_NEAREST; |
| 527 | } |
| 528 | |
| 529 | return GrBackendFormat::MakeVk(ycbcrConversion); |
| 530 | } |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 531 | } |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 532 | } |
| 533 | return GrBackendFormat(); |
| 534 | } |
| 535 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 536 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::makeProxy(GrContext* context) { |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 537 | if (context->abandoned()) { |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 538 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 539 | } |
| 540 | |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 541 | GrPixelConfig pixelConfig; |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 542 | GrBackendFormat backendFormat = get_backend_format(context, fHardwareBuffer, |
| 543 | context->contextPriv().getBackend(), |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 544 | fBufferFormat); |
| 545 | if (!context->contextPriv().caps()->getConfigFromBackendFormat( |
| 546 | backendFormat, this->getInfo().colorType(), &pixelConfig)) { |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 547 | return nullptr; |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 550 | int width = this->getInfo().width(); |
| 551 | int height = this->getInfo().height(); |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 552 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 553 | GrSurfaceDesc desc; |
| 554 | desc.fWidth = width; |
| 555 | desc.fHeight = height; |
| 556 | desc.fConfig = pixelConfig; |
Greg Daniel | 9af948d | 2018-08-27 09:53:51 -0400 | [diff] [blame] | 557 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 558 | GrTextureType textureType = GrTextureType::k2D; |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 559 | if (context->contextPriv().getBackend() == GrBackendApi::kOpenGL) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 560 | textureType = GrTextureType::kExternal; |
Greg Daniel | 14c55c2 | 2018-12-04 11:25:03 -0500 | [diff] [blame^] | 561 | } else if (context->contextPriv().getBackend() == GrBackendApi::kVulkan) { |
| 562 | const VkFormat* format = backendFormat.getVkFormat(); |
| 563 | SkASSERT(format); |
| 564 | if (*format == VK_FORMAT_UNDEFINED) { |
| 565 | textureType = GrTextureType::kExternal; |
| 566 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 567 | } |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 568 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 569 | auto proxyProvider = context->contextPriv().proxyProvider(); |
| 570 | |
| 571 | AHardwareBuffer* hardwareBuffer = fHardwareBuffer; |
| 572 | AHardwareBuffer_acquire(hardwareBuffer); |
| 573 | |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 574 | const bool isProtectedContent = fIsProtectedContent; |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 575 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 576 | sk_sp<GrTextureProxy> texProxy = proxyProvider->createLazyProxy( |
| 577 | [context, hardwareBuffer, width, height, pixelConfig, isProtectedContent, backendFormat] |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 578 | (GrResourceProvider* resourceProvider) { |
| 579 | if (!resourceProvider) { |
| 580 | AHardwareBuffer_release(hardwareBuffer); |
| 581 | return sk_sp<GrTexture>(); |
| 582 | } |
| 583 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 584 | DeleteImageProc deleteImageProc = nullptr; |
| 585 | DeleteImageCtx deleteImageCtx = nullptr; |
| 586 | |
| 587 | GrBackendTexture backendTex = make_backend_texture(context, hardwareBuffer, |
| 588 | width, height, pixelConfig, |
| 589 | &deleteImageProc, |
Stan Iliev | c01b5c7 | 2018-08-28 10:18:19 -0400 | [diff] [blame] | 590 | &deleteImageCtx, |
Stan Iliev | 114b091 | 2018-08-31 14:02:55 -0400 | [diff] [blame] | 591 | isProtectedContent, |
| 592 | backendFormat); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 593 | if (!backendTex.isValid()) { |
| 594 | return sk_sp<GrTexture>(); |
| 595 | } |
| 596 | SkASSERT(deleteImageProc && deleteImageCtx); |
| 597 | |
| 598 | backendTex.fConfig = pixelConfig; |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 599 | sk_sp<GrTexture> tex = resourceProvider->wrapBackendTexture(backendTex); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 600 | if (!tex) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 601 | deleteImageProc(deleteImageCtx); |
| 602 | return sk_sp<GrTexture>(); |
| 603 | } |
| 604 | |
Greg Daniel | 637c06a | 2018-09-12 09:44:25 -0400 | [diff] [blame] | 605 | if (deleteImageProc) { |
| 606 | sk_sp<GrReleaseProcHelper> releaseProcHelper( |
| 607 | new GrReleaseProcHelper(deleteImageProc, deleteImageCtx)); |
| 608 | tex->setRelease(releaseProcHelper); |
| 609 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 610 | |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 611 | return tex; |
| 612 | }, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 613 | backendFormat, desc, fSurfaceOrigin, GrMipMapped::kNo, SkBackingFit::kExact, |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 614 | SkBudgeted::kNo); |
| 615 | |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 616 | if (!texProxy) { |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 617 | AHardwareBuffer_release(hardwareBuffer); |
Greg Daniel | f125111 | 2018-08-27 09:55:03 -0400 | [diff] [blame] | 618 | } |
Greg Daniel | 3860cfd | 2018-09-07 09:13:54 -0400 | [diff] [blame] | 619 | return texProxy; |
| 620 | } |
| 621 | |
| 622 | sk_sp<GrTextureProxy> GrAHardwareBufferImageGenerator::onGenerateTexture( |
| 623 | GrContext* context, const SkImageInfo& info, const SkIPoint& origin, bool willNeedMipMaps) { |
| 624 | sk_sp<GrTextureProxy> texProxy = this->makeProxy(context); |
| 625 | if (!texProxy) { |
| 626 | return nullptr; |
| 627 | } |
| 628 | |
| 629 | if (0 == origin.fX && 0 == origin.fY && |
| 630 | info.width() == this->getInfo().width() && info.height() == this->getInfo().height()) { |
| 631 | // If the caller wants the full texture we're done. The caller will handle making a copy for |
| 632 | // mip maps if that is required. |
| 633 | return texProxy; |
| 634 | } |
| 635 | // Otherwise, make a copy for the requested subset. |
| 636 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, info.width(), info.height()); |
| 637 | |
| 638 | GrMipMapped mipMapped = willNeedMipMaps ? GrMipMapped::kYes : GrMipMapped::kNo; |
| 639 | |
| 640 | return GrSurfaceProxy::Copy(context, texProxy.get(), mipMapped, subset, SkBudgeted::kYes); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 641 | } |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 642 | |
| 643 | bool GrAHardwareBufferImageGenerator::onIsValid(GrContext* context) const { |
| 644 | if (nullptr == context) { |
| 645 | return false; //CPU backend is not supported, because hardware buffer can be swizzled |
| 646 | } |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 647 | return GrBackendApi::kOpenGL == context->contextPriv().getBackend() || |
| 648 | GrBackendApi::kVulkan == context->contextPriv().getBackend(); |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | #endif //SK_BUILD_FOR_ANDROID_FRAMEWORK |