| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2015 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 |
| 5 #include "ui/gl/gl_surface_egl_x11.h" |
| 6 |
| 7 #include "ui/events/platform/platform_event_source.h" |
| 8 #include "ui/gl/egl_util.h" |
| 9 |
| 10 extern "C" { |
| 11 #include <X11/Xlib.h> |
| 12 } |
| 13 |
| 14 using ui::GetLastEGLErrorString; |
| 15 using ui::PlatformEvent; |
| 16 using ui::PlatformEventSource; |
| 17 |
| 18 namespace gfx { |
| 19 |
| 20 NativeViewGLSurfaceEGLX11::NativeViewGLSurfaceEGLX11(EGLNativeWindowType window) |
| 21 : NativeViewGLSurfaceEGL(0), |
| 22 parent_window_(window) { |
| 23 } |
| 24 |
| 25 bool NativeViewGLSurfaceEGLX11::InitializeNativeWindow() { |
| 26 Display* x11_display = GetNativeDisplay(); |
| 27 XWindowAttributes attributes; |
| 28 if (!XGetWindowAttributes(x11_display, parent_window_, &attributes)) { |
| 29 LOG(ERROR) << "XGetWindowAttributes failed for window " << parent_window_ |
| 30 << "."; |
| 31 return false; |
| 32 } |
| 33 |
| 34 size_ = gfx::Size(attributes.width, attributes.height); |
| 35 |
| 36 // Create a child window, with a CopyFromParent visual (to avoid inducing |
| 37 // extra blits in the driver), that we can resize exactly in Resize(), |
| 38 // correctly ordered with GL, so that we don't have invalid transient states. |
| 39 // See https://crbug.com/326995. |
| 40 XSetWindowAttributes swa; |
| 41 memset(&swa, 0, sizeof(swa)); |
| 42 swa.background_pixmap = 0; |
| 43 swa.bit_gravity = NorthWestGravity; |
| 44 window_ = XCreateWindow(x11_display, parent_window_, 0, 0, size_.width(), |
| 45 size_.height(), 0, CopyFromParent, InputOutput, |
| 46 CopyFromParent, CWBackPixmap | CWBitGravity, &swa); |
| 47 XMapWindow(x11_display, window_); |
| 48 |
| 49 // The event source can be nullptr in tests, when we don't care about Exposes. |
| 50 if (PlatformEventSource* source = PlatformEventSource::GetInstance()) { |
| 51 XSelectInput(x11_display, window_, ExposureMask); |
| 52 source->AddPlatformEventDispatcher(this); |
| 53 } |
| 54 XFlush(x11_display); |
| 55 |
| 56 return true; |
| 57 } |
| 58 |
| 59 void NativeViewGLSurfaceEGLX11::Destroy() { |
| 60 if (window_) { |
| 61 if (PlatformEventSource* source = PlatformEventSource::GetInstance()) |
| 62 source->RemovePlatformEventDispatcher(this); |
| 63 |
| 64 Display* x11_display = GetNativeDisplay(); |
| 65 XDestroyWindow(x11_display, window_); |
| 66 window_ = 0; |
| 67 XFlush(x11_display); |
| 68 } |
| 69 |
| 70 NativeViewGLSurfaceEGL::Destroy(); |
| 71 } |
| 72 |
| 73 EGLConfig NativeViewGLSurfaceEGLX11::GetConfig() { |
| 74 if (!config_) { |
| 75 // Get a config compatible with the window |
| 76 DCHECK(window_); |
| 77 XWindowAttributes win_attribs; |
| 78 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) { |
| 79 return NULL; |
| 80 } |
| 81 |
| 82 // Try matching the window depth with an alpha channel, |
| 83 // because we're worried the destination alpha width could |
| 84 // constrain blending precision. |
| 85 const int kBufferSizeOffset = 1; |
| 86 const int kAlphaSizeOffset = 3; |
| 87 EGLint config_attribs[] = { |
| 88 EGL_BUFFER_SIZE, ~0, |
| 89 EGL_ALPHA_SIZE, 8, |
| 90 EGL_BLUE_SIZE, 8, |
| 91 EGL_GREEN_SIZE, 8, |
| 92 EGL_RED_SIZE, 8, |
| 93 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 94 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT, |
| 95 EGL_NONE |
| 96 }; |
| 97 config_attribs[kBufferSizeOffset] = win_attribs.depth; |
| 98 |
| 99 EGLDisplay display = GetHardwareDisplay(); |
| 100 EGLint num_configs; |
| 101 if (!eglChooseConfig(display, |
| 102 config_attribs, |
| 103 &config_, |
| 104 1, |
| 105 &num_configs)) { |
| 106 LOG(ERROR) << "eglChooseConfig failed with error " |
| 107 << GetLastEGLErrorString(); |
| 108 return NULL; |
| 109 } |
| 110 |
| 111 if (num_configs) { |
| 112 EGLint config_depth; |
| 113 if (!eglGetConfigAttrib(display, |
| 114 config_, |
| 115 EGL_BUFFER_SIZE, |
| 116 &config_depth)) { |
| 117 LOG(ERROR) << "eglGetConfigAttrib failed with error " |
| 118 << GetLastEGLErrorString(); |
| 119 return NULL; |
| 120 } |
| 121 |
| 122 if (config_depth == win_attribs.depth) { |
| 123 return config_; |
| 124 } |
| 125 } |
| 126 |
| 127 // Try without an alpha channel. |
| 128 config_attribs[kAlphaSizeOffset] = 0; |
| 129 if (!eglChooseConfig(display, |
| 130 config_attribs, |
| 131 &config_, |
| 132 1, |
| 133 &num_configs)) { |
| 134 LOG(ERROR) << "eglChooseConfig failed with error " |
| 135 << GetLastEGLErrorString(); |
| 136 return NULL; |
| 137 } |
| 138 |
| 139 if (num_configs == 0) { |
| 140 LOG(ERROR) << "No suitable EGL configs found."; |
| 141 return NULL; |
| 142 } |
| 143 } |
| 144 return config_; |
| 145 } |
| 146 |
| 147 bool NativeViewGLSurfaceEGLX11::Resize(const gfx::Size& size, |
| 148 float scale_factor) { |
| 149 if (size == GetSize()) |
| 150 return true; |
| 151 |
| 152 size_ = size; |
| 153 |
| 154 eglWaitGL(); |
| 155 XResizeWindow(GetNativeDisplay(), window_, size.width(), size.height()); |
| 156 eglWaitNative(EGL_CORE_NATIVE_ENGINE); |
| 157 |
| 158 return true; |
| 159 } |
| 160 |
| 161 bool NativeViewGLSurfaceEGLX11::CanDispatchEvent(const PlatformEvent& event) { |
| 162 return event->type == Expose && event->xexpose.window == window_; |
| 163 } |
| 164 |
| 165 uint32_t NativeViewGLSurfaceEGLX11::DispatchEvent(const PlatformEvent& event) { |
| 166 XEvent x_event = *event; |
| 167 x_event.xexpose.window = parent_window_; |
| 168 |
| 169 Display* x11_display = GetNativeDisplay(); |
| 170 XSendEvent(x11_display, parent_window_, False, ExposureMask, &x_event); |
| 171 XFlush(x11_display); |
| 172 return ui::POST_DISPATCH_STOP_PROPAGATION; |
| 173 } |
| 174 |
| 175 NativeViewGLSurfaceEGLX11::~NativeViewGLSurfaceEGLX11() { |
| 176 Destroy(); |
| 177 } |
| 178 |
| 179 } // namespace gfx |
| OLD | NEW |