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
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257

cc / raster / task_graph_work_queue_unittest.cc [blame]

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

#include "cc/raster/task_graph_work_queue.h"

#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
namespace {

class FakeTaskImpl : public Task {
 public:
  FakeTaskImpl() = default;
  FakeTaskImpl(const FakeTaskImpl&) = delete;

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

  // Overridden from Task:
  void RunOnWorkerThread() override {}

 private:
  ~FakeTaskImpl() override = default;
};

TEST(TaskGraphWorkQueueTest, TestChangingDependency) {
  TaskGraphWorkQueue work_queue;
  NamespaceToken token = work_queue.GenerateNamespaceToken();

  // Create a graph with one task
  TaskGraph graph1;
  scoped_refptr<FakeTaskImpl> task(new FakeTaskImpl());
  graph1.nodes.push_back(TaskGraph::Node(task.get(), 0u, 0u, 0u));

  // Schedule the graph
  work_queue.ScheduleTasks(token, &graph1);

  // Run the task.
  TaskGraphWorkQueue::PrioritizedTask prioritized_task =
      work_queue.GetNextTaskToRun(0u);
  work_queue.CompleteTask(std::move(prioritized_task));

  // Create a graph where task1 has a dependency
  TaskGraph graph2;
  scoped_refptr<FakeTaskImpl> dependency_task(new FakeTaskImpl());
  graph2.nodes.push_back(TaskGraph::Node(task.get(), 0u, 0u, 1u));
  graph2.nodes.push_back(TaskGraph::Node(dependency_task.get(), 0u, 0u, 0u));
  graph2.edges.push_back(TaskGraph::Edge(dependency_task.get(), task.get()));

  // Schedule the second graph.
  work_queue.ScheduleTasks(token, &graph2);

  // Run the |dependency_task|
  TaskGraphWorkQueue::PrioritizedTask prioritized_dependency_task =
      work_queue.GetNextTaskToRun(0u);
  EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task.get());
  work_queue.CompleteTask(std::move(prioritized_dependency_task));

  // We should have no tasks to run, as the dependent task already completed.
  EXPECT_FALSE(work_queue.HasReadyToRunTasks());
}

