Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gl/gl_surface_egl.h" | 5 #include "ui/gl/gl_surface_egl.h" |
| 6 | 6 |
| 7 #if defined(OS_ANDROID) | 7 #if defined(OS_ANDROID) |
| 8 #include <android/native_window_jni.h> | 8 #include <android/native_window_jni.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "ui/gfx/geometry/rect.h" | 19 #include "ui/gfx/geometry/rect.h" |
| 20 #include "ui/gl/egl_util.h" | 20 #include "ui/gl/egl_util.h" |
| 21 #include "ui/gl/gl_context.h" | 21 #include "ui/gl/gl_context.h" |
| 22 #include "ui/gl/gl_image.h" | 22 #include "ui/gl/gl_image.h" |
| 23 #include "ui/gl/gl_implementation.h" | 23 #include "ui/gl/gl_implementation.h" |
| 24 #include "ui/gl/gl_surface_stub.h" | 24 #include "ui/gl/gl_surface_stub.h" |
| 25 #include "ui/gl/gl_switches.h" | 25 #include "ui/gl/gl_switches.h" |
| 26 #include "ui/gl/scoped_make_current.h" | 26 #include "ui/gl/scoped_make_current.h" |
| 27 #include "ui/gl/sync_control_vsync_provider.h" | 27 #include "ui/gl/sync_control_vsync_provider.h" |
| 28 | 28 |
| 29 #if defined(USE_X11) | |
| 30 extern "C" { | |
| 31 #include <X11/Xlib.h> | |
| 32 } | |
| 33 #endif | |
| 34 | |
| 35 #if defined (USE_OZONE) | 29 #if defined (USE_OZONE) |
| 36 #include "ui/ozone/public/ozone_platform.h" | 30 #include "ui/ozone/public/ozone_platform.h" |
| 37 #include "ui/ozone/public/surface_factory_ozone.h" | 31 #include "ui/ozone/public/surface_factory_ozone.h" |
| 38 #endif | 32 #endif |
| 39 | 33 |
| 40 #if defined(USE_X11) && !defined(OS_CHROMEOS) | 34 #if defined(USE_X11) && !defined(OS_CHROMEOS) |
| 41 #include "ui/gfx/x/x11_switches.h" | 35 #include "ui/gfx/x/x11_switches.h" |
| 42 #endif | 36 #endif |
| 43 | 37 |
| 44 #if !defined(EGL_FIXED_SIZE_ANGLE) | 38 #if !defined(EGL_FIXED_SIZE_ANGLE) |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 g_display = display; | 459 g_display = display; |
| 466 break; | 460 break; |
| 467 } | 461 } |
| 468 } | 462 } |
| 469 | 463 |
| 470 return g_display; | 464 return g_display; |
| 471 } | 465 } |
| 472 | 466 |
| 473 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window) | 467 NativeViewGLSurfaceEGL::NativeViewGLSurfaceEGL(EGLNativeWindowType window) |
| 474 : window_(window), | 468 : window_(window), |
| 469 config_(NULL), | |
| 470 size_(1, 1), | |
| 475 surface_(NULL), | 471 surface_(NULL), |
| 476 supports_post_sub_buffer_(false), | 472 supports_post_sub_buffer_(false), |
| 477 config_(NULL), | |
| 478 size_(1, 1), | |
| 479 swap_interval_(1) { | 473 swap_interval_(1) { |
| 480 #if defined(OS_ANDROID) | 474 #if defined(OS_ANDROID) |
| 481 if (window) | 475 if (window) |
| 482 ANativeWindow_acquire(window); | 476 ANativeWindow_acquire(window); |
| 483 #endif | 477 #endif |
| 484 | 478 |
| 485 #if defined(OS_WIN) | 479 #if defined(OS_WIN) |
| 486 vsync_override_ = false; | 480 vsync_override_ = false; |
| 487 swap_generation_ = 0; | 481 swap_generation_ = 0; |
| 488 RECT windowRect; | 482 RECT windowRect; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 514 egl_window_attributes.push_back(EGL_HEIGHT); | 508 egl_window_attributes.push_back(EGL_HEIGHT); |
| 515 egl_window_attributes.push_back(size_.height()); | 509 egl_window_attributes.push_back(size_.height()); |
| 516 } | 510 } |
| 517 | 511 |
| 518 if (gfx::g_driver_egl.ext.b_EGL_NV_post_sub_buffer) { | 512 if (gfx::g_driver_egl.ext.b_EGL_NV_post_sub_buffer) { |
| 519 egl_window_attributes.push_back(EGL_POST_SUB_BUFFER_SUPPORTED_NV); | 513 egl_window_attributes.push_back(EGL_POST_SUB_BUFFER_SUPPORTED_NV); |
| 520 egl_window_attributes.push_back(EGL_TRUE); | 514 egl_window_attributes.push_back(EGL_TRUE); |
| 521 } | 515 } |
| 522 | 516 |
| 523 egl_window_attributes.push_back(EGL_NONE); | 517 egl_window_attributes.push_back(EGL_NONE); |
| 518 | |
| 519 // We need to make sure that window_ is correctly initialized with all | |
| 520 // the platform-dependant quirks, if any, before creating the surface. | |
| 521 if (!InitializeNativeWindow()) { | |
|
piman
2015/12/02 00:59:45
nit: move this before egl_window_attributes is con
| |
| 522 LOG(ERROR) << "Error trying to initialize the native window."; | |
| 523 return false; | |
| 524 } | |
| 525 | |
| 524 // Create a surface for the native window. | 526 // Create a surface for the native window. |
| 525 surface_ = eglCreateWindowSurface( | 527 surface_ = eglCreateWindowSurface( |
| 526 GetDisplay(), GetConfig(), window_, &egl_window_attributes[0]); | 528 GetDisplay(), GetConfig(), window_, &egl_window_attributes[0]); |
| 527 | 529 |
| 528 if (!surface_) { | 530 if (!surface_) { |
| 529 LOG(ERROR) << "eglCreateWindowSurface failed with error " | 531 LOG(ERROR) << "eglCreateWindowSurface failed with error " |
| 530 << GetLastEGLErrorString(); | 532 << GetLastEGLErrorString(); |
| 531 Destroy(); | 533 Destroy(); |
| 532 return false; | 534 return false; |
| 533 } | 535 } |
| 534 | 536 |
| 535 if (gfx::g_driver_egl.ext.b_EGL_NV_post_sub_buffer) { | 537 if (gfx::g_driver_egl.ext.b_EGL_NV_post_sub_buffer) { |
| 536 EGLint surfaceVal; | 538 EGLint surfaceVal; |
| 537 EGLBoolean retVal = eglQuerySurface( | 539 EGLBoolean retVal = eglQuerySurface( |
| 538 GetDisplay(), surface_, EGL_POST_SUB_BUFFER_SUPPORTED_NV, &surfaceVal); | 540 GetDisplay(), surface_, EGL_POST_SUB_BUFFER_SUPPORTED_NV, &surfaceVal); |
| 539 supports_post_sub_buffer_ = (surfaceVal && retVal) == EGL_TRUE; | 541 supports_post_sub_buffer_ = (surfaceVal && retVal) == EGL_TRUE; |
| 540 } | 542 } |
| 541 | 543 |
| 542 if (sync_provider) | 544 if (sync_provider) |
| 543 vsync_provider_.reset(sync_provider.release()); | 545 vsync_provider_.reset(sync_provider.release()); |
| 544 else if (g_egl_sync_control_supported) | 546 else if (g_egl_sync_control_supported) |
| 545 vsync_provider_.reset(new EGLSyncControlVSyncProvider(surface_)); | 547 vsync_provider_.reset(new EGLSyncControlVSyncProvider(surface_)); |
| 546 return true; | 548 return true; |
| 547 } | 549 } |
| 548 | 550 |
| 551 bool NativeViewGLSurfaceEGL::InitializeNativeWindow() { | |
| 552 return true; | |
| 553 } | |
| 554 | |
| 549 void NativeViewGLSurfaceEGL::Destroy() { | 555 void NativeViewGLSurfaceEGL::Destroy() { |
| 550 if (surface_) { | 556 if (surface_) { |
| 551 if (!eglDestroySurface(GetDisplay(), surface_)) { | 557 if (!eglDestroySurface(GetDisplay(), surface_)) { |
| 552 LOG(ERROR) << "eglDestroySurface failed with error " | 558 LOG(ERROR) << "eglDestroySurface failed with error " |
| 553 << GetLastEGLErrorString(); | 559 << GetLastEGLErrorString(); |
| 554 } | 560 } |
| 555 surface_ = NULL; | 561 surface_ = NULL; |
| 556 } | 562 } |
| 557 } | 563 } |
| 558 | 564 |
| 559 EGLConfig NativeViewGLSurfaceEGL::GetConfig() { | 565 EGLConfig NativeViewGLSurfaceEGL::GetConfig() { |
| 560 #if !defined(USE_X11) | |
| 561 return g_config; | 566 return g_config; |
| 562 #else | |
| 563 if (!config_) { | |
| 564 // Get a config compatible with the window | |
| 565 DCHECK(window_); | |
| 566 XWindowAttributes win_attribs; | |
| 567 if (!XGetWindowAttributes(GetNativeDisplay(), window_, &win_attribs)) { | |
| 568 return NULL; | |
| 569 } | |
| 570 | |
| 571 // Try matching the window depth with an alpha channel, | |
| 572 // because we're worried the destination alpha width could | |
| 573 // constrain blending precision. | |
| 574 const int kBufferSizeOffset = 1; | |
| 575 const int kAlphaSizeOffset = 3; | |
| 576 EGLint config_attribs[] = { | |
| 577 EGL_BUFFER_SIZE, ~0, | |
| 578 EGL_ALPHA_SIZE, 8, | |
| 579 EGL_BLUE_SIZE, 8, | |
| 580 EGL_GREEN_SIZE, 8, | |
| 581 EGL_RED_SIZE, 8, | |
| 582 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
| 583 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT, | |
| 584 EGL_NONE | |
| 585 }; | |
| 586 config_attribs[kBufferSizeOffset] = win_attribs.depth; | |
| 587 | |
| 588 EGLint num_configs; | |
| 589 if (!eglChooseConfig(g_display, | |
| 590 config_attribs, | |
| 591 &config_, | |
| 592 1, | |
| 593 &num_configs)) { | |
| 594 LOG(ERROR) << "eglChooseConfig failed with error " | |
| 595 << GetLastEGLErrorString(); | |
| 596 return NULL; | |
| 597 } | |
| 598 | |
| 599 if (num_configs) { | |
| 600 EGLint config_depth; | |
| 601 if (!eglGetConfigAttrib(g_display, | |
| 602 config_, | |
| 603 EGL_BUFFER_SIZE, | |
| 604 &config_depth)) { | |
| 605 LOG(ERROR) << "eglGetConfigAttrib failed with error " | |
| 606 << GetLastEGLErrorString(); | |
| 607 return NULL; | |
| 608 } | |
| 609 | |
| 610 if (config_depth == win_attribs.depth) { | |
| 611 return config_; | |
| 612 } | |
| 613 } | |
| 614 | |
| 615 // Try without an alpha channel. | |
| 616 config_attribs[kAlphaSizeOffset] = 0; | |
| 617 if (!eglChooseConfig(g_display, | |
| 618 config_attribs, | |
| 619 &config_, | |
| 620 1, | |
| 621 &num_configs)) { | |
| 622 LOG(ERROR) << "eglChooseConfig failed with error " | |
| 623 << GetLastEGLErrorString(); | |
| 624 return NULL; | |
| 625 } | |
| 626 | |
| 627 if (num_configs == 0) { | |
| 628 LOG(ERROR) << "No suitable EGL configs found."; | |
| 629 return NULL; | |
| 630 } | |
| 631 } | |
| 632 return config_; | |
| 633 #endif | |
| 634 } | 567 } |
| 635 | 568 |
| 636 bool NativeViewGLSurfaceEGL::IsOffscreen() { | 569 bool NativeViewGLSurfaceEGL::IsOffscreen() { |
| 637 return false; | 570 return false; |
| 638 } | 571 } |
| 639 | 572 |
| 640 gfx::SwapResult NativeViewGLSurfaceEGL::SwapBuffers() { | 573 gfx::SwapResult NativeViewGLSurfaceEGL::SwapBuffers() { |
| 641 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers", | 574 TRACE_EVENT2("gpu", "NativeViewGLSurfaceEGL:RealSwapBuffers", |
| 642 "width", GetSize().width(), | 575 "width", GetSize().width(), |
| 643 "height", GetSize().height()); | 576 "height", GetSize().height()); |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 955 } | 888 } |
| 956 | 889 |
| 957 void* SurfacelessEGL::GetShareHandle() { | 890 void* SurfacelessEGL::GetShareHandle() { |
| 958 return NULL; | 891 return NULL; |
| 959 } | 892 } |
| 960 | 893 |
| 961 SurfacelessEGL::~SurfacelessEGL() { | 894 SurfacelessEGL::~SurfacelessEGL() { |
| 962 } | 895 } |
| 963 | 896 |
| 964 } // namespace gfx | 897 } // namespace gfx |
| OLD | NEW |