OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/gpu/gl_context.h" | 5 #include "mojo/gpu/gl_context.h" |
6 | |
7 #include "mojo/gpu/mojo_gles2_impl_autogen.h" | |
8 #include "mojo/public/cpp/application/connect.h" | 6 #include "mojo/public/cpp/application/connect.h" |
9 #include "mojo/public/interfaces/application/shell.mojom.h" | 7 #include "mojo/public/interfaces/application/shell.mojom.h" |
10 #include "mojo/services/gpu/interfaces/gpu.mojom.h" | 8 #include "mojo/services/gpu/interfaces/gpu.mojom.h" |
11 | 9 |
12 namespace mojo { | 10 namespace mojo { |
13 | 11 |
14 GLContext::Observer::~Observer() {} | 12 GLContext::Observer::~Observer() {} |
15 | 13 |
16 GLContext::GLContext(CommandBufferPtr command_buffer) : weak_factory_(this) { | 14 GLContext::GLContext(CommandBufferPtr command_buffer) : weak_factory_(this) { |
17 context_ = MGLCreateContext( | 15 context_ = MGLCreateContext( |
18 MGL_API_VERSION_GLES2, | 16 MGL_API_VERSION_GLES2, |
19 command_buffer.PassInterface().PassHandle().release().value(), | 17 command_buffer.PassInterface().PassHandle().release().value(), |
20 MGL_NO_CONTEXT, &ContextLostThunk, this, | 18 MGL_NO_CONTEXT, &ContextLostThunk, this, |
21 Environment::GetDefaultAsyncWaiter()); | 19 Environment::GetDefaultAsyncWaiter()); |
22 gl_impl_.reset(new MojoGLES2Impl(context_)); | 20 DCHECK(context_ != MGL_NO_CONTEXT); |
23 } | 21 } |
24 | 22 |
25 GLContext::~GLContext() { | 23 GLContext::~GLContext() { |
26 MGLDestroyContext(context_); | 24 MGLDestroyContext(context_); |
27 } | 25 } |
28 | 26 |
29 base::WeakPtr<GLContext> GLContext::Create(Shell* shell) { | 27 base::WeakPtr<GLContext> GLContext::Create(Shell* shell) { |
30 ServiceProviderPtr native_viewport; | 28 ServiceProviderPtr native_viewport; |
31 shell->ConnectToApplication("mojo:native_viewport_service", | 29 shell->ConnectToApplication("mojo:native_viewport_service", |
32 GetProxy(&native_viewport), nullptr); | 30 GetProxy(&native_viewport), nullptr); |
33 GpuPtr gpu_service; | 31 GpuPtr gpu_service; |
34 ConnectToService(native_viewport.get(), &gpu_service); | 32 ConnectToService(native_viewport.get(), &gpu_service); |
35 CommandBufferPtr command_buffer; | 33 CommandBufferPtr command_buffer; |
36 gpu_service->CreateOffscreenGLES2Context(GetProxy(&command_buffer)); | 34 gpu_service->CreateOffscreenGLES2Context(GetProxy(&command_buffer)); |
37 return CreateFromCommandBuffer(command_buffer.Pass()); | 35 return CreateFromCommandBuffer(command_buffer.Pass()); |
38 } | 36 } |
39 | 37 |
40 base::WeakPtr<GLContext> GLContext::CreateFromCommandBuffer( | 38 base::WeakPtr<GLContext> GLContext::CreateFromCommandBuffer( |
41 CommandBufferPtr command_buffer) { | 39 CommandBufferPtr command_buffer) { |
42 return (new GLContext(command_buffer.Pass()))->weak_factory_.GetWeakPtr(); | 40 return (new GLContext(command_buffer.Pass()))->weak_factory_.GetWeakPtr(); |
43 } | 41 } |
44 | 42 |
45 void GLContext::MakeCurrent() { | 43 void GLContext::MakeCurrent() { |
46 MGLMakeCurrent(context_); | 44 MGLMakeCurrent(context_); |
47 } | 45 } |
48 | 46 |
| 47 bool GLContext::IsCurrent() { |
| 48 return context_ == MGLGetCurrentContext(); |
| 49 } |
| 50 |
49 void GLContext::Destroy() { | 51 void GLContext::Destroy() { |
50 delete this; | 52 delete this; |
51 } | 53 } |
52 | 54 |
53 gpu::gles2::GLES2Interface* GLContext::gl() const { | |
54 return gl_impl_.get(); | |
55 } | |
56 | |
57 void GLContext::AddObserver(Observer* observer) { | 55 void GLContext::AddObserver(Observer* observer) { |
58 observers_.AddObserver(observer); | 56 observers_.AddObserver(observer); |
59 } | 57 } |
60 | 58 |
61 void GLContext::RemoveObserver(Observer* observer) { | 59 void GLContext::RemoveObserver(Observer* observer) { |
62 observers_.RemoveObserver(observer); | 60 observers_.RemoveObserver(observer); |
63 } | 61 } |
64 | 62 |
65 void GLContext::ContextLostThunk(void* self) { | 63 void GLContext::ContextLostThunk(void* self) { |
66 static_cast<GLContext*>(self)->OnContextLost(); | 64 static_cast<GLContext*>(self)->OnContextLost(); |
67 } | 65 } |
68 | 66 |
69 void GLContext::OnContextLost() { | 67 void GLContext::OnContextLost() { |
70 FOR_EACH_OBSERVER(Observer, observers_, OnContextLost()); | 68 FOR_EACH_OBSERVER(Observer, observers_, OnContextLost()); |
71 } | 69 } |
72 | 70 |
73 } // namespace mojo | 71 } // namespace mojo |
OLD | NEW |