2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "commons/Exception.h"
14 #include "platform/win32/CharsetConverter.h"
17 #include <ppltasks.h> // For create_task
24 PCIV_MICROSOFT
= 0x1414,
29 #define RATIONAL_TO_FLOAT(rational) ((rational.Denominator != 0) ? \
30 static_cast<float>(rational.Numerator) / static_cast<float>(rational.Denominator) : 0.0f)
32 namespace DisplayMetrics
34 // High resolution displays can require a lot of GPU and battery power to render.
35 // High resolution phones, for example, may suffer from poor battery life if
36 // games attempt to render at 60 frames per second at full fidelity.
37 // The decision to render at full fidelity across all platforms and form factors
38 // should be deliberate.
39 static const bool SupportHighResolutions
= true;
41 // The default thresholds that define a "high resolution" display. If the thresholds
42 // are exceeded and SupportHighResolutions is false, the dimensions will be scaled
44 static const float Dpi100
= 96.0f
; // 100% of standard desktop display.
45 static const float DpiThreshold
= 192.0f
; // 200% of standard desktop display.
46 static const float WidthThreshold
= 1920.0f
; // 1080p width.
47 static const float HeightThreshold
= 1080.0f
; // 1080p height.
50 inline void BreakIfFailed(HRESULT hr
)
54 // Set a breakpoint on this line to catch Win32 API errors.
55 #if _DEBUG && !defined(TARGET_WINDOWS_STORE)
58 throw new XbmcCommons::UncheckedException(__FUNCTION__
, "Unhandled error");
62 // Converts a length in device-independent pixels (DIPs) to a length in physical pixels.
63 inline float ConvertDipsToPixels(float dips
, float dpi
)
65 static const float dipsPerInch
= DisplayMetrics::Dpi100
;
66 return floorf(dips
* dpi
/ dipsPerInch
+ 0.5f
); // Round to nearest integer.
69 inline float ConvertPixelsToDips(float pixels
, float dpi
)
71 static const float dipsPerInch
= DisplayMetrics::Dpi100
;
72 return floorf(pixels
/ (dpi
/ dipsPerInch
) + 0.5f
); // Round to nearest integer.
75 inline float RationalToFloat(DXGI_RATIONAL rational
)
77 return RATIONAL_TO_FLOAT(rational
);
80 inline void GetRefreshRatio(uint32_t refresh
, uint32_t *num
, uint32_t *den
)
82 int i
= (((refresh
+ 1) % 24) == 0 || ((refresh
+ 1) % 30) == 0) ? 1 : 0;
83 *num
= (refresh
+ i
) * 1000;
87 inline std::string
GetErrorDescription(HRESULT hr
)
89 using namespace KODI::PLATFORM::WINDOWS
;
92 DXGetErrorDescriptionW(hr
, buff
, 2048);
94 return FromW(StringUtils::Format(L
"{:X} - {} ({})", hr
, DXGetErrorStringW(hr
), buff
));
97 inline std::string
GetFeatureLevelDescription(D3D_FEATURE_LEVEL featureLevel
)
99 uint32_t fl_major
= (featureLevel
& 0xF000u
) >> 12;
100 uint32_t fl_minor
= (featureLevel
& 0x0F00u
) >> 8;
102 return StringUtils::Format("D3D_FEATURE_LEVEL_{}_{}", fl_major
, fl_minor
);
105 constexpr std::string_view
GetGFXProviderName(UINT vendorId
)
122 constexpr std::string_view
DXGIFormatToShortString(const DXGI_FORMAT format
)
126 case DXGI_FORMAT_B8G8R8A8_UNORM
:
128 case DXGI_FORMAT_R10G10B10A2_UNORM
:
130 case DXGI_FORMAT_R16G16B16A16_FLOAT
:
132 case DXGI_FORMAT_R32G32B32A32_FLOAT
:
139 template <typename T
> struct SizeGen
141 SizeGen
<T
>() { Width
= Height
= 0; }
142 SizeGen
<T
>(T width
, T height
) { Width
= width
; Height
= height
; }
144 bool operator !=(const SizeGen
<T
> &size
) const
146 return Width
!= size
.Width
|| Height
!= size
.Height
;
149 const SizeGen
<T
> &operator -=(const SizeGen
<T
> &size
)
152 Height
-= size
.Height
;
156 const SizeGen
<T
> &operator +=(const SizeGen
<T
> &size
)
159 Height
+= size
.Height
;
163 const SizeGen
<T
> &operator -=(const T
&size
)
170 const SizeGen
<T
> &operator +=(const T
&size
)
181 // Check for SDK Layer support.
182 inline bool SdkLayersAvailable()
184 HRESULT hr
= D3D11CreateDevice(
186 D3D_DRIVER_TYPE_NULL
, // There is no need to create a real hardware device.
188 D3D11_CREATE_DEVICE_DEBUG
, // Check for the SDK layers.
189 nullptr, // Any feature level will do.
191 D3D11_SDK_VERSION
, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
192 nullptr, // No need to keep the D3D device reference.
193 nullptr, // No need to know the feature level.
194 nullptr // No need to keep the D3D device context reference.
197 return SUCCEEDED(hr
);
201 const std::string
DXGIFormatToString(const DXGI_FORMAT format
);
202 const std::string
DXGIColorSpaceTypeToString(DXGI_COLOR_SPACE_TYPE type
);
203 const std::string
D3D11VideoProcessorFormatSupportToString(
204 D3D11_VIDEO_PROCESSOR_FORMAT_SUPPORT value
);
207 #ifdef TARGET_WINDOWS_DESKTOP
214 typedef DX::SizeGen
<float> Size
;
215 typedef DX::SizeGen
<int> SizeInt
;