summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/platform/drm/gpu/proxy_helpers.h
blob: 086709c9a01b36fbb9347a88424983064e7e2130 (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
// Copyright 2015 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.

#ifndef UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_
#define UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_

#include "base/bind.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_task_runner_handle.h"

namespace ui {

namespace internal {

template <typename... Args>
void PostAsyncTask(
    const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
    const base::Callback<void(Args...)>& callback,
    Args... args) {
  task_runner->PostTask(FROM_HERE, base::Bind(callback, args...));
}

template <typename... Args>
void PostAsyncTaskOnce(
    const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
    base::OnceCallback<void(Args...)> callback,
    Args... args) {
  auto closure = base::BindOnce(std::move(callback), std::move(args)...);
  task_runner->PostTask(FROM_HERE, std::move(closure));
}

}  // namespace internal

// Posts a task to a different thread and blocks waiting for the task to finish
// executing.
void PostSyncTask(
    const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
    const base::Closure& callback);

// Creates a callback that will run |callback| on the calling thread. Useful
// when posting a task on a different thread and expecting a callback when the
// task finished (and the callback needs to run on the original thread).
template <typename... Args>
base::Callback<void(Args...)> CreateSafeCallback(
    const base::Callback<void(Args...)>& callback) {
  return base::Bind(&internal::PostAsyncTask<Args...>,
                    base::ThreadTaskRunnerHandle::Get(), callback);
}

// Creates a OnceCallback that will run |callback| on the calling thread. Useful
// when posting a task on a different thread and expecting a callback when the
// task finished (and the callback needs to run on the original thread).
template <typename... Args>
base::OnceCallback<void(Args...)> CreateSafeOnceCallback(
    base::OnceCallback<void(Args...)> callback) {
  return base::BindOnce(&internal::PostAsyncTaskOnce<Args...>,
                        base::ThreadTaskRunnerHandle::Get(),
                        std::move(callback));
}

}  // namespace ui

#endif  // UI_OZONE_PLATFORM_DRM_GPU_PROXY_HELPERS_H_