| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/gfx/gl/gl_context_egl.h" | 5 #include "ui/gfx/gl/gl_context_egl.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "third_party/angle/include/EGL/egl.h" | 10 #include "third_party/angle/include/EGL/egl.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 std::string GLContextEGL::GetExtensions() { | 26 std::string GLContextEGL::GetExtensions() { |
| 27 const char* extensions = eglQueryString(GLSurfaceEGL::GetDisplay(), | 27 const char* extensions = eglQueryString(GLSurfaceEGL::GetDisplay(), |
| 28 EGL_EXTENSIONS); | 28 EGL_EXTENSIONS); |
| 29 if (!extensions) | 29 if (!extensions) |
| 30 return GLContext::GetExtensions(); | 30 return GLContext::GetExtensions(); |
| 31 | 31 |
| 32 return GLContext::GetExtensions() + " " + extensions; | 32 return GLContext::GetExtensions() + " " + extensions; |
| 33 } | 33 } |
| 34 | 34 |
| 35 GLContextEGL::GLContextEGL(GLSurfaceEGL* surface) | 35 GLContextEGL::GLContextEGL() |
| 36 : surface_(surface), | 36 : context_(NULL) |
| 37 context_(NULL) | |
| 38 { | 37 { |
| 39 } | 38 } |
| 40 | 39 |
| 41 GLContextEGL::~GLContextEGL() { | 40 GLContextEGL::~GLContextEGL() { |
| 42 Destroy(); | 41 Destroy(); |
| 43 } | 42 } |
| 44 | 43 |
| 45 bool GLContextEGL::Initialize(GLContext* shared_context) { | 44 bool GLContextEGL::Initialize(GLContext* shared_context) { |
| 46 DCHECK(!context_); | 45 DCHECK(!context_); |
| 47 | 46 |
| 48 static const EGLint kContextAttributes[] = { | 47 static const EGLint kContextAttributes[] = { |
| 49 EGL_CONTEXT_CLIENT_VERSION, 2, | 48 EGL_CONTEXT_CLIENT_VERSION, 2, |
| 50 EGL_NONE | 49 EGL_NONE |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 context_ = eglCreateContext( | 52 context_ = eglCreateContext( |
| 54 GLSurfaceEGL::GetDisplay(), | 53 GLSurfaceEGL::GetDisplay(), |
| 55 GLSurfaceEGL::GetConfig(), | 54 GLSurfaceEGL::GetConfig(), |
| 56 shared_context ? shared_context->GetHandle() : NULL, | 55 shared_context ? shared_context->GetHandle() : NULL, |
| 57 kContextAttributes); | 56 kContextAttributes); |
| 58 if (!context_) { | 57 if (!context_) { |
| 59 LOG(ERROR) << "eglCreateContext failed with error " | 58 LOG(ERROR) << "eglCreateContext failed with error " |
| 60 << GetLastEGLErrorString(); | 59 << GetLastEGLErrorString(); |
| 61 Destroy(); | 60 Destroy(); |
| 62 return false; | 61 return false; |
| 63 } | 62 } |
| 64 | 63 |
| 65 if (!MakeCurrent()) { | |
| 66 LOG(ERROR) << "MakeCurrent failed."; | |
| 67 Destroy(); | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 if (!InitializeCommon()) { | |
| 72 LOG(ERROR) << "GLContext::InitializeCommon failed."; | |
| 73 Destroy(); | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 return true; | 64 return true; |
| 78 } | 65 } |
| 79 | 66 |
| 80 void GLContextEGL::Destroy() { | 67 void GLContextEGL::Destroy() { |
| 81 if (context_) { | 68 if (context_) { |
| 82 if (!eglDestroyContext(GLSurfaceEGL::GetDisplay(), context_)) { | 69 if (!eglDestroyContext(GLSurfaceEGL::GetDisplay(), context_)) { |
| 83 LOG(ERROR) << "eglDestroyContext failed with error " | 70 LOG(ERROR) << "eglDestroyContext failed with error " |
| 84 << GetLastEGLErrorString(); | 71 << GetLastEGLErrorString(); |
| 85 } | 72 } |
| 86 | 73 |
| 87 context_ = NULL; | 74 context_ = NULL; |
| 88 } | 75 } |
| 89 | |
| 90 if (surface_.get()) { | |
| 91 surface_->Destroy(); | |
| 92 surface_.reset(); | |
| 93 } | |
| 94 } | 76 } |
| 95 | 77 |
| 96 bool GLContextEGL::MakeCurrent() { | 78 bool GLContextEGL::MakeCurrent(GLSurface* surface) { |
| 97 DCHECK(context_); | 79 DCHECK(context_); |
| 98 if (IsCurrent()) | 80 if (IsCurrent(surface)) |
| 99 return true; | 81 return true; |
| 100 | 82 |
| 101 if (!eglMakeCurrent(GLSurfaceEGL::GetDisplay(), | 83 if (!eglMakeCurrent(GLSurfaceEGL::GetDisplay(), |
| 102 surface_->GetHandle(), | 84 surface->GetHandle(), |
| 103 surface_->GetHandle(), | 85 surface->GetHandle(), |
| 104 context_)) { | 86 context_)) { |
| 105 VLOG(1) << "eglMakeCurrent failed with error " | 87 VLOG(1) << "eglMakeCurrent failed with error " |
| 106 << GetLastEGLErrorString(); | 88 << GetLastEGLErrorString(); |
| 107 return false; | 89 return false; |
| 108 } | 90 } |
| 109 | 91 |
| 110 return true; | 92 return true; |
| 111 } | 93 } |
| 112 | 94 |
| 113 void GLContextEGL::ReleaseCurrent() { | 95 void GLContextEGL::ReleaseCurrent(GLSurface* surface) { |
| 114 if (!IsCurrent()) | 96 if (!IsCurrent(surface)) |
| 115 return; | 97 return; |
| 116 | 98 |
| 117 eglMakeCurrent(GLSurfaceEGL::GetDisplay(), | 99 eglMakeCurrent(GLSurfaceEGL::GetDisplay(), |
| 118 EGL_NO_SURFACE, | 100 EGL_NO_SURFACE, |
| 119 EGL_NO_SURFACE, | 101 EGL_NO_SURFACE, |
| 120 EGL_NO_CONTEXT); | 102 EGL_NO_CONTEXT); |
| 121 } | 103 } |
| 122 | 104 |
| 123 bool GLContextEGL::IsCurrent() { | 105 bool GLContextEGL::IsCurrent(GLSurface* surface) { |
| 124 DCHECK(context_); | 106 DCHECK(context_); |
| 125 return context_ == eglGetCurrentContext() && | 107 if (context_ != eglGetCurrentContext()) |
| 126 surface_->GetHandle() == eglGetCurrentSurface(EGL_DRAW); | 108 return false; |
| 127 } | |
| 128 | 109 |
| 129 bool GLContextEGL::IsOffscreen() { | 110 if (surface) { |
| 130 // TODO(apatrick): remove this from GLContext interface. | 111 if (surface->GetHandle() != eglGetCurrentSurface(EGL_DRAW)) |
| 131 return surface_->IsOffscreen(); | 112 return false; |
| 132 } | 113 } |
| 133 | 114 |
| 134 bool GLContextEGL::SwapBuffers() { | 115 return true; |
| 135 // TODO(apatrick): remove this from GLContext interface. | |
| 136 return surface_->SwapBuffers(); | |
| 137 } | |
| 138 | |
| 139 gfx::Size GLContextEGL::GetSize() { | |
| 140 // TODO(apatrick): remove this from GLContext interface. | |
| 141 return surface_->GetSize(); | |
| 142 } | |
| 143 | |
| 144 GLSurface* GLContextEGL::GetSurface() { | |
| 145 // TODO(apatrick): remove this from GLContext interface. | |
| 146 return surface_.get(); | |
| 147 } | 116 } |
| 148 | 117 |
| 149 void* GLContextEGL::GetHandle() { | 118 void* GLContextEGL::GetHandle() { |
| 150 return context_; | 119 return context_; |
| 151 } | 120 } |
| 152 | 121 |
| 153 void GLContextEGL::SetSwapInterval(int interval) { | 122 void GLContextEGL::SetSwapInterval(int interval) { |
| 154 DCHECK(IsCurrent()); | 123 DCHECK(IsCurrent(NULL)); |
| 155 if (!eglSwapInterval(GLSurfaceEGL::GetDisplay(), interval)) { | 124 if (!eglSwapInterval(GLSurfaceEGL::GetDisplay(), interval)) { |
| 156 LOG(ERROR) << "eglSwapInterval failed with error " | 125 LOG(ERROR) << "eglSwapInterval failed with error " |
| 157 << GetLastEGLErrorString(); | 126 << GetLastEGLErrorString(); |
| 158 } | 127 } |
| 159 } | 128 } |
| 160 | 129 |
| 161 } // namespace gfx | 130 } // namespace gfx |
| OLD | NEW |