// Tasks with same priority but in different category.
TEST(TaskGraphWorkQueueTest, TestTaskWithDifferentCategory) {
  TaskGraphWorkQueue work_queue;
  NamespaceToken token = work_queue.GenerateNamespaceToken();

  // Create a graph where | task| has dependencies.
  TaskGraph graph;
  scoped_refptr<FakeTaskImpl> task(new FakeTaskImpl());
  scoped_refptr<FakeTaskImpl> dependency_task1(new FakeTaskImpl());
  scoped_refptr<FakeTaskImpl> dependency_task2(new FakeTaskImpl());
  scoped_refptr<FakeTaskImpl> dependency_task3(new FakeTaskImpl());

  graph.nodes.push_back(TaskGraph::Node(task.get(), 0u, 0u, 3u));
  graph.nodes.push_back(TaskGraph::Node(dependency_task1.get(), 0u, 0u, 0u));
  graph.nodes.push_back(TaskGraph::Node(dependency_task2.get(), 1u, 0u, 0u));
  graph.nodes.push_back(TaskGraph::Node(dependency_task3.get(), 2u, 0u, 0u));

  graph.edges.push_back(TaskGraph::Edge(dependency_task1.get(), task.get()));
  graph.edges.push_back(TaskGraph::Edge(dependency_task2.get(), task.get()));
  graph.edges.push_back(TaskGraph::Edge(dependency_task3.get(), task.get()));

  // Schedule the graph.
  work_queue.ScheduleTasks(token, &graph);

  // Run the |dependency_task1|from category 0.
  TaskGraphWorkQueue::PrioritizedTask prioritized_dependency_task =
      work_queue.GetNextTaskToRun(0u);
  EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task1.get());
  work_queue.CompleteTask(std::move(prioritized_dependency_task));
  EXPECT_TRUE(work_queue.HasReadyToRunTasks());
  EXPECT_FALSE(work_queue.HasReadyToRunTasksForCategory(0u));
  EXPECT_TRUE(work_queue.HasReadyToRunTasksForCategory(1u));
  EXPECT_TRUE(work_queue.HasReadyToRunTasksForCategory(2u));

  // Run the |dependency_task2|from category 1.
  prioritized_dependency_task = work_queue.GetNextTaskToRun(1u);
  EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task2.get());
  work_queue.CompleteTask(std::move(prioritized_dependency_task));
  EXPECT_TRUE(work_queue.HasReadyToRunTasks());
  EXPECT_FALSE(work_queue.HasReadyToRunTasksForCategory(0u));
  EXPECT_FALSE(work_queue.HasReadyToRunTasksForCategory(1u));
  EXPECT_TRUE(work_queue.HasReadyToRunTasksForCategory(2u));

  // Run the |dependency_task3|from category 2.
  prioritized_dependency_task = work_queue.GetNextTaskToRun(2u);
  EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task3.get());
  work_queue.CompleteTask(std::move(prioritized_dependency_task));
  EXPECT_TRUE(work_queue.HasReadyToRunTasks());
  // Once all dependencies from different category completed, | task| turns
  // ready to run.
  EXPECT_TRUE(work_queue.HasReadyToRunTasksForCategory(0u));
  EXPECT_FALSE(work_queue.HasReadyToRunTasksForCategory(1u));
  EXPECT_FALSE(work_queue.HasReadyToRunTasksForCategory(2u));

  prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
  EXPECT_EQ(prioritized_dependency_task.task.get(), task.get());
  work_queue.CompleteTask(std::move(prioritized_dependency_task));
  EXPECT_FALSE(work_queue.HasReadyToRunTasks());
}

// Tasks with different priority run in a priority order. But need to guarantee
// its dependences are completed.
TEST(TaskGraphWorkQueueTest, TestTaskWithDifferentPriority) {
  TaskGraphWorkQueue work_queue;
  NamespaceToken token = work_queue.GenerateNamespaceToken();
  {
    // Create a graph where task has a dependency
    TaskGraph graph;
    scoped_refptr<FakeTaskImpl> task(new FakeTaskImpl());
    scoped_refptr<FakeTaskImpl> dependency_task1(new FakeTaskImpl());
    scoped_refptr<FakeTaskImpl> dependency_task2(new FakeTaskImpl());
    scoped_refptr<FakeTaskImpl> dependency_task3(new FakeTaskImpl());

    // | task| has the lowest priority and 3 dependences, will run last.
    graph.nodes.push_back(TaskGraph::Node(task.get(), 0u, 2u, 3u));
    graph.nodes.push_back(TaskGraph::Node(dependency_task1.get(), 0u, 3u, 0u));
    graph.nodes.push_back(TaskGraph::Node(dependency_task2.get(), 0u, 2u, 0u));
    graph.nodes.push_back(TaskGraph::Node(dependency_task3.get(), 0u, 1u, 0u));

    graph.edges.push_back(TaskGraph::Edge(dependency_task1.get(), task.get()));
    graph.edges.push_back(TaskGraph::Edge(dependency_task2.get(), task.get()));
    graph.edges.push_back(TaskGraph::Edge(dependency_task3.get(), task.get()));

    // Schedule the graph.
    work_queue.ScheduleTasks(token, &graph);

    // Run the |dependency_task|
    TaskGraphWorkQueue::PrioritizedTask prioritized_dependency_task =
        work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task3.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_TRUE(work_queue.HasReadyToRunTasks());

    prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task2.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_TRUE(work_queue.HasReadyToRunTasks());

    prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task1.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_TRUE(work_queue.HasReadyToRunTasks());

    // | task| runs last.
    prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), task.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_FALSE(work_queue.HasReadyToRunTasks());
  }

  {
    // Create a graph where task has dependencies
    TaskGraph graph;
    scoped_refptr<FakeTaskImpl> task(new FakeTaskImpl());
    scoped_refptr<FakeTaskImpl> dependency_task1(new FakeTaskImpl());
    scoped_refptr<FakeTaskImpl> dependency_task2(new FakeTaskImpl());
    scoped_refptr<FakeTaskImpl> dependency_task3(new FakeTaskImpl());

    // | task| has the highest priority and 3 dependences, also will run last.
    graph.nodes.push_back(TaskGraph::Node(task.get(), 0u, 0u, 3u));
    graph.nodes.push_back(TaskGraph::Node(dependency_task1.get(), 0u, 3u, 0u));
    graph.nodes.push_back(TaskGraph::Node(dependency_task2.get(), 0u, 2u, 0u));
    graph.nodes.push_back(TaskGraph::Node(dependency_task3.get(), 0u, 1u, 0u));

    graph.edges.push_back(TaskGraph::Edge(dependency_task1.get(), task.get()));
    graph.edges.push_back(TaskGraph::Edge(dependency_task2.get(), task.get()));
    graph.edges.push_back(TaskGraph::Edge(dependency_task3.get(), task.get()));

    // Schedule the graph.
    work_queue.ScheduleTasks(token, &graph);

    // Run the |dependency_task|
    TaskGraphWorkQueue::PrioritizedTask prioritized_dependency_task =
        work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task3.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_TRUE(work_queue.HasReadyToRunTasks());

    prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task2.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_TRUE(work_queue.HasReadyToRunTasks());

    prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), dependency_task1.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_TRUE(work_queue.HasReadyToRunTasks());

    // | task| runs last.
    prioritized_dependency_task = work_queue.GetNextTaskToRun(0u);
    EXPECT_EQ(prioritized_dependency_task.task.get(), task.get());
    work_queue.CompleteTask(std::move(prioritized_dependency_task));
    EXPECT_FALSE(work_queue.HasReadyToRunTasks());
  }
}

TEST(TaskGraphWorkQueueTest, ExternalDependency) {
  TaskGraphWorkQueue work_queue;
  NamespaceToken token = work_queue.GenerateNamespaceToken();

  // Create a graph where task has an external dependency
  TaskGraph graph;
  scoped_refptr<FakeTaskImpl> task(new FakeTaskImpl());

  uint16_t category = 5u;
  uint16_t priority = 7u;

  graph.nodes.emplace_back(task, category, priority, 1u /*dependencies*/,
                           true /*has_external_dependency*/);
  work_queue.ScheduleTasks(token, &graph);
  auto* task_namespace = work_queue.GetNamespaceForToken(token);
  EXPECT_FALSE(TaskGraphWorkQueue::DependencyMismatch(&graph));
  EXPECT_FALSE(work_queue.HasReadyToRunTasks());
  EXPECT_FALSE(work_queue.HasFinishedRunningTasksInAllNamespaces());
  EXPECT_TRUE(
      TaskGraphWorkQueue::HasTasksBlockedOnExternalDependencyInNamespace(
          task_namespace));

  // Complete external dependency; `task` should be ready to run.
  work_queue.ExternalDependencyCompletedForTask(token, task);
  EXPECT_FALSE(TaskGraphWorkQueue::DependencyMismatch(&graph));
  EXPECT_TRUE(work_queue.HasReadyToRunTasks());
  EXPECT_FALSE(
      TaskGraphWorkQueue::HasTasksBlockedOnExternalDependencyInNamespace(
          task_namespace));
  TaskGraphWorkQueue::PrioritizedTask prioritized_dependency_task =
      work_queue.GetNextTaskToRun(category);
  EXPECT_EQ(prioritized_dependency_task.task.get(), task.get());

  work_queue.CompleteTask(std::move(prioritized_dependency_task));
}

}  // namespace
}  // namespace cc