summaryrefslogtreecommitdiff
path: root/chromium/components/mus/surfaces/surfaces_context_provider.cc
blob: 291a4402eb51732e2c38cdd1f00040abff0daf63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/mus/surfaces/surfaces_context_provider.h"

#include <stddef.h>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "components/mus/common/switches.h"
#include "components/mus/gles2/command_buffer_driver.h"
#include "components/mus/gles2/command_buffer_impl.h"
#include "components/mus/gles2/command_buffer_local.h"
#include "components/mus/gles2/gpu_state.h"
#include "components/mus/gpu/gpu_service_mus.h"
#include "components/mus/surfaces/surfaces_context_provider_delegate.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/client/shared_memory_limits.h"
#include "gpu/command_buffer/client/transfer_buffer.h"
#include "gpu/ipc/client/command_buffer_proxy_impl.h"
#include "ui/gl/gpu_preference.h"

namespace mus {

SurfacesContextProvider::SurfacesContextProvider(
    gfx::AcceleratedWidget widget,
    const scoped_refptr<GpuState>& state)
    : use_chrome_gpu_command_buffer_(false),
      delegate_(nullptr),
      widget_(widget),
      command_buffer_local_(nullptr) {
// TODO(penghuang): Kludge: Use mojo command buffer when running on Windows
// since Chrome command buffer breaks unit tests
#if defined(OS_WIN)
  use_chrome_gpu_command_buffer_ = false;
#else
  use_chrome_gpu_command_buffer_ =
      !base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kUseMojoGpuCommandBufferInMus);
#endif
  if (!use_chrome_gpu_command_buffer_) {
    command_buffer_local_ = new CommandBufferLocal(this, widget_, state);
  } else {
    GpuServiceMus* service = GpuServiceMus::GetInstance();
    gpu::CommandBufferProxyImpl* shared_command_buffer = nullptr;
    gpu::GpuStreamId stream_id = gpu::GpuStreamId::GPU_STREAM_DEFAULT;
    gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL;
    gpu::gles2::ContextCreationAttribHelper attributes;
    attributes.alpha_size = -1;
    attributes.depth_size = 0;
    attributes.stencil_size = 0;
    attributes.samples = 0;
    attributes.sample_buffers = 0;
    attributes.bind_generates_resource = false;
    attributes.lose_context_when_out_of_memory = true;
    GURL active_url;
    scoped_refptr<base::SingleThreadTaskRunner> task_runner =
        base::ThreadTaskRunnerHandle::Get();
    command_buffer_proxy_impl_ = gpu::CommandBufferProxyImpl::Create(
        service->gpu_channel_local(), widget, shared_command_buffer, stream_id,
        stream_priority, attributes, active_url, task_runner);
    command_buffer_proxy_impl_->SetSwapBuffersCompletionCallback(
        base::Bind(&SurfacesContextProvider::OnGpuSwapBuffersCompleted,
                   base::Unretained(this)));
    command_buffer_proxy_impl_->SetUpdateVSyncParametersCallback(
        base::Bind(&SurfacesContextProvider::OnUpdateVSyncParameters,
                   base::Unretained(this)));
  }
}

void SurfacesContextProvider::SetDelegate(
    SurfacesContextProviderDelegate* delegate) {
  DCHECK(!delegate_);
  delegate_ = delegate;
}

// This routine needs to be safe to call more than once.
// This is called when we have an accelerated widget.
bool SurfacesContextProvider::BindToCurrentThread() {
  if (implementation_)
    return true;

  // SurfacesContextProvider should always live on the same thread as the
  // Window Manager.
  DCHECK(CalledOnValidThread());
  gpu::GpuControl* gpu_control = nullptr;
  gpu::CommandBuffer* command_buffer = nullptr;
  if (!use_chrome_gpu_command_buffer_) {
    if (!command_buffer_local_->Initialize())
      return false;
    gpu_control = command_buffer_local_;
    command_buffer = command_buffer_local_;
  } else {
    if (!command_buffer_proxy_impl_)
      return false;
    gpu_control = command_buffer_proxy_impl_.get();
    command_buffer = command_buffer_proxy_impl_.get();
  }

  gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer));
  constexpr gpu::SharedMemoryLimits default_limits;
  if (!gles2_helper_->Initialize(default_limits.command_buffer_size))
    return false;
  gles2_helper_->SetAutomaticFlushes(false);
  transfer_buffer_.reset(new gpu::TransferBuffer(gles2_helper_.get()));
  capabilities_ = gpu_control->GetCapabilities();
  bool bind_generates_resource =
      !!capabilities_.bind_generates_resource_chromium;
  // TODO(piman): Some contexts (such as compositor) want this to be true, so
  // this needs to be a public parameter.
  bool lose_context_when_out_of_memory = false;
  bool support_client_side_arrays = false;
  implementation_.reset(new gpu::gles2::GLES2Implementation(
      gles2_helper_.get(), NULL, transfer_buffer_.get(),
      bind_generates_resource, lose_context_when_out_of_memory,
      support_client_side_arrays, gpu_control));
  return implementation_->Initialize(
      default_limits.start_transfer_buffer_size,
      default_limits.min_transfer_buffer_size,
      default_limits.max_transfer_buffer_size,
      default_limits.mapped_memory_reclaim_limit);
}

gpu::gles2::GLES2Interface* SurfacesContextProvider::ContextGL() {
  DCHECK(implementation_);
  return implementation_.get();
}

gpu::ContextSupport* SurfacesContextProvider::ContextSupport() {
  return implementation_.get();
}

class GrContext* SurfacesContextProvider::GrContext() {
  return NULL;
}

void SurfacesContextProvider::InvalidateGrContext(uint32_t state) {}

gpu::Capabilities SurfacesContextProvider::ContextCapabilities() {
  return capabilities_;
}

base::Lock* SurfacesContextProvider::GetLock() {
  // This context provider is not used on multiple threads.
  NOTREACHED();
  return nullptr;
}

void SurfacesContextProvider::SetLostContextCallback(
    const LostContextCallback& lost_context_callback) {
  implementation_->SetLostContextCallback(lost_context_callback);
}

SurfacesContextProvider::~SurfacesContextProvider() {
  implementation_->Flush();
  implementation_.reset();
  transfer_buffer_.reset();
  gles2_helper_.reset();
  command_buffer_proxy_impl_.reset();
  if (command_buffer_local_) {
    command_buffer_local_->Destroy();
    command_buffer_local_ = nullptr;
  }
}

void SurfacesContextProvider::UpdateVSyncParameters(
    const base::TimeTicks& timebase,
    const base::TimeDelta& interval) {
  if (delegate_)
    delegate_->OnVSyncParametersUpdated(timebase, interval);
}

void SurfacesContextProvider::GpuCompletedSwapBuffers(gfx::SwapResult result) {
  if (!swap_buffers_completion_callback_.is_null()) {
    swap_buffers_completion_callback_.Run(result);
  }
}

void SurfacesContextProvider::OnGpuSwapBuffersCompleted(
    const std::vector<ui::LatencyInfo>& latency_info,
    gfx::SwapResult result,
    const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) {
  if (!swap_buffers_completion_callback_.is_null()) {
    swap_buffers_completion_callback_.Run(result);
  }
}

void SurfacesContextProvider::OnUpdateVSyncParameters(
    base::TimeTicks timebase,
    base::TimeDelta interval) {
  if (delegate_)
    delegate_->OnVSyncParametersUpdated(timebase, interval);
}

void SurfacesContextProvider::SetSwapBuffersCompletionCallback(
    gl::GLSurface::SwapCompletionCallback callback) {
  swap_buffers_completion_callback_ = callback;
}

}  // namespace mus