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 "components/view_manager/surfaces/surfaces_context_provider.h" | 5 #include "components/view_manager/surfaces/surfaces_context_provider.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/bind.h" |
8 #include "third_party/mojo/src/mojo/public/cpp/environment/environment.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "components/view_manager/gles2/command_buffer_driver.h" |
| 11 #include "components/view_manager/gles2/command_buffer_impl.h" |
| 12 #include "components/view_manager/gles2/gpu_state.h" |
| 13 #include "gpu/command_buffer/client/gles2_lib.h" |
| 14 |
9 | 15 |
10 namespace surfaces { | 16 namespace surfaces { |
11 | 17 |
12 SurfacesContextProvider::SurfacesContextProvider( | 18 namespace { |
13 mojo::ScopedMessagePipeHandle command_buffer_handle) | 19 const size_t kDefaultCommandBufferSize = 1024 * 1024; |
14 : command_buffer_handle_(command_buffer_handle.Pass()), context_(nullptr) { | 20 const size_t kDefaultStartTransferBufferSize = 1 * 1024 * 1024; |
15 capabilities_.gpu.image = true; | 21 const size_t kDefaultMinTransferBufferSize = 1 * 256 * 1024; |
| 22 const size_t kDefaultMaxTransferBufferSize = 16 * 1024 * 1024; |
| 23 |
16 } | 24 } |
17 | 25 |
| 26 SurfacesContextProvider::SurfacesContextProvider( |
| 27 Delegate* delegate, |
| 28 gfx::AcceleratedWidget widget, |
| 29 const scoped_refptr<gles2::GpuState>& state) |
| 30 : delegate_(delegate), |
| 31 state_(state), |
| 32 widget_(widget) { |
| 33 capabilities_.gpu.image = true; |
| 34 command_buffer_local_.reset(new CommandBufferLocal(this, widget_, state_)); |
| 35 } |
| 36 |
| 37 // This is called when we have an accelerated widget. |
18 bool SurfacesContextProvider::BindToCurrentThread() { | 38 bool SurfacesContextProvider::BindToCurrentThread() { |
19 DCHECK(command_buffer_handle_.is_valid()); | 39 // SurfacesContextProvider should always live on the same thread as the |
20 context_ = MojoGLES2CreateContext(command_buffer_handle_.release().value(), | 40 // View Manager. |
21 &ContextLostThunk, this, | 41 DCHECK(CalledOnValidThread()); |
22 mojo::Environment::GetDefaultAsyncWaiter()); | 42 if (!command_buffer_local_->Initialize()) |
23 DCHECK(context_); | 43 return false; |
24 return !!context_; | 44 gles2_helper_.reset( |
| 45 new gpu::gles2::GLES2CmdHelper( |
| 46 command_buffer_local_->command_buffer())); |
| 47 if (!gles2_helper_->Initialize(kDefaultCommandBufferSize)) |
| 48 return false; |
| 49 gles2_helper_->SetAutomaticFlushes(false); |
| 50 transfer_buffer_.reset(new gpu::TransferBuffer(gles2_helper_.get())); |
| 51 gpu::Capabilities capabilities = command_buffer_local_->GetCapabilities(); |
| 52 bool bind_generates_resource = |
| 53 !!capabilities.bind_generates_resource_chromium; |
| 54 // TODO(piman): Some contexts (such as compositor) want this to be true, so |
| 55 // this needs to be a public parameter. |
| 56 bool lose_context_when_out_of_memory = false; |
| 57 bool support_client_side_arrays = false; |
| 58 implementation_.reset( |
| 59 new gpu::gles2::GLES2Implementation(gles2_helper_.get(), |
| 60 NULL, |
| 61 transfer_buffer_.get(), |
| 62 bind_generates_resource, |
| 63 lose_context_when_out_of_memory, |
| 64 support_client_side_arrays, |
| 65 command_buffer_local_.get())); |
| 66 return implementation_->Initialize(kDefaultStartTransferBufferSize, |
| 67 kDefaultMinTransferBufferSize, |
| 68 kDefaultMaxTransferBufferSize, |
| 69 gpu::gles2::GLES2Implementation::kNoLimit); |
25 } | 70 } |
26 | 71 |
27 gpu::gles2::GLES2Interface* SurfacesContextProvider::ContextGL() { | 72 gpu::gles2::GLES2Interface* SurfacesContextProvider::ContextGL() { |
28 if (!context_) | 73 return implementation_.get(); |
29 return nullptr; | |
30 return static_cast<gpu::gles2::GLES2Interface*>( | |
31 MojoGLES2GetGLES2Interface(context_)); | |
32 } | 74 } |
33 | 75 |
34 gpu::ContextSupport* SurfacesContextProvider::ContextSupport() { | 76 gpu::ContextSupport* SurfacesContextProvider::ContextSupport() { |
35 if (!context_) | 77 return implementation_.get(); |
36 return nullptr; | |
37 return static_cast<gpu::ContextSupport*>( | |
38 MojoGLES2GetContextSupport(context_)); | |
39 } | 78 } |
40 | 79 |
41 class GrContext* SurfacesContextProvider::GrContext() { | 80 class GrContext* SurfacesContextProvider::GrContext() { |
42 return NULL; | 81 return NULL; |
43 } | 82 } |
44 | 83 |
45 void SurfacesContextProvider::InvalidateGrContext(uint32_t state) { | 84 void SurfacesContextProvider::InvalidateGrContext(uint32_t state) { |
46 } | 85 } |
47 | 86 |
48 cc::ContextProvider::Capabilities | 87 cc::ContextProvider::Capabilities |
49 SurfacesContextProvider::ContextCapabilities() { | 88 SurfacesContextProvider::ContextCapabilities() { |
50 return capabilities_; | 89 return capabilities_; |
51 } | 90 } |
52 | 91 |
53 void SurfacesContextProvider::SetupLock() { | 92 void SurfacesContextProvider::SetupLock() { |
54 } | 93 } |
55 | 94 |
56 base::Lock* SurfacesContextProvider::GetLock() { | 95 base::Lock* SurfacesContextProvider::GetLock() { |
57 return &context_lock_; | 96 return &context_lock_; |
58 } | 97 } |
59 | 98 |
60 bool SurfacesContextProvider::DestroyedOnMainThread() { | 99 bool SurfacesContextProvider::DestroyedOnMainThread() { |
61 return !context_; | 100 return !command_buffer_local_; |
62 } | 101 } |
63 | 102 |
64 void SurfacesContextProvider::SetLostContextCallback( | 103 void SurfacesContextProvider::SetLostContextCallback( |
65 const LostContextCallback& lost_context_callback) { | 104 const LostContextCallback& lost_context_callback) { |
66 lost_context_callback_ = lost_context_callback; | 105 lost_context_callback_ = lost_context_callback; |
67 } | 106 } |
68 | 107 |
69 SurfacesContextProvider::~SurfacesContextProvider() { | 108 SurfacesContextProvider::~SurfacesContextProvider() { |
70 if (context_) | 109 implementation_->Flush(); |
71 MojoGLES2DestroyContext(context_); | 110 implementation_.reset(); |
| 111 transfer_buffer_.reset(); |
| 112 gles2_helper_.reset(); |
| 113 command_buffer_local_.reset(); |
72 } | 114 } |
73 | 115 |
74 void SurfacesContextProvider::ContextLost() { | 116 void SurfacesContextProvider::UpdateVSyncParameters(int64_t timebase, |
75 if (!lost_context_callback_.is_null()) | 117 int64_t interval) { |
76 lost_context_callback_.Run(); | 118 if (delegate_) |
| 119 delegate_->OnVSyncParametersUpdated(timebase, interval); |
| 120 } |
| 121 |
| 122 void SurfacesContextProvider::DidLoseContext() { |
| 123 lost_context_callback_.Run(); |
77 } | 124 } |
78 | 125 |
79 } // namespace surfaces | 126 } // namespace surfaces |
OLD | NEW |