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
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128

cc / trees / task_runner_provider.h [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_TREES_TASK_RUNNER_PROVIDER_H_
#define CC_TREES_TASK_RUNNER_PROVIDER_H_

#include <memory>
#include <string>

#include "base/check.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "cc/cc_export.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace cc {

// Class responsible for controlling access to the main and impl task runners.
// Useful for assertion checks.
class CC_EXPORT TaskRunnerProvider {
 public:
  static std::unique_ptr<TaskRunnerProvider> Create(
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
      scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) {
    return base::WrapUnique(
        new TaskRunnerProvider(main_task_runner, impl_task_runner));
  }

  static std::unique_ptr<TaskRunnerProvider> CreateForDisplayTree(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
    auto provider = Create(task_runner, task_runner);
    provider->for_display_tree_ = true;
    return provider;
  }

  TaskRunnerProvider(const TaskRunnerProvider&) = delete;
  TaskRunnerProvider& operator=(const TaskRunnerProvider&) = delete;

  // TODO(vmpstr): Should these return scoped_refptr to task runners? Many
  // places turn them into scoped_refptrs. How many of them need to?
  base::SingleThreadTaskRunner* MainThreadTaskRunner() const;
  bool HasImplThread() const;
  base::SingleThreadTaskRunner* ImplThreadTaskRunner() const;

  // Debug hooks.
  bool IsMainThread() const;
  bool IsImplThread() const;
  bool IsMainThreadBlocked() const;
#if DCHECK_IS_ON()
  void SetMainThreadBlocked(bool is_main_thread_blocked);
  void SetCurrentThreadIsImplThread(bool is_impl_thread);
#endif

  virtual ~TaskRunnerProvider();

 protected:
  TaskRunnerProvider(
      scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
      scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);

  friend class DebugScopedSetImplThread;
  friend class DebugScopedSetMainThread;
  friend class DebugScopedSetMainThreadBlocked;

 private:
  scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner_;

#if DCHECK_IS_ON()
  const base::PlatformThreadId main_thread_id_;
  bool impl_thread_is_overridden_;
  bool is_main_thread_blocked_;
#endif

  // For GPU-side display trees, TaskRunnerProvider treats the main thread as
  // both the "main" thread and the "impl" thread to placate various assertions
  // about which thread is doing what inside the layer tree. This is a
  // sufficient adaptation since the display tree consists only of a manually
  // driven LayerTreeHostImpl on a single thread with no Proxy or Scheduler.
  bool for_display_tree_ = false;
};

#if DCHECK_IS_ON()
class DebugScopedSetMainThreadBlocked {
 public:
  explicit DebugScopedSetMainThreadBlocked(
      TaskRunnerProvider* task_runner_provider)
      : task_runner_provider_(task_runner_provider) {
    DCHECK(!task_runner_provider_->IsMainThreadBlocked());
    task_runner_provider_->SetMainThreadBlocked(true);
  }
  DebugScopedSetMainThreadBlocked(const DebugScopedSetMainThreadBlocked&) =
      delete;
  ~DebugScopedSetMainThreadBlocked() {
    DCHECK(task_runner_provider_->IsMainThreadBlocked());
    task_runner_provider_->SetMainThreadBlocked(false);
  }

  DebugScopedSetMainThreadBlocked& operator=(
      const DebugScopedSetMainThreadBlocked&) = delete;

 private:
  raw_ptr<TaskRunnerProvider> task_runner_provider_;
};
#else
class DebugScopedSetMainThreadBlocked {
 public:
  explicit DebugScopedSetMainThreadBlocked(
      TaskRunnerProvider* task_runner_provider) {}
  DebugScopedSetMainThreadBlocked(const DebugScopedSetMainThreadBlocked&) =
      delete;
  ~DebugScopedSetMainThreadBlocked() {}

  DebugScopedSetMainThreadBlocked& operator=(
      const DebugScopedSetMainThreadBlocked&) = delete;
};
#endif

}  // namespace cc

#endif  // CC_TREES_TASK_RUNNER_PROVIDER_H_