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 class FakeGpuProcess
: public IPC::Sender
{
27 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
)
28 : ui_task_runner_(ui_task_runner
), weak_factory_(this) {}
29 ~FakeGpuProcess() override
{}
32 ui::OzonePlatform::GetInstance()
33 ->GetGpuPlatformSupport()
34 ->OnChannelEstablished(this);
38 ui::OzonePlatform::GetInstance()
39 ->GetGpuPlatformSupport()
41 ->OnFilterAdded(this);
44 bool Send(IPC::Message
* msg
) override
{
45 ui_task_runner_
->PostTask(
47 base::Bind(&FakeGpuProcess::DispatchToGpuPlatformSupportHostTask
,
48 weak_factory_
.GetWeakPtr(), msg
));
53 void DispatchToGpuPlatformSupportHostTask(IPC::Message
* msg
) {
54 ui::OzonePlatform::GetInstance()
55 ->GetGpuPlatformSupportHost()
56 ->OnMessageReceived(*msg
);
60 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner_
;
61 base::WeakPtrFactory
<FakeGpuProcess
> weak_factory_
;
64 class FakeGpuProcessHost
{
67 const scoped_refptr
<base::SingleThreadTaskRunner
>& gpu_task_runner
)
68 : gpu_task_runner_(gpu_task_runner
), weak_factory_(this) {}
69 ~FakeGpuProcessHost() {}
72 base::Callback
<void(IPC::Message
*)> sender
=
73 base::Bind(&FakeGpuProcessHost::DispatchToGpuPlatformSupportTask
,
74 weak_factory_
.GetWeakPtr());
76 ui::OzonePlatform::GetInstance()
77 ->GetGpuPlatformSupportHost()
78 ->OnChannelEstablished(kGpuProcessHostId
, gpu_task_runner_
, sender
);
82 void DispatchToGpuPlatformSupportTask(IPC::Message
* msg
) {
83 ui::OzonePlatform::GetInstance()
84 ->GetGpuPlatformSupport()
85 ->OnMessageReceived(*msg
);
89 scoped_refptr
<base::SingleThreadTaskRunner
> gpu_task_runner_
;
90 base::WeakPtrFactory
<FakeGpuProcessHost
> weak_factory_
;
93 OzoneGpuTestHelper::OzoneGpuTestHelper() {
96 OzoneGpuTestHelper::~OzoneGpuTestHelper() {
99 bool OzoneGpuTestHelper::Initialize(
100 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_task_runner
,
101 const scoped_refptr
<base::SingleThreadTaskRunner
>& gpu_task_runner
) {
102 io_helper_thread_
.reset(new base::Thread("IOHelperThread"));
103 if (!io_helper_thread_
->StartWithOptions(
104 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0)))
107 fake_gpu_process_
.reset(new FakeGpuProcess(ui_task_runner
));
108 io_helper_thread_
->task_runner()->PostTask(
109 FROM_HERE
, base::Bind(&FakeGpuProcess::InitOnIO
,
110 base::Unretained(fake_gpu_process_
.get())));
111 fake_gpu_process_
->Init();
113 fake_gpu_process_host_
.reset(new FakeGpuProcessHost(gpu_task_runner
));
114 fake_gpu_process_host_
->Init();