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