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 "ui/ozone/public/ozone_gpu_test_helper.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "ipc/ipc_listener.h"
9 #include "ipc/ipc_message.h"
10 #include "ipc/ipc_sender.h"
11 #include "ipc/message_filter.h"
12 #include "ui/ozone/public/gpu_platform_support.h"
13 #include "ui/ozone/public/gpu_platform_support_host.h"
14 #include "ui/ozone/public/ozone_platform.h"
20 const int kGpuProcessHostId
= 1;
24 static void DispatchToGpuPlatformSupportHostTask(IPC::Message
* msg
) {
25 ui::OzonePlatform::GetInstance()
26 ->GetGpuPlatformSupportHost()
27 ->OnMessageReceived(*msg
);
31 class FakeGpuProcess
: public IPC::Sender
{
34 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
)
35 : ui_task_runner_(ui_task_runner
) {}
36 ~FakeGpuProcess() override
{}
39 ui::OzonePlatform::GetInstance()
40 ->GetGpuPlatformSupport()
41 ->OnChannelEstablished(this);
45 IPC::MessageFilter
* filter
= ui::OzonePlatform::GetInstance()
46 ->GetGpuPlatformSupport()
50 filter
->OnFilterAdded(this);
53 bool Send(IPC::Message
* msg
) override
{
54 ui_task_runner_
->PostTask(
55 FROM_HERE
, base::Bind(&DispatchToGpuPlatformSupportHostTask
, msg
));
60 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
63 static void DispatchToGpuPlatformSupportTask(IPC::Message
* msg
) {
64 ui::OzonePlatform::GetInstance()->GetGpuPlatformSupport()->OnMessageReceived(
69 class FakeGpuProcessHost
{
72 const scoped_refptr
<base::SingleThreadTaskRunner
>& gpu_task_runner
)
73 : gpu_task_runner_(gpu_task_runner
) {}
74 ~FakeGpuProcessHost() {}
77 base::Callback
<void(IPC::Message
*)> sender
=
78 base::Bind(&DispatchToGpuPlatformSupportTask
);
80 ui::OzonePlatform::GetInstance()
81 ->GetGpuPlatformSupportHost()
82 ->OnChannelEstablished(kGpuProcessHostId
, gpu_task_runner_
, sender
);
86 scoped_refptr
<base::SingleThreadTaskRunner
> gpu_task_runner_
;
89 OzoneGpuTestHelper::OzoneGpuTestHelper() {
92 OzoneGpuTestHelper::~OzoneGpuTestHelper() {
95 bool OzoneGpuTestHelper::Initialize(
96 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
,
97 const scoped_refptr
<base::SingleThreadTaskRunner
>& gpu_task_runner
) {
98 io_helper_thread_
.reset(new base::Thread("IOHelperThread"));
99 if (!io_helper_thread_
->StartWithOptions(
100 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0)))
103 fake_gpu_process_
.reset(new FakeGpuProcess(ui_task_runner
));
104 io_helper_thread_
->task_runner()->PostTask(
105 FROM_HERE
, base::Bind(&FakeGpuProcess::InitOnIO
,
106 base::Unretained(fake_gpu_process_
.get())));
107 fake_gpu_process_
->Init();
109 fake_gpu_process_host_
.reset(new FakeGpuProcessHost(gpu_task_runner
));
110 fake_gpu_process_host_
->Init();