cc: Add more eviction categories to picture layer impl.
[chromium-blink-merge.git] / ui / gl / gl_surface_egl.cc
blobe73ba4ed2ba39c5908dd678f677db573592ab4d7
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>
13 #endif
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"
30 #if defined(USE_X11)
31 extern "C" {
32 #include <X11/Xlib.h>
34 #endif
36 #if defined (USE_OZONE)
37 #include "ui/ozone/public/surface_factory_ozone.h"
38 #endif
40 #if !defined(EGL_FIXED_SIZE_ANGLE)
41 #define EGL_FIXED_SIZE_ANGLE 0x3201
42 #endif
44 using ui::GetLastEGLErrorString;
46 namespace gfx {
48 namespace {
50 EGLConfig g_config;
51 EGLDisplay g_display;
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 {
62 public:
63 explicit EGLSyncControlVSyncProvider(EGLSurface surface)
64 : SyncControlVSyncProvider(),
65 surface_(surface) {
68 virtual ~EGLSyncControlVSyncProvider() { }
70 protected:
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;
78 if (result) {
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);
83 return result;
86 virtual bool GetMscRate(int32* numerator, int32* denominator) OVERRIDE {
87 return false;
90 private:
91 EGLSurface surface_;
93 DISALLOW_COPY_AND_ASSIGN(EGLSyncControlVSyncProvider);
96 } // namespace
98 GLSurfaceEGL::GLSurfaceEGL() {}
100 bool GLSurfaceEGL::InitializeOneOff() {
101 static bool initialized = false;
102 if (initialized)
103 return true;
105 g_native_display = GetPlatformDefaultEGLNativeDisplay();
106 g_display = eglGetDisplay(g_native_display);
107 if (!g_display) {
108 LOG(ERROR) << "eglGetDisplay failed with error " << GetLastEGLErrorString();
109 return false;
112 if (!eglInitialize(g_display, NULL, NULL)) {
113 LOG(ERROR) << "eglInitialize failed with error " << GetLastEGLErrorString();
114 return false;
117 // Choose an EGL configuration.
118 // On X this is only used for PBuffer surfaces.
119 static const EGLint kConfigAttribs[] = {
120 EGL_BUFFER_SIZE, 32,
121 EGL_ALPHA_SIZE, 8,
122 EGL_BLUE_SIZE, 8,
123 EGL_GREEN_SIZE, 8,
124 EGL_RED_SIZE, 8,
125 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
126 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
127 EGL_NONE
130 #if defined(USE_OZONE)
131 const EGLint* config_attribs =
132 ui::SurfaceFactoryOzone::GetInstance()->GetEGLSurfaceProperties(
133 kConfigAttribs);
134 #else
135 const EGLint* config_attribs = kConfigAttribs;
136 #endif
138 EGLint num_configs;
139 if (!eglChooseConfig(g_display,
140 config_attribs,
141 NULL,
143 &num_configs)) {
144 LOG(ERROR) << "eglChooseConfig failed with error "
145 << GetLastEGLErrorString();
146 return false;
149 if (num_configs == 0) {
150 LOG(ERROR) << "No suitable EGL configs found.";
151 return false;
154 if (!eglChooseConfig(g_display,
155 config_attribs,
156 &g_config,
158 &num_configs)) {
159 LOG(ERROR) << "eglChooseConfig failed with error "
160 << GetLastEGLErrorString();
161 return false;
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);
178 #else
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());
199 #endif
201 initialized = true;
203 return true;
206 EGLDisplay GLSurfaceEGL::GetDisplay() {
207 return g_display;
210 EGLDisplay GLSurfaceEGL::GetHardwareDisplay() {
211 return g_display;
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)
237 : window_(window),
238 surface_(NULL),
239 supports_post_sub_buffer_(false),
240 config_(NULL),
241 size_(1, 1) {
242 #if defined(OS_ANDROID)
243 if (window)
244 ANativeWindow_acquire(window);
245 #endif
247 #if defined(OS_WIN)
248 RECT windowRect;
249 if (GetClientRect(window_, &windowRect))
250 size_ = gfx::Rect(windowRect).size();
251 #endif
254 bool NativeViewGLSurfaceEGL::Initialize() {
255 return Initialize(scoped_ptr<VSyncProvider>());
258 bool NativeViewGLSurfaceEGL::Initialize(
259 scoped_ptr<VSyncProvider> sync_provider) {
260 DCHECK(!surface_);
262 if (!GetDisplay()) {
263 LOG(ERROR) << "Trying to create surface with invalid display.";
264 return false;
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]);
288 if (!surface_) {
289 LOG(ERROR) << "eglCreateWindowSurface failed with error "
290 << GetLastEGLErrorString();
291 Destroy();
292 return false;
295 EGLint surfaceVal;
296 EGLBoolean retVal = eglQuerySurface(GetDisplay(),
297 surface_,
298 EGL_POST_SUB_BUFFER_SUPPORTED_NV,
299 &surfaceVal);
300 supports_post_sub_buffer_ = (surfaceVal && retVal) == EGL_TRUE;
302 if (sync_provider)
303 vsync_provider_.reset(sync_provider.release());
304 else if (g_egl_sync_control_supported)
305 vsync_provider_.reset(new EGLSyncControlVSyncProvider(surface_));
306 return true;
309 void NativeViewGLSurfaceEGL::Destroy() {
310 if (surface_) {
311 if (!eglDestroySurface(GetDisplay(), surface_)) {
312 LOG(ERROR) << "eglDestroySurface failed with error "
313 << GetLastEGLErrorString();
315 surface_ = NULL;
319 EGLConfig NativeViewGLSurfaceEGL::GetConfig() {
320 #if !defined(USE_X11)
321 return g_config;
322 #else
323 if (!config_) {
324 // Get a config compatible with the window
325 DCHECK(window_);
326 XWindowAttributes win_attribs;
327 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) {
328 return NULL;
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[] = {
337 EGL_BUFFER_SIZE, ~0,
338 EGL_ALPHA_SIZE, 8,
339 EGL_BLUE_SIZE, 8,
340 EGL_GREEN_SIZE, 8,
341 EGL_RED_SIZE, 8,
342 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
343 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
344 EGL_NONE
346 config_attribs[kBufferSizeOffset] = win_attribs.depth;
348 EGLint num_configs;
349 if (!eglChooseConfig(g_display,
350 config_attribs,
351 &config_,
353 &num_configs)) {
354 LOG(ERROR) << "eglChooseConfig failed with error "
355 << GetLastEGLErrorString();
356 return NULL;
359 if (num_configs) {
360 EGLint config_depth;
361 if (!eglGetConfigAttrib(g_display,
362 config_,
363 EGL_BUFFER_SIZE,
364 &config_depth)) {
365 LOG(ERROR) << "eglGetConfigAttrib failed with error "
366 << GetLastEGLErrorString();
367 return NULL;
370 if (config_depth == win_attribs.depth) {
371 return config_;
375 // Try without an alpha channel.
376 config_attribs[kAlphaSizeOffset] = 0;
377 if (!eglChooseConfig(g_display,
378 config_attribs,
379 &config_,
381 &num_configs)) {
382 LOG(ERROR) << "eglChooseConfig failed with error "
383 << GetLastEGLErrorString();
384 return NULL;
387 if (num_configs == 0) {
388 LOG(ERROR) << "No suitable EGL configs found.";
389 return NULL;
392 return config_;
393 #endif
396 bool NativeViewGLSurfaceEGL::IsOffscreen() {
397 return false;
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();
408 return false;
411 return true;
414 gfx::Size NativeViewGLSurfaceEGL::GetSize() {
415 EGLint width;
416 EGLint height;
417 if (!eglQuerySurface(GetDisplay(), surface_, EGL_WIDTH, &width) ||
418 !eglQuerySurface(GetDisplay(), surface_, EGL_HEIGHT, &height)) {
419 NOTREACHED() << "eglQuerySurface failed with error "
420 << GetLastEGLErrorString();
421 return gfx::Size();
424 return gfx::Size(width, height);
427 bool NativeViewGLSurfaceEGL::Resize(const gfx::Size& size) {
428 if (size == GetSize())
429 return true;
431 size_ = size;
433 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
434 GLContext* current_context = GLContext::GetCurrent();
435 bool was_current =
436 current_context && current_context->IsCurrent(this);
437 if (was_current) {
438 scoped_make_current.reset(
439 new ui::ScopedMakeCurrent(current_context, this));
440 current_context->ReleaseCurrent(this);
443 Destroy();
445 if (!Initialize()) {
446 LOG(ERROR) << "Failed to resize window.";
447 return false;
450 return true;
453 bool NativeViewGLSurfaceEGL::Recreate() {
454 Destroy();
455 if (!Initialize()) {
456 LOG(ERROR) << "Failed to create surface.";
457 return false;
459 return true;
462 EGLSurface NativeViewGLSurfaceEGL::GetHandle() {
463 return surface_;
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();
476 return false;
478 return true;
481 VSyncProvider* NativeViewGLSurfaceEGL::GetVSyncProvider() {
482 return vsync_provider_.get();
485 NativeViewGLSurfaceEGL::~NativeViewGLSurfaceEGL() {
486 Destroy();
487 #if defined(OS_ANDROID)
488 if (window_)
489 ANativeWindow_release(window_);
490 #endif
493 PbufferGLSurfaceEGL::PbufferGLSurfaceEGL(const gfx::Size& size)
494 : size_(size),
495 surface_(NULL) {
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)
499 size_.SetSize(1, 1);
502 bool PbufferGLSurfaceEGL::Initialize() {
503 EGLSurface old_surface = surface_;
505 EGLDisplay display = GetDisplay();
506 if (!display) {
507 LOG(ERROR) << "Trying to create surface with invalid display.";
508 return false;
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(),
518 EGL_NONE
521 EGLSurface new_surface = eglCreatePbufferSurface(display,
522 GetConfig(),
523 pbuffer_attribs);
524 if (!new_surface) {
525 LOG(ERROR) << "eglCreatePbufferSurface failed with error "
526 << GetLastEGLErrorString();
527 return false;
530 if (old_surface)
531 eglDestroySurface(display, old_surface);
533 surface_ = new_surface;
534 return true;
537 void PbufferGLSurfaceEGL::Destroy() {
538 if (surface_) {
539 if (!eglDestroySurface(GetDisplay(), surface_)) {
540 LOG(ERROR) << "eglDestroySurface failed with error "
541 << GetLastEGLErrorString();
543 surface_ = NULL;
547 EGLConfig PbufferGLSurfaceEGL::GetConfig() {
548 return g_config;
551 bool PbufferGLSurfaceEGL::IsOffscreen() {
552 return true;
555 bool PbufferGLSurfaceEGL::SwapBuffers() {
556 NOTREACHED() << "Attempted to call SwapBuffers on a PbufferGLSurfaceEGL.";
557 return false;
560 gfx::Size PbufferGLSurfaceEGL::GetSize() {
561 return size_;
564 bool PbufferGLSurfaceEGL::Resize(const gfx::Size& size) {
565 if (size == size_)
566 return true;
568 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
569 GLContext* current_context = GLContext::GetCurrent();
570 bool was_current =
571 current_context && current_context->IsCurrent(this);
572 if (was_current) {
573 scoped_make_current.reset(
574 new ui::ScopedMakeCurrent(current_context, this));
577 size_ = size;
579 if (!Initialize()) {
580 LOG(ERROR) << "Failed to resize pbuffer.";
581 return false;
584 return true;
587 EGLSurface PbufferGLSurfaceEGL::GetHandle() {
588 return surface_;
591 void* PbufferGLSurfaceEGL::GetShareHandle() {
592 #if defined(OS_ANDROID)
593 NOTREACHED();
594 return NULL;
595 #else
596 if (!gfx::g_driver_egl.ext.b_EGL_ANGLE_query_surface_pointer)
597 return NULL;
599 if (!gfx::g_driver_egl.ext.b_EGL_ANGLE_surface_d3d_texture_2d_share_handle)
600 return NULL;
602 void* handle;
603 if (!eglQuerySurfacePointerANGLE(g_display,
604 GetHandle(),
605 EGL_D3D_TEXTURE_2D_SHARE_HANDLE_ANGLE,
606 &handle)) {
607 return NULL;
610 return handle;
611 #endif
614 PbufferGLSurfaceEGL::~PbufferGLSurfaceEGL() {
615 Destroy();
618 SurfacelessEGL::SurfacelessEGL(const gfx::Size& size)
619 : size_(size) {
622 bool SurfacelessEGL::Initialize() {
623 return true;
626 void SurfacelessEGL::Destroy() {
629 EGLConfig SurfacelessEGL::GetConfig() {
630 return g_config;
633 bool SurfacelessEGL::IsOffscreen() {
634 return true;
637 bool SurfacelessEGL::SwapBuffers() {
638 LOG(ERROR) << "Attempted to call SwapBuffers with SurfacelessEGL.";
639 return false;
642 gfx::Size SurfacelessEGL::GetSize() {
643 return size_;
646 bool SurfacelessEGL::Resize(const gfx::Size& size) {
647 size_ = size;
648 return true;
651 EGLSurface SurfacelessEGL::GetHandle() {
652 return EGL_NO_SURFACE;
655 void* SurfacelessEGL::GetShareHandle() {
656 return NULL;
659 SurfacelessEGL::~SurfacelessEGL() {
662 } // namespace gfx