gl_utils.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright (c) 2017 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. // This file contains some useful utilities for the ui/gl classes.
  5. #include "ui/gl/gl_utils.h"
  6. #include "base/command_line.h"
  7. #include "base/debug/alias.h"
  8. #include "base/logging.h"
  9. #include "build/build_config.h"
  10. #include "ui/gl/gl_bindings.h"
  11. #include "ui/gl/gl_display_manager.h"
  12. #include "ui/gl/gl_features.h"
  13. #include "ui/gl/gl_switches.h"
  14. #if defined(USE_EGL)
  15. #include "ui/gl/gl_surface_egl.h"
  16. #endif // defined(USE_EGL)
  17. #if BUILDFLAG(IS_ANDROID)
  18. #include "base/posix/eintr_wrapper.h"
  19. #include "third_party/libsync/src/include/sync/sync.h"
  20. #endif
  21. #if BUILDFLAG(IS_WIN)
  22. #include <d3d11_1.h>
  23. #include "base/strings/stringprintf.h"
  24. #include "media/base/win/mf_helpers.h"
  25. #include "ui/gl/direct_composition_support.h"
  26. #endif
  27. namespace gl {
  28. namespace {
  29. int GetIntegerv(unsigned int name) {
  30. int value = 0;
  31. glGetIntegerv(name, &value);
  32. return value;
  33. }
  34. } // namespace
  35. // Used by chrome://gpucrash and gpu_benchmarking_extension's
  36. // CrashForTesting.
  37. void Crash() {
  38. DVLOG(1) << "GPU: Simulating GPU crash";
  39. // Good bye, cruel world.
  40. volatile int* it_s_the_end_of_the_world_as_we_know_it = nullptr;
  41. *it_s_the_end_of_the_world_as_we_know_it = 0xdead;
  42. }
  43. // Used by chrome://gpuhang.
  44. void Hang() {
  45. DVLOG(1) << "GPU: Simulating GPU hang";
  46. int do_not_delete_me = 0;
  47. for (;;) {
  48. // Do not sleep here. The GPU watchdog timer tracks
  49. // the amount of user time this thread is using and
  50. // it doesn't use much while calling Sleep.
  51. // The following are multiple mechanisms to prevent compilers from
  52. // optimizing out the endless loop. Hope at least one of them works.
  53. base::debug::Alias(&do_not_delete_me);
  54. ++do_not_delete_me;
  55. __asm__ volatile("");
  56. }
  57. }
  58. #if BUILDFLAG(IS_ANDROID)
  59. base::ScopedFD MergeFDs(base::ScopedFD a, base::ScopedFD b) {
  60. if (!a.is_valid())
  61. return b;
  62. if (!b.is_valid())
  63. return a;
  64. base::ScopedFD merged(HANDLE_EINTR(sync_merge("", a.get(), b.get())));
  65. if (!merged.is_valid())
  66. LOG(ERROR) << "Failed to merge fences.";
  67. return merged;
  68. }
  69. #endif
  70. bool UsePassthroughCommandDecoder(const base::CommandLine* command_line) {
  71. std::string switch_value;
  72. if (command_line->HasSwitch(switches::kUseCmdDecoder)) {
  73. switch_value = command_line->GetSwitchValueASCII(switches::kUseCmdDecoder);
  74. }
  75. if (switch_value == kCmdDecoderPassthroughName) {
  76. return true;
  77. } else if (switch_value == kCmdDecoderValidatingName) {
  78. return false;
  79. } else {
  80. // Unrecognized or missing switch, use the default.
  81. return features::UsePassthroughCommandDecoder();
  82. }
  83. }
  84. bool PassthroughCommandDecoderSupported() {
  85. #if defined(USE_EGL)
  86. GLDisplayEGL* display = gl::GLSurfaceEGL::GetGLDisplayEGL();
  87. // Using the passthrough command buffer requires that specific ANGLE
  88. // extensions are exposed
  89. return display->ext->b_EGL_CHROMIUM_create_context_bind_generates_resource &&
  90. display->ext->b_EGL_ANGLE_create_context_webgl_compatibility &&
  91. display->ext->b_EGL_ANGLE_robust_resource_initialization &&
  92. display->ext->b_EGL_ANGLE_display_texture_share_group &&
  93. display->ext->b_EGL_ANGLE_create_context_client_arrays;
  94. #else
  95. // The passthrough command buffer is only supported on top of ANGLE/EGL
  96. return false;
  97. #endif // defined(USE_EGL)
  98. }
  99. #if BUILDFLAG(IS_WIN)
  100. unsigned int FrameRateToPresentDuration(float frame_rate) {
  101. if (frame_rate == 0)
  102. return 0u;
  103. // Present duration unit is 100 ns.
  104. return static_cast<unsigned int>(1.0E7 / frame_rate);
  105. }
  106. unsigned int DirectCompositionRootSurfaceBufferCount() {
  107. return base::FeatureList::IsEnabled(features::kDCompTripleBufferRootSwapChain)
  108. ? 3u
  109. : 2u;
  110. }
  111. bool ShouldForceDirectCompositionRootSurfaceFullDamage() {
  112. static bool should_force = []() {
  113. const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  114. if (cmd_line->HasSwitch(
  115. switches::kDirectCompositionForceFullDamageForTesting)) {
  116. return true;
  117. }
  118. UINT brga_flags =
  119. GetDirectCompositionOverlaySupportFlags(DXGI_FORMAT_B8G8R8A8_UNORM);
  120. constexpr UINT kSupportBits =
  121. DXGI_OVERLAY_SUPPORT_FLAG_DIRECT | DXGI_OVERLAY_SUPPORT_FLAG_SCALING;
  122. if ((brga_flags & kSupportBits) == 0)
  123. return false;
  124. if (!base::FeatureList::IsEnabled(
  125. features::kDirectCompositionForceFullDamage)) {
  126. return false;
  127. }
  128. return true;
  129. }();
  130. return should_force;
  131. }
  132. // Labels swapchain buffers with the string name_prefix + _Buffer_ +
  133. // <buffer_number>
  134. void LabelSwapChainBuffers(IDXGISwapChain* swap_chain,
  135. const char* name_prefix) {
  136. DXGI_SWAP_CHAIN_DESC desc;
  137. HRESULT hr = swap_chain->GetDesc(&desc);
  138. if (FAILED(hr)) {
  139. DLOG(ERROR) << "Failed to GetDesc from swap chain: "
  140. << logging::SystemErrorCodeToString(hr);
  141. return;
  142. }
  143. for (unsigned int i = 0; i < desc.BufferCount; i++) {
  144. Microsoft::WRL::ComPtr<ID3D11Texture2D> swap_chain_buffer;
  145. hr = swap_chain->GetBuffer(i, IID_PPV_ARGS(&swap_chain_buffer));
  146. if (FAILED(hr)) {
  147. DLOG(ERROR) << "GetBuffer on swap chain buffer " << i
  148. << "failed: " << logging::SystemErrorCodeToString(hr);
  149. return;
  150. }
  151. const std::string buffer_name =
  152. base::StringPrintf("%s_Buffer_%d", name_prefix, i);
  153. hr = media::SetDebugName(swap_chain_buffer.Get(), buffer_name.c_str());
  154. if (FAILED(hr)) {
  155. DLOG(ERROR) << "Failed to label swap chain buffer " << i << ": "
  156. << logging::SystemErrorCodeToString(hr);
  157. }
  158. }
  159. }
  160. // Same as LabelSwapChainAndBuffers, but only does the buffers. Used for resize
  161. // operations
  162. void LabelSwapChainAndBuffers(IDXGISwapChain* swap_chain,
  163. const char* name_prefix) {
  164. media::SetDebugName(swap_chain, name_prefix);
  165. LabelSwapChainBuffers(swap_chain, name_prefix);
  166. }
  167. #endif // BUILDFLAG(IS_WIN)
  168. GLDisplay* GetDisplay(GpuPreference gpu_preference) {
  169. #if defined(USE_GLX)
  170. if (!GLDisplayManagerX11::GetInstance()->IsEmpty()) {
  171. return GLDisplayManagerX11::GetInstance()->GetDisplay(gpu_preference);
  172. }
  173. #endif
  174. #if defined(USE_EGL)
  175. return GLDisplayManagerEGL::GetInstance()->GetDisplay(gpu_preference);
  176. #endif
  177. NOTREACHED();
  178. return nullptr;
  179. }
  180. GLDisplay* GetDefaultDisplay() {
  181. return GetDisplay(GpuPreference::kDefault);
  182. }
  183. #if defined(USE_EGL)
  184. void SetGpuPreferenceEGL(GpuPreference preference, uint64_t system_device_id) {
  185. GLDisplayManagerEGL::GetInstance()->SetGpuPreference(preference,
  186. system_device_id);
  187. }
  188. GLDisplayEGL* GetDefaultDisplayEGL() {
  189. return GLDisplayManagerEGL::GetInstance()->GetDisplay(
  190. GpuPreference::kDefault);
  191. }
  192. GLDisplayEGL* GetDisplayEGL(uint64_t system_device_id) {
  193. return GLDisplayManagerEGL::GetInstance()->GetDisplay(system_device_id);
  194. }
  195. #endif // USE_EGL
  196. #if defined(USE_GLX)
  197. GLDisplayX11* GetDisplayX11(uint64_t system_device_id) {
  198. return GLDisplayManagerX11::GetInstance()->GetDisplay(system_device_id);
  199. }
  200. #endif // USE_GLX
  201. #if BUILDFLAG(IS_MAC)
  202. ScopedEnableTextureRectangleInShaderCompiler::
  203. ScopedEnableTextureRectangleInShaderCompiler(gl::GLApi* gl_api) {
  204. if (gl_api) {
  205. DCHECK(!gl_api->glIsEnabledFn(GL_TEXTURE_RECTANGLE_ANGLE));
  206. gl_api->glEnableFn(GL_TEXTURE_RECTANGLE_ANGLE);
  207. gl_api_ = gl_api;
  208. } else {
  209. gl_api_ = nullptr; // Signal to the destructor that this is a no-op.
  210. }
  211. }
  212. ScopedEnableTextureRectangleInShaderCompiler::
  213. ~ScopedEnableTextureRectangleInShaderCompiler() {
  214. if (gl_api_)
  215. gl_api_->glDisableFn(GL_TEXTURE_RECTANGLE_ANGLE);
  216. }
  217. #endif // BUILDFLAG(IS_MAC)
  218. ScopedPixelStore::ScopedPixelStore(unsigned int name, int value)
  219. : name_(name), old_value_(GetIntegerv(name)), value_(value) {
  220. if (value_ != old_value_)
  221. glPixelStorei(name_, value_);
  222. }
  223. ScopedPixelStore::~ScopedPixelStore() {
  224. if (value_ != old_value_)
  225. glPixelStorei(name_, old_value_);
  226. }
  227. } // namespace gl