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 #include "ui/gl/gl_surface_egl.h"
7 #if defined(OS_ANDROID)
8 #include <android/native_window_jni.h>
11 #include "base/command_line.h"
12 #include "base/debug/trace_event.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "build/build_config.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gl/egl_util.h"
19 #include "ui/gl/gl_context.h"
20 #include "ui/gl/gl_implementation.h"
21 #include "ui/gl/gl_surface_stub.h"
22 #include "ui/gl/gl_switches.h"
23 #include "ui/gl/scoped_make_current.h"
24 #include "ui/gl/sync_control_vsync_provider.h"
32 #if defined (USE_OZONE)
33 #include "ui/ozone/public/surface_factory_ozone.h"
36 #if !defined(EGL_FIXED_SIZE_ANGLE)
37 #define EGL_FIXED_SIZE_ANGLE 0x3201
41 // From ANGLE's egl/eglext.h.
42 #if !defined(EGL_PLATFORM_ANGLE_ANGLE)
43 #define EGL_PLATFORM_ANGLE_ANGLE 0x3201
45 #if !defined(EGL_PLATFORM_ANGLE_TYPE_ANGLE)
46 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3202
48 #if !defined(EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE)
49 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE 0x3206
51 #endif // defined(OS_WIN)
53 using ui::GetLastEGLErrorString
;
61 EGLNativeDisplayType g_native_display
;
63 const char* g_egl_extensions
= NULL
;
64 bool g_egl_create_context_robustness_supported
= false;
65 bool g_egl_sync_control_supported
= false;
66 bool g_egl_window_fixed_size_supported
= false;
67 bool g_egl_surfaceless_context_supported
= false;
69 class EGLSyncControlVSyncProvider
70 : public gfx::SyncControlVSyncProvider
{
72 explicit EGLSyncControlVSyncProvider(EGLSurface surface
)
73 : SyncControlVSyncProvider(),
77 virtual ~EGLSyncControlVSyncProvider() { }
80 virtual bool GetSyncValues(int64
* system_time
,
81 int64
* media_stream_counter
,
82 int64
* swap_buffer_counter
) override
{
83 uint64 u_system_time
, u_media_stream_counter
, u_swap_buffer_counter
;
84 bool result
= eglGetSyncValuesCHROMIUM(
85 g_display
, surface_
, &u_system_time
,
86 &u_media_stream_counter
, &u_swap_buffer_counter
) == EGL_TRUE
;
88 *system_time
= static_cast<int64
>(u_system_time
);
89 *media_stream_counter
= static_cast<int64
>(u_media_stream_counter
);
90 *swap_buffer_counter
= static_cast<int64
>(u_swap_buffer_counter
);
95 virtual bool GetMscRate(int32
* numerator
, int32
* denominator
) override
{
102 DISALLOW_COPY_AND_ASSIGN(EGLSyncControlVSyncProvider
);
107 GLSurfaceEGL::GLSurfaceEGL() {}
109 bool GLSurfaceEGL::InitializeOneOff() {
110 static bool initialized
= false;
114 g_native_display
= GetPlatformDefaultEGLNativeDisplay();
117 g_display
= GetPlatformDisplay(g_native_display
);
119 g_display
= eglGetDisplay(g_native_display
);
123 LOG(ERROR
) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
127 if (!eglInitialize(g_display
, NULL
, NULL
)) {
128 LOG(ERROR
) << "eglInitialize failed with error " << GetLastEGLErrorString();
132 // Choose an EGL configuration.
133 // On X this is only used for PBuffer surfaces.
134 static const EGLint kConfigAttribs
[] = {
140 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
141 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
,
145 #if defined(USE_OZONE)
146 const EGLint
* config_attribs
=
147 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties(
150 const EGLint
* config_attribs
= kConfigAttribs
;
154 if (!eglChooseConfig(g_display
,
159 LOG(ERROR
) << "eglChooseConfig failed with error "
160 << GetLastEGLErrorString();
164 if (num_configs
== 0) {
165 LOG(ERROR
) << "No suitable EGL configs found.";
169 if (!eglChooseConfig(g_display
,
174 LOG(ERROR
) << "eglChooseConfig failed with error "
175 << GetLastEGLErrorString();
179 g_egl_extensions
= eglQueryString(g_display
, EGL_EXTENSIONS
);
180 g_egl_create_context_robustness_supported
=
181 HasEGLExtension("EGL_EXT_create_context_robustness");
182 g_egl_sync_control_supported
=
183 HasEGLExtension("EGL_CHROMIUM_sync_control");
184 g_egl_window_fixed_size_supported
=
185 HasEGLExtension("EGL_ANGLE_window_fixed_size");
187 // TODO(oetuaho@nvidia.com): Surfaceless is disabled on Android as a temporary
188 // workaround, since code written for Android WebView takes different paths
189 // based on whether GL surface objects have underlying EGL surface handles,
190 // conflicting with the use of surfaceless. See https://crbug.com/382349
191 #if defined(OS_ANDROID)
192 DCHECK(!g_egl_surfaceless_context_supported
);
194 // Check if SurfacelessEGL is supported.
195 g_egl_surfaceless_context_supported
=
196 HasEGLExtension("EGL_KHR_surfaceless_context");
197 if (g_egl_surfaceless_context_supported
) {
198 // EGL_KHR_surfaceless_context is supported but ensure
199 // GL_OES_surfaceless_context is also supported. We need a current context
200 // to query for supported GL extensions.
201 scoped_refptr
<GLSurface
> surface
= new SurfacelessEGL(Size(1, 1));
202 scoped_refptr
<GLContext
> context
= GLContext::CreateGLContext(
203 NULL
, surface
.get(), PreferIntegratedGpu
);
204 if (!context
->MakeCurrent(surface
.get()))
205 g_egl_surfaceless_context_supported
= false;
207 // Ensure context supports GL_OES_surfaceless_context.
208 if (g_egl_surfaceless_context_supported
) {
209 g_egl_surfaceless_context_supported
= context
->HasExtension(
210 "GL_OES_surfaceless_context");
211 context
->ReleaseCurrent(surface
.get());
221 EGLDisplay
GLSurfaceEGL::GetDisplay() {
225 EGLDisplay
GLSurfaceEGL::GetHardwareDisplay() {
229 EGLNativeDisplayType
GLSurfaceEGL::GetNativeDisplay() {
230 return g_native_display
;
233 const char* GLSurfaceEGL::GetEGLExtensions() {
234 return g_egl_extensions
;
237 bool GLSurfaceEGL::HasEGLExtension(const char* name
) {
238 return ExtensionsContain(GetEGLExtensions(), name
);
241 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() {
242 return g_egl_create_context_robustness_supported
;
245 bool GLSurfaceEGL::IsEGLSurfacelessContextSupported() {
246 return g_egl_surfaceless_context_supported
;
249 GLSurfaceEGL::~GLSurfaceEGL() {}
252 static const EGLint kDisplayAttribsWarp
[] {
253 EGL_PLATFORM_ANGLE_TYPE_ANGLE
,
254 EGL_PLATFORM_ANGLE_TYPE_D3D11_WARP_ANGLE
,
259 EGLDisplay
GLSurfaceEGL::GetPlatformDisplay(
260 EGLNativeDisplayType native_display
) {
261 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp
)) {
262 // Check for availability of WARP via ANGLE extension.
263 bool supports_warp
= false;
264 const char* no_display_extensions
= eglQueryString(EGL_NO_DISPLAY
,
266 // If EGL_EXT_client_extensions not supported this call to eglQueryString
268 if (no_display_extensions
)
270 ExtensionsContain(no_display_extensions
, "ANGLE_platform_angle") &&
271 ExtensionsContain(no_display_extensions
, "ANGLE_platform_angle_d3d");
276 return eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE
, native_display
,
277 kDisplayAttribsWarp
);
280 return eglGetDisplay(native_display
);
284 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window
)
287 supports_post_sub_buffer_(false),
290 #if defined(OS_ANDROID)
292 ANativeWindow_acquire(window
);
297 if (GetClientRect(window_
, &windowRect
))
298 size_
= gfx::Rect(windowRect
).size();
302 bool NativeViewGLSurfaceEGL::Initialize() {
303 return Initialize(scoped_ptr
<VSyncProvider
>());
306 bool NativeViewGLSurfaceEGL::Initialize(
307 scoped_ptr
<VSyncProvider
> sync_provider
) {
311 LOG(ERROR
) << "Trying to create surface with invalid display.";
315 std::vector
<EGLint
> egl_window_attributes
;
317 if (g_egl_window_fixed_size_supported
) {
318 egl_window_attributes
.push_back(EGL_FIXED_SIZE_ANGLE
);
319 egl_window_attributes
.push_back(EGL_TRUE
);
320 egl_window_attributes
.push_back(EGL_WIDTH
);
321 egl_window_attributes
.push_back(size_
.width());
322 egl_window_attributes
.push_back(EGL_HEIGHT
);
323 egl_window_attributes
.push_back(size_
.height());
326 if (gfx::g_driver_egl
.ext
.b_EGL_NV_post_sub_buffer
) {
327 egl_window_attributes
.push_back(EGL_POST_SUB_BUFFER_SUPPORTED_NV
);
328 egl_window_attributes
.push_back(EGL_TRUE
);
331 egl_window_attributes
.push_back(EGL_NONE
);
332 // Create a surface for the native window.
333 surface_
= eglCreateWindowSurface(
334 GetDisplay(), GetConfig(), window_
, &egl_window_attributes
[0]);
337 LOG(ERROR
) << "eglCreateWindowSurface failed with error "
338 << GetLastEGLErrorString();
344 EGLBoolean retVal
= eglQuerySurface(GetDisplay(),
346 EGL_POST_SUB_BUFFER_SUPPORTED_NV
,
348 supports_post_sub_buffer_
= (surfaceVal
&& retVal
) == EGL_TRUE
;
351 vsync_provider_
.reset(sync_provider
.release());
352 else if (g_egl_sync_control_supported
)
353 vsync_provider_
.reset(new EGLSyncControlVSyncProvider(surface_
));
357 void NativeViewGLSurfaceEGL::Destroy() {
359 if (!eglDestroySurface(GetDisplay(), surface_
)) {
360 LOG(ERROR
) << "eglDestroySurface failed with error "
361 << GetLastEGLErrorString();
367 EGLConfig
NativeViewGLSurfaceEGL::GetConfig() {
368 #if !defined(USE_X11)
372 // Get a config compatible with the window
374 XWindowAttributes win_attribs
;
375 if (!XGetWindowAttributes(GetNativeDisplay(), window_
, &win_attribs
)) {
379 // Try matching the window depth with an alpha channel,
380 // because we're worried the destination alpha width could
381 // constrain blending precision.
382 const int kBufferSizeOffset
= 1;
383 const int kAlphaSizeOffset
= 3;
384 EGLint config_attribs
[] = {
390 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
391 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
,
394 config_attribs
[kBufferSizeOffset
] = win_attribs
.depth
;
397 if (!eglChooseConfig(g_display
,
402 LOG(ERROR
) << "eglChooseConfig failed with error "
403 << GetLastEGLErrorString();
409 if (!eglGetConfigAttrib(g_display
,
413 LOG(ERROR
) << "eglGetConfigAttrib failed with error "
414 << GetLastEGLErrorString();
418 if (config_depth
== win_attribs
.depth
) {
423 // Try without an alpha channel.
424 config_attribs
[kAlphaSizeOffset
] = 0;
425 if (!eglChooseConfig(g_display
,
430 LOG(ERROR
) << "eglChooseConfig failed with error "
431 << GetLastEGLErrorString();
435 if (num_configs
== 0) {
436 LOG(ERROR
) << "No suitable EGL configs found.";
444 bool NativeViewGLSurfaceEGL::IsOffscreen() {
448 bool NativeViewGLSurfaceEGL::SwapBuffers() {
449 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers",
450 "width", GetSize().width(),
451 "height", GetSize().height());
453 if (!eglSwapBuffers(GetDisplay(), surface_
)) {
454 DVLOG(1) << "eglSwapBuffers failed with error "
455 << GetLastEGLErrorString();
462 gfx::Size
NativeViewGLSurfaceEGL::GetSize() {
465 if (!eglQuerySurface(GetDisplay(), surface_
, EGL_WIDTH
, &width
) ||
466 !eglQuerySurface(GetDisplay(), surface_
, EGL_HEIGHT
, &height
)) {
467 NOTREACHED() << "eglQuerySurface failed with error "
468 << GetLastEGLErrorString();
472 return gfx::Size(width
, height
);
475 bool NativeViewGLSurfaceEGL::Resize(const gfx::Size
& size
) {
476 if (size
== GetSize())
481 scoped_ptr
<ui::ScopedMakeCurrent
> scoped_make_current
;
482 GLContext
* current_context
= GLContext::GetCurrent();
484 current_context
&& current_context
->IsCurrent(this);
486 scoped_make_current
.reset(
487 new ui::ScopedMakeCurrent(current_context
, this));
488 current_context
->ReleaseCurrent(this);
494 LOG(ERROR
) << "Failed to resize window.";
501 bool NativeViewGLSurfaceEGL::Recreate() {
504 LOG(ERROR
) << "Failed to create surface.";
510 EGLSurface
NativeViewGLSurfaceEGL::GetHandle() {
514 bool NativeViewGLSurfaceEGL::SupportsPostSubBuffer() {
515 return supports_post_sub_buffer_
;
518 bool NativeViewGLSurfaceEGL::PostSubBuffer(
519 int x
, int y
, int width
, int height
) {
520 DCHECK(supports_post_sub_buffer_
);
521 if (!eglPostSubBufferNV(GetDisplay(), surface_
, x
, y
, width
, height
)) {
522 DVLOG(1) << "eglPostSubBufferNV failed with error "
523 << GetLastEGLErrorString();
529 VSyncProvider
* NativeViewGLSurfaceEGL::GetVSyncProvider() {
530 return vsync_provider_
.get();
533 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() {
535 #if defined(OS_ANDROID)
537 ANativeWindow_release(window_
);
541 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(const gfx::Size
& size
)
544 // Some implementations of Pbuffer do not support having a 0 size. For such
545 // cases use a (1, 1) surface.
546 if (size_
.GetArea() == 0)
550 bool PbufferGLSurfaceEGL::Initialize() {
551 EGLSurface old_surface
= surface_
;
553 EGLDisplay display
= GetDisplay();
555 LOG(ERROR
) << "Trying to create surface with invalid display.";
559 // Allocate the new pbuffer surface before freeing the old one to ensure
560 // they have different addresses. If they have the same address then a
561 // future call to MakeCurrent might early out because it appears the current
562 // context and surface have not changed.
563 const EGLint pbuffer_attribs
[] = {
564 EGL_WIDTH
, size_
.width(),
565 EGL_HEIGHT
, size_
.height(),
569 EGLSurface new_surface
= eglCreatePbufferSurface(display
,
573 LOG(ERROR
) << "eglCreatePbufferSurface failed with error "
574 << GetLastEGLErrorString();
579 eglDestroySurface(display
, old_surface
);
581 surface_
= new_surface
;
585 void PbufferGLSurfaceEGL::Destroy() {
587 if (!eglDestroySurface(GetDisplay(), surface_
)) {
588 LOG(ERROR
) << "eglDestroySurface failed with error "
589 << GetLastEGLErrorString();
595 EGLConfig
PbufferGLSurfaceEGL::GetConfig() {
599 bool PbufferGLSurfaceEGL::IsOffscreen() {
603 bool PbufferGLSurfaceEGL::SwapBuffers() {
604 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL.";
608 gfx::Size
PbufferGLSurfaceEGL::GetSize() {
612 bool PbufferGLSurfaceEGL::Resize(const gfx::Size
& size
) {
616 scoped_ptr
<ui::ScopedMakeCurrent
> scoped_make_current
;
617 GLContext
* current_context
= GLContext::GetCurrent();
619 current_context
&& current_context
->IsCurrent(this);
621 scoped_make_current
.reset(
622 new ui::ScopedMakeCurrent(current_context
, this));
628 LOG(ERROR
) << "Failed to resize pbuffer.";
635 EGLSurface
PbufferGLSurfaceEGL::GetHandle() {
639 void* PbufferGLSurfaceEGL::GetShareHandle() {
640 #if defined(OS_ANDROID)
644 if (!gfx::g_driver_egl
.ext
.b_EGL_ANGLE_query_surface_pointer
)
647 if (!gfx::g_driver_egl
.ext
.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle
)
651 if (!eglQuerySurfacePointerANGLE(g_display
,
653 EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE
,
662 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() {
666 SurfacelessEGL::SurfacelessEGL(const gfx::Size
& size
)
670 bool SurfacelessEGL::Initialize() {
674 void SurfacelessEGL::Destroy() {
677 EGLConfig
SurfacelessEGL::GetConfig() {
681 bool SurfacelessEGL::IsOffscreen() {
685 bool SurfacelessEGL::SwapBuffers() {
686 LOG(ERROR
) << "Attempted to call SwapBuffers with SurfacelessEGL.";
690 gfx::Size
SurfacelessEGL::GetSize() {
694 bool SurfacelessEGL::Resize(const gfx::Size
& size
) {
699 EGLSurface
SurfacelessEGL::GetHandle() {
700 return EGL_NO_SURFACE
;
703 void* SurfacelessEGL::GetShareHandle() {
707 SurfacelessEGL::~SurfacelessEGL() {