direct_composition_support.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GL_DIRECT_COMPOSITION_SUPPORT_H_
  5. #define UI_GL_DIRECT_COMPOSITION_SUPPORT_H_
  6. #include <d3d11.h>
  7. #include <dcomp.h>
  8. #include <windows.h>
  9. #include <wrl/client.h>
  10. #include "base/no_destructor.h"
  11. #include "base/observer_list_threadsafe.h"
  12. #include "ui/gfx/geometry/size.h"
  13. #include "ui/gfx/mojom/dxgi_info.mojom.h"
  14. #include "ui/gl/gl_export.h"
  15. #include "ui/gl/gpu_switching_observer.h"
  16. namespace gl {
  17. class GLDisplayEGL;
  18. GL_EXPORT void InitializeDirectComposition(GLDisplayEGL* display);
  19. GL_EXPORT void ShutdownDirectComposition();
  20. // Retrieves the global direct composition device. InitializeDirectComposition
  21. // must be called on GPU process startup before the device is retrieved, and
  22. // ShutdownDirectComposition must be called at process shutdown.
  23. GL_EXPORT IDCompositionDevice2* GetDirectCompositionDevice();
  24. // Returns true if direct composition is supported. We prefer to use direct
  25. // composition even without hardware overlays, because it allows us to bypass
  26. // blitting by DWM to the window redirection surface by using a flip mode swap
  27. // chain. Overridden with --disable-direct-composition.
  28. GL_EXPORT bool DirectCompositionSupported();
  29. // Returns true if video overlays are supported and should be used. Overridden
  30. // with --enable-direct-composition-video-overlays and
  31. // --disable-direct-composition-video-overlays. This function is thread safe.
  32. GL_EXPORT bool DirectCompositionOverlaysSupported();
  33. // Returns true if hardware overlays are supported. This function is thread
  34. // safe.
  35. GL_EXPORT bool DirectCompositionHardwareOverlaysSupported();
  36. // After this is called, overlay support is disabled during the current GPU
  37. // process' lifetime.
  38. GL_EXPORT void DisableDirectCompositionOverlays();
  39. // Similar to the above but disables software overlay support.
  40. GL_EXPORT void DisableDirectCompositionSoftwareOverlays();
  41. // Returns true if zero copy decode swap chain is supported.
  42. GL_EXPORT bool DirectCompositionDecodeSwapChainSupported();
  43. GL_EXPORT void DisableDirectCompositionDecodeSwapChain();
  44. // Returns true if scaled hardware overlays are supported.
  45. GL_EXPORT bool DirectCompositionScaledOverlaysSupported();
  46. // Returns preferred overlay format set when detecting overlay support.
  47. GL_EXPORT DXGI_FORMAT GetDirectCompositionSDROverlayFormat();
  48. // Returns overlay support flags for the given format.
  49. // Caller should check for DXGI_OVERLAY_SUPPORT_FLAG_DIRECT and
  50. // DXGI_OVERLAY_SUPPORT_FLAG_SCALING bits.
  51. // This function is thread safe.
  52. GL_EXPORT UINT GetDirectCompositionOverlaySupportFlags(DXGI_FORMAT format);
  53. // Returns true if swap chain tearing flag is supported.
  54. GL_EXPORT bool DXGISwapChainTearingSupported();
  55. // Returns true if tearing should be used for dcomp root and video swap chains.
  56. GL_EXPORT bool DirectCompositionSwapChainTearingEnabled();
  57. // Returns true if there is an HDR capable display connected.
  58. GL_EXPORT gfx::mojom::DXGIInfoPtr GetDirectCompositionHDRMonitorDXGIInfo();
  59. // Set direct composition swap chain failure so that direct composition is
  60. // marked as unsupported from now on.
  61. GL_EXPORT void SetDirectCompositionSwapChainFailed();
  62. struct DirectCompositionOverlayWorkarounds {
  63. // On Intel GPUs where YUV overlays are supported, BGRA8 overlays are
  64. // supported as well but IDXGIOutput3::CheckOverlaySupport() returns
  65. // unsupported. So allow manually enabling BGRA8 overlay support.
  66. bool enable_bgra8_overlays_with_yuv_overlay_support = false;
  67. // Forces to enable NV12 overlay support regardless of the query results from
  68. // IDXGIOutput3::CheckOverlaySupport().
  69. bool force_nv12_overlay_support = false;
  70. // Forces to enable RGBA101010A2 overlay support regardless of the query
  71. // results from IDXGIOutput3::CheckOverlaySupport().
  72. bool force_rgb10a2_overlay_support = false;
  73. // Enable NV12 overlay support only when
  74. // DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709 is supported.
  75. bool check_ycbcr_studio_g22_left_p709_for_nv12_support = false;
  76. };
  77. GL_EXPORT void SetDirectCompositionOverlayWorkarounds(
  78. const DirectCompositionOverlayWorkarounds& workarounds);
  79. // Returns monitor size.
  80. GL_EXPORT gfx::Size GetDirectCompositionPrimaryMonitorSize();
  81. // Get the current number of all visible display monitors on the desktop.
  82. GL_EXPORT int GetDirectCompositionNumMonitors();
  83. // Testing helpers.
  84. GL_EXPORT void SetDirectCompositionScaledOverlaysSupportedForTesting(
  85. bool value);
  86. GL_EXPORT void SetDirectCompositionOverlayFormatUsedForTesting(
  87. DXGI_FORMAT format);
  88. GL_EXPORT void SetDirectCompositionMonitorInfoForTesting(
  89. int num_monitors,
  90. const gfx::Size& primary_monitor_size);
  91. class GL_EXPORT DirectCompositionOverlayCapsObserver
  92. : public base::CheckedObserver {
  93. public:
  94. virtual void OnOverlayCapsChanged() = 0;
  95. protected:
  96. DirectCompositionOverlayCapsObserver() = default;
  97. ~DirectCompositionOverlayCapsObserver() override = default;
  98. };
  99. // Upon receiving display notifications from ui::GpuSwitchingManager,
  100. // DirectCompositionOverlayCapsMonitor updates its overlay caps with the new
  101. // display setting and notifies DirectCompositionOverlayCapsObserver for the
  102. // overlay cap change.
  103. class GL_EXPORT DirectCompositionOverlayCapsMonitor
  104. : public ui::GpuSwitchingObserver {
  105. public:
  106. DirectCompositionOverlayCapsMonitor(
  107. const DirectCompositionOverlayCapsMonitor&) = delete;
  108. DirectCompositionOverlayCapsMonitor& operator=(
  109. const DirectCompositionOverlayCapsMonitor&) = delete;
  110. static DirectCompositionOverlayCapsMonitor* GetInstance();
  111. // DirectCompositionOverlayCapsMonitor is running on GpuMain thread.
  112. // AddObserver()/RemoveObserver() are thread safe.
  113. void AddObserver(DirectCompositionOverlayCapsObserver* observer);
  114. void RemoveObserver(DirectCompositionOverlayCapsObserver* observer);
  115. // Called when the overlay caps have changed.
  116. void NotifyOverlayCapsChanged();
  117. // Implements GpuSwitchingObserver.
  118. void OnGpuSwitched(gl::GpuPreference active_gpu_heuristic) override;
  119. void OnDisplayAdded() override;
  120. void OnDisplayRemoved() override;
  121. void OnDisplayMetricsChanged() override;
  122. private:
  123. friend class base::NoDestructor<DirectCompositionOverlayCapsMonitor>;
  124. DirectCompositionOverlayCapsMonitor();
  125. ~DirectCompositionOverlayCapsMonitor() override;
  126. scoped_refptr<
  127. base::ObserverListThreadSafe<DirectCompositionOverlayCapsObserver>>
  128. observer_list_;
  129. };
  130. } // namespace gl
  131. #endif // UI_GL_DIRECT_COMPOSITION_SUPPORT_H_