aw_gl_surface.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2013 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.
  4. #include "android_webview/browser/gfx/aw_gl_surface.h"
  5. #include <utility>
  6. #include "android_webview/browser/gfx/scoped_app_gl_state_restore.h"
  7. #include "ui/gl/gl_bindings.h"
  8. #define EGL_EXTERNAL_SURFACE_ANGLE 0x348F
  9. namespace android_webview {
  10. AwGLSurface::AwGLSurface(gl::GLDisplayEGL* display, bool is_angle)
  11. : gl::GLSurfaceEGL(display), is_angle_(is_angle) {}
  12. AwGLSurface::AwGLSurface(gl::GLDisplayEGL* display,
  13. scoped_refptr<gl::GLSurface> surface)
  14. : gl::GLSurfaceEGL(display),
  15. is_angle_(false),
  16. wrapped_surface_(std::move(surface)) {}
  17. AwGLSurface::~AwGLSurface() {
  18. Destroy();
  19. }
  20. bool AwGLSurface::Initialize(gl::GLSurfaceFormat format) {
  21. if (!is_angle_)
  22. return true;
  23. Destroy();
  24. EGLint attribs[] = {EGL_WIDTH, size_.width(), EGL_HEIGHT,
  25. size_.height(), EGL_NONE, EGL_NONE};
  26. surface_ = eglCreatePbufferFromClientBuffer(GetGLDisplay()->GetDisplay(),
  27. EGL_EXTERNAL_SURFACE_ANGLE,
  28. nullptr, GetConfig(), attribs);
  29. DCHECK_NE(surface_, EGL_NO_SURFACE);
  30. return surface_ != EGL_NO_SURFACE;
  31. }
  32. void AwGLSurface::Destroy() {
  33. if (surface_) {
  34. eglDestroySurface(GetGLDisplay()->GetDisplay(), surface_);
  35. surface_ = nullptr;
  36. }
  37. }
  38. bool AwGLSurface::IsOffscreen() {
  39. return false;
  40. }
  41. unsigned int AwGLSurface::GetBackingFramebufferObject() {
  42. return ScopedAppGLStateRestore::Current()->framebuffer_binding_ext();
  43. }
  44. gfx::SwapResult AwGLSurface::SwapBuffers(PresentationCallback callback) {
  45. DCHECK(!pending_presentation_callback_);
  46. pending_presentation_callback_ = std::move(callback);
  47. return gfx::SwapResult::SWAP_ACK;
  48. }
  49. gfx::Size AwGLSurface::GetSize() {
  50. return size_;
  51. }
  52. void* AwGLSurface::GetHandle() {
  53. if (wrapped_surface_)
  54. return wrapped_surface_->GetHandle();
  55. return surface_;
  56. }
  57. gl::GLDisplay* AwGLSurface::GetGLDisplay() {
  58. if (wrapped_surface_)
  59. return wrapped_surface_->GetGLDisplay();
  60. if (!is_angle_)
  61. return nullptr;
  62. return gl::GLSurfaceEGL::GetGLDisplay();
  63. }
  64. gl::GLSurfaceFormat AwGLSurface::GetFormat() {
  65. return gl::GLSurfaceFormat();
  66. }
  67. bool AwGLSurface::Resize(const gfx::Size& size,
  68. float scale_factor,
  69. const gfx::ColorSpace& color_space,
  70. bool has_alpha) {
  71. if (size_ == size)
  72. return true;
  73. size_ = size;
  74. return Initialize(gl::GLSurfaceFormat());
  75. }
  76. bool AwGLSurface::OnMakeCurrent(gl::GLContext* context) {
  77. if (!gl::GLSurfaceEGL::OnMakeCurrent(context))
  78. return false;
  79. return !wrapped_surface_ || wrapped_surface_->OnMakeCurrent(context);
  80. }
  81. void AwGLSurface::SetSize(const gfx::Size& size) {
  82. size_ = size;
  83. }
  84. EGLConfig AwGLSurface::GetConfig() {
  85. if (wrapped_surface_)
  86. return wrapped_surface_->GetConfig();
  87. if (!is_angle_)
  88. return nullptr;
  89. return gl::GLSurfaceEGL::GetConfig();
  90. }
  91. void AwGLSurface::MaybeDidPresent(const gfx::PresentationFeedback& feedback) {
  92. if (!pending_presentation_callback_)
  93. return;
  94. std::move(pending_presentation_callback_).Run(feedback);
  95. }
  96. bool AwGLSurface::IsDrawingToFBO() {
  97. return false;
  98. }
  99. } // namespace android_webview