From d376fe9e173379630d869faa8e21c07bf78cd199 Mon Sep 17 00:00:00 2001 From: Sergey Ivanov Date: Thu, 21 Oct 2021 19:12:03 +0300 Subject: [PATCH] Merge pull request #20921 from sivanov-work:atl_patch G-API: FIX OpenVINO build - Add CComPtr RAII replacement into G-API sample * Add CComPtr RAII replacement into G-API sample * Remove completely `atlbase.h' --- .../gapi/samples/onevpl_infer_single_roi.cpp | 47 +++++++++++++------ .../onevpl/cfg_param_device_selector.cpp | 13 +++-- modules/gapi/src/streaming/onevpl/utils.hpp | 21 +++++++++ 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/modules/gapi/samples/onevpl_infer_single_roi.cpp b/modules/gapi/samples/onevpl_infer_single_roi.cpp index deca86f1ca..9da64c99d3 100644 --- a/modules/gapi/samples/onevpl_infer_single_roi.cpp +++ b/modules/gapi/samples/onevpl_infer_single_roi.cpp @@ -25,7 +25,6 @@ #define D3D11_NO_HELPERS #define NOMINMAX #include -#include #include #pragma comment(lib, "dxgi") #undef NOMINMAX @@ -64,9 +63,29 @@ std::string get_weights_path(const std::string &model_path) { #ifdef HAVE_DIRECTX #ifdef HAVE_D3D11 -using AccelParamsType = std::tuple, CComPtr>; +// Since ATL headers might not be available on specific MSVS Build Tools +// we use simple `CComPtr` implementation like as `ComPtrGuard` +// which is not supposed to be the full functional replacement of `CComPtr` +// and it uses as RAII to make sure utilization is correct +template +void release(COMNonManageableType *ptr) { + if (ptr) { + ptr->Release(); + } +} -AccelParamsType create_device_with_ctx(CComPtr adapter) { +template +using ComPtrGuard = std::unique_ptr)>; + +template +ComPtrGuard createCOMPtrGuard(COMNonManageableType *ptr = nullptr) { + return ComPtrGuard {ptr, &release}; +} + + +using AccelParamsType = std::tuple, ComPtrGuard>; + +AccelParamsType create_device_with_ctx(IDXGIAdapter* adapter) { UINT flags = 0; D3D_FEATURE_LEVEL feature_levels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, @@ -85,7 +104,8 @@ AccelParamsType create_device_with_ctx(CComPtr adapter) { std::to_string(HRESULT_CODE(err))); } - return std::make_tuple(ret_device_ptr, ret_ctx_ptr); + return std::make_tuple(createCOMPtrGuard(ret_device_ptr), + createCOMPtrGuard(ret_ctx_ptr)); } #endif // HAVE_D3D11 #endif // HAVE_DIRECTX @@ -270,12 +290,11 @@ int main(int argc, char *argv[]) { #ifdef HAVE_INF_ENGINE #ifdef HAVE_DIRECTX #ifdef HAVE_D3D11 - CComPtr dx11_dev; - CComPtr dx11_ctx; + auto dx11_dev = createCOMPtrGuard(); + auto dx11_ctx = createCOMPtrGuard(); if (device_id.find("GPU") != std::string::npos) { - CComPtr adapter_factory; - CComPtr intel_adapters; + auto adapter_factory = createCOMPtrGuard(); { IDXGIFactory* out_factory = nullptr; HRESULT err = CreateDXGIFactory(__uuidof(IDXGIFactory), @@ -284,10 +303,10 @@ int main(int argc, char *argv[]) { std::cerr << "Cannot create CreateDXGIFactory, error: " << HRESULT_CODE(err) << std::endl; return -1; } - adapter_factory.Attach(out_factory); + adapter_factory = createCOMPtrGuard(out_factory); } - CComPtr intel_adapter; + auto intel_adapter = createCOMPtrGuard(); UINT adapter_index = 0; const unsigned int refIntelVendorID = 0x8086; IDXGIAdapter* out_adapter = nullptr; @@ -296,7 +315,7 @@ int main(int argc, char *argv[]) { DXGI_ADAPTER_DESC desc{}; out_adapter->GetDesc(&desc); if (desc.VendorId == refIntelVendorID) { - intel_adapter.Attach(out_adapter); + intel_adapter = createCOMPtrGuard(out_adapter); break; } ++adapter_index; @@ -307,9 +326,9 @@ int main(int argc, char *argv[]) { return -1; } - std::tie(dx11_dev, dx11_ctx) = create_device_with_ctx(intel_adapter); - accel_device_ptr = reinterpret_cast(dx11_dev.p); - accel_ctx_ptr = reinterpret_cast(dx11_ctx.p); + std::tie(dx11_dev, dx11_ctx) = create_device_with_ctx(intel_adapter.get()); + accel_device_ptr = reinterpret_cast(dx11_dev.get()); + accel_ctx_ptr = reinterpret_cast(dx11_ctx.get()); // put accel type description for VPL source source_cfgs.push_back(cfg::create_from_string( diff --git a/modules/gapi/src/streaming/onevpl/cfg_param_device_selector.cpp b/modules/gapi/src/streaming/onevpl/cfg_param_device_selector.cpp index c8fd49c1ad..0bdec70986 100644 --- a/modules/gapi/src/streaming/onevpl/cfg_param_device_selector.cpp +++ b/modules/gapi/src/streaming/onevpl/cfg_param_device_selector.cpp @@ -10,6 +10,7 @@ #include #include "streaming/onevpl/cfg_param_device_selector.hpp" +#include "streaming/onevpl/utils.hpp" #include "logger.hpp" #ifdef HAVE_DIRECTX @@ -19,7 +20,6 @@ // get rid of generate macro max/min/etc from DX side #define D3D11_NO_HELPERS #define NOMINMAX -#include #include #include #pragma comment(lib, "dxgi") @@ -113,8 +113,7 @@ CfgParamDeviceSelector::CfgParamDeviceSelector(const CfgParams& cfg_params) : }; D3D_FEATURE_LEVEL featureLevel; - CComPtr adapter_factory; - CComPtr intel_adapters; + auto adapter_factory = createCOMPtrGuard(); { IDXGIFactory* out_factory = nullptr; HRESULT err = CreateDXGIFactory(__uuidof(IDXGIFactory), @@ -122,10 +121,10 @@ CfgParamDeviceSelector::CfgParamDeviceSelector(const CfgParams& cfg_params) : if (FAILED(err)) { throw std::runtime_error("Cannot create CreateDXGIFactory, error: " + std::to_string(HRESULT_CODE(err))); } - adapter_factory.Attach(out_factory); + adapter_factory = createCOMPtrGuard(out_factory); } - CComPtr intel_adapter; + auto intel_adapter = createCOMPtrGuard(); UINT adapter_index = 0; const unsigned int refIntelVendorID = 0x8086; IDXGIAdapter* out_adapter = nullptr; @@ -134,7 +133,7 @@ CfgParamDeviceSelector::CfgParamDeviceSelector(const CfgParams& cfg_params) : DXGI_ADAPTER_DESC desc{}; out_adapter->GetDesc(&desc); if (desc.VendorId == refIntelVendorID) { - intel_adapter.Attach(out_adapter); + intel_adapter = createCOMPtrGuard(out_adapter); break; } ++adapter_index; @@ -145,7 +144,7 @@ CfgParamDeviceSelector::CfgParamDeviceSelector(const CfgParams& cfg_params) : } // Create the Direct3D 11 API device object and a corresponding context. - HRESULT err = D3D11CreateDevice(intel_adapter, + HRESULT err = D3D11CreateDevice(intel_adapter.get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, creationFlags, featureLevels, ARRAYSIZE(featureLevels), diff --git a/modules/gapi/src/streaming/onevpl/utils.hpp b/modules/gapi/src/streaming/onevpl/utils.hpp index 94f5a249e8..19c1b135dc 100644 --- a/modules/gapi/src/streaming/onevpl/utils.hpp +++ b/modules/gapi/src/streaming/onevpl/utils.hpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -26,6 +27,26 @@ namespace gapi { namespace wip { namespace onevpl { +// Since ATL headers might not be available on specific MSVS Build Tools +// we use simple `CComPtr` implementation like as `ComPtrGuard` +// which is not supposed to be the full functional replacement of `CComPtr` +// and it uses as RAII to make sure utilization is correct +template +void release(COMNonManageableType *ptr) { + if (ptr) { + ptr->Release(); + } +} + +template +using ComPtrGuard = std::unique_ptr)>; + +template +ComPtrGuard createCOMPtrGuard(COMNonManageableType *ptr = nullptr) { + return ComPtrGuard {ptr, &release}; +} + + const char* mfx_impl_to_cstr(const mfxIMPL impl); mfxIMPL cstr_to_mfx_impl(const char* cstr);