From d1340c6e165009ce57a02e4ee9c1dd57e9f40238 Mon Sep 17 00:00:00 2001 From: oltolm Date: Sun, 24 Mar 2024 18:41:18 +0100 Subject: [PATCH] use ComPtr for misc. things --- GPU/Common/Draw2D.h | 2 ++ UI/RemoteISOScreen.cpp | 8 ++--- Windows/BufferLock.h | 12 +++---- Windows/CaptureDevice.cpp | 59 +++++++++++++++-------------------- Windows/CaptureDevice.h | 9 +++--- Windows/DSoundStream.cpp | 11 +++---- Windows/DSoundStream.h | 9 +++--- Windows/DinputDevice.cpp | 25 +++++++-------- Windows/DinputDevice.h | 5 +-- Windows/W32Util/ShellUtil.cpp | 11 +++---- Windows/WASAPIStream.cpp | 57 +++++++++++++++------------------ Windows/main.cpp | 28 ++++++++++------- 12 files changed, 113 insertions(+), 123 deletions(-) diff --git a/GPU/Common/Draw2D.h b/GPU/Common/Draw2D.h index 5df67f7493d0..180b7e85abdd 100644 --- a/GPU/Common/Draw2D.h +++ b/GPU/Common/Draw2D.h @@ -1,7 +1,9 @@ #pragma once +#include "Data/Collections/Slice.h" #include "GPU/GPU.h" #include "Common/GPU/Shader.h" +#include "GPU/thin3d.h" // For framebuffer copies and similar things that just require passthrough. struct Draw2DVertex { diff --git a/UI/RemoteISOScreen.cpp b/UI/RemoteISOScreen.cpp index 611f0bb58a20..77e6779ab89c 100644 --- a/UI/RemoteISOScreen.cpp +++ b/UI/RemoteISOScreen.cpp @@ -16,13 +16,14 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "ppsspp_config.h" -#include #include #include #if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP) +#include #include "Common/CommonWindows.h" #include +#include #endif // TODO: For text align flags, probably shouldn't be in gfx_es2/... @@ -62,8 +63,8 @@ enum class ServerAllowStatus { static ServerAllowStatus IsServerAllowed(int port) { #if PPSSPP_PLATFORM(WINDOWS) && !PPSSPP_PLATFORM(UWP) - INetFwMgr *fwMgr = nullptr; - HRESULT hr = CoCreateInstance(__uuidof(NetFwMgr), nullptr, CLSCTX_INPROC_SERVER, __uuidof(INetFwMgr), (void **)&fwMgr); + Microsoft::WRL::ComPtr fwMgr; + HRESULT hr = CoCreateInstance(CLSID_NetFwMgr, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fwMgr)); if (FAILED(hr)) { return ServerAllowStatus::UNKNOWN; } @@ -80,7 +81,6 @@ static ServerAllowStatus IsServerAllowed(int port) { VariantInit(&allowedV); VariantInit(&restrictedV); hr = fwMgr->IsPortAllowed(&app[0], NET_FW_IP_VERSION_ANY, port, nullptr, NET_FW_IP_PROTOCOL_TCP, &allowedV, &restrictedV); - fwMgr->Release(); if (FAILED(hr)) { return ServerAllowStatus::UNKNOWN; diff --git a/Windows/BufferLock.h b/Windows/BufferLock.h index a19b8c965d13..4deeb149e51f 100644 --- a/Windows/BufferLock.h +++ b/Windows/BufferLock.h @@ -13,6 +13,9 @@ #pragma once +#include "CaptureDevice.h" +#include + //------------------------------------------------------------------- // VideoBufferLock class @@ -27,8 +30,7 @@ class VideoBufferLock VideoBufferLock(IMFMediaBuffer *pBuffer) : m_p2DBuffer(NULL), m_bLocked(FALSE) { m_pBuffer = pBuffer; - m_pBuffer->AddRef(); - + // Query for the 2-D buffer interface. OK if this fails. (void)m_pBuffer->QueryInterface(IID_PPV_ARGS(&m_p2DBuffer)); } @@ -36,8 +38,6 @@ class VideoBufferLock ~VideoBufferLock() { UnlockBuffer(); - SafeRelease(&m_pBuffer); - SafeRelease(&m_p2DBuffer); } //------------------------------------------------------------------- @@ -116,8 +116,8 @@ class VideoBufferLock } private: - IMFMediaBuffer *m_pBuffer; - IMF2DBuffer *m_p2DBuffer; + Microsoft::WRL::ComPtr m_pBuffer; + Microsoft::WRL::ComPtr m_p2DBuffer; BOOL m_bLocked; }; diff --git a/Windows/CaptureDevice.cpp b/Windows/CaptureDevice.cpp index 93376078c89d..6447b0272043 100644 --- a/Windows/CaptureDevice.cpp +++ b/Windows/CaptureDevice.cpp @@ -16,6 +16,7 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include +#include #include "Common/Thread/ThreadUtil.h" #include "CaptureDevice.h" @@ -25,6 +26,8 @@ #include "Core/HLE/sceUsbCam.h" #include "Core/Config.h" +using Microsoft::WRL::ComPtr; + namespace MFAPI { HINSTANCE Mflib; HINSTANCE Mfplatlib; @@ -142,7 +145,7 @@ HRESULT ReaderCallback::OnReadSample( LONGLONG llTimestamp, IMFSample *pSample) { HRESULT hr = S_OK; - IMFMediaBuffer *pBuffer = nullptr; + ComPtr pBuffer; std::lock_guard lock(device->sdMutex); if (device->isShutDown()) return hr; @@ -173,7 +176,7 @@ HRESULT ReaderCallback::OnReadSample( // pSample can be null, in this case ReadSample still should be called to request next frame. if (pSample) { - videoBuffer = new VideoBufferLock(pBuffer); + videoBuffer = new VideoBufferLock(pBuffer.Get()); hr = videoBuffer->LockBuffer(device->deviceParam.default_stride, device->deviceParam.height, &pbScanline0, &lStride); if (lStride > 0) @@ -278,7 +281,6 @@ HRESULT ReaderCallback::OnReadSample( } } - SafeRelease(&pBuffer); return hr; } @@ -522,18 +524,15 @@ bool WindowsCaptureDevice::init() { bool WindowsCaptureDevice::start(void *startParam) { HRESULT hr = S_OK; - IMFAttributes *pAttributes = nullptr; - IMFMediaType *pType = nullptr; + ComPtr pAttributes; + ComPtr pType; UINT32 selection = 0; UINT32 count = 0; // Release old sources first(if any). - SafeRelease(&m_pSource); - SafeRelease(&m_pReader); - if (m_pCallback) { - delete m_pCallback; - m_pCallback = nullptr; - } + m_pSource = nullptr; + m_pReader = nullptr; + m_pCallback = nullptr; // Need to re-enumerate the list,because old sources were released. std::vector deviceList = getDeviceList(true); @@ -555,25 +554,24 @@ bool WindowsCaptureDevice::start(void *startParam) { } ++count; } - setSelction(selection); + setSelection(selection); hr = param.ppDevices[param.selection]->ActivateObject( - __uuidof(IMFMediaSource), - (void**)&m_pSource); + IID_PPV_ARGS(&m_pSource)); if (SUCCEEDED(hr)) hr = MFCreateAttributes(&pAttributes, 2); // Use async mode if (SUCCEEDED(hr)) - hr = pAttributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, m_pCallback); + hr = pAttributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, m_pCallback.Get()); if (SUCCEEDED(hr)) hr = pAttributes->SetUINT32(MF_READWRITE_DISABLE_CONVERTERS, TRUE); if (SUCCEEDED(hr)) { hr = CreateSourceReaderFromMediaSource( - m_pSource, - pAttributes, + m_pSource.Get(), + pAttributes.Get(), &m_pReader ); } @@ -609,7 +607,7 @@ bool WindowsCaptureDevice::start(void *startParam) { if (FAILED(hr)) { break; } - hr = setDeviceParam(pType); + hr = setDeviceParam(pType.Get()); if (SUCCEEDED(hr)) break; @@ -654,7 +652,7 @@ bool WindowsCaptureDevice::start(void *startParam) { if (FAILED(hr)) { break; } - hr = setDeviceParam(pType); + hr = setDeviceParam(pType.Get()); if (SUCCEEDED(hr)) break; @@ -679,15 +677,11 @@ bool WindowsCaptureDevice::start(void *startParam) { setError(CAPTUREDEVIDE_ERROR_START_FAILED, "Cannot start"); if(m_pSource) m_pSource->Shutdown(); - SafeRelease(&m_pSource); - SafeRelease(&pAttributes); - SafeRelease(&pType); - SafeRelease(&m_pReader); + m_pSource = nullptr; + m_pReader = nullptr; return false; } - SafeRelease(&pAttributes); - SafeRelease(&pType); updateState(CAPTUREDEVIDE_STATE::STARTED); break; case CAPTUREDEVIDE_STATE::LOST: @@ -751,13 +745,13 @@ std::vector WindowsCaptureDevice::getDeviceList(bool forceEnum, int if (SUCCEEDED(hr)) { // Get the size needed first - dwMinSize = WideCharToMultiByte(CP_UTF8, NULL, pwstrName, -1, nullptr, 0, nullptr, FALSE); + dwMinSize = WideCharToMultiByte(CP_UTF8, 0, pwstrName, -1, nullptr, 0, nullptr, FALSE); if (dwMinSize == 0) hr = E_FAIL; } if (SUCCEEDED(hr)) { cstrName = new char[dwMinSize]; - WideCharToMultiByte(CP_UTF8, NULL, pwstrName, -1, cstrName, dwMinSize, NULL, FALSE); + WideCharToMultiByte(CP_UTF8, 0, pwstrName, -1, cstrName, dwMinSize, NULL, FALSE); strName = cstrName; delete[] cstrName; @@ -937,9 +931,9 @@ void WindowsCaptureDevice::messageHandler() { stop(); std::lock_guard lock(sdMutex); - SafeRelease(&m_pSource); - SafeRelease(&m_pReader); - delete m_pCallback; + m_pSource = nullptr; + m_pReader = nullptr; + m_pCallback = nullptr; unRegisterCMPTMFApis(); std::unique_lock lock2(paramMutex); @@ -957,7 +951,7 @@ void WindowsCaptureDevice::messageHandler() { HRESULT WindowsCaptureDevice::enumDevices() { HRESULT hr = S_OK; - IMFAttributes *pAttributes = nullptr; + ComPtr pAttributes; hr = MFCreateAttributes(&pAttributes, 1); if (SUCCEEDED(hr)) { @@ -982,10 +976,9 @@ HRESULT WindowsCaptureDevice::enumDevices() { } } if (SUCCEEDED(hr)) { - hr = EnumDeviceSources(pAttributes, ¶m.ppDevices, ¶m.count); + hr = EnumDeviceSources(pAttributes.Get(), ¶m.ppDevices, ¶m.count); } - SafeRelease(&pAttributes); return hr; } diff --git a/Windows/CaptureDevice.h b/Windows/CaptureDevice.h index 0107f3d7747e..c1575d92b47e 100644 --- a/Windows/CaptureDevice.h +++ b/Windows/CaptureDevice.h @@ -25,6 +25,7 @@ #include #include #include +#include #include "Core/HLE/sceUsbMic.h" @@ -193,7 +194,7 @@ class WindowsCaptureDevice { std::vector getDeviceList(bool forceEnum = false, int *pActuallCount = nullptr); void setError(const CAPTUREDEVIDE_ERROR &newError, const std::string &newErrorMessage) { error = newError; errorMessage = newErrorMessage; } - void setSelction(const UINT32 &selection) { param.selection = selection; } + void setSelection(const UINT32 &selection) { param.selection = selection; } HRESULT setDeviceParam(IMFMediaType *pType); bool isShutDown() const { return state == CAPTUREDEVIDE_STATE::SHUTDOWN; } @@ -226,9 +227,9 @@ class WindowsCaptureDevice { bool isDeviceChanged = false; // MF interface. - ReaderCallback *m_pCallback = nullptr; - IMFSourceReader *m_pReader = nullptr; - IMFMediaSource *m_pSource = nullptr; + Microsoft::WRL::ComPtr m_pCallback; + Microsoft::WRL::ComPtr m_pReader; + Microsoft::WRL::ComPtr m_pSource; // Message loop. std::mutex mutex; diff --git a/Windows/DSoundStream.cpp b/Windows/DSoundStream.cpp index 9c12d7e3464b..7cd0e58a8939 100644 --- a/Windows/DSoundStream.cpp +++ b/Windows/DSoundStream.cpp @@ -78,22 +78,21 @@ bool DSoundAudioBackend::CreateBuffer() { dsBuffer_->SetCurrentPosition(0); return true; } else { - dsBuffer_ = NULL; + dsBuffer_ = nullptr; return false; } } int DSoundAudioBackend::RunThread() { if (FAILED(DirectSoundCreate8(0, &ds_, 0))) { - ds_ = NULL; + ds_ = nullptr; threadData_ = 2; return 1; } ds_->SetCooperativeLevel(window_, DSSCL_PRIORITY); if (!CreateBuffer()) { - ds_->Release(); - ds_ = NULL; + ds_ = nullptr; threadData_ = 2; return 1; } @@ -141,8 +140,8 @@ int DSoundAudioBackend::RunThread() { } dsBuffer_->Stop(); - dsBuffer_->Release(); - ds_->Release(); + dsBuffer_ = nullptr; + ds_ = nullptr; threadData_ = 2; return 0; diff --git a/Windows/DSoundStream.h b/Windows/DSoundStream.h index 76d60f5207de..12eda78b502c 100644 --- a/Windows/DSoundStream.h +++ b/Windows/DSoundStream.h @@ -4,9 +4,8 @@ #include "WindowsAudio.h" #include - -struct IDirectSound8; -struct IDirectSoundBuffer; +#include +#include class DSoundAudioBackend : public WindowsAudioBackend { public: @@ -29,8 +28,8 @@ class DSoundAudioBackend : public WindowsAudioBackend { StreamCallback callback_; - IDirectSound8 *ds_ = nullptr; - IDirectSoundBuffer *dsBuffer_ = nullptr; + Microsoft::WRL::ComPtr ds_; + Microsoft::WRL::ComPtr dsBuffer_; int bufferSize_ = 0; // bytes int totalRenderedBytes_ = 0; diff --git a/Windows/DinputDevice.cpp b/Windows/DinputDevice.cpp index 8cfdbeb64bda..b8959b6be9c9 100644 --- a/Windows/DinputDevice.cpp +++ b/Windows/DinputDevice.cpp @@ -16,25 +16,25 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "stdafx.h" +#include +#include #include #include #include #include +#include #include "Common/Input/InputState.h" #include "Common/Input/KeyCodes.h" -#include "Common/LogReporting.h" #include "Common/StringUtils.h" #include "Common/System/NativeApp.h" -#include "Core/Config.h" -#include "Core/HLE/sceCtrl.h" #include "Core/KeyMap.h" #include "Windows/DinputDevice.h" #pragma comment(lib,"dinput8.lib") //initialize static members of DinputDevice unsigned int DinputDevice::pInstances = 0; -LPDIRECTINPUT8 DinputDevice::pDI = NULL; +Microsoft::WRL::ComPtr DinputDevice::pDI; std::vector DinputDevice::devices; bool DinputDevice::needsCheck_ = true; @@ -84,14 +84,14 @@ bool IsXInputDevice( const GUID* pGuidProductFromDirectInput ) { LPDIRECTINPUT8 DinputDevice::getPDI() { - if (pDI == NULL) + if (pDI == nullptr) { if (FAILED(DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDI, NULL))) { - pDI = NULL; + pDI = nullptr; } } - return pDI; + return pDI.Get(); } BOOL CALLBACK DinputDevice::DevicesCallback( @@ -125,7 +125,7 @@ void DinputDevice::getDevices(bool refresh) DinputDevice::DinputDevice(int devnum) { pInstances++; pDevNum = devnum; - pJoystick = NULL; + pJoystick = nullptr; memset(lastButtons_, 0, sizeof(lastButtons_)); memset(lastPOV_, 0, sizeof(lastPOV_)); last_lX_ = 0; @@ -157,8 +157,7 @@ DinputDevice::DinputDevice(int devnum) { } if (FAILED(pJoystick->SetDataFormat(&c_dfDIJoystick2))) { - pJoystick->Release(); - pJoystick = NULL; + pJoystick = nullptr; return; } @@ -187,8 +186,7 @@ DinputDevice::DinputDevice(int devnum) { DinputDevice::~DinputDevice() { if (pJoystick) { - pJoystick->Release(); - pJoystick = NULL; + pJoystick = nullptr; } pInstances--; @@ -198,8 +196,7 @@ DinputDevice::~DinputDevice() { //happening at the same time and other values like pDI are //unsafe as well anyway if (pInstances == 0 && pDI) { - pDI->Release(); - pDI = NULL; + pDI = nullptr; } } diff --git a/Windows/DinputDevice.h b/Windows/DinputDevice.h index 5dc2474e2aa7..88d6655fca19 100644 --- a/Windows/DinputDevice.h +++ b/Windows/DinputDevice.h @@ -19,6 +19,7 @@ #include #include +#include #define DIRECTINPUT_VERSION 0x0800 #define DIRECTINPUT_RGBBUTTONS_MAX 128 #include "InputDevice.h" @@ -54,10 +55,10 @@ class DinputDevice final : ); static unsigned int pInstances; static std::vector devices; - static LPDIRECTINPUT8 pDI; + static Microsoft::WRL::ComPtr pDI; static bool needsCheck_; int pDevNum; - LPDIRECTINPUTDEVICE8 pJoystick; + Microsoft::WRL::ComPtr pJoystick; DIJOYSTATE2 pPrevState; bool analog; BYTE lastButtons_[128]; diff --git a/Windows/W32Util/ShellUtil.cpp b/Windows/W32Util/ShellUtil.cpp index c0f3af4a4939..798e97cba02f 100644 --- a/Windows/W32Util/ShellUtil.cpp +++ b/Windows/W32Util/ShellUtil.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace W32Util { std::string BrowseForFolder(HWND parent, std::string_view title, std::string_view initialPath) { @@ -192,16 +193,16 @@ namespace W32Util { // http://msdn.microsoft.com/en-us/library/aa969393.aspx static HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lpszPathLink, LPCWSTR lpszDesc, LPCWSTR lpszIcon, int iconIndex) { HRESULT hres; - IShellLink *psl = nullptr; + Microsoft::WRL::ComPtr psl; hres = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (FAILED(hres)) return hres; // Get a pointer to the IShellLink interface. It is assumed that CoInitialize // has already been called. - hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *)&psl); + hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl)); if (SUCCEEDED(hres) && psl) { - IPersistFile *ppf = nullptr; + Microsoft::WRL::ComPtr ppf; // Set the path to the shortcut target and add the description. psl->SetPath(lpszPathObj); @@ -213,14 +214,12 @@ static HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lp // Query IShellLink for the IPersistFile interface, used for saving the // shortcut in persistent storage. - hres = psl->QueryInterface(IID_IPersistFile, (LPVOID *)&ppf); + hres = psl.As(&ppf); if (SUCCEEDED(hres) && ppf) { // Save the link by calling IPersistFile::Save. hres = ppf->Save(lpszPathLink, TRUE); - ppf->Release(); } - psl->Release(); } CoUninitialize(); diff --git a/Windows/WASAPIStream.cpp b/Windows/WASAPIStream.cpp index 034bdafd74e9..7e5844883e04 100644 --- a/Windows/WASAPIStream.cpp +++ b/Windows/WASAPIStream.cpp @@ -10,12 +10,14 @@ #include "Common/Thread/ThreadUtil.h" +#include #include #include #include #include #include #include +#include #include "Functiondiscoverykeys_devpkey.h" // Includes some code from https://msdn.microsoft.com/en-us/library/dd370810%28VS.85%29.aspx?f=255&MSPPError=-2147217396 @@ -26,12 +28,11 @@ const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); const IID IID_IAudioClient = __uuidof(IAudioClient); const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); +const IID IID_IMMNotificationClient = __uuidof(IMMNotificationClient ); // Adapted from a MSDN sample. -#define SAFE_RELEASE(punk) \ - if ((punk) != NULL) \ - { (punk)->Release(); (punk) = NULL; } +using Microsoft::WRL::ComPtr; class CMMNotificationClient final : public IMMNotificationClient { public: @@ -41,7 +42,6 @@ class CMMNotificationClient final : public IMMNotificationClient { virtual ~CMMNotificationClient() { CoTaskMemFree(currentDevice_); currentDevice_ = nullptr; - SAFE_RELEASE(_pEnumerator) } void SetCurrentDevice(IMMDevice *device) { @@ -81,7 +81,7 @@ class CMMNotificationClient final : public IMMNotificationClient { if (IID_IUnknown == riid) { AddRef(); *ppvInterface = (IUnknown*)this; - } else if (__uuidof(IMMNotificationClient) == riid) { + } else if (IID_IMMNotificationClient == riid) { AddRef(); *ppvInterface = (IMMNotificationClient*)this; } else { @@ -146,18 +146,17 @@ class CMMNotificationClient final : public IMMNotificationClient { std::string GetDeviceName(LPCWSTR pwstrId) { HRESULT hr = S_OK; - IMMDevice *pDevice = NULL; - IPropertyStore *pProps = NULL; + ComPtr pDevice; + ComPtr pProps; PROPVARIANT varString; PropVariantInit(&varString); if (_pEnumerator == NULL) { // Get enumerator for audio endpoint devices. - hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), + hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, - __uuidof(IMMDeviceEnumerator), - (void**)&_pEnumerator); + IID_PPV_ARGS(&_pEnumerator)); } if (hr == S_OK && _pEnumerator) { hr = _pEnumerator->GetDevice(pwstrId, &pDevice); @@ -174,15 +173,13 @@ class CMMNotificationClient final : public IMMNotificationClient { PropVariantClear(&varString); - SAFE_RELEASE(pProps) - SAFE_RELEASE(pDevice) return name; } private: std::mutex lock_; LONG _cRef = 1; - IMMDeviceEnumerator *_pEnumerator = nullptr; + ComPtr _pEnumerator; wchar_t *currentDevice_ = nullptr; bool deviceChanged_ = false; }; @@ -249,12 +246,12 @@ class WASAPIAudioThread { int &sampleRate_; StreamCallback &callback_; - IMMDeviceEnumerator *deviceEnumerator_ = nullptr; - IMMDevice *device_ = nullptr; - IAudioClient *audioInterface_ = nullptr; - CMMNotificationClient *notificationClient_ = nullptr; + ComPtr deviceEnumerator_; + ComPtr device_; + ComPtr audioInterface_; + ComPtr notificationClient_; WAVEFORMATEXTENSIBLE *deviceFormat_ = nullptr; - IAudioRenderClient *renderClient_ = nullptr; + ComPtr renderClient_; int16_t *shortBuf_ = nullptr; enum class Format { @@ -273,10 +270,9 @@ WASAPIAudioThread::~WASAPIAudioThread() { shortBuf_ = nullptr; ShutdownAudioDevice(); if (notificationClient_ && deviceEnumerator_) - deviceEnumerator_->UnregisterEndpointNotificationCallback(notificationClient_); - delete notificationClient_; + deviceEnumerator_->UnregisterEndpointNotificationCallback(notificationClient_.Get()); notificationClient_ = nullptr; - SAFE_RELEASE(deviceEnumerator_); + deviceEnumerator_ = nullptr; } bool WASAPIAudioThread::ActivateDefaultDevice() { @@ -286,7 +282,7 @@ bool WASAPIAudioThread::ActivateDefaultDevice() { return false; _assert_(audioInterface_ == nullptr); - hresult = device_->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&audioInterface_); + hresult = device_->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, &audioInterface_); if (FAILED(hresult) || audioInterface_ == nullptr) return false; @@ -309,7 +305,7 @@ bool WASAPIAudioThread::InitAudioDevice() { if (FAILED(hresult)) return false; _assert_(renderClient_ == nullptr); - hresult = audioInterface_->GetService(IID_IAudioRenderClient, (void **)&renderClient_); + hresult = audioInterface_->GetService(IID_PPV_ARGS(&renderClient_)); if (FAILED(hresult) || !renderClient_) return false; @@ -324,11 +320,11 @@ bool WASAPIAudioThread::InitAudioDevice() { } void WASAPIAudioThread::ShutdownAudioDevice() { - SAFE_RELEASE(renderClient_); + renderClient_ = nullptr; CoTaskMemFree(deviceFormat_); deviceFormat_ = nullptr; - SAFE_RELEASE(audioInterface_); - SAFE_RELEASE(device_); + audioInterface_ = nullptr; + device_ = nullptr; } bool WASAPIAudioThread::DetectFormat() { @@ -447,7 +443,7 @@ void WASAPIAudioThread::Run() { _assert_(deviceEnumerator_ == nullptr); HRESULT hresult = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, /* Object is not created as the part of the aggregate */ - CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&deviceEnumerator_); + CLSCTX_ALL, IID_PPV_ARGS(&deviceEnumerator_)); if (FAILED(hresult) || deviceEnumerator_ == nullptr) return; @@ -458,11 +454,10 @@ void WASAPIAudioThread::Run() { } notificationClient_ = new CMMNotificationClient(); - notificationClient_->SetCurrentDevice(device_); - hresult = deviceEnumerator_->RegisterEndpointNotificationCallback(notificationClient_); + notificationClient_->SetCurrentDevice(device_.Get()); + hresult = deviceEnumerator_->RegisterEndpointNotificationCallback(notificationClient_.Get()); if (FAILED(hresult)) { // Let's just keep going, but release the client since it doesn't work. - delete notificationClient_; notificationClient_ = nullptr; } @@ -546,7 +541,7 @@ void WASAPIAudioThread::Run() { // TODO: Return to the old device here? return; } - notificationClient_->SetCurrentDevice(device_); + notificationClient_->SetCurrentDevice(device_.Get()); if (!InitAudioDevice()) { ERROR_LOG(Log::sceAudio, "WASAPI: Could not init audio device"); return; diff --git a/Windows/main.cpp b/Windows/main.cpp index cfd7d987e948..030157439c63 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -16,6 +16,9 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "stdafx.h" +#ifdef _WIN32 +#include +#endif #include #include #include @@ -30,6 +33,7 @@ #include #include #include +#include #include "Common/System/Display.h" #include "Common/System/NativeApp.h" @@ -84,12 +88,17 @@ #include "Windows/WindowsHost.h" #include "Windows/main.h" +#ifdef _MSC_VER +#pragma comment(lib, "wbemuuid") +#endif + +using Microsoft::WRL::ComPtr; // Nvidia OpenGL drivers >= v302 will check if the application exports a global // variable named NvOptimusEnablement to know if it should run the app in high // performance graphics mode or using the IGP. extern "C" { - __declspec(dllexport) DWORD NvOptimusEnablement = 1; +__declspec(dllexport) DWORD NvOptimusEnablement = 1; } // Also on AMD PowerExpress: https://community.amd.com/thread/169965 @@ -147,35 +156,33 @@ std::string GetVideoCardDriverVersion() { return retvalue; } - IWbemLocator *pIWbemLocator = NULL; - hr = CoCreateInstance(__uuidof(WbemLocator), NULL, CLSCTX_INPROC_SERVER, - __uuidof(IWbemLocator), (LPVOID *)&pIWbemLocator); + ComPtr pIWbemLocator; + hr = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWbemLocator)); if (FAILED(hr)) { CoUninitialize(); return retvalue; } BSTR bstrServer = SysAllocString(L"\\\\.\\root\\cimv2"); - IWbemServices *pIWbemServices; + ComPtr pIWbemServices; hr = pIWbemLocator->ConnectServer(bstrServer, NULL, NULL, 0L, 0L, NULL, NULL, &pIWbemServices); if (FAILED(hr)) { - pIWbemLocator->Release(); SysFreeString(bstrServer); CoUninitialize(); return retvalue; } - hr = CoSetProxyBlanket(pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, + hr = CoSetProxyBlanket(pIWbemServices.Get(), RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,EOAC_DEFAULT); BSTR bstrWQL = SysAllocString(L"WQL"); BSTR bstrPath = SysAllocString(L"select * from Win32_VideoController"); - IEnumWbemClassObject* pEnum; + ComPtr pEnum; hr = pIWbemServices->ExecQuery(bstrWQL, bstrPath, WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum); ULONG uReturned = 0; VARIANT var{}; - IWbemClassObject* pObj = NULL; + ComPtr pObj; if (!FAILED(hr)) { hr = pEnum->Next(WBEM_INFINITE, 1, &pObj, &uReturned); } @@ -189,11 +196,8 @@ std::string GetVideoCardDriverVersion() { } } - pEnum->Release(); SysFreeString(bstrPath); SysFreeString(bstrWQL); - pIWbemServices->Release(); - pIWbemLocator->Release(); SysFreeString(bstrServer); CoUninitialize(); return retvalue;