1 // Copyright 2018 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.
4
5 #include "content/test/gpu_browsertest_helpers.h"
6
7 #include <utility>
8
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/run_loop.h"
12 #include "content/browser/browser_main_loop.h"
13 #include "content/public/common/gpu_stream_constants.h"
14 #include "gpu/command_buffer/client/shared_memory_limits.h"
15 #include "gpu/command_buffer/common/context_creation_attribs.h"
16 #include "gpu/ipc/client/gpu_channel_host.h"
17 #include "gpu/ipc/common/surface_handle.h"
18 #include "services/viz/public/cpp/gpu/command_buffer_metrics.h"
19 #include "services/viz/public/cpp/gpu/context_provider_command_buffer.h"
20 #include "url/gurl.h"
21
22 namespace content {
23
24 namespace {
25
OnEstablishedGpuChannel(const base::RepeatingClosure & quit_closure,scoped_refptr<gpu::GpuChannelHost> * retvalue,scoped_refptr<gpu::GpuChannelHost> established_host)26 void OnEstablishedGpuChannel(
27 const base::RepeatingClosure& quit_closure,
28 scoped_refptr<gpu::GpuChannelHost>* retvalue,
29 scoped_refptr<gpu::GpuChannelHost> established_host) {
30 if (retvalue)
31 *retvalue = std::move(established_host);
32 quit_closure.Run();
33 }
34
35 } // namespace
36
37 scoped_refptr<gpu::GpuChannelHost>
GpuBrowsertestEstablishGpuChannelSyncRunLoop()38 GpuBrowsertestEstablishGpuChannelSyncRunLoop() {
39 gpu::GpuChannelEstablishFactory* factory =
40 content::BrowserMainLoop::GetInstance()->gpu_channel_establish_factory();
41 CHECK(factory);
42 base::RunLoop run_loop;
43 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host;
44 factory->EstablishGpuChannel(base::BindOnce(
45 &OnEstablishedGpuChannel, run_loop.QuitClosure(), &gpu_channel_host));
46 run_loop.Run();
47 return gpu_channel_host;
48 }
49
GpuBrowsertestCreateContext(scoped_refptr<gpu::GpuChannelHost> gpu_channel_host)50 scoped_refptr<viz::ContextProviderCommandBuffer> GpuBrowsertestCreateContext(
51 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) {
52 gpu::GpuChannelEstablishFactory* factory =
53 content::BrowserMainLoop::GetInstance()->gpu_channel_establish_factory();
54 // This is for an offscreen context, so the default framebuffer doesn't need
55 // any alpha, depth, stencil, antialiasing.
56 gpu::ContextCreationAttribs attributes;
57 attributes.alpha_size = -1;
58 attributes.depth_size = 0;
59 attributes.stencil_size = 0;
60 attributes.samples = 0;
61 attributes.sample_buffers = 0;
62 attributes.bind_generates_resource = false;
63 constexpr bool automatic_flushes = false;
64 constexpr bool support_locking = false;
65 constexpr bool support_grcontext = true;
66 return base::MakeRefCounted<viz::ContextProviderCommandBuffer>(
67 std::move(gpu_channel_host), factory->GetGpuMemoryBufferManager(),
68 content::kGpuStreamIdDefault, content::kGpuStreamPriorityDefault,
69 gpu::kNullSurfaceHandle, GURL(), automatic_flushes, support_locking,
70 support_grcontext, gpu::SharedMemoryLimits(), attributes,
71 viz::command_buffer_metrics::ContextType::FOR_TESTING);
72 }
73
74 } // namespace content
75