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
3 // found in the LICENSE file.
5 #include "components/view_manager/surfaces/surfaces_context_provider.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/command_buffer_local.h"
13 #include "components/view_manager/gles2/gpu_state.h"
14 #include "components/view_manager/surfaces/surfaces_context_provider_delegate.h"
15 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
16 #include "gpu/command_buffer/client/gles2_implementation.h"
17 #include "gpu/command_buffer/client/transfer_buffer.h"
22 const size_t kDefaultCommandBufferSize
= 1024 * 1024;
23 const size_t kDefaultStartTransferBufferSize
= 1 * 1024 * 1024;
24 const size_t kDefaultMinTransferBufferSize
= 1 * 256 * 1024;
25 const size_t kDefaultMaxTransferBufferSize
= 16 * 1024 * 1024;
29 SurfacesContextProvider::SurfacesContextProvider(
30 SurfacesContextProviderDelegate
* delegate
,
31 gfx::AcceleratedWidget widget
,
32 const scoped_refptr
<gles2::GpuState
>& state
)
33 : delegate_(delegate
),
36 capabilities_
.gpu
.image
= true;
37 command_buffer_local_
.reset(
38 new gles2::CommandBufferLocal(this, widget_
, state_
));
41 // This is called when we have an accelerated widget.
42 bool SurfacesContextProvider::BindToCurrentThread() {
43 // SurfacesContextProvider should always live on the same thread as the
45 DCHECK(CalledOnValidThread());
46 if (!command_buffer_local_
->Initialize())
49 new gpu::gles2::GLES2CmdHelper(
50 command_buffer_local_
->GetCommandBuffer()));
51 if (!gles2_helper_
->Initialize(kDefaultCommandBufferSize
))
53 gles2_helper_
->SetAutomaticFlushes(false);
54 transfer_buffer_
.reset(new gpu::TransferBuffer(gles2_helper_
.get()));
55 gpu::Capabilities capabilities
= command_buffer_local_
->GetCapabilities();
56 bool bind_generates_resource
=
57 !!capabilities
.bind_generates_resource_chromium
;
58 // TODO(piman): Some contexts (such as compositor) want this to be true, so
59 // this needs to be a public parameter.
60 bool lose_context_when_out_of_memory
= false;
61 bool support_client_side_arrays
= false;
62 implementation_
.reset(
63 new gpu::gles2::GLES2Implementation(gles2_helper_
.get(),
65 transfer_buffer_
.get(),
66 bind_generates_resource
,
67 lose_context_when_out_of_memory
,
68 support_client_side_arrays
,
69 command_buffer_local_
.get()));
70 return implementation_
->Initialize(kDefaultStartTransferBufferSize
,
71 kDefaultMinTransferBufferSize
,
72 kDefaultMaxTransferBufferSize
,
73 gpu::gles2::GLES2Implementation::kNoLimit
);
76 gpu::gles2::GLES2Interface
* SurfacesContextProvider::ContextGL() {
77 return implementation_
.get();
80 gpu::ContextSupport
* SurfacesContextProvider::ContextSupport() {
81 return implementation_
.get();
84 class GrContext
* SurfacesContextProvider::GrContext() {
88 void SurfacesContextProvider::InvalidateGrContext(uint32_t state
) {
91 cc::ContextProvider::Capabilities
92 SurfacesContextProvider::ContextCapabilities() {
96 void SurfacesContextProvider::SetupLock() {
99 base::Lock
* SurfacesContextProvider::GetLock() {
100 return &context_lock_
;
103 bool SurfacesContextProvider::DestroyedOnMainThread() {
104 return !command_buffer_local_
;
107 void SurfacesContextProvider::SetLostContextCallback(
108 const LostContextCallback
& lost_context_callback
) {
109 lost_context_callback_
= lost_context_callback
;
112 SurfacesContextProvider::~SurfacesContextProvider() {
113 implementation_
->Flush();
114 implementation_
.reset();
115 transfer_buffer_
.reset();
116 gles2_helper_
.reset();
117 command_buffer_local_
.reset();
120 void SurfacesContextProvider::UpdateVSyncParameters(int64_t timebase
,
123 delegate_
->OnVSyncParametersUpdated(timebase
, interval
);
126 void SurfacesContextProvider::DidLoseContext() {
127 lost_context_callback_
.Run();
130 } // namespace surfaces