| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/ozone/platform/x11/x11_surface_factory.h" | 5 #include "ui/ozone/platform/x11/x11_surface_factory.h" |
| 6 | 6 |
| 7 #include <X11/Xlib.h> | 7 #include <X11/Xlib.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "third_party/khronos/EGL/egl.h" | 9 #include "third_party/khronos/EGL/egl.h" |
| 12 #include "ui/gfx/vsync_provider.h" | |
| 13 #include "ui/gfx/x/x11_types.h" | 10 #include "ui/gfx/x/x11_types.h" |
| 11 #include "ui/gl/egl_util.h" |
| 12 #include "ui/gl/gl_surface_egl.h" |
| 14 #include "ui/ozone/common/egl_util.h" | 13 #include "ui/ozone/common/egl_util.h" |
| 15 #include "ui/ozone/public/surface_ozone_egl.h" | |
| 16 | 14 |
| 17 namespace ui { | 15 namespace ui { |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 class X11SurfaceEGL : public SurfaceOzoneEGL { | 19 // GLSurface implementation for Ozone X11 EGL. |
| 20 class GLSurfaceEGLOzoneX11 : public gl::NativeViewGLSurfaceEGL { |
| 22 public: | 21 public: |
| 23 explicit X11SurfaceEGL(gfx::AcceleratedWidget widget) : widget_(widget) {} | 22 explicit GLSurfaceEGLOzoneX11(EGLNativeWindowType window); |
| 24 ~X11SurfaceEGL() override {} | |
| 25 | 23 |
| 26 intptr_t GetNativeWindow() override { return widget_; } | 24 // gl::NativeViewGLSurfaceEGL: |
| 27 | 25 EGLConfig GetConfig() override; |
| 28 bool OnSwapBuffers() override { return true; } | 26 bool Resize(const gfx::Size& size, |
| 29 | 27 float scale_factor, |
| 30 void OnSwapBuffersAsync(const SwapCompletionCallback& callback) override { | 28 bool has_alpha) override; |
| 31 NOTREACHED(); | |
| 32 } | |
| 33 | |
| 34 bool ResizeNativeWindow(const gfx::Size& viewport_size) override { | |
| 35 return true; | |
| 36 } | |
| 37 | |
| 38 std::unique_ptr<gfx::VSyncProvider> CreateVSyncProvider() override { | |
| 39 return nullptr; | |
| 40 } | |
| 41 | |
| 42 void* /* EGLConfig */ GetEGLSurfaceConfig( | |
| 43 const EglConfigCallbacks& egl) override; | |
| 44 | 29 |
| 45 private: | 30 private: |
| 46 gfx::AcceleratedWidget widget_; | 31 ~GLSurfaceEGLOzoneX11() override; |
| 47 | 32 |
| 48 DISALLOW_COPY_AND_ASSIGN(X11SurfaceEGL); | 33 DISALLOW_COPY_AND_ASSIGN(GLSurfaceEGLOzoneX11); |
| 49 }; | 34 }; |
| 50 | 35 |
| 51 void* /* EGLConfig */ X11SurfaceEGL::GetEGLSurfaceConfig( | 36 GLSurfaceEGLOzoneX11::GLSurfaceEGLOzoneX11(EGLNativeWindowType window) |
| 52 const EglConfigCallbacks& egl) { | 37 : NativeViewGLSurfaceEGL(window) {} |
| 53 // Try matching the window depth with an alpha channel, | 38 |
| 54 // because we're worried the destination alpha width could | 39 EGLConfig GLSurfaceEGLOzoneX11::GetConfig() { |
| 55 // constrain blending precision. | 40 // Try matching the window depth with an alpha channel, because we're worried |
| 56 EGLConfig config; | 41 // the destination alpha width could constrain blending precision. |
| 57 const int kBufferSizeOffset = 1; | 42 const int kBufferSizeOffset = 1; |
| 58 const int kAlphaSizeOffset = 3; | 43 const int kAlphaSizeOffset = 3; |
| 59 EGLint config_attribs[] = {EGL_BUFFER_SIZE, | 44 EGLint config_attribs[] = {EGL_BUFFER_SIZE, |
| 60 ~0, // To be replaced. | 45 ~0, // To be replaced. |
| 61 EGL_ALPHA_SIZE, | 46 EGL_ALPHA_SIZE, |
| 62 8, | 47 8, |
| 63 EGL_BLUE_SIZE, | 48 EGL_BLUE_SIZE, |
| 64 8, | 49 8, |
| 65 EGL_GREEN_SIZE, | 50 EGL_GREEN_SIZE, |
| 66 8, | 51 8, |
| 67 EGL_RED_SIZE, | 52 EGL_RED_SIZE, |
| 68 8, | 53 8, |
| 69 EGL_RENDERABLE_TYPE, | 54 EGL_RENDERABLE_TYPE, |
| 70 EGL_OPENGL_ES2_BIT, | 55 EGL_OPENGL_ES2_BIT, |
| 71 EGL_SURFACE_TYPE, | 56 EGL_SURFACE_TYPE, |
| 72 EGL_WINDOW_BIT, | 57 EGL_WINDOW_BIT, |
| 73 EGL_NONE}; | 58 EGL_NONE}; |
| 74 | 59 |
| 75 // Get the depth of XWindow for surface | 60 // Get the depth of XWindow for surface. |
| 76 XWindowAttributes win_attribs; | 61 XWindowAttributes win_attribs; |
| 77 if (XGetWindowAttributes(gfx::GetXDisplay(), widget_, &win_attribs)) { | 62 if (XGetWindowAttributes(gfx::GetXDisplay(), window_, &win_attribs)) { |
| 78 config_attribs[kBufferSizeOffset] = win_attribs.depth; | 63 config_attribs[kBufferSizeOffset] = win_attribs.depth; |
| 79 } | 64 } |
| 80 | 65 |
| 66 EGLDisplay display = GetDisplay(); |
| 67 |
| 68 EGLConfig config; |
| 81 EGLint num_configs; | 69 EGLint num_configs; |
| 82 if (!egl.choose_config.Run(config_attribs, &config, 1, &num_configs)) { | 70 if (!eglChooseConfig(display, config_attribs, &config, 1, &num_configs)) { |
| 83 LOG(ERROR) << "eglChooseConfig failed with error " | 71 LOG(ERROR) << "eglChooseConfig failed with error " |
| 84 << egl.get_last_error_string.Run(); | 72 << GetLastEGLErrorString(); |
| 85 return nullptr; | 73 return nullptr; |
| 86 } | 74 } |
| 75 |
| 87 if (num_configs > 0) { | 76 if (num_configs > 0) { |
| 88 EGLint config_depth; | 77 EGLint config_depth; |
| 89 if (!egl.get_config_attribute.Run(config, EGL_BUFFER_SIZE, &config_depth)) { | 78 if (!eglGetConfigAttrib(display, config, EGL_BUFFER_SIZE, &config_depth)) { |
| 90 LOG(ERROR) << "eglGetConfigAttrib failed with error " | 79 LOG(ERROR) << "eglGetConfigAttrib failed with error " |
| 91 << egl.get_last_error_string.Run(); | 80 << GetLastEGLErrorString(); |
| 92 return nullptr; | 81 return nullptr; |
| 93 } | 82 } |
| 94 if (config_depth == config_attribs[kBufferSizeOffset]) { | 83 if (config_depth == config_attribs[kBufferSizeOffset]) { |
| 95 return config; | 84 return config; |
| 96 } | 85 } |
| 97 } | 86 } |
| 98 | 87 |
| 99 // Try without an alpha channel. | 88 // Try without an alpha channel. |
| 100 config_attribs[kAlphaSizeOffset] = 0; | 89 config_attribs[kAlphaSizeOffset] = 0; |
| 101 if (!egl.choose_config.Run(config_attribs, &config, 1, &num_configs)) { | 90 if (!eglChooseConfig(display, config_attribs, &config, 1, &num_configs)) { |
| 102 LOG(ERROR) << "eglChooseConfig failed with error " | 91 LOG(ERROR) << "eglChooseConfig failed with error " |
| 103 << egl.get_last_error_string.Run(); | 92 << GetLastEGLErrorString(); |
| 104 return nullptr; | 93 return nullptr; |
| 105 } | 94 } |
| 95 |
| 106 if (num_configs == 0) { | 96 if (num_configs == 0) { |
| 107 LOG(ERROR) << "No suitable EGL configs found."; | 97 LOG(ERROR) << "No suitable EGL configs found."; |
| 108 return nullptr; | 98 return nullptr; |
| 109 } | 99 } |
| 110 return config; | 100 return config; |
| 111 } | 101 } |
| 112 | 102 |
| 103 bool GLSurfaceEGLOzoneX11::Resize(const gfx::Size& size, |
| 104 float scale_factor, |
| 105 bool has_alpha) { |
| 106 if (size == GetSize()) |
| 107 return true; |
| 108 |
| 109 size_ = size; |
| 110 |
| 111 eglWaitGL(); |
| 112 XResizeWindow(gfx::GetXDisplay(), window_, size.width(), size.height()); |
| 113 eglWaitNative(EGL_CORE_NATIVE_ENGINE); |
| 114 |
| 115 return true; |
| 116 } |
| 117 |
| 118 GLSurfaceEGLOzoneX11::~GLSurfaceEGLOzoneX11() { |
| 119 Destroy(); |
| 120 } |
| 121 |
| 113 } // namespace | 122 } // namespace |
| 114 | 123 |
| 115 X11SurfaceFactory::X11SurfaceFactory() {} | 124 X11SurfaceFactory::X11SurfaceFactory() {} |
| 116 | 125 |
| 117 X11SurfaceFactory::~X11SurfaceFactory() {} | 126 X11SurfaceFactory::~X11SurfaceFactory() {} |
| 118 | 127 |
| 119 std::unique_ptr<SurfaceOzoneEGL> X11SurfaceFactory::CreateEGLSurfaceForWidget( | 128 bool X11SurfaceFactory::UseNewSurfaceAPI() { |
| 129 return true; |
| 130 } |
| 131 |
| 132 scoped_refptr<gl::GLSurface> X11SurfaceFactory::CreateViewGLSurface( |
| 133 gl::GLImplementation implementation, |
| 120 gfx::AcceleratedWidget widget) { | 134 gfx::AcceleratedWidget widget) { |
| 121 return base::WrapUnique(new X11SurfaceEGL(widget)); | 135 if (implementation != gl::kGLImplementationEGLGLES2) { |
| 136 NOTREACHED(); |
| 137 return nullptr; |
| 138 } |
| 139 |
| 140 return gl::InitializeGLSurface(new GLSurfaceEGLOzoneX11(widget)); |
| 141 } |
| 142 |
| 143 scoped_refptr<gl::GLSurface> X11SurfaceFactory::CreateOffscreenGLSurface( |
| 144 gl::GLImplementation implementation, |
| 145 const gfx::Size& size) { |
| 146 if (implementation != gl::kGLImplementationEGLGLES2) { |
| 147 NOTREACHED(); |
| 148 return nullptr; |
| 149 } |
| 150 |
| 151 return gl::InitializeGLSurface(new gl::PbufferGLSurfaceEGL(size)); |
| 122 } | 152 } |
| 123 | 153 |
| 124 bool X11SurfaceFactory::LoadEGLGLES2Bindings( | 154 bool X11SurfaceFactory::LoadEGLGLES2Bindings( |
| 125 AddGLLibraryCallback add_gl_library, | 155 AddGLLibraryCallback add_gl_library, |
| 126 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { | 156 SetGLGetProcAddressProcCallback set_gl_get_proc_address) { |
| 127 return LoadDefaultEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address); | 157 return LoadDefaultEGLGLES2Bindings(add_gl_library, set_gl_get_proc_address); |
| 128 } | 158 } |
| 129 | 159 |
| 130 intptr_t X11SurfaceFactory::GetNativeDisplay() { | 160 intptr_t X11SurfaceFactory::GetNativeDisplay() { |
| 131 return reinterpret_cast<intptr_t>(gfx::GetXDisplay()); | 161 return reinterpret_cast<intptr_t>(gfx::GetXDisplay()); |
| 132 } | 162 } |
| 133 | 163 |
| 134 } // namespace ui | 164 } // namespace ui |
| OLD | NEW |