| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_context.h" | 5 #include "ui/gl/init/gl_factory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/macros.h" | 8 #include "base/trace_event/trace_event.h" |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/sys_info.h" | |
| 11 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/gl_bindings.h" |
| 10 #include "ui/gl/gl_context.h" |
| 12 #include "ui/gl/gl_context_egl.h" | 11 #include "ui/gl/gl_context_egl.h" |
| 13 #include "ui/gl/gl_context_osmesa.h" | 12 #include "ui/gl/gl_context_osmesa.h" |
| 14 #include "ui/gl/gl_context_stub.h" | 13 #include "ui/gl/gl_context_stub.h" |
| 15 #include "ui/gl/gl_implementation.h" | 14 #include "ui/gl/gl_implementation.h" |
| 15 #include "ui/gl/gl_share_group.h" |
| 16 #include "ui/gl/gl_surface.h" | 16 #include "ui/gl/gl_surface.h" |
| 17 | 17 |
| 18 namespace gl { | 18 namespace gl { |
| 19 namespace init { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // Used to render into an already current context+surface, | 23 // Used to render into an already current context+surface, |
| 23 // that we do not have ownership of (draw callback). | 24 // that we do not have ownership of (draw callback). |
| 24 // TODO(boliu): Make this inherit from GLContextEGL. | 25 // TODO(boliu): Make this inherit from GLContextEGL. |
| 25 class GLNonOwnedContext : public GLContextReal { | 26 class GLNonOwnedContext : public GLContextReal { |
| 26 public: | 27 public: |
| 27 GLNonOwnedContext(GLShareGroup* share_group); | 28 explicit GLNonOwnedContext(GLShareGroup* share_group); |
| 28 | 29 |
| 29 // Implement GLContext. | 30 // Implement GLContext. |
| 30 bool Initialize(GLSurface* compatible_surface, | 31 bool Initialize(GLSurface* compatible_surface, |
| 31 GpuPreference gpu_preference) override; | 32 GpuPreference gpu_preference) override; |
| 32 bool MakeCurrent(GLSurface* surface) override; | 33 bool MakeCurrent(GLSurface* surface) override; |
| 33 void ReleaseCurrent(GLSurface* surface) override {} | 34 void ReleaseCurrent(GLSurface* surface) override {} |
| 34 bool IsCurrent(GLSurface* surface) override { return true; } | 35 bool IsCurrent(GLSurface* surface) override { return true; } |
| 35 void* GetHandle() override { return nullptr; } | 36 void* GetHandle() override { return nullptr; } |
| 36 void OnSetSwapInterval(int interval) override {} | 37 void OnSetSwapInterval(int interval) override {} |
| 37 std::string GetExtensions() override; | 38 std::string GetExtensions() override; |
| 38 | 39 |
| 39 protected: | 40 protected: |
| 40 ~GLNonOwnedContext() override {} | 41 ~GLNonOwnedContext() override {} |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 EGLDisplay display_; | 44 EGLDisplay display_; |
| 44 | 45 |
| 45 DISALLOW_COPY_AND_ASSIGN(GLNonOwnedContext); | 46 DISALLOW_COPY_AND_ASSIGN(GLNonOwnedContext); |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 GLNonOwnedContext::GLNonOwnedContext(GLShareGroup* share_group) | 49 GLNonOwnedContext::GLNonOwnedContext(GLShareGroup* share_group) |
| 49 : GLContextReal(share_group), display_(nullptr) {} | 50 : GLContextReal(share_group), display_(nullptr) {} |
| 50 | 51 |
| 51 bool GLNonOwnedContext::Initialize(GLSurface* compatible_surface, | 52 bool GLNonOwnedContext::Initialize(GLSurface* compatible_surface, |
| 52 GpuPreference gpu_preference) { | 53 GpuPreference gpu_preference) { |
| 53 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 54 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 54 return true; | 55 return true; |
| 55 } | 56 } |
| 56 | 57 |
| 57 bool GLNonOwnedContext::MakeCurrent(GLSurface* surface) { | 58 bool GLNonOwnedContext::MakeCurrent(GLSurface* surface) { |
| 58 SetCurrent(surface); | 59 SetCurrent(surface); |
| 59 SetRealGLApi(); | 60 SetRealGLApi(); |
| 60 return true; | 61 return true; |
| 61 } | 62 } |
| 62 | 63 |
| 63 std::string GLNonOwnedContext::GetExtensions() { | 64 std::string GLNonOwnedContext::GetExtensions() { |
| 64 const char* extensions = eglQueryString(display_, EGL_EXTENSIONS); | 65 const char* extensions = eglQueryString(display_, EGL_EXTENSIONS); |
| 65 if (!extensions) | 66 if (!extensions) |
| 66 return GLContext::GetExtensions(); | 67 return GLContext::GetExtensions(); |
| 67 | 68 |
| 68 return GLContext::GetExtensions() + " " + extensions; | 69 return GLContext::GetExtensions() + " " + extensions; |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // anonymous namespace | 72 } // anonymous namespace |
| 72 | 73 |
| 73 // static | 74 scoped_refptr<GLContext> CreateGLContext(GLShareGroup* share_group, |
| 74 scoped_refptr<GLContext> GLContext::CreateGLContext( | 75 GLSurface* compatible_surface, |
| 75 GLShareGroup* share_group, | 76 GpuPreference gpu_preference) { |
| 76 GLSurface* compatible_surface, | 77 TRACE_EVENT0("gpu", "gl::init::CreateGLContext"); |
| 77 GpuPreference gpu_preference) { | |
| 78 scoped_refptr<GLContext> context; | |
| 79 switch (GetGLImplementation()) { | 78 switch (GetGLImplementation()) { |
| 80 case kGLImplementationMockGL: | 79 case kGLImplementationMockGL: |
| 81 return scoped_refptr<GLContext>(new GLContextStub(share_group)); | 80 return scoped_refptr<GLContext>(new GLContextStub(share_group)); |
| 82 case kGLImplementationOSMesaGL: | 81 case kGLImplementationOSMesaGL: |
| 83 context = new GLContextOSMesa(share_group); | 82 return InitializeGLContext(new GLContextOSMesa(share_group), |
| 84 break; | 83 compatible_surface, gpu_preference); |
| 85 default: | 84 default: |
| 86 if (compatible_surface->GetHandle()) | 85 if (compatible_surface->GetHandle()) { |
| 87 context = new GLContextEGL(share_group); | 86 return InitializeGLContext(new GLContextEGL(share_group), |
| 88 else | 87 compatible_surface, gpu_preference); |
| 89 context = new GLNonOwnedContext(share_group); | 88 } else { |
| 90 break; | 89 return InitializeGLContext(new GLNonOwnedContext(share_group), |
| 90 compatible_surface, gpu_preference); |
| 91 } |
| 91 } | 92 } |
| 92 | |
| 93 if (!context->Initialize(compatible_surface, gpu_preference)) | |
| 94 return nullptr; | |
| 95 | |
| 96 return context; | |
| 97 } | 93 } |
| 98 | 94 |
| 99 } | 95 } // namespace init |
| 96 } // namespace gl |
| OLD | NEW |