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/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/trace_event/trace_event.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
40 #if !defined(EGL_OPENGL_ES3_BIT)
41 #define EGL_OPENGL_ES3_BIT 0x00000040
45 // From ANGLE's egl/eglext.h.
47 #ifndef EGL_ANGLE_platform_angle
48 #define EGL_ANGLE_platform_angle 1
49 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
50 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
51 #define EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE 0x3204
52 #define EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE 0x3205
53 #define EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE 0x3206
54 #endif /* EGL_ANGLE_platform_angle */
56 #ifndef EGL_ANGLE_platform_angle_d3d
57 #define EGL_ANGLE_platform_angle_d3d 1
58 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207
59 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
60 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE 0x3209
61 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE 0x320A
62 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE 0x320B
63 #define EGL_PLATFORM_ANGLE_DEVICE_TYPE_REFERENCE_ANGLE 0x320C
64 #endif /* EGL_ANGLE_platform_angle_d3d */
66 #endif // defined(OS_WIN)
68 using ui::GetLastEGLErrorString
;
73 unsigned int NativeViewGLSurfaceEGL::current_swap_generation_
= 0;
74 unsigned int NativeViewGLSurfaceEGL::swaps_this_generation_
= 0;
75 unsigned int NativeViewGLSurfaceEGL::last_multiswap_generation_
= 0;
77 const unsigned int MULTISWAP_FRAME_VSYNC_THRESHOLD
= 60;
84 EGLNativeDisplayType g_native_display_type
;
86 // In the Cast environment, we need to destroy the EGLNativeDisplayType and
87 // EGLDisplay returned by the GPU platform when we switch to an external app
88 // which will temporarily own all screen and GPU resources.
89 // Even though Chromium is still in the background.
90 // As such, it must be reinitialized each time we come back to the foreground.
91 bool g_initialized
= false;
92 int g_num_surfaces
= 0;
93 bool g_terminate_pending
= false;
95 const char* g_egl_extensions
= NULL
;
96 bool g_egl_create_context_robustness_supported
= false;
97 bool g_egl_sync_control_supported
= false;
98 bool g_egl_window_fixed_size_supported
= false;
99 bool g_egl_surfaceless_context_supported
= false;
101 class EGLSyncControlVSyncProvider
102 : public gfx::SyncControlVSyncProvider
{
104 explicit EGLSyncControlVSyncProvider(EGLSurface surface
)
105 : SyncControlVSyncProvider(),
109 ~EGLSyncControlVSyncProvider() override
{}
112 bool GetSyncValues(int64
* system_time
,
113 int64
* media_stream_counter
,
114 int64
* swap_buffer_counter
) override
{
115 uint64 u_system_time
, u_media_stream_counter
, u_swap_buffer_counter
;
116 bool result
= eglGetSyncValuesCHROMIUM(
117 g_display
, surface_
, &u_system_time
,
118 &u_media_stream_counter
, &u_swap_buffer_counter
) == EGL_TRUE
;
120 *system_time
= static_cast<int64
>(u_system_time
);
121 *media_stream_counter
= static_cast<int64
>(u_media_stream_counter
);
122 *swap_buffer_counter
= static_cast<int64
>(u_swap_buffer_counter
);
127 bool GetMscRate(int32
* numerator
, int32
* denominator
) override
{
134 DISALLOW_COPY_AND_ASSIGN(EGLSyncControlVSyncProvider
);
137 void DeinitializeEgl() {
139 g_initialized
= false;
140 eglTerminate(g_display
);
146 GLSurfaceEGL::GLSurfaceEGL() {
148 if (!g_initialized
) {
149 bool result
= GLSurfaceEGL::InitializeOneOff();
151 DCHECK(g_initialized
);
155 bool GLSurfaceEGL::InitializeOneOff() {
159 g_native_display_type
= GetPlatformDefaultEGLNativeDisplay();
162 g_display
= GetPlatformDisplay(g_native_display_type
);
164 g_display
= eglGetDisplay(g_native_display_type
);
168 LOG(ERROR
) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
172 if (!eglInitialize(g_display
, NULL
, NULL
)) {
173 LOG(ERROR
) << "eglInitialize failed with error " << GetLastEGLErrorString();
177 // Choose an EGL configuration.
178 // On X this is only used for PBuffer surfaces.
179 EGLint renderable_type
= EGL_OPENGL_ES2_BIT
;
180 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
181 switches::kEnableUnsafeES3APIs
)) {
182 renderable_type
= EGL_OPENGL_ES3_BIT
;
184 const EGLint kConfigAttribs
[] = {
190 EGL_RENDERABLE_TYPE
, renderable_type
,
191 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
,
195 #if defined(USE_OZONE)
196 const EGLint
* config_attribs
=
197 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties(
200 const EGLint
* config_attribs
= kConfigAttribs
;
204 if (!eglChooseConfig(g_display
,
209 LOG(ERROR
) << "eglChooseConfig failed with error "
210 << GetLastEGLErrorString();
214 if (num_configs
== 0) {
215 LOG(ERROR
) << "No suitable EGL configs found.";
219 if (!eglChooseConfig(g_display
,
224 LOG(ERROR
) << "eglChooseConfig failed with error "
225 << GetLastEGLErrorString();
229 g_egl_extensions
= eglQueryString(g_display
, EGL_EXTENSIONS
);
230 g_egl_create_context_robustness_supported
=
231 HasEGLExtension("EGL_EXT_create_context_robustness");
232 g_egl_sync_control_supported
=
233 HasEGLExtension("EGL_CHROMIUM_sync_control");
234 g_egl_window_fixed_size_supported
=
235 HasEGLExtension("EGL_ANGLE_window_fixed_size");
237 // We always succeed beyond this point so set g_initialized here to avoid
238 // infinite recursion through CreateGLContext and GetDisplay
239 // if g_egl_surfaceless_context_supported.
240 g_initialized
= true;
241 g_terminate_pending
= false;
243 // TODO(oetuaho@nvidia.com): Surfaceless is disabled on Android as a temporary
244 // workaround, since code written for Android WebView takes different paths
245 // based on whether GL surface objects have underlying EGL surface handles,
246 // conflicting with the use of surfaceless. See https://crbug.com/382349
247 #if defined(OS_ANDROID)
248 DCHECK(!g_egl_surfaceless_context_supported
);
250 // Check if SurfacelessEGL is supported.
251 g_egl_surfaceless_context_supported
=
252 HasEGLExtension("EGL_KHR_surfaceless_context");
253 if (g_egl_surfaceless_context_supported
) {
254 // EGL_KHR_surfaceless_context is supported but ensure
255 // GL_OES_surfaceless_context is also supported. We need a current context
256 // to query for supported GL extensions.
257 scoped_refptr
<GLSurface
> surface
= new SurfacelessEGL(Size(1, 1));
258 scoped_refptr
<GLContext
> context
= GLContext::CreateGLContext(
259 NULL
, surface
.get(), PreferIntegratedGpu
);
260 if (!context
->MakeCurrent(surface
.get()))
261 g_egl_surfaceless_context_supported
= false;
263 // Ensure context supports GL_OES_surfaceless_context.
264 if (g_egl_surfaceless_context_supported
) {
265 g_egl_surfaceless_context_supported
= context
->HasExtension(
266 "GL_OES_surfaceless_context");
267 context
->ReleaseCurrent(surface
.get());
275 EGLDisplay
GLSurfaceEGL::GetDisplay() {
276 DCHECK(g_initialized
);
281 EGLDisplay
GLSurfaceEGL::GetHardwareDisplay() {
282 if (!g_initialized
) {
283 bool result
= GLSurfaceEGL::InitializeOneOff();
290 EGLNativeDisplayType
GLSurfaceEGL::GetNativeDisplay() {
291 if (!g_initialized
) {
292 bool result
= GLSurfaceEGL::InitializeOneOff();
295 return g_native_display_type
;
298 const char* GLSurfaceEGL::GetEGLExtensions() {
299 // No need for InitializeOneOff. Assume that extensions will not change
300 // after the first initialization.
301 return g_egl_extensions
;
304 bool GLSurfaceEGL::HasEGLExtension(const char* name
) {
305 return ExtensionsContain(GetEGLExtensions(), name
);
308 bool GLSurfaceEGL::IsCreateContextRobustnessSupported() {
309 return g_egl_create_context_robustness_supported
;
312 bool GLSurfaceEGL::IsEGLSurfacelessContextSupported() {
313 return g_egl_surfaceless_context_supported
;
316 void GLSurfaceEGL::DestroyAndTerminateDisplay() {
317 DCHECK(g_initialized
);
318 DCHECK_EQ(g_num_surfaces
, 1);
320 g_terminate_pending
= true;
323 GLSurfaceEGL::~GLSurfaceEGL() {
324 DCHECK_GT(g_num_surfaces
, 0) << "Bad surface count";
325 if (--g_num_surfaces
== 0 && g_terminate_pending
) {
327 g_terminate_pending
= false;
332 static const EGLint kDisplayAttribsWarp
[] {
333 EGL_PLATFORM_ANGLE_TYPE_ANGLE
,
334 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE
,
336 EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE
,
337 EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE
,
343 EGLDisplay
GLSurfaceEGL::GetPlatformDisplay(
344 EGLNativeDisplayType native_display
) {
345 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseWarp
)) {
346 // Check for availability of WARP via ANGLE extension.
347 bool supports_warp
= false;
348 const char* no_display_extensions
= eglQueryString(EGL_NO_DISPLAY
,
350 // If EGL_EXT_client_extensions not supported this call to eglQueryString
352 if (no_display_extensions
)
354 ExtensionsContain(no_display_extensions
, "ANGLE_platform_angle") &&
355 ExtensionsContain(no_display_extensions
, "ANGLE_platform_angle_d3d");
360 return eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE
, native_display
,
361 kDisplayAttribsWarp
);
364 return eglGetDisplay(native_display
);
368 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window
)
371 supports_post_sub_buffer_(false),
375 #if defined(OS_ANDROID)
377 ANativeWindow_acquire(window
);
381 vsync_override_
= false;
382 swap_generation_
= 0;
384 if (GetClientRect(window_
, &windowRect
))
385 size_
= gfx::Rect(windowRect
).size();
389 bool NativeViewGLSurfaceEGL::Initialize() {
390 return Initialize(nullptr);
393 bool NativeViewGLSurfaceEGL::Initialize(
394 scoped_ptr
<VSyncProvider
> sync_provider
) {
398 LOG(ERROR
) << "Trying to create surface with invalid display.";
402 std::vector
<EGLint
> egl_window_attributes
;
404 if (g_egl_window_fixed_size_supported
) {
405 egl_window_attributes
.push_back(EGL_FIXED_SIZE_ANGLE
);
406 egl_window_attributes
.push_back(EGL_TRUE
);
407 egl_window_attributes
.push_back(EGL_WIDTH
);
408 egl_window_attributes
.push_back(size_
.width());
409 egl_window_attributes
.push_back(EGL_HEIGHT
);
410 egl_window_attributes
.push_back(size_
.height());
413 if (gfx::g_driver_egl
.ext
.b_EGL_NV_post_sub_buffer
) {
414 egl_window_attributes
.push_back(EGL_POST_SUB_BUFFER_SUPPORTED_NV
);
415 egl_window_attributes
.push_back(EGL_TRUE
);
418 egl_window_attributes
.push_back(EGL_NONE
);
419 // Create a surface for the native window.
420 surface_
= eglCreateWindowSurface(
421 GetDisplay(), GetConfig(), window_
, &egl_window_attributes
[0]);
424 LOG(ERROR
) << "eglCreateWindowSurface failed with error "
425 << GetLastEGLErrorString();
430 if (gfx::g_driver_egl
.ext
.b_EGL_NV_post_sub_buffer
) {
432 EGLBoolean retVal
= eglQuerySurface(
433 GetDisplay(), surface_
, EGL_POST_SUB_BUFFER_SUPPORTED_NV
, &surfaceVal
);
434 supports_post_sub_buffer_
= (surfaceVal
&& retVal
) == EGL_TRUE
;
438 vsync_provider_
.reset(sync_provider
.release());
439 else if (g_egl_sync_control_supported
)
440 vsync_provider_
.reset(new EGLSyncControlVSyncProvider(surface_
));
444 void NativeViewGLSurfaceEGL::Destroy() {
446 if (!eglDestroySurface(GetDisplay(), surface_
)) {
447 LOG(ERROR
) << "eglDestroySurface failed with error "
448 << GetLastEGLErrorString();
454 EGLConfig
NativeViewGLSurfaceEGL::GetConfig() {
455 #if !defined(USE_X11)
459 // Get a config compatible with the window
461 XWindowAttributes win_attribs
;
462 if (!XGetWindowAttributes(GetNativeDisplay(), window_
, &win_attribs
)) {
466 // Try matching the window depth with an alpha channel,
467 // because we're worried the destination alpha width could
468 // constrain blending precision.
469 const int kBufferSizeOffset
= 1;
470 const int kAlphaSizeOffset
= 3;
471 EGLint config_attribs
[] = {
477 EGL_RENDERABLE_TYPE
, EGL_OPENGL_ES2_BIT
,
478 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
| EGL_PBUFFER_BIT
,
481 config_attribs
[kBufferSizeOffset
] = win_attribs
.depth
;
484 if (!eglChooseConfig(g_display
,
489 LOG(ERROR
) << "eglChooseConfig failed with error "
490 << GetLastEGLErrorString();
496 if (!eglGetConfigAttrib(g_display
,
500 LOG(ERROR
) << "eglGetConfigAttrib failed with error "
501 << GetLastEGLErrorString();
505 if (config_depth
== win_attribs
.depth
) {
510 // Try without an alpha channel.
511 config_attribs
[kAlphaSizeOffset
] = 0;
512 if (!eglChooseConfig(g_display
,
517 LOG(ERROR
) << "eglChooseConfig failed with error "
518 << GetLastEGLErrorString();
522 if (num_configs
== 0) {
523 LOG(ERROR
) << "No suitable EGL configs found.";
531 bool NativeViewGLSurfaceEGL::IsOffscreen() {
535 bool NativeViewGLSurfaceEGL::SwapBuffers() {
536 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers",
537 "width", GetSize().width(),
538 "height", GetSize().height());
541 if (swap_interval_
!= 0) {
542 // This code is a simple way of enforcing that we only vsync if one surface
543 // is swapping per frame. This provides single window cases a stable refresh
544 // while allowing multi-window cases to not slow down due to multiple syncs
545 // on a single thread. A better way to fix this problem would be to have
546 // each surface present on its own thread.
548 if (current_swap_generation_
== swap_generation_
) {
549 if (swaps_this_generation_
> 1)
550 last_multiswap_generation_
= current_swap_generation_
;
551 swaps_this_generation_
= 0;
552 current_swap_generation_
++;
555 swap_generation_
= current_swap_generation_
;
557 if (swaps_this_generation_
!= 0 ||
558 (current_swap_generation_
- last_multiswap_generation_
<
559 MULTISWAP_FRAME_VSYNC_THRESHOLD
)) {
560 // Override vsync settings and switch it off
561 if (!vsync_override_
) {
562 eglSwapInterval(GetDisplay(), 0);
563 vsync_override_
= true;
565 } else if (vsync_override_
) {
566 // Only one window swapping, so let the normal vsync setting take over
567 eglSwapInterval(GetDisplay(), swap_interval_
);
568 vsync_override_
= false;
571 swaps_this_generation_
++;
575 if (!eglSwapBuffers(GetDisplay(), surface_
)) {
576 DVLOG(1) << "eglSwapBuffers failed with error "
577 << GetLastEGLErrorString();
584 gfx::Size
NativeViewGLSurfaceEGL::GetSize() {
587 if (!eglQuerySurface(GetDisplay(), surface_
, EGL_WIDTH
, &width
) ||
588 !eglQuerySurface(GetDisplay(), surface_
, EGL_HEIGHT
, &height
)) {
589 NOTREACHED() << "eglQuerySurface failed with error "
590 << GetLastEGLErrorString();
594 return gfx::Size(width
, height
);
597 bool NativeViewGLSurfaceEGL::Resize(const gfx::Size
& size
) {
598 if (size
== GetSize())
603 scoped_ptr
<ui::ScopedMakeCurrent
> scoped_make_current
;
604 GLContext
* current_context
= GLContext::GetCurrent();
606 current_context
&& current_context
->IsCurrent(this);
608 scoped_make_current
.reset(
609 new ui::ScopedMakeCurrent(current_context
, this));
610 current_context
->ReleaseCurrent(this);
616 LOG(ERROR
) << "Failed to resize window.";
623 bool NativeViewGLSurfaceEGL::Recreate() {
626 LOG(ERROR
) << "Failed to create surface.";
632 EGLSurface
NativeViewGLSurfaceEGL::GetHandle() {
636 bool NativeViewGLSurfaceEGL::SupportsPostSubBuffer() {
637 return supports_post_sub_buffer_
;
640 bool NativeViewGLSurfaceEGL::PostSubBuffer(
641 int x
, int y
, int width
, int height
) {
642 DCHECK(supports_post_sub_buffer_
);
643 if (!eglPostSubBufferNV(GetDisplay(), surface_
, x
, y
, width
, height
)) {
644 DVLOG(1) << "eglPostSubBufferNV failed with error "
645 << GetLastEGLErrorString();
651 VSyncProvider
* NativeViewGLSurfaceEGL::GetVSyncProvider() {
652 return vsync_provider_
.get();
655 void NativeViewGLSurfaceEGL::OnSetSwapInterval(int interval
) {
656 swap_interval_
= interval
;
659 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() {
661 #if defined(OS_ANDROID)
663 ANativeWindow_release(window_
);
667 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(const gfx::Size
& size
)
670 // Some implementations of Pbuffer do not support having a 0 size. For such
671 // cases use a (1, 1) surface.
672 if (size_
.GetArea() == 0)
676 bool PbufferGLSurfaceEGL::Initialize() {
677 EGLSurface old_surface
= surface_
;
679 EGLDisplay display
= GetDisplay();
681 LOG(ERROR
) << "Trying to create surface with invalid display.";
685 // Allocate the new pbuffer surface before freeing the old one to ensure
686 // they have different addresses. If they have the same address then a
687 // future call to MakeCurrent might early out because it appears the current
688 // context and surface have not changed.
689 const EGLint pbuffer_attribs
[] = {
690 EGL_WIDTH
, size_
.width(),
691 EGL_HEIGHT
, size_
.height(),
695 EGLSurface new_surface
= eglCreatePbufferSurface(display
,
699 LOG(ERROR
) << "eglCreatePbufferSurface failed with error "
700 << GetLastEGLErrorString();
705 eglDestroySurface(display
, old_surface
);
707 surface_
= new_surface
;
711 void PbufferGLSurfaceEGL::Destroy() {
713 if (!eglDestroySurface(GetDisplay(), surface_
)) {
714 LOG(ERROR
) << "eglDestroySurface failed with error "
715 << GetLastEGLErrorString();
721 EGLConfig
PbufferGLSurfaceEGL::GetConfig() {
725 bool PbufferGLSurfaceEGL::IsOffscreen() {
729 bool PbufferGLSurfaceEGL::SwapBuffers() {
730 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL.";
734 gfx::Size
PbufferGLSurfaceEGL::GetSize() {
738 bool PbufferGLSurfaceEGL::Resize(const gfx::Size
& size
) {
742 scoped_ptr
<ui::ScopedMakeCurrent
> scoped_make_current
;
743 GLContext
* current_context
= GLContext::GetCurrent();
745 current_context
&& current_context
->IsCurrent(this);
747 scoped_make_current
.reset(
748 new ui::ScopedMakeCurrent(current_context
, this));
754 LOG(ERROR
) << "Failed to resize pbuffer.";
761 EGLSurface
PbufferGLSurfaceEGL::GetHandle() {
765 void* PbufferGLSurfaceEGL::GetShareHandle() {
766 #if defined(OS_ANDROID)
770 if (!gfx::g_driver_egl
.ext
.b_EGL_ANGLE_query_surface_pointer
)
773 if (!gfx::g_driver_egl
.ext
.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle
)
777 if (!eglQuerySurfacePointerANGLE(g_display
,
779 EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE
,
788 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() {
792 SurfacelessEGL::SurfacelessEGL(const gfx::Size
& size
)
796 bool SurfacelessEGL::Initialize() {
800 void SurfacelessEGL::Destroy() {
803 EGLConfig
SurfacelessEGL::GetConfig() {
807 bool SurfacelessEGL::IsOffscreen() {
811 bool SurfacelessEGL::IsSurfaceless() const {
815 bool SurfacelessEGL::SwapBuffers() {
816 LOG(ERROR
) << "Attempted to call SwapBuffers with SurfacelessEGL.";
820 gfx::Size
SurfacelessEGL::GetSize() {
824 bool SurfacelessEGL::Resize(const gfx::Size
& size
) {
829 EGLSurface
SurfacelessEGL::GetHandle() {
830 return EGL_NO_SURFACE
;
833 void* SurfacelessEGL::GetShareHandle() {
837 SurfacelessEGL::~SurfacelessEGL() {