Hash :
6136620b
Author :
Date :
2021-03-25T15:43:06
Reland "EGL: GLES: Implement GL_EXT_protected_textures" This is a reland of 6210a9b34a721df2c84cf69170ad9bf7ba40e4aa This removes changes in gl backend. Original change's description: > EGL: GLES: Implement GL_EXT_protected_textures > > Implement EGL_EXT_protected_content Images > Add protected member to Images and Textures > Add error when creating objects if not supported or > does't match native buffer > When creating siblings pass protected state > Add extension caps > Add Validation > Add GetTexParameter and SetTextparameter > Add protected to Texture and state > Expand tests for images and textures > > Test: angle_end2end_test --gtest_filter=EGLProtectedContentTest > > Bug: angleproject:3965 > Change-Id: I35a89b4e80bba6d9b6831c68e71630eef304dacb > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2802852 > Commit-Queue: Mohan Maiya <m.maiya@samsung.com> > Reviewed-by: Geoff Lang <geofflang@chromium.org> > Reviewed-by: Jamie Madill <jmadill@chromium.org> > Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Test: angle_end2end_test --gtest_filter=EGLProtectedContentTest Bug: angleproject:3965 Change-Id: Id36d697c53afc0f0dadf92bda4565f9157f4fc2a Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3076825 Commit-Queue: Brandon Schade <b.schade@samsung.com> Reviewed-by: Ian Elliott <ianelliott@google.com> Reviewed-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// HardwareBufferImageSiblingVkAndroid.cpp: Implements HardwareBufferImageSiblingVkAndroid.
#include "libANGLE/renderer/vulkan/android/HardwareBufferImageSiblingVkAndroid.h"
#include "common/android_util.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/vulkan/DisplayVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
#include "libANGLE/renderer/vulkan/android/AHBFunctions.h"
#include "libANGLE/renderer/vulkan/android/DisplayVkAndroid.h"
namespace rx
{
namespace
{
VkImageTiling AhbDescUsageToVkImageTiling(const AHardwareBuffer_Desc &ahbDescription)
{
// A note about the choice of OPTIMAL here.
// When running Android on certain GPUs, there are problems creating Vulkan
// image siblings of AHardwareBuffers because it's currently assumed that
// the underlying driver can create linear tiling images that have input
// attachment usage, which isn't supported on NVIDIA for example, resulting
// in failure to create the image siblings. Yet, we don't currently take
// advantage of linear elsewhere in ANGLE. To maintain maximum
// compatibility on Android for such drivers, use optimal tiling for image
// siblings.
//
// Note that while we have switched to optimal unconditionally in this path
// versus linear, it's possible that previously compatible linear usages
// might become uncompatible after switching to optimal. However, from what
// we've seen on Samsung/NVIDIA/Intel/AMD GPUs so far, formats generally
// have more possible usages in optimal tiling versus linear tiling:
//
// http://vulkan.gpuinfo.org/displayreport.php?id=10804#formats_linear
// http://vulkan.gpuinfo.org/displayreport.php?id=10804#formats_optimal
//
// http://vulkan.gpuinfo.org/displayreport.php?id=10807#formats_linear
// http://vulkan.gpuinfo.org/displayreport.php?id=10807#formats_optimal
//
// http://vulkan.gpuinfo.org/displayreport.php?id=10809#formats_linear
// http://vulkan.gpuinfo.org/displayreport.php?id=10809#formats_optimal
//
// http://vulkan.gpuinfo.org/displayreport.php?id=10787#formats_linear
// http://vulkan.gpuinfo.org/displayreport.php?id=10787#formats_optimal
//
// Also, as an aside, in terms of what's generally expected from the Vulkan
// ICD in Android when determining AHB compatibility, if the vendor wants
// to declare a particular combinatino of format/tiling/usage/etc as not
// supported AHB-wise, it's up to the ICD vendor to zero out bits in
// supportedHandleTypes in the vkGetPhysicalDeviceImageFormatProperties2
// query:
//
// ``` *
// [VUID-VkImageCreateInfo-pNext-00990](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/vkspec.html#VUID-VkImageCreateInfo-pNext-00990)
// If the pNext chain includes a VkExternalMemoryImageCreateInfo structure,
// its handleTypes member must only contain bits that are also in
// VkExternalImageFormatProperties::externalMemoryProperties.compatibleHandleTypes,
// as returned by vkGetPhysicalDeviceImageFormatProperties2 with format,
// imageType, tiling, usage, and flags equal to those in this structure,
// and with a VkPhysicalDeviceExternalImageFormatInfo structure included in
// the pNext chain, with a handleType equal to any one of the handle types
// specified in VkExternalMemoryImageCreateInfo::handleTypes ```
return VK_IMAGE_TILING_OPTIMAL;
}
} // namespace
HardwareBufferImageSiblingVkAndroid::HardwareBufferImageSiblingVkAndroid(EGLClientBuffer buffer)
: mBuffer(buffer),
mFormat(GL_NONE),
mRenderable(false),
mTextureable(false),
mYUV(false),
mSamples(0),
mImage(nullptr)
{}
HardwareBufferImageSiblingVkAndroid::~HardwareBufferImageSiblingVkAndroid() {}
// Static
egl::Error HardwareBufferImageSiblingVkAndroid::ValidateHardwareBuffer(
RendererVk *renderer,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
struct ANativeWindowBuffer *windowBuffer =
angle::android::ClientBufferToANativeWindowBuffer(buffer);
struct AHardwareBuffer *hardwareBuffer =
angle::android::ANativeWindowBufferToAHardwareBuffer(windowBuffer);
VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties = {};
bufferFormatProperties.sType =
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID;
bufferFormatProperties.pNext = nullptr;
VkAndroidHardwareBufferPropertiesANDROID bufferProperties = {};
bufferProperties.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID;
bufferProperties.pNext = &bufferFormatProperties;
VkDevice device = renderer->getDevice();
VkResult result =
vkGetAndroidHardwareBufferPropertiesANDROID(device, hardwareBuffer, &bufferProperties);
if (result != VK_SUCCESS)
{
return egl::EglBadParameter() << "Failed to query AHardwareBuffer properties";
}
if (bufferFormatProperties.format == VK_FORMAT_UNDEFINED)
{
ASSERT(bufferFormatProperties.externalFormat != 0);
// We must have an external format, check that it supports texture sampling
if (!(bufferFormatProperties.formatFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
{
return egl::EglBadParameter()
<< "Sampling from AHardwareBuffer externalFormat 0x" << std::hex
<< bufferFormatProperties.externalFormat << " is unsupported ";
}
}
else
{
angle::FormatID formatID = vk::GetFormatIDFromVkFormat(bufferFormatProperties.format);
if (!HasFullTextureFormatSupport(renderer, formatID))
{
return egl::EglBadParameter() << "AHardwareBuffer format does not support enough "
"features to use as a texture.";
}
}
if (attribs.getAsInt(EGL_PROTECTED_CONTENT_EXT, EGL_FALSE) == EGL_TRUE)
{
int width = 0;
int height = 0;
int depth = 0;
int pixelFormat = 0;
uint64_t usage = 0;
angle::android::GetANativeWindowBufferProperties(windowBuffer, &width, &height, &depth,
&pixelFormat, &usage);
if ((usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) == 0)
{
return egl::EglBadAccess()
<< "EGL_PROTECTED_CONTENT_EXT attribute does not match protected state "
"of EGLCleintBuffer.";
}
}
return egl::NoError();
}
egl::Error HardwareBufferImageSiblingVkAndroid::initialize(const egl::Display *display)
{
DisplayVk *displayVk = vk::GetImpl(display);
return angle::ToEGL(initImpl(displayVk), displayVk, EGL_BAD_PARAMETER);
}
// Map AHB usage flags to VkImageUsageFlags using this table from the Vulkan spec
// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap10.html#memory-external-android-hardware-buffer-usage
VkImageUsageFlags AhbDescUsageToVkImageUsage(const AHardwareBuffer_Desc &ahbDescription,
bool isDepthOrStencilFormat)
{
VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) != 0)
{
usage |= VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
}
if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) != 0)
{
if (isDepthOrStencilFormat)
{
usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
}
else
{
usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
}
}
if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) != 0)
{
usage |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
}
if ((ahbDescription.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) != 0)
{
usage |= VK_IMAGE_CREATE_PROTECTED_BIT;
}
return usage;
}
angle::Result HardwareBufferImageSiblingVkAndroid::initImpl(DisplayVk *displayVk)
{
const AHBFunctions &functions = static_cast<DisplayVkAndroid *>(displayVk)->getAHBFunctions();
ANGLE_VK_CHECK(displayVk, functions.valid(), VK_ERROR_INITIALIZATION_FAILED);
RendererVk *renderer = displayVk->getRenderer();
struct ANativeWindowBuffer *windowBuffer =
angle::android::ClientBufferToANativeWindowBuffer(mBuffer);
int pixelFormat = 0;
angle::android::GetANativeWindowBufferProperties(windowBuffer, &mSize.width, &mSize.height,
&mSize.depth, &pixelFormat, &mUsage);
GLenum internalFormat = angle::android::NativePixelFormatToGLInternalFormat(pixelFormat);
mFormat = gl::Format(internalFormat);
struct AHardwareBuffer *hardwareBuffer =
angle::android::ANativeWindowBufferToAHardwareBuffer(windowBuffer);
functions.acquire(hardwareBuffer);
VkAndroidHardwareBufferFormatPropertiesANDROID bufferFormatProperties;
bufferFormatProperties.sType =
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID;
bufferFormatProperties.pNext = nullptr;
VkAndroidHardwareBufferPropertiesANDROID bufferProperties = {};
bufferProperties.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID;
bufferProperties.pNext = &bufferFormatProperties;
VkDevice device = renderer->getDevice();
ANGLE_VK_TRY(displayVk, vkGetAndroidHardwareBufferPropertiesANDROID(device, hardwareBuffer,
&bufferProperties));
VkExternalFormatANDROID externalFormat = {};
externalFormat.sType = VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID;
externalFormat.externalFormat = 0;
const vk::Format &vkFormat = renderer->getFormat(internalFormat);
const vk::Format &externalVkFormat = renderer->getFormat(angle::FormatID::NONE);
const angle::Format &imageFormat = vkFormat.actualImageFormat();
bool isDepthOrStencilFormat = imageFormat.hasDepthOrStencilBits();
// Query AHB description and do the following -
// 1. Derive VkImageTiling mode based on AHB usage flags
// 2. Map AHB usage flags to VkImageUsageFlags
AHardwareBuffer_Desc ahbDescription;
functions.describe(hardwareBuffer, &ahbDescription);
VkImageTiling imageTilingMode = AhbDescUsageToVkImageTiling(ahbDescription);
VkImageUsageFlags usage = AhbDescUsageToVkImageUsage(ahbDescription, isDepthOrStencilFormat);
if (bufferFormatProperties.format == VK_FORMAT_UNDEFINED)
{
ANGLE_VK_CHECK(displayVk, bufferFormatProperties.externalFormat != 0, VK_ERROR_UNKNOWN);
externalFormat.externalFormat = bufferFormatProperties.externalFormat;
// VkImageCreateInfo struct: If the pNext chain includes a VkExternalFormatANDROID structure
// whose externalFormat member is not 0, usage must not include any usages except
// VK_IMAGE_USAGE_SAMPLED_BIT
usage = VK_IMAGE_USAGE_SAMPLED_BIT;
// If the pNext chain includes a VkExternalFormatANDROID structure whose externalFormat
// member is not 0, tiling must be VK_IMAGE_TILING_OPTIMAL
imageTilingMode = VK_IMAGE_TILING_OPTIMAL;
}
VkExternalMemoryImageCreateInfo externalMemoryImageCreateInfo = {};
externalMemoryImageCreateInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;
externalMemoryImageCreateInfo.pNext = &externalFormat;
externalMemoryImageCreateInfo.handleTypes =
VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
VkExtent3D vkExtents;
gl_vk::GetExtent(mSize, &vkExtents);
mImage = new vk::ImageHelper();
// disable robust init for this external image.
bool robustInitEnabled = false;
mImage->setTilingMode(imageTilingMode);
VkImageCreateFlags imageCreateFlags = vk::kVkImageCreateFlagsNone;
if (hasProtectedContent())
{
imageCreateFlags |= VK_IMAGE_CREATE_PROTECTED_BIT;
}
ANGLE_TRY(mImage->initExternal(
displayVk, gl::TextureType::_2D, vkExtents,
bufferFormatProperties.format == VK_FORMAT_UNDEFINED ? externalVkFormat : vkFormat, 1,
usage, imageCreateFlags, vk::ImageLayout::ExternalPreInitialized,
&externalMemoryImageCreateInfo, gl::LevelIndex(0), 1, 1, robustInitEnabled, nullptr,
hasProtectedContent()));
VkImportAndroidHardwareBufferInfoANDROID importHardwareBufferInfo = {};
importHardwareBufferInfo.sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID;
importHardwareBufferInfo.buffer = hardwareBuffer;
VkMemoryDedicatedAllocateInfo dedicatedAllocInfo = {};
dedicatedAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
dedicatedAllocInfo.pNext = &importHardwareBufferInfo;
dedicatedAllocInfo.image = mImage->getImage().getHandle();
dedicatedAllocInfo.buffer = VK_NULL_HANDLE;
VkMemoryRequirements externalMemoryRequirements = {};
externalMemoryRequirements.size = bufferProperties.allocationSize;
externalMemoryRequirements.alignment = 0;
externalMemoryRequirements.memoryTypeBits = bufferProperties.memoryTypeBits;
VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
(hasProtectedContent() ? VK_MEMORY_PROPERTY_PROTECTED_BIT : 0);
if (bufferFormatProperties.format == VK_FORMAT_UNDEFINED)
{
// Note from Vulkan spec: Since GL_OES_EGL_image_external does not require the same sampling
// and conversion calculations as Vulkan does, achieving identical results between APIs may
// not be possible on some implementations.
ANGLE_VK_CHECK(displayVk, renderer->getFeatures().supportsYUVSamplerConversion.enabled,
VK_ERROR_FEATURE_NOT_PRESENT);
ASSERT(externalFormat.pNext == nullptr);
VkSamplerYcbcrConversionCreateInfo yuvConversionInfo = {};
yuvConversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO;
yuvConversionInfo.pNext = &externalFormat;
yuvConversionInfo.format = VK_FORMAT_UNDEFINED;
yuvConversionInfo.xChromaOffset = bufferFormatProperties.suggestedXChromaOffset;
yuvConversionInfo.yChromaOffset = bufferFormatProperties.suggestedYChromaOffset;
yuvConversionInfo.ycbcrModel = bufferFormatProperties.suggestedYcbcrModel;
yuvConversionInfo.ycbcrRange = bufferFormatProperties.suggestedYcbcrRange;
yuvConversionInfo.chromaFilter = VK_FILTER_NEAREST;
yuvConversionInfo.components = bufferFormatProperties.samplerYcbcrConversionComponents;
ANGLE_TRY(mImage->initExternalMemory(
displayVk, renderer->getMemoryProperties(), externalMemoryRequirements,
&yuvConversionInfo, &dedicatedAllocInfo, VK_QUEUE_FAMILY_FOREIGN_EXT, flags));
mYUV = true;
}
else
{
ANGLE_TRY(mImage->initExternalMemory(
displayVk, renderer->getMemoryProperties(), externalMemoryRequirements, nullptr,
&dedicatedAllocInfo, VK_QUEUE_FAMILY_FOREIGN_EXT, flags));
}
constexpr uint32_t kColorRenderableRequiredBits = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
constexpr uint32_t kDepthStencilRenderableRequiredBits = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
mRenderable = renderer->hasImageFormatFeatureBits(vkFormat.actualImageFormatID,
kColorRenderableRequiredBits) ||
renderer->hasImageFormatFeatureBits(vkFormat.actualImageFormatID,
kDepthStencilRenderableRequiredBits);
constexpr uint32_t kTextureableRequiredBits =
VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
mTextureable =
renderer->hasImageFormatFeatureBits(vkFormat.actualImageFormatID, kTextureableRequiredBits);
return angle::Result::Continue;
}
void HardwareBufferImageSiblingVkAndroid::onDestroy(const egl::Display *display)
{
const AHBFunctions &functions = GetImplAs<DisplayVkAndroid>(display)->getAHBFunctions();
ASSERT(functions.valid());
functions.release(angle::android::ANativeWindowBufferToAHardwareBuffer(
angle::android::ClientBufferToANativeWindowBuffer(mBuffer)));
ASSERT(mImage == nullptr);
}
gl::Format HardwareBufferImageSiblingVkAndroid::getFormat() const
{
return mFormat;
}
bool HardwareBufferImageSiblingVkAndroid::isRenderable(const gl::Context *context) const
{
return mRenderable;
}
bool HardwareBufferImageSiblingVkAndroid::isTexturable(const gl::Context *context) const
{
return mTextureable;
}
bool HardwareBufferImageSiblingVkAndroid::isYUV() const
{
return mYUV;
}
bool HardwareBufferImageSiblingVkAndroid::hasProtectedContent() const
{
return ((mUsage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) != 0);
}
gl::Extents HardwareBufferImageSiblingVkAndroid::getSize() const
{
return mSize;
}
size_t HardwareBufferImageSiblingVkAndroid::getSamples() const
{
return mSamples;
}
// ExternalImageSiblingVk interface
vk::ImageHelper *HardwareBufferImageSiblingVkAndroid::getImage() const
{
return mImage;
}
void HardwareBufferImageSiblingVkAndroid::release(RendererVk *renderer)
{
if (mImage != nullptr)
{
// TODO: We need to handle the case that EGLImage used in two context that aren't shared.
// https://issuetracker.google.com/169868803
mImage->releaseImage(renderer);
mImage->releaseStagingBuffer(renderer);
SafeDelete(mImage);
}
}
} // namespace rx