• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // HardwareBufferImageSiblingVkAndroid.cpp: Implements HardwareBufferImageSiblingVkAndroid.
8 
9 #include "libANGLE/renderer/vulkan/android/HardwareBufferImageSiblingVkAndroid.h"
10 
11 #include "common/android_util.h"
12 
13 #include "libANGLE/Display.h"
14 #include "libANGLE/renderer/vulkan/DisplayVk.h"
15 #include "libANGLE/renderer/vulkan/android/AHBFunctions.h"
16 #include "libANGLE/renderer/vulkan/android/DisplayVkAndroid.h"
17 #include "libANGLE/renderer/vulkan/vk_renderer.h"
18 
19 namespace rx
20 {
21 
22 namespace
23 {
AhbDescUsageToVkImageTiling(const AHardwareBuffer_Desc & ahbDescription)24 VkImageTiling AhbDescUsageToVkImageTiling(const AHardwareBuffer_Desc &ahbDescription)
25 {
26     // A note about the choice of OPTIMAL here.
27 
28     // When running Android on certain GPUs, there are problems creating Vulkan
29     // image siblings of AHardwareBuffers because it's currently assumed that
30     // the underlying driver can create linear tiling images that have input
31     // attachment usage, which isn't supported on NVIDIA for example, resulting
32     // in failure to create the image siblings. Yet, we don't currently take
33     // advantage of linear elsewhere in ANGLE. To maintain maximum
34     // compatibility on Android for such drivers, use optimal tiling for image
35     // siblings.
36     //
37     // Note that while we have switched to optimal unconditionally in this path
38     // versus linear, it's possible that previously compatible linear usages
39     // might become uncompatible after switching to optimal. However, from what
40     // we've seen on Samsung/NVIDIA/Intel/AMD GPUs so far, formats generally
41     // have more possible usages in optimal tiling versus linear tiling:
42     //
43     // http://vulkan.gpuinfo.org/displayreport.php?id=10804#formats_linear
44     // http://vulkan.gpuinfo.org/displayreport.php?id=10804#formats_optimal
45     //
46     // http://vulkan.gpuinfo.org/displayreport.php?id=10807#formats_linear
47     // http://vulkan.gpuinfo.org/displayreport.php?id=10807#formats_optimal
48     //
49     // http://vulkan.gpuinfo.org/displayreport.php?id=10809#formats_linear
50     // http://vulkan.gpuinfo.org/displayreport.php?id=10809#formats_optimal
51     //
52     // http://vulkan.gpuinfo.org/displayreport.php?id=10787#formats_linear
53     // http://vulkan.gpuinfo.org/displayreport.php?id=10787#formats_optimal
54     //
55     // Also, as an aside, in terms of what's generally expected from the Vulkan
56     // ICD in Android when determining AHB compatibility, if the vendor wants
57     // to declare a particular combination of format/tiling/usage/etc as not
58     // supported AHB-wise, it's up to the ICD vendor to zero out bits in
59     // supportedHandleTypes in the vkGetPhysicalDeviceImageFormatProperties2
60     // query:
61     //
62     // ``` *
63     // [VUID-VkImageCreateInfo-pNext-00990](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VUID-VkImageCreateInfo-pNext-00990)
64     // If the pNext chain includes a VkExternalMemoryImageCreateInfo structure,
65     // its handleTypes member must only contain bits that are also in
66     // VkExternalImageFormatProperties::externalMemoryProperties.compatibleHandleTypes,
67     // as returned by vkGetPhysicalDeviceImageFormatProperties2 with format,
68     // imageType, tiling, usage, and flags equal to those in this structure,
69     // and with a VkPhysicalDeviceExternalImageFormatInfo structure included in
70     // the pNext chain, with a handleType equal to any one of the handle types
71     // specified in VkExternalMemoryImageCreateInfo::handleTypes ```
72 
73     return VK_IMAGE_TILING_OPTIMAL;
74 }
75 
76 // Map AHB usage flags to VkImageUsageFlags using this table from the Vulkan spec
77 // https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap11.html#memory-external-android-hardware-buffer-usage
AhbDescUsageToVkImageUsage(const AHardwareBuffer_Desc & ahbDescription,bool isDepthOrStencilFormat)78 VkImageUsageFlags AhbDescUsageToVkImageUsage(const AHardwareBuffer_Desc &ahbDescription,
79                                              bool isDepthOrStencilFormat)
80 {
81     VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
82 
83     if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) != 0)
84     {
85         usage |= VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
86     }
87 
88     if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) != 0)
89     {
90         if (isDepthOrStencilFormat)
91         {
92             usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
93         }
94         else
95         {
96             usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
97         }
98     }
99 
100     return usage;
101 }
102 
103 // Map AHB usage flags to VkImageCreateFlags using this table from the Vulkan spec
104 // https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap11.html#memory-external-android-hardware-buffer-usage
AhbDescUsageToVkImageCreateFlags(const AHardwareBuffer_Desc & ahbDescription)105 VkImageCreateFlags AhbDescUsageToVkImageCreateFlags(const AHardwareBuffer_Desc &ahbDescription)
106 {
107     VkImageCreateFlags imageCreateFlags = vk::kVkImageCreateFlagsNone;
108 
109     if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) != 0)
110     {
111         imageCreateFlags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
112     }
113 
114     if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) != 0)
115     {
116         imageCreateFlags |= VK_IMAGE_CREATE_PROTECTED_BIT;
117     }
118 
119     return imageCreateFlags;
120 }
121 
122 // Deduce texture type based on AHB usage flags and layer count
AhbDescUsageToTextureType(const AHardwareBuffer_Desc & ahbDescription,const uint32_t layerCount)123 gl::TextureType AhbDescUsageToTextureType(const AHardwareBuffer_Desc &ahbDescription,
124                                           const uint32_t layerCount)
125 {
126     gl::TextureType textureType = layerCount > 1 ? gl::TextureType::_2DArray : gl::TextureType::_2D;
127     if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) != 0)
128     {
129         textureType = layerCount > gl::kCubeFaceCount ? gl::TextureType::CubeMapArray
130                                                       : gl::TextureType::CubeMap;
131     }
132     return textureType;
133 }
134 // TODO(anglebug.com/7956): remove when NDK header is updated to contain FRONT_BUFFER usage flag
135 constexpr uint64_t kAHardwareBufferUsageFrontBuffer = (1ULL << 32);
136 }  // namespace
137 
HardwareBufferImageSiblingVkAndroid(EGLClientBuffer buffer)138 HardwareBufferImageSiblingVkAndroid::HardwareBufferImageSiblingVkAndroid(EGLClientBuffer buffer)
139     : mBuffer(buffer),
140       mFormat(GL_NONE),
141       mRenderable(false),
142       mTextureable(false),
143       mYUV(false),
144       mLevelCount(0),
145       mUsage(0),
146       mSamples(0),
147       mImage(nullptr)
148 {}
149 
~HardwareBufferImageSiblingVkAndroid()150 HardwareBufferImageSiblingVkAndroid::~HardwareBufferImageSiblingVkAndroid() {}
151 
152 // Static
ValidateHardwareBuffer(vk::Renderer * renderer,EGLClientBuffer buffer,const egl::AttributeMap & attribs)153 egl::Error HardwareBufferImageSiblingVkAndroid::ValidateHardwareBuffer(
154     vk::Renderer *renderer,
155     EGLClientBuffer buffer,
156     const egl::AttributeMap &attribs)
157 {
158     struct ANativeWindowBuffer *windowBuffer =
159         angle::android::ClientBufferToANativeWindowBuffer(buffer);
160     struct AHardwareBuffer *hardwareBuffer =
161         angle::android::ANativeWindowBufferToAHardwareBuffer(windowBuffer);
162 
163     VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties = {};
164     bufferFormatProperties.sType =
165         VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID;
166     bufferFormatProperties.pNext = nullptr;
167 
168     VkAndroidHardwareBufferPropertiesANDROID bufferProperties = {};
169     bufferProperties.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID;
170     bufferProperties.pNext = &bufferFormatProperties;
171 
172     VkDevice device = renderer->getDevice();
173     VkResult result =
174         vkGetAndroidHardwareBufferPropertiesANDROID(device, hardwareBuffer, &bufferProperties);
175     if (result != VK_SUCCESS)
176     {
177         return egl::EglBadParameter() << "Failed to query AHardwareBuffer properties";
178     }
179 
180     if (bufferFormatProperties.format == VK_FORMAT_UNDEFINED)
181     {
182         ASSERT(bufferFormatProperties.externalFormat != 0);
183         // We must have an external format, check that it supports texture sampling
184         if (!(bufferFormatProperties.formatFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
185         {
186             return egl::EglBadParameter()
187                    << "Sampling from AHardwareBuffer externalFormat 0x" << std::hex
188                    << bufferFormatProperties.externalFormat << " is unsupported ";
189         }
190     }
191     else
192     {
193         angle::FormatID formatID = vk::GetFormatIDFromVkFormat(bufferFormatProperties.format);
194         if (!HasFullTextureFormatSupport(renderer, formatID))
195         {
196             return egl::EglBadParameter()
197                    << "AHardwareBuffer format " << bufferFormatProperties.format
198                    << " does not support enough features to use as a texture.";
199         }
200     }
201 
202     if (attribs.getAsInt(EGL_PROTECTED_CONTENT_EXT, EGL_FALSE) == EGL_TRUE)
203     {
204         int width       = 0;
205         int height      = 0;
206         int depth       = 0;
207         int pixelFormat = 0;
208         uint64_t usage  = 0;
209         angle::android::GetANativeWindowBufferProperties(windowBuffer, &width, &height, &depth,
210                                                          &pixelFormat, &usage);
211         if ((usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) == 0)
212         {
213             return egl::EglBadAccess()
214                    << "EGL_PROTECTED_CONTENT_EXT attribute does not match protected state "
215                       "of EGLCleintBuffer.";
216         }
217     }
218 
219     return egl::NoError();
220 }
221 
initialize(const egl::Display * display)222 egl::Error HardwareBufferImageSiblingVkAndroid::initialize(const egl::Display *display)
223 {
224     DisplayVk *displayVk = vk::GetImpl(display);
225     return angle::ToEGL(initImpl(displayVk), EGL_BAD_PARAMETER);
226 }
227 
initImpl(DisplayVk * displayVk)228 angle::Result HardwareBufferImageSiblingVkAndroid::initImpl(DisplayVk *displayVk)
229 {
230     const AHBFunctions &functions = static_cast<DisplayVkAndroid *>(displayVk)->getAHBFunctions();
231     ANGLE_VK_CHECK(displayVk, functions.valid(), VK_ERROR_INITIALIZATION_FAILED);
232 
233     vk::Renderer *renderer = displayVk->getRenderer();
234 
235     struct ANativeWindowBuffer *windowBuffer =
236         angle::android::ClientBufferToANativeWindowBuffer(mBuffer);
237 
238     int pixelFormat = 0;
239     angle::android::GetANativeWindowBufferProperties(windowBuffer, &mSize.width, &mSize.height,
240                                                      &mSize.depth, &pixelFormat, &mUsage);
241 
242     struct AHardwareBuffer *hardwareBuffer =
243         angle::android::ANativeWindowBufferToAHardwareBuffer(windowBuffer);
244 
245     functions.acquire(hardwareBuffer);
246 
247     VkAndroidHardwareBufferPropertiesANDROID bufferProperties = {};
248     bufferProperties.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID;
249     bufferProperties.pNext = nullptr;
250 
251     VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties;
252     bufferFormatProperties.sType =
253         VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID;
254     bufferFormatProperties.pNext = nullptr;
255     vk::AddToPNextChain(&bufferProperties, &bufferFormatProperties);
256 
257     VkAndroidHardwareBufferFormatResolvePropertiesANDROID bufferFormatResolveProperties = {};
258     if (renderer->getFeatures().supportsExternalFormatResolve.enabled)
259     {
260         bufferFormatResolveProperties.sType =
261             VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_RESOLVE_PROPERTIES_ANDROID;
262         bufferFormatResolveProperties.pNext = nullptr;
263         vk::AddToPNextChain(&bufferFormatProperties, &bufferFormatResolveProperties);
264     }
265 
266     VkDevice device = renderer->getDevice();
267     ANGLE_VK_TRY(displayVk, vkGetAndroidHardwareBufferPropertiesANDROID(device, hardwareBuffer,
268                                                                         &bufferProperties));
269 
270     const bool isExternal = bufferFormatProperties.format == VK_FORMAT_UNDEFINED;
271 
272     VkExternalFormatANDROID externalFormat = {};
273     externalFormat.sType                   = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID;
274     externalFormat.externalFormat          = 0;
275 
276     // Use bufferFormatProperties.format directly when possible.  For RGBX, the spec requires the
277     // corresponding format to be RGB, which is not _technically_ correct.  The Vulkan backend uses
278     // the RGBX8_ANGLE format, so that's overriden.
279     //
280     // Where bufferFormatProperties.format returns UNDEFINED, NativePixelFormatToGLInternalFormat is
281     // used to infer the format.
282     const vk::Format *vkFormat = nullptr;
283     if (pixelFormat == AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM)
284     {
285         vkFormat = &renderer->getFormat(GL_RGBX8_ANGLE);
286     }
287     else if (!isExternal)
288     {
289         vkFormat = &renderer->getFormat(vk::GetFormatIDFromVkFormat(bufferFormatProperties.format));
290     }
291     else
292     {
293         vkFormat =
294             &renderer->getFormat(angle::android::NativePixelFormatToGLInternalFormat(pixelFormat));
295     }
296 
297     const angle::Format &imageFormat = vkFormat->getActualRenderableImageFormat();
298     bool isDepthOrStencilFormat      = imageFormat.hasDepthOrStencilBits();
299     mFormat                          = gl::Format(vkFormat->getIntendedGLFormat());
300 
301     bool externalRenderTargetSupported =
302         renderer->getFeatures().supportsExternalFormatResolve.enabled &&
303         bufferFormatResolveProperties.colorAttachmentFormat != VK_FORMAT_UNDEFINED;
304     // Can assume based on us getting here already. The supportsYUVSamplerConversion
305     // check below should serve as a backup otherwise.
306     bool externalTexturingSupported = true;
307 
308     // Query AHB description and do the following -
309     // 1. Derive VkImageTiling mode based on AHB usage flags
310     // 2. Map AHB usage flags to VkImageUsageFlags
311     AHardwareBuffer_Desc ahbDescription;
312     functions.describe(hardwareBuffer, &ahbDescription);
313     VkImageTiling imageTilingMode = AhbDescUsageToVkImageTiling(ahbDescription);
314     VkImageUsageFlags usage = AhbDescUsageToVkImageUsage(ahbDescription, isDepthOrStencilFormat);
315 
316     if (isExternal)
317     {
318         ANGLE_VK_CHECK(displayVk, bufferFormatProperties.externalFormat != 0, VK_ERROR_UNKNOWN);
319         externalFormat.externalFormat = bufferFormatProperties.externalFormat;
320 
321         // VkImageCreateInfo struct: If the pNext chain includes a VkExternalFormatANDROID structure
322         // whose externalFormat member is not 0, usage must not include any usages except
323         // VK_IMAGE_USAGE_SAMPLED_BIT
324         if (externalFormat.externalFormat != 0 && !externalRenderTargetSupported)
325         {
326             // Clear all other bits except sampled
327             usage &= VK_IMAGE_USAGE_SAMPLED_BIT;
328         }
329 
330         // If the pNext chain includes a VkExternalFormatANDROID structure whose externalFormat
331         // member is not 0, tiling must be VK_IMAGE_TILING_OPTIMAL
332         imageTilingMode = VK_IMAGE_TILING_OPTIMAL;
333     }
334 
335     // If forceSampleUsageForAhbBackedImages feature is enabled force enable
336     // VK_IMAGE_USAGE_SAMPLED_BIT
337     if (renderer->getFeatures().forceSampleUsageForAhbBackedImages.enabled)
338     {
339         usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
340     }
341 
342     VkExternalMemoryImageCreateInfo externalMemoryImageCreateInfo = {};
343     externalMemoryImageCreateInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;
344     externalMemoryImageCreateInfo.pNext = &externalFormat;
345     externalMemoryImageCreateInfo.handleTypes =
346         VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
347 
348     VkExtent3D vkExtents;
349     gl_vk::GetExtent(mSize, &vkExtents);
350 
351     // Setup level count
352     mLevelCount = ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE) != 0)
353                       ? static_cast<uint32_t>(log2(std::max(mSize.width, mSize.height))) + 1
354                       : 1;
355 
356     // No support for rendering to external YUV AHB with multiple miplevels
357     ANGLE_VK_CHECK(displayVk, (!externalRenderTargetSupported || mLevelCount == 1),
358                    VK_ERROR_INITIALIZATION_FAILED);
359 
360     // Setup layer count
361     const uint32_t layerCount = mSize.depth;
362     vkExtents.depth           = 1;
363 
364     mImage = new vk::ImageHelper();
365 
366     // disable robust init for this external image.
367     bool robustInitEnabled = false;
368 
369     mImage->setTilingMode(imageTilingMode);
370     VkImageCreateFlags imageCreateFlags = AhbDescUsageToVkImageCreateFlags(ahbDescription);
371     vk::YcbcrConversionDesc conversionDesc{};
372 
373     if (isExternal)
374     {
375         if (externalRenderTargetSupported)
376         {
377             angle::FormatID externalFormatID =
378                 renderer->getExternalFormatTable()->getOrAllocExternalFormatID(
379                     bufferFormatProperties.externalFormat,
380                     bufferFormatResolveProperties.colorAttachmentFormat,
381                     bufferFormatProperties.formatFeatures);
382 
383             vkFormat = &renderer->getFormat(externalFormatID);
384         }
385         else
386         {
387             // If not renderable, don't burn a slot on it.
388             vkFormat = &renderer->getFormat(angle::FormatID::NONE);
389         }
390 
391         // Note from Vulkan spec: Since GL_OES_EGL_image_external does not require the same sampling
392         // and conversion calculations as Vulkan does, achieving identical results between APIs may
393         // not be possible on some implementations.
394         ANGLE_VK_CHECK(displayVk, renderer->getFeatures().supportsYUVSamplerConversion.enabled,
395                        VK_ERROR_FEATURE_NOT_PRESENT);
396         ASSERT(externalFormat.pNext == nullptr);
397 
398         // This may not actually mean the format is YUV. But the rest of ANGLE makes this
399         // assumption and needs this member variable.
400         mYUV = true;
401 
402         vk::YcbcrLinearFilterSupport linearFilterSupported =
403             (bufferFormatProperties.formatFeatures &
404              VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT) != 0
405                 ? vk::YcbcrLinearFilterSupport::Supported
406                 : vk::YcbcrLinearFilterSupport::Unsupported;
407 
408         conversionDesc.update(
409             renderer, bufferFormatProperties.externalFormat,
410             bufferFormatProperties.suggestedYcbcrModel, bufferFormatProperties.suggestedYcbcrRange,
411             bufferFormatProperties.suggestedXChromaOffset,
412             bufferFormatProperties.suggestedYChromaOffset, vk::kDefaultYCbCrChromaFilter,
413             bufferFormatProperties.samplerYcbcrConversionComponents, angle::FormatID::NONE,
414             linearFilterSupported);
415     }
416 
417     const gl::TextureType textureType = AhbDescUsageToTextureType(ahbDescription, layerCount);
418 
419     VkImageFormatListCreateInfoKHR imageFormatListInfoStorage;
420     vk::ImageHelper::ImageListFormats imageListFormatsStorage;
421     const void *imageCreateInfoPNext = vk::ImageHelper::DeriveCreateInfoPNext(
422         displayVk, vkFormat->getActualRenderableImageFormatID(), &externalMemoryImageCreateInfo,
423         &imageFormatListInfoStorage, &imageListFormatsStorage, &imageCreateFlags);
424 
425     ANGLE_TRY(mImage->initExternal(
426         displayVk, textureType, vkExtents, vkFormat->getIntendedFormatID(),
427         vkFormat->getActualRenderableImageFormatID(), 1, usage, imageCreateFlags,
428         vk::ImageLayout::ExternalPreInitialized, imageCreateInfoPNext, gl::LevelIndex(0),
429         mLevelCount, layerCount, robustInitEnabled, hasProtectedContent(), conversionDesc));
430 
431     VkImportAndroidHardwareBufferInfoANDROID importHardwareBufferInfo = {};
432     importHardwareBufferInfo.sType  = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID;
433     importHardwareBufferInfo.buffer = hardwareBuffer;
434 
435     VkMemoryDedicatedAllocateInfo dedicatedAllocInfo = {};
436     dedicatedAllocInfo.sType          = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
437     dedicatedAllocInfo.pNext          = &importHardwareBufferInfo;
438     dedicatedAllocInfo.image          = mImage->getImage().getHandle();
439     dedicatedAllocInfo.buffer         = VK_NULL_HANDLE;
440     const void *dedicatedAllocInfoPtr = &dedicatedAllocInfo;
441 
442     VkMemoryRequirements externalMemoryRequirements = {};
443     externalMemoryRequirements.size                 = bufferProperties.allocationSize;
444     externalMemoryRequirements.alignment            = 0;
445     externalMemoryRequirements.memoryTypeBits       = bufferProperties.memoryTypeBits;
446 
447     const VkMemoryPropertyFlags flags =
448         VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
449         (hasProtectedContent() ? VK_MEMORY_PROPERTY_PROTECTED_BIT : 0);
450 
451     ANGLE_TRY(mImage->initExternalMemory(displayVk, renderer->getMemoryProperties(),
452                                          externalMemoryRequirements, 1, &dedicatedAllocInfoPtr,
453                                          vk::kForeignDeviceQueueIndex, flags));
454 
455     if (isExternal)
456     {
457         // External format means that we are working with VK_FORMAT_UNDEFINED,
458         // so hasImageFormatFeatureBits will assert. Set these based on
459         // presence of extensions or assumption.
460         mRenderable  = externalRenderTargetSupported;
461         mTextureable = externalTexturingSupported;
462     }
463     else
464     {
465         constexpr uint32_t kColorRenderableRequiredBits = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
466         constexpr uint32_t kDepthStencilRenderableRequiredBits =
467             VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
468         mRenderable =
469             renderer->hasImageFormatFeatureBits(vkFormat->getActualRenderableImageFormatID(),
470                                                 kColorRenderableRequiredBits) ||
471             renderer->hasImageFormatFeatureBits(vkFormat->getActualRenderableImageFormatID(),
472                                                 kDepthStencilRenderableRequiredBits);
473         constexpr uint32_t kTextureableRequiredBits =
474             VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
475         mTextureable = renderer->hasImageFormatFeatureBits(
476             vkFormat->getActualRenderableImageFormatID(), kTextureableRequiredBits);
477     }
478 
479     return angle::Result::Continue;
480 }
481 
onDestroy(const egl::Display * display)482 void HardwareBufferImageSiblingVkAndroid::onDestroy(const egl::Display *display)
483 {
484     const AHBFunctions &functions = GetImplAs<DisplayVkAndroid>(display)->getAHBFunctions();
485     ASSERT(functions.valid());
486 
487     functions.release(angle::android::ANativeWindowBufferToAHardwareBuffer(
488         angle::android::ClientBufferToANativeWindowBuffer(mBuffer)));
489 
490     ASSERT(mImage == nullptr);
491 }
492 
getFormat() const493 gl::Format HardwareBufferImageSiblingVkAndroid::getFormat() const
494 {
495     return mFormat;
496 }
497 
isRenderable(const gl::Context * context) const498 bool HardwareBufferImageSiblingVkAndroid::isRenderable(const gl::Context *context) const
499 {
500     return mRenderable;
501 }
502 
isTexturable(const gl::Context * context) const503 bool HardwareBufferImageSiblingVkAndroid::isTexturable(const gl::Context *context) const
504 {
505     return mTextureable;
506 }
507 
isYUV() const508 bool HardwareBufferImageSiblingVkAndroid::isYUV() const
509 {
510     return mYUV;
511 }
512 
hasFrontBufferUsage() const513 bool HardwareBufferImageSiblingVkAndroid::hasFrontBufferUsage() const
514 {
515     return (mUsage & kAHardwareBufferUsageFrontBuffer) != 0;
516 }
517 
isCubeMap() const518 bool HardwareBufferImageSiblingVkAndroid::isCubeMap() const
519 {
520     return (mUsage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) != 0;
521 }
522 
hasProtectedContent() const523 bool HardwareBufferImageSiblingVkAndroid::hasProtectedContent() const
524 {
525     return ((mUsage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) != 0);
526 }
527 
getSize() const528 gl::Extents HardwareBufferImageSiblingVkAndroid::getSize() const
529 {
530     return mSize;
531 }
532 
getSamples() const533 size_t HardwareBufferImageSiblingVkAndroid::getSamples() const
534 {
535     return mSamples;
536 }
537 
getLevelCount() const538 uint32_t HardwareBufferImageSiblingVkAndroid::getLevelCount() const
539 {
540     return mLevelCount;
541 }
542 
543 // ExternalImageSiblingVk interface
getImage() const544 vk::ImageHelper *HardwareBufferImageSiblingVkAndroid::getImage() const
545 {
546     return mImage;
547 }
548 
release(vk::Renderer * renderer)549 void HardwareBufferImageSiblingVkAndroid::release(vk::Renderer *renderer)
550 {
551     if (mImage != nullptr)
552     {
553         // TODO: Handle the case where the EGLImage is used in two contexts not in the same share
554         // group.  https://issuetracker.google.com/169868803
555         mImage->releaseImage(renderer);
556         mImage->releaseStagedUpdates(renderer);
557         SafeDelete(mImage);
558     }
559 }
560 
561 }  // namespace rx
562