Hash :
7a85d114
Author :
Date :
2022-03-25T15:01:17
Use [[nodiscard]] on RAII classes
Scoped* classes provide an RAII way of adding cleanup/restore state/etc
in a robust way. Unfortunatley, it's very easy to mistakenly leave the
variable name, leading to the destructor being called immediately
instead of at the end of the scope:
{
ScopedX(parameters); // instead of ScopedX x(parameters);
// Code here is run after destructor
}
The [[nodiscard]] attribute, if specified on the ScopedX class would
lead to a warning (turned to error with -Werror). This change does
that for classes named *Scoped* in ANGLE.
Bug: chromium:1103817
Change-Id: I65c9922c9b4eba1f9c033e093fe8fe534648ab62
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3552092
Reviewed-by: Lingfeng Yang <lfy@google.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: 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
//
// Copyright 2016 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.
//
// DeviceVk.cpp:
// Implements the class methods for DeviceVk.
//
#include "libANGLE/renderer/vulkan/DeviceVk.h"
#include <stdint.h>
#include "common/debug.h"
#include "common/vulkan/vulkan_icd.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/vulkan/DisplayVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
namespace rx
{
namespace
{
DeviceVk *gDevice = nullptr;
class ANGLE_NO_DISCARD ScopedEnv : public angle::vk::ScopedVkLoaderEnvironment
{
public:
ScopedEnv()
: angle::vk::ScopedVkLoaderEnvironment(
gDevice ? gDevice->getRenderer()->getEnableValidationLayers() : false,
gDevice ? gDevice->getRenderer()->getEnabledICD() : angle::vk::ICD::Default)
{
if (!gDevice)
{
WARN() << "No DeviceVk instance.";
}
}
};
} // namespace
DeviceVk::DeviceVk() = default;
DeviceVk::~DeviceVk()
{
if (gDevice == this)
{
gDevice = nullptr;
}
}
egl::Error DeviceVk::initialize()
{
return egl::NoError();
}
egl::Error DeviceVk::getAttribute(const egl::Display *display, EGLint attribute, void **outValue)
{
RendererVk *renderer =
static_cast<rx::DisplayVk *>(display->getImplementation())->getRenderer();
ASSERT(mRenderer == nullptr || mRenderer == renderer);
mRenderer = renderer;
switch (attribute)
{
case EGL_VULKAN_VERSION_ANGLE:
{
auto version = static_cast<intptr_t>(mRenderer->getApiVersion());
*outValue = reinterpret_cast<void *>(version);
return egl::NoError();
}
case EGL_VULKAN_INSTANCE_ANGLE:
{
*outValue = mRenderer->getInstance();
return egl::NoError();
}
case EGL_VULKAN_DEVICE_ANGLE:
{
*outValue = mRenderer->getDevice();
return egl::NoError();
}
case EGL_VULKAN_PHYSICAL_DEVICE_ANGLE:
{
*outValue = mRenderer->getPhysicalDevice();
return egl::NoError();
}
case EGL_VULKAN_QUEUE_ANGLE:
{
// egl::ContextPriority::Medium is the default context priority.
*outValue = mRenderer->getQueue(egl::ContextPriority::Medium);
return egl::NoError();
}
case EGL_VULKAN_QUEUE_FAMILIY_INDEX_ANGLE:
{
intptr_t index = static_cast<intptr_t>(mRenderer->getQueueFamilyIndex());
*outValue = reinterpret_cast<void *>(index);
return egl::NoError();
}
case EGL_VULKAN_DEVICE_EXTENSIONS_ANGLE:
{
char **extensions = const_cast<char **>(mRenderer->getEnabledDeviceExtensions().data());
*outValue = reinterpret_cast<void *>(extensions);
return egl::NoError();
}
case EGL_VULKAN_INSTANCE_EXTENSIONS_ANGLE:
{
char **extensions =
const_cast<char **>(mRenderer->getEnabledInstanceExtensions().data());
*outValue = reinterpret_cast<void *>(extensions);
return egl::NoError();
}
case EGL_VULKAN_FEATURES_ANGLE:
{
const auto *features = &mRenderer->getEnabledFeatures();
*outValue = const_cast<void *>(reinterpret_cast<const void *>(features));
return egl::NoError();
}
case EGL_VULKAN_GET_INSTANCE_PROC_ADDR:
{
*outValue = reinterpret_cast<void *>(DeviceVk::WrappedGetInstanceProcAddr);
ASSERT(!gDevice || gDevice == this);
gDevice = this;
return egl::NoError();
}
default:
return egl::EglBadAccess();
}
}
EGLint DeviceVk::getType()
{
return EGL_VULKAN_DEVICE_ANGLE;
}
void DeviceVk::generateExtensions(egl::DeviceExtensions *outExtensions) const
{
outExtensions->deviceVulkan = true;
}
// static
VKAPI_ATTR VkResult VKAPI_CALL
DeviceVk::WrappedCreateInstance(const VkInstanceCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkInstance *pInstance)
{
ScopedEnv scopedEnv;
return vkCreateInstance(pCreateInfo, pAllocator, pInstance);
}
// static
VKAPI_ATTR VkResult VKAPI_CALL
DeviceVk::WrappedEnumerateInstanceExtensionProperties(const char *pLayerName,
uint32_t *pPropertyCount,
VkExtensionProperties *pProperties)
{
ScopedEnv scopedEnv;
return vkEnumerateInstanceExtensionProperties(pLayerName, pPropertyCount, pProperties);
}
// static
VKAPI_ATTR VkResult VKAPI_CALL
DeviceVk::WrappedEnumerateInstanceLayerProperties(uint32_t *pPropertyCount,
VkLayerProperties *pProperties)
{
ScopedEnv scopedEnv;
return vkEnumerateInstanceLayerProperties(pPropertyCount, pProperties);
}
// static
VKAPI_ATTR VkResult VKAPI_CALL DeviceVk::WrappedEnumerateInstanceVersion(uint32_t *pApiVersion)
{
ScopedEnv scopedEnv;
return vkEnumerateInstanceVersion(pApiVersion);
}
// static
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DeviceVk::WrappedGetInstanceProcAddr(VkInstance instance,
const char *pName)
{
if (!pName || pName[0] != 'v' || pName[1] != 'k')
{
return nullptr;
}
if (instance != VK_NULL_HANDLE)
{
return vkGetInstanceProcAddr(instance, pName);
}
if (!strcmp(pName, "vkCreateInstance"))
{
return reinterpret_cast<PFN_vkVoidFunction>(DeviceVk::WrappedCreateInstance);
}
if (!strcmp(pName, "vkEnumerateInstanceExtensionProperties"))
{
return reinterpret_cast<PFN_vkVoidFunction>(
DeviceVk::WrappedEnumerateInstanceExtensionProperties);
}
if (!strcmp(pName, "vkEnumerateInstanceLayerProperties"))
{
return reinterpret_cast<PFN_vkVoidFunction>(
DeviceVk::WrappedEnumerateInstanceLayerProperties);
}
if (!strcmp(pName, "vkEnumerateInstanceVersion"))
{
if (!vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion"))
{
// Vulkan 1.0 doesn't have vkEnumerateInstanceVersion.
return nullptr;
}
return reinterpret_cast<PFN_vkVoidFunction>(DeviceVk::WrappedEnumerateInstanceVersion);
}
if (!strcmp(pName, "vkGetInstanceProcAddr"))
{
return reinterpret_cast<PFN_vkVoidFunction>(DeviceVk::WrappedGetInstanceProcAddr);
}
return vkGetInstanceProcAddr(instance, pName);
}
} // namespace rx