1 // Copyright (c) 2012 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.
5 // This include must be here so that the includes provided transitively
6 // by gl_surface_egl.h don't make it impossible to compile this code.
7 #include "third_party/mesa/src/include/GL/osmesa.h"
9 #include "ui/gl/gl_surface_egl.h"
11 #if defined(OS_ANDROID)
12 #include <android/native_window_jni.h>
15 #include "base/debug/trace_event.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "build/build_config.h"
20 #include "ui/gfx/geometry/rect.h"
21 #include "ui/gl/egl_util.h"
22 #include "ui/gl/gl_context.h"
23 #include "ui/gl/gl_implementation.h"
24 #include "ui/gl/gl_surface_osmesa.h"
25 #include "ui/gl/gl_surface_stub.h"
26 #include "ui/gl/gl_switches.h"
27 #include "ui/gl/scoped_make_current.h"
28 #include "ui/gl/sync_control_vsync_provider.h"
36 #if defined (USE_OZONE)
37 #include "ui/ozone/public/surface_factory_ozone.h"
40 #if !defined(EGL_FIXED_SIZE_ANGLE)
41 #define EGL_FIXED_SIZE_ANGLE 0x3201
44 using ui::GetLastEGLErrorString;
52 EGLNativeDisplayType g_native_display;
54 const char* g_egl_extensions = NULL;
55 bool g_egl_create_context_robustness_supported = false;
56 bool g_egl_sync_control_supported = false;
57 bool g_egl_window_fixed_size_supported = false;
58 bool g_egl_surfaceless_context_supported = false;
60 class EGLSyncControlVSyncProvider
61 : public gfx::SyncControlVSyncProvider {
63 explicit EGLSyncControlVSyncProvider(EGLSurface surface)
64 : SyncControlVSyncProvider(),
68 virtual ~EGLSyncControlVSyncProvider() { }
71 virtual bool GetSyncValues(int64* system_time,
72 int64* media_stream_counter,
73 int64* swap_buffer_counter) OVERRIDE {
74 uint64 u_system_time, u_media_stream_counter, u_swap_buffer_counter;
75 bool result = eglGetSyncValuesCHROMIUM(
76 g_display, surface_, &u_system_time,
77 &u_media_stream_counter, &u_swap_buffer_counter) == EGL_TRUE;
79 *system_time = static_cast<int64>(u_system_time);
80 *media_stream_counter = static_cast<int64>(u_media_stream_counter);
81 *swap_buffer_counter = static_cast<int64>(u_swap_buffer_counter);
86 virtual bool GetMscRate(int32* numerator, int32* denominator) OVERRIDE {
93 DISALLOW_COPY_AND_ASSIGN(EGLSyncControlVSyncProvider);
98 GLSurfaceEGL::GLSurfaceEGL() {}
100 bool GLSurfaceEGL::InitializeOneOff() {
101 static bool initialized = false;
105 g_native_display = GetPlatformDefaultEGLNativeDisplay();
106 g_display = eglGetDisplay(g_native_display);
108 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
112 if (!eglInitialize(g_display, NULL, NULL)) {
113 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString();
117 // Choose an EGL configuration.
118 // On X this is only used for PBuffer surfaces.
119 static const EGLint kConfigAttribs[] = {
125 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
126 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
130 #if defined(USE_OZONE)
131 const EGLint* config_attribs =
132 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties(
135 const EGLint* config_attribs = kConfigAttribs;
139 if (!eglChooseConfig(g_display,
144 LOG(ERROR) << "eglChooseConfig failed with error "
145 << GetLastEGLErrorString();
149 if (num_configs == 0) {
150 LOG(ERROR) << "No suitable EGL configs found.";
154 if (!eglChooseConfig(g_display,
159 LOG(ERROR) << "eglChooseConfig failed with error "
160 << GetLastEGLErrorString();
164 g_egl_extensions = eglQueryString(g_display, EGL_EXTENSIONS);
165 g_egl_create_context_robustness_supported =
166 HasEGLExtension("EGL_EXT_create_context_robustness");
167 g_egl_sync_control_supported =
168 HasEGLExtension("EGL_CHROMIUM_sync_control");
169 g_egl_window_fixed_size_supported =
170 HasEGLExtension("EGL_ANGLE_window_fixed_size");
172 // TODO(oetuaho@nvidia.com): Surfaceless is disabled on Android as a temporary
173 // workaround, since code written for Android WebView takes different paths
174 // based on whether GL surface objects have underlying EGL surface handles,
175 // conflicting with the use of surfaceless. See https://crbug.com/382349
176 #if defined(OS_ANDROID)
177 DCHECK(!g_egl_surfaceless_context_supported);
179 // Check if SurfacelessEGL is supported.
180 g_egl_surfaceless_context_supported =
181 HasEGLExtension("EGL_KHR_surfaceless_context");
182 if (g_egl_surfaceless_context_supported) {
183 // EGL_KHR_surfaceless_context is supported but ensure
184 // GL_OES_surfaceless_context is also supported. We need a current context
185 // to query for supported GL extensions.
186 scoped_refptr<GLSurface> surface = new SurfacelessEGL(Size(1, 1));
187 scoped_refptr<GLContext> context = GLContext::CreateGLContext(
188 NULL, surface.get(), PreferIntegratedGpu);
189 if (!context->MakeCurrent(surface.get()))
190 g_egl_surfaceless_context_supported = false;
192 // Ensure context supports GL_OES_surfaceless_context.
193 if (g_egl_surfaceless_context_supported) {
194 g_egl_surfaceless_context_supported = context->HasExtension(
195 "GL_OES_surfaceless_context");
196 context->ReleaseCurrent(surface.get());
206 EGLDisplay GLSurfaceEGL::GetDisplay() {
210 EGLDisplay GLSurfaceEGL::GetHardwareDisplay() {
214 EGLNativeDisplayType GLSurfaceEGL::GetNativeDisplay() {
215 return g_native_display;
218 const char* GLSurfaceEGL::GetEGLExtensions() {
219 return g_egl_extensions;
222 bool GLSurfaceEGL::HasEGLExtension(const char* name) {
223 return ExtensionsContain(GetEGLExtensions(), name);
226 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() {
227 return g_egl_create_context_robustness_supported;
230 bool GLSurfaceEGL::IsEGLSurfacelessContextSupported() {
231 return g_egl_surfaceless_context_supported;
234 GLSurfaceEGL::~GLSurfaceEGL() {}
236 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window)
239 supports_post_sub_buffer_(false),
242 #if defined(OS_ANDROID)
244 ANativeWindow_acquire(window);
249 if (GetClientRect(window_, &windowRect))
250 size_ = gfx::Rect(windowRect).size();
254 bool NativeViewGLSurfaceEGL::Initialize() {
255 return Initialize(scoped_ptr<VSyncProvider>());
258 bool NativeViewGLSurfaceEGL::Initialize(
259 scoped_ptr<VSyncProvider> sync_provider) {
263 LOG(ERROR) << "Trying to create surface with invalid display.";
267 std::vector<EGLint> egl_window_attributes;
269 if (g_egl_window_fixed_size_supported) {
270 egl_window_attributes.push_back(EGL_FIXED_SIZE_ANGLE);
271 egl_window_attributes.push_back(EGL_TRUE);
272 egl_window_attributes.push_back(EGL_WIDTH);
273 egl_window_attributes.push_back(size_.width());
274 egl_window_attributes.push_back(EGL_HEIGHT);
275 egl_window_attributes.push_back(size_.height());
278 if (gfx::g_driver_egl.ext.b_EGL_NV_post_sub_buffer) {
279 egl_window_attributes.push_back(EGL_POST_SUB_BUFFER_SUPPORTED_NV);
280 egl_window_attributes.push_back(EGL_TRUE);
283 egl_window_attributes.push_back(EGL_NONE);
284 // Create a surface for the native window.
285 surface_ = eglCreateWindowSurface(
286 GetDisplay(), GetConfig(), window_, &egl_window_attributes[0]);
289 LOG(ERROR) << "eglCreateWindowSurface failed with error "
290 << GetLastEGLErrorString();
296 EGLBoolean retVal = eglQuerySurface(GetDisplay(),
298 EGL_POST_SUB_BUFFER_SUPPORTED_NV,
300 supports_post_sub_buffer_ = (surfaceVal && retVal) == EGL_TRUE;
303 vsync_provider_.reset(sync_provider.release());
304 else if (g_egl_sync_control_supported)
305 vsync_provider_.reset(new EGLSyncControlVSyncProvider(surface_));
309 void NativeViewGLSurfaceEGL::Destroy() {
311 if (!eglDestroySurface(GetDisplay(), surface_)) {
312 LOG(ERROR) << "eglDestroySurface failed with error "
313 << GetLastEGLErrorString();
319 EGLConfig NativeViewGLSurfaceEGL::GetConfig() {
320 #if !defined(USE_X11)
324 // Get a config compatible with the window
326 XWindowAttributes win_attribs;
327 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) {
331 // Try matching the window depth with an alpha channel,
332 // because we're worried the destination alpha width could
333 // constrain blending precision.
334 const int kBufferSizeOffset = 1;
335 const int kAlphaSizeOffset = 3;
336 EGLint config_attribs[] = {
342 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
343 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
346 config_attribs[kBufferSizeOffset] = win_attribs.depth;
349 if (!eglChooseConfig(g_display,
354 LOG(ERROR) << "eglChooseConfig failed with error "
355 << GetLastEGLErrorString();
361 if (!eglGetConfigAttrib(g_display,
365 LOG(ERROR) << "eglGetConfigAttrib failed with error "
366 << GetLastEGLErrorString();
370 if (config_depth == win_attribs.depth) {
375 // Try without an alpha channel.
376 config_attribs[kAlphaSizeOffset] = 0;
377 if (!eglChooseConfig(g_display,
382 LOG(ERROR) << "eglChooseConfig failed with error "
383 << GetLastEGLErrorString();
387 if (num_configs == 0) {
388 LOG(ERROR) << "No suitable EGL configs found.";
396 bool NativeViewGLSurfaceEGL::IsOffscreen() {
400 bool NativeViewGLSurfaceEGL::SwapBuffers() {
401 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers",
402 "width", GetSize().width(),
403 "height", GetSize().height());
405 if (!eglSwapBuffers(GetDisplay(), surface_)) {
406 DVLOG(1) << "eglSwapBuffers failed with error "
407 << GetLastEGLErrorString();
414 gfx::Size NativeViewGLSurfaceEGL::GetSize() {
417 if (!eglQuerySurface(GetDisplay(), surface_, EGL_WIDTH, &width) ||
418 !eglQuerySurface(GetDisplay(), surface_, EGL_HEIGHT, &height)) {
419 NOTREACHED() << "eglQuerySurface failed with error "
420 << GetLastEGLErrorString();
424 return gfx::Size(width, height);
427 bool NativeViewGLSurfaceEGL::Resize(const gfx::Size& size) {
428 if (size == GetSize())
433 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
434 GLContext* current_context = GLContext::GetCurrent();
436 current_context && current_context->IsCurrent(this);
438 scoped_make_current.reset(
439 new ui::ScopedMakeCurrent(current_context, this));
440 current_context->ReleaseCurrent(this);
446 LOG(ERROR) << "Failed to resize window.";
453 bool NativeViewGLSurfaceEGL::Recreate() {
456 LOG(ERROR) << "Failed to create surface.";
462 EGLSurface NativeViewGLSurfaceEGL::GetHandle() {
466 bool NativeViewGLSurfaceEGL::SupportsPostSubBuffer() {
467 return supports_post_sub_buffer_;
470 bool NativeViewGLSurfaceEGL::PostSubBuffer(
471 int x, int y, int width, int height) {
472 DCHECK(supports_post_sub_buffer_);
473 if (!eglPostSubBufferNV(GetDisplay(), surface_, x, y, width, height)) {
474 DVLOG(1) << "eglPostSubBufferNV failed with error "
475 << GetLastEGLErrorString();
481 VSyncProvider* NativeViewGLSurfaceEGL::GetVSyncProvider() {
482 return vsync_provider_.get();
485 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() {
487 #if defined(OS_ANDROID)
489 ANativeWindow_release(window_);
493 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(const gfx::Size& size)
496 // Some implementations of Pbuffer do not support having a 0 size. For such
497 // cases use a (1, 1) surface.
498 if (size_.GetArea() == 0)
502 bool PbufferGLSurfaceEGL::Initialize() {
503 EGLSurface old_surface = surface_;
505 EGLDisplay display = GetDisplay();
507 LOG(ERROR) << "Trying to create surface with invalid display.";
511 // Allocate the new pbuffer surface before freeing the old one to ensure
512 // they have different addresses. If they have the same address then a
513 // future call to MakeCurrent might early out because it appears the current
514 // context and surface have not changed.
515 const EGLint pbuffer_attribs[] = {
516 EGL_WIDTH, size_.width(),
517 EGL_HEIGHT, size_.height(),
521 EGLSurface new_surface = eglCreatePbufferSurface(display,
525 LOG(ERROR) << "eglCreatePbufferSurface failed with error "
526 << GetLastEGLErrorString();
531 eglDestroySurface(display, old_surface);
533 surface_ = new_surface;
537 void PbufferGLSurfaceEGL::Destroy() {
539 if (!eglDestroySurface(GetDisplay(), surface_)) {
540 LOG(ERROR) << "eglDestroySurface failed with error "
541 << GetLastEGLErrorString();
547 EGLConfig PbufferGLSurfaceEGL::GetConfig() {
551 bool PbufferGLSurfaceEGL::IsOffscreen() {
555 bool PbufferGLSurfaceEGL::SwapBuffers() {
556 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL.";
560 gfx::Size PbufferGLSurfaceEGL::GetSize() {
564 bool PbufferGLSurfaceEGL::Resize(const gfx::Size& size) {
568 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
569 GLContext* current_context = GLContext::GetCurrent();
571 current_context && current_context->IsCurrent(this);
573 scoped_make_current.reset(
574 new ui::ScopedMakeCurrent(current_context, this));
580 LOG(ERROR) << "Failed to resize pbuffer.";
587 EGLSurface PbufferGLSurfaceEGL::GetHandle() {
591 void* PbufferGLSurfaceEGL::GetShareHandle() {
592 #if defined(OS_ANDROID)
596 if (!gfx::g_driver_egl.ext.b_EGL_ANGLE_query_surface_pointer)
599 if (!gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle)
603 if (!eglQuerySurfacePointerANGLE(g_display,
605 EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
614 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() {
618 SurfacelessEGL::SurfacelessEGL(const gfx::Size& size)
622 bool SurfacelessEGL::Initialize() {
626 void SurfacelessEGL::Destroy() {
629 EGLConfig SurfacelessEGL::GetConfig() {
633 bool SurfacelessEGL::IsOffscreen() {
637 bool SurfacelessEGL::SwapBuffers() {
638 LOG(ERROR) << "Attempted to call SwapBuffers with SurfacelessEGL.";
642 gfx::Size SurfacelessEGL::GetSize() {
646 bool SurfacelessEGL::Resize(const gfx::Size& size) {
651 EGLSurface SurfacelessEGL::GetHandle() {
652 return EGL_NO_SURFACE;
655 void* SurfacelessEGL::GetShareHandle() {
659 SurfacelessEGL::~SurfacelessEGL() {