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/mus/surfaces/surfaces_context_provider.h"
8 #include "base/bind_helpers.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "components/mus/gles2/command_buffer_driver.h"
11 #include "components/mus/gles2/command_buffer_impl.h"
12 #include "components/mus/gles2/command_buffer_local.h"
13 #include "components/mus/gles2/gpu_state.h"
14 #include "components/mus/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;
28 SurfacesContextProvider::SurfacesContextProvider(
29 SurfacesContextProviderDelegate
* delegate
,
30 gfx::AcceleratedWidget widget
,
31 const scoped_refptr
<GpuState
>& state
)
32 : delegate_(delegate
), widget_(widget
) {
33 capabilities_
.gpu
.image
= true;
34 command_buffer_local_
.reset(new CommandBufferLocal(this, widget_
, state
));
37 // This is called when we have an accelerated widget.
38 bool SurfacesContextProvider::BindToCurrentThread() {
39 // SurfacesContextProvider should always live on the same thread as the
41 DCHECK(CalledOnValidThread());
42 if (!command_buffer_local_
->Initialize())
44 gles2_helper_
.reset(new gpu::gles2::GLES2CmdHelper(
45 command_buffer_local_
->GetCommandBuffer()));
46 if (!gles2_helper_
->Initialize(kDefaultCommandBufferSize
))
48 gles2_helper_
->SetAutomaticFlushes(false);
49 transfer_buffer_
.reset(new gpu::TransferBuffer(gles2_helper_
.get()));
50 gpu::Capabilities capabilities
= command_buffer_local_
->GetCapabilities();
51 bool bind_generates_resource
=
52 !!capabilities
.bind_generates_resource_chromium
;
53 // TODO(piman): Some contexts (such as compositor) want this to be true, so
54 // this needs to be a public parameter.
55 bool lose_context_when_out_of_memory
= false;
56 bool support_client_side_arrays
= false;
57 implementation_
.reset(new gpu::gles2::GLES2Implementation(
58 gles2_helper_
.get(), NULL
, transfer_buffer_
.get(),
59 bind_generates_resource
, lose_context_when_out_of_memory
,
60 support_client_side_arrays
, command_buffer_local_
.get()));
61 return implementation_
->Initialize(
62 kDefaultStartTransferBufferSize
, kDefaultMinTransferBufferSize
,
63 kDefaultMaxTransferBufferSize
, gpu::gles2::GLES2Implementation::kNoLimit
);
66 gpu::gles2::GLES2Interface
* SurfacesContextProvider::ContextGL() {
67 return implementation_
.get();
70 gpu::ContextSupport
* SurfacesContextProvider::ContextSupport() {
71 return implementation_
.get();
74 class GrContext
* SurfacesContextProvider::GrContext() {
78 void SurfacesContextProvider::InvalidateGrContext(uint32_t state
) {}
80 cc::ContextProvider::Capabilities
81 SurfacesContextProvider::ContextCapabilities() {
85 void SurfacesContextProvider::SetupLock() {}
87 base::Lock
* SurfacesContextProvider::GetLock() {
88 return &context_lock_
;
91 bool SurfacesContextProvider::DestroyedOnMainThread() {
92 return !command_buffer_local_
;
95 void SurfacesContextProvider::SetLostContextCallback(
96 const LostContextCallback
& lost_context_callback
) {
97 lost_context_callback_
= lost_context_callback
;
100 SurfacesContextProvider::~SurfacesContextProvider() {
101 implementation_
->Flush();
102 implementation_
.reset();
103 transfer_buffer_
.reset();
104 gles2_helper_
.reset();
105 command_buffer_local_
.reset();
108 void SurfacesContextProvider::UpdateVSyncParameters(int64_t timebase
,
111 delegate_
->OnVSyncParametersUpdated(timebase
, interval
);
114 void SurfacesContextProvider::DidLoseContext() {
115 lost_context_callback_
.Run();