1 // Copyright 2015 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 #ifndef UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_
6 #define UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_
7 
8 #include <utility>
9 
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread_task_runner_handle.h"
15 
16 namespace ui {
17 
18 namespace internal {
19 
20 template <typename... Args>
PostAsyncTask(const scoped_refptr<base::SingleThreadTaskRunner> & task_runner,base::OnceCallback<void (Args...)> callback,Args...args)21 void PostAsyncTask(
22     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
23     base::OnceCallback<void(Args...)> callback,
24     Args... args) {
25   auto closure = base::BindOnce(std::move(callback), std::move(args)...);
26   task_runner->PostTask(FROM_HERE, std::move(closure));
27 }
28 
29 }  // namespace internal
30 
31 // Posts a task to a different thread and blocks waiting for the task to finish
32 // executing.
33 void PostSyncTask(
34     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
35     base::OnceCallback<void(base::WaitableEvent*)> callback);
36 
37 // Creates a RepeatingCallback that will run |callback| on the calling thread.
38 // Useful when posting a task on a different thread and expecting a callback
39 // when the task finished (and the callback needs to run on the original
40 // thread).
41 template <typename... Args>
CreateSafeRepeatingCallback(base::RepeatingCallback<void (Args...)> callback)42 base::RepeatingCallback<void(Args...)> CreateSafeRepeatingCallback(
43     base::RepeatingCallback<void(Args...)> callback) {
44   return base::BindRepeating(&internal::PostAsyncTask<Args...>,
45                              base::ThreadTaskRunnerHandle::Get(),
46                              std::move(callback));
47 }
48 
49 // Creates a OnceCallback that will run |callback| on the calling thread. Useful
50 // when posting a task on a different thread and expecting a callback when the
51 // task finished (and the callback needs to run on the original thread).
52 template <typename... Args>
CreateSafeOnceCallback(base::OnceCallback<void (Args...)> callback)53 base::OnceCallback<void(Args...)> CreateSafeOnceCallback(
54     base::OnceCallback<void(Args...)> callback) {
55   return base::BindOnce(&internal::PostAsyncTask<Args...>,
56                         base::ThreadTaskRunnerHandle::Get(),
57                         std::move(callback));
58 }
59 
60 }  // namespace ui
61 
62 #endif  // UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_
63