| 6 | using namespace OpenGlass; |
| 7 | |
| 8 | HRESULT CReflectionRealizer::LoadTexture(ID2D1DeviceContext* context) |
| 9 | { |
| 10 | winrt::com_ptr<IStream> stream{ nullptr }; |
| 11 | if ( |
| 12 | Shared::g_reflectionTexturePath.empty() || |
| 13 | PathIsRelativeW(Shared::g_reflectionTexturePath.data()) || |
| 14 | PathIsNetworkPathW(Shared::g_reflectionTexturePath.data()) || |
| 15 | !PathFileExistsW(Shared::g_reflectionTexturePath.data()) |
| 16 | ) |
| 17 | { |
| 18 | LOG_HR_IF_MSG(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), !Shared::g_reflectionTexturePath.empty(), "invalid reflection texture path"); |
| 19 | std::span<const UCHAR> textureBytes{}; |
| 20 | RETURN_IF_FAILED(Util::GetResDataView(textureBytes, IDB_REFLECTION, wil::GetModuleInstanceHandle(), L"PNG")); |
| 21 | |
| 22 | stream = { SHCreateMemStream(textureBytes.data(), static_cast<UINT>(textureBytes.size_bytes())), winrt::take_ownership_from_abi }; |
| 23 | } |
| 24 | else |
| 25 | { |
| 26 | wil::unique_hfile file{ CreateFileW(Shared::g_reflectionTexturePath.data(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0) }; |
| 27 | RETURN_LAST_ERROR_IF(!file.is_valid()); |
| 28 | |
| 29 | LARGE_INTEGER fileSize{}; |
| 30 | RETURN_IF_WIN32_BOOL_FALSE(GetFileSizeEx(file.get(), &fileSize)); |
| 31 | |
| 32 | auto buffer{ std::make_unique<BYTE[]>(static_cast<size_t>(fileSize.QuadPart)) }; |
| 33 | RETURN_IF_WIN32_BOOL_FALSE(ReadFile(file.get(), buffer.get(), static_cast<DWORD>(fileSize.QuadPart), nullptr, nullptr)); |
| 34 | stream = { SHCreateMemStream(buffer.get(), static_cast<UINT>(fileSize.QuadPart)), winrt::take_ownership_from_abi }; |
| 35 | } |
| 36 | RETURN_HR_IF_NULL(E_OUTOFMEMORY, stream); |
| 37 | |
| 38 | winrt::com_ptr<IWICImagingFactory> wicFactory{ nullptr }; |
| 39 | wicFactory.copy_from(uDWM::CDesktopManager::GetInstance()->GetWICFactory()); |
| 40 | winrt::com_ptr<IWICBitmapDecoder> wicDecoder{ nullptr }; |
| 41 | RETURN_IF_FAILED(wicFactory->CreateDecoderFromStream(stream.get(), &GUID_VendorMicrosoft, WICDecodeMetadataCacheOnDemand, wicDecoder.put())); |
| 42 | winrt::com_ptr<IWICBitmapFrameDecode> wicFrame{ nullptr }; |
| 43 | RETURN_IF_FAILED(wicDecoder->GetFrame(0, wicFrame.put())); |
| 44 | winrt::com_ptr<IWICFormatConverter> wicConverter{ nullptr }; |
| 45 | RETURN_IF_FAILED(wicFactory->CreateFormatConverter(wicConverter.put())); |
| 46 | RETURN_IF_FAILED( |
| 47 | wicConverter->Initialize( |
| 48 | wicFrame.get(), |
| 49 | GUID_WICPixelFormat32bppPBGRA, |
| 50 | WICBitmapDitherTypeNone, |
| 51 | nullptr, |
| 52 | 0, |
| 53 | WICBitmapPaletteTypeCustom |
| 54 | ) |
| 55 | ); |
| 56 | |
| 57 | RETURN_IF_FAILED( |
| 58 | context->CreateBitmapFromWicBitmap( |
| 59 | wicConverter.get(), |
| 60 | D2D1::BitmapProperties1( |
| 61 | D2D1_BITMAP_OPTIONS_NONE, |
| 62 | D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED) |
| 63 | ), |
| 64 | m_reflectionBitmap.put() |
| 65 | ) |
nothing calls this directly
no test coverage detected