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
28 #define RATIONAL_TO_FLOAT(rational) ((rational.Denominator != 0) ? \
29 static_cast<float>(rational.Numerator) / static_cast<float>(rational.Denominator) : 0.0f)
31 namespace DisplayMetrics
33 // High resolution displays can require a lot of GPU and battery power to render.
34 // High resolution phones, for example, may suffer from poor battery life if
35 // games attempt to render at 60 frames per second at full fidelity.
36 // The decision to render at full fidelity across all platforms and form factors
37 // should be deliberate.
38 static const bool SupportHighResolutions
= true;
40 // The default thresholds that define a "high resolution" display. If the thresholds
41 // are exceeded and SupportHighResolutions is false, the dimensions will be scaled
43 static const float Dpi100
= 96.0f
; // 100% of standard desktop display.
44 static const float DpiThreshold
= 192.0f
; // 200% of standard desktop display.
45 static const float WidthThreshold
= 1920.0f
; // 1080p width.
46 static const float HeightThreshold
= 1080.0f
; // 1080p height.
49 inline void BreakIfFailed(HRESULT hr
)
53 // Set a breakpoint on this line to catch Win32 API errors.
54 #if _DEBUG && !defined(TARGET_WINDOWS_STORE)
57 throw new XbmcCommons::UncheckedException(__FUNCTION__
, "Unhandled error");
61 // Converts a length in device-independent pixels (DIPs) to a length in physical pixels.
62 inline float ConvertDipsToPixels(float dips
, float dpi
)
64 static const float dipsPerInch
= DisplayMetrics::Dpi100
;
65 return floorf(dips
* dpi
/ dipsPerInch
+ 0.5f
); // Round to nearest integer.
68 inline float ConvertPixelsToDips(float pixels
, float dpi
)
70 static const float dipsPerInch
= DisplayMetrics::Dpi100
;
71 return floorf(pixels
/ (dpi
/ dipsPerInch
) + 0.5f
); // Round to nearest integer.
74 inline float RationalToFloat(DXGI_RATIONAL rational
)
76 return RATIONAL_TO_FLOAT(rational
);
79 inline void GetRefreshRatio(uint32_t refresh
, uint32_t *num
, uint32_t *den
)
81 int i
= (((refresh
+ 1) % 24) == 0 || ((refresh
+ 1) % 30) == 0) ? 1 : 0;
82 *num
= (refresh
+ i
) * 1000;
86 inline std::string
GetErrorDescription(HRESULT hr
)
88 using namespace KODI::PLATFORM::WINDOWS
;
91 DXGetErrorDescriptionW(hr
, buff
, 2048);
93 return FromW(StringUtils::Format(L
"{:X} - {} ({})", hr
, DXGetErrorStringW(hr
), buff
));
96 inline std::string
GetFeatureLevelDescription(D3D_FEATURE_LEVEL featureLevel
)
98 uint32_t fl_major
= (featureLevel
& 0xF000u
) >> 12;
99 uint32_t fl_minor
= (featureLevel
& 0x0F00u
) >> 8;
101 return StringUtils::Format("D3D_FEATURE_LEVEL_{}_{}", fl_major
, fl_minor
);
104 inline std::string
GetGFXProviderName(UINT vendorId
)
123 template <typename T
> struct SizeGen
125 SizeGen
<T
>() { Width
= Height
= 0; }
126 SizeGen
<T
>(T width
, T height
) { Width
= width
; Height
= height
; }
128 bool operator !=(const SizeGen
<T
> &size
) const
130 return Width
!= size
.Width
|| Height
!= size
.Height
;
133 const SizeGen
<T
> &operator -=(const SizeGen
<T
> &size
)
136 Height
-= size
.Height
;
140 const SizeGen
<T
> &operator +=(const SizeGen
<T
> &size
)
143 Height
+= size
.Height
;
147 const SizeGen
<T
> &operator -=(const T
&size
)
154 const SizeGen
<T
> &operator +=(const T
&size
)
165 // Check for SDK Layer support.
166 inline bool SdkLayersAvailable()
168 HRESULT hr
= D3D11CreateDevice(
170 D3D_DRIVER_TYPE_NULL
, // There is no need to create a real hardware device.
172 D3D11_CREATE_DEVICE_DEBUG
, // Check for the SDK layers.
173 nullptr, // Any feature level will do.
175 D3D11_SDK_VERSION
, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
176 nullptr, // No need to keep the D3D device reference.
177 nullptr, // No need to know the feature level.
178 nullptr // No need to keep the D3D device context reference.
181 return SUCCEEDED(hr
);
186 #ifdef TARGET_WINDOWS_DESKTOP
193 typedef DX::SizeGen
<float> Size
;
194 typedef DX::SizeGen
<int> SizeInt
;