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