x11_gl_egl_utility.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2020 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. #include "ui/base/x/x11_gl_egl_utility.h"
  5. #include "ui/base/x/x11_util.h"
  6. #include "ui/gl/gl_surface_egl.h"
  7. #ifndef EGL_ANGLE_x11_visual
  8. #define EGL_ANGLE_x11_visual 1
  9. #define EGL_X11_VISUAL_ID_ANGLE 0x33A3
  10. #endif /* EGL_ANGLE_x11_visual */
  11. #ifndef EGL_ANGLE_platform_angle_null
  12. #define EGL_ANGLE_platform_angle_null 1
  13. #define EGL_PLATFORM_ANGLE_TYPE_NULL_ANGLE 0x33AE
  14. #endif /* EGL_ANGLE_platform_angle_null */
  15. #ifndef EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE
  16. #define EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE 0x348F
  17. #endif
  18. #ifndef EGL_PLATFORM_ANGLE_DEVICE_TYPE_SWIFTSHADER_ANGLE
  19. #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_SWIFTSHADER_ANGLE 0x3487
  20. #endif
  21. #ifndef EGL_ANGLE_platform_angle
  22. #define EGL_ANGLE_platform_angle 1
  23. #define EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE 0x348F
  24. #endif /* EGL_ANGLE_platform_angle */
  25. #ifndef EGL_EXT_platform_x11
  26. #define EGL_EXT_platform_x11 1
  27. #define EGL_PLATFORM_X11_EXT 0x31D5
  28. #endif /* EGL_EXT_platform_x11 */
  29. namespace ui {
  30. void GetPlatformExtraDisplayAttribs(EGLenum platform_type,
  31. std::vector<EGLAttrib>* attributes) {
  32. // ANGLE_NULL and SwiftShader backends don't use the visual,
  33. // and may run without X11 where we can't get it anyway.
  34. if ((platform_type != EGL_PLATFORM_ANGLE_TYPE_NULL_ANGLE) &&
  35. (std::find(attributes->begin(), attributes->end(),
  36. EGL_PLATFORM_ANGLE_DEVICE_TYPE_SWIFTSHADER_ANGLE) ==
  37. attributes->end())) {
  38. x11::VisualId visual_id;
  39. XVisualManager::GetInstance()->ChooseVisualForWindow(
  40. true, &visual_id, nullptr, nullptr, nullptr);
  41. attributes->push_back(EGL_X11_VISUAL_ID_ANGLE);
  42. attributes->push_back(static_cast<EGLAttrib>(visual_id));
  43. attributes->push_back(EGL_PLATFORM_ANGLE_NATIVE_PLATFORM_TYPE_ANGLE);
  44. attributes->push_back(EGL_PLATFORM_X11_EXT);
  45. }
  46. }
  47. void ChoosePlatformCustomAlphaAndBufferSize(EGLint* alpha_size,
  48. EGLint* buffer_size) {
  49. // If we're using ANGLE_NULL, we may not have a display, in which case we
  50. // can't use XVisualManager.
  51. if (gl::GLSurfaceEGL::GetGLDisplayEGL()->GetNativeDisplay().GetDisplay() !=
  52. EGL_DEFAULT_DISPLAY) {
  53. uint8_t depth;
  54. XVisualManager::GetInstance()->ChooseVisualForWindow(true, nullptr, &depth,
  55. nullptr, nullptr);
  56. *buffer_size = depth;
  57. *alpha_size = *buffer_size == 32 ? 8 : 0;
  58. }
  59. }
  60. bool IsTransparentBackgroundSupported() {
  61. return ui::XVisualManager::GetInstance()->ArgbVisualAvailable();
  62. }
  63. } // namespace ui