gl_utils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef UI_GL_GL_UTILS_H_
  6. #define UI_GL_GL_UTILS_H_
  7. #include "base/command_line.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "build/build_config.h"
  10. #include "ui/gl/gl_export.h"
  11. #include "ui/gl/gpu_preference.h"
  12. #if BUILDFLAG(IS_WIN)
  13. #include <dxgi1_6.h>
  14. #endif
  15. #if BUILDFLAG(IS_ANDROID)
  16. #include "base/files/scoped_file.h"
  17. #endif
  18. namespace gl {
  19. class GLApi;
  20. #if defined(USE_EGL)
  21. class GLDisplayEGL;
  22. #endif // USE_EGL
  23. #if defined(USE_GLX)
  24. class GLDisplayX11;
  25. #endif // USE_GLX
  26. class GLDisplay;
  27. GL_EXPORT void Crash();
  28. GL_EXPORT void Hang();
  29. #if BUILDFLAG(IS_ANDROID)
  30. GL_EXPORT base::ScopedFD MergeFDs(base::ScopedFD a, base::ScopedFD b);
  31. #endif
  32. GL_EXPORT bool UsePassthroughCommandDecoder(
  33. const base::CommandLine* command_line);
  34. GL_EXPORT bool PassthroughCommandDecoderSupported();
  35. #if BUILDFLAG(IS_WIN)
  36. // Calculates present during in 100 ns from number of frames per second.
  37. GL_EXPORT unsigned int FrameRateToPresentDuration(float frame_rate);
  38. // BufferCount for the root surface swap chain.
  39. GL_EXPORT unsigned int DirectCompositionRootSurfaceBufferCount();
  40. // Whether to use full damage when direct compostion root surface presents.
  41. // This function is thread safe.
  42. GL_EXPORT bool ShouldForceDirectCompositionRootSurfaceFullDamage();
  43. // Labels swapchain with the name_prefix and ts buffers buffers with the string
  44. // name_prefix + _Buffer_ + <buffer_number>.
  45. void LabelSwapChainAndBuffers(IDXGISwapChain* swap_chain,
  46. const char* name_prefix);
  47. // Same as LabelSwapChainAndBuffers, but only does the buffers. Used for resize
  48. // operations.
  49. void LabelSwapChainBuffers(IDXGISwapChain* swap_chain, const char* name_prefix);
  50. #endif
  51. // The following functions expose functionalities from GLDisplayManagerEGL
  52. // and GLDisplayManagerX11 for access outside the ui/gl module. This is because
  53. // the two GLDisplayManager classes are singletons and in component build,
  54. // calling GetInstance() directly returns different instances in different
  55. // components.
  56. #if defined(USE_EGL)
  57. // Add an entry <preference, system_device_id> to GLDisplayManagerEGL.
  58. GL_EXPORT void SetGpuPreferenceEGL(GpuPreference preference,
  59. uint64_t system_device_id);
  60. // Query the default GLDisplay. May return either a GLDisplayEGL or
  61. // GLDisplayX11.
  62. GL_EXPORT GLDisplay* GetDefaultDisplay();
  63. // Query the GLDisplay by |gpu_preference|. May return either a GLDisplayEGL or
  64. // GLDisplayX11.
  65. GL_EXPORT GLDisplay* GetDisplay(GpuPreference gpu_preference);
  66. // Query the default GLDisplayEGL.
  67. GL_EXPORT GLDisplayEGL* GetDefaultDisplayEGL();
  68. // Query the GLDisplayEGL by |system_device_id|.
  69. GL_EXPORT GLDisplayEGL* GetDisplayEGL(uint64_t system_device_id);
  70. #endif // USE_EGL
  71. #if defined(USE_GLX)
  72. // Query the GLDisplayX11 by |system_device_id|.
  73. GL_EXPORT GLDisplayX11* GetDisplayX11(uint64_t system_device_id);
  74. #endif // USE_GLX
  75. // Temporarily allows compilation of shaders that use the
  76. // ARB_texture_rectangle/ANGLE_texture_rectangle extension. We don't want to
  77. // expose the extension to WebGL user shaders but we still need to use it for
  78. // parts of the implementation on macOS. Note that the extension is always
  79. // enabled on macOS and this only controls shader compilation.
  80. class GL_EXPORT ScopedEnableTextureRectangleInShaderCompiler {
  81. public:
  82. ScopedEnableTextureRectangleInShaderCompiler(
  83. const ScopedEnableTextureRectangleInShaderCompiler&) = delete;
  84. ScopedEnableTextureRectangleInShaderCompiler& operator=(
  85. const ScopedEnableTextureRectangleInShaderCompiler&) = delete;
  86. // This class is a no-op except on macOS.
  87. #if !BUILDFLAG(IS_MAC)
  88. explicit ScopedEnableTextureRectangleInShaderCompiler(gl::GLApi* gl_api) {}
  89. #else
  90. explicit ScopedEnableTextureRectangleInShaderCompiler(gl::GLApi* gl_api);
  91. ~ScopedEnableTextureRectangleInShaderCompiler();
  92. private:
  93. raw_ptr<gl::GLApi> gl_api_;
  94. #endif
  95. };
  96. class GL_EXPORT ScopedPixelStore {
  97. public:
  98. ScopedPixelStore(unsigned int name, int value);
  99. ~ScopedPixelStore();
  100. ScopedPixelStore(ScopedPixelStore&) = delete;
  101. ScopedPixelStore& operator=(ScopedPixelStore&) = delete;
  102. private:
  103. const unsigned int name_;
  104. const int old_value_;
  105. const int value_;
  106. };
  107. } // namespace gl
  108. #endif // UI_GL_GL_UTILS_H_