summaryrefslogtreecommitdiffstats
path: root/cc/resources/image_raster_worker_pool.cc
blob: 4ce87df8bb3ad53526e69d42d4fcb79f5c9fc6dc (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
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
// Copyright 2013 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.

#include "cc/resources/image_raster_worker_pool.h"

#include "base/debug/trace_event.h"
#include "cc/resources/resource.h"
#include "third_party/skia/include/core/SkDevice.h"

namespace cc {

namespace {

class ImageWorkerPoolTaskImpl : public internal::WorkerPoolTask {
 public:
  typedef base::Callback<void(bool was_canceled)> Reply;

  ImageWorkerPoolTaskImpl(internal::RasterWorkerPoolTask* task,
                          TaskVector* dependencies,
                          uint8_t* buffer,
                          int stride,
                          const Reply& reply)
      : internal::WorkerPoolTask(dependencies),
        task_(task),
        buffer_(buffer),
        stride_(stride),
        reply_(reply) {
  }

  // Overridden from internal::WorkerPoolTask:
  virtual void RunOnThread(unsigned thread_index) OVERRIDE {
    DCHECK(buffer_);
    SkBitmap bitmap;
    bitmap.setConfig(SkBitmap::kARGB_8888_Config,
                     task_->resource()->size().width(),
                     task_->resource()->size().height(),
                     stride_);
    bitmap.setPixels(buffer_);
    SkDevice device(bitmap);
    task_->RunOnThread(&device, thread_index);
  }
  virtual void DispatchCompletionCallback() OVERRIDE {
    reply_.Run(!HasFinishedRunning());
  }

 private:
  virtual ~ImageWorkerPoolTaskImpl() {}

  scoped_refptr<internal::RasterWorkerPoolTask> task_;
  uint8_t* buffer_;
  int stride_;
  const Reply reply_;

  DISALLOW_COPY_AND_ASSIGN(ImageWorkerPoolTaskImpl);
};

}  // namespace

ImageRasterWorkerPool::ImageRasterWorkerPool(
    ResourceProvider* resource_provider, size_t num_threads)
    : RasterWorkerPool(resource_provider, num_threads) {
  // TODO(reveman): Remove WorkerPool client interface.
  WorkerPool::SetClient(this);
}

ImageRasterWorkerPool::~ImageRasterWorkerPool() {
  DCHECK_EQ(0u, image_tasks_.size());
}

void ImageRasterWorkerPool::ScheduleTasks(RasterTask::Queue* queue) {
  TRACE_EVENT0("cc", "ImageRasterWorkerPool::ScheduleTasks");

  internal::WorkerPoolTask::TaskVector tasks;

  RasterWorkerPool::SetRasterTasks(queue);

  for (RasterTask::Queue::TaskVector::const_iterator it =
           raster_tasks().begin();
       it != raster_tasks().end(); ++it) {
    internal::RasterWorkerPoolTask* task = *it;

    TaskMap::iterator image_it = image_tasks_.find(task);
    if (image_it != image_tasks_.end()) {
      internal::WorkerPoolTask* image_task = image_it->second;
      tasks.push_back(image_task);
      continue;
    }

    // Acquire image for resource.
    resource_provider()->AcquireImage(task->resource()->id());

    // Map image for raster.
    uint8* buffer = resource_provider()->MapImage(task->resource()->id());
    int stride = resource_provider()->GetImageStride(task->resource()->id());

    // TODO(reveman): Avoid having to make a copy of dependencies.
    internal::WorkerPoolTask::TaskVector dependencies = task->dependencies();
    scoped_refptr<internal::WorkerPoolTask> image_task(
        new ImageWorkerPoolTaskImpl(
            task,
            &dependencies,
            buffer,
            stride,
            base::Bind(&ImageRasterWorkerPool::OnRasterTaskCompleted,
                       base::Unretained(this),
                       make_scoped_refptr(task))));

    image_tasks_[task] = image_task;
    tasks.push_back(image_task);
  }

  if (tasks.empty()) {
    ScheduleRasterTasks(RootTask());
    return;
  }

  RootTask root(&tasks);
  ScheduleRasterTasks(root);
}

void ImageRasterWorkerPool::OnRasterTaskCompleted(
    scoped_refptr<internal::RasterWorkerPoolTask> task,
    bool was_canceled) {
  TRACE_EVENT1("cc", "ImageRasterWorkerPool::OnRasterTaskCompleted",
               "was_canceled", was_canceled);

  DCHECK(image_tasks_.find(task) != image_tasks_.end());

  // Balanced with MapImage() call in ScheduleTasks().
  resource_provider()->UnmapImage(task->resource()->id());

  // Bind image to resource.
  resource_provider()->BindImage(task->resource()->id());

  if (!was_canceled)
    task->DidRun();

  DidCompleteRasterTask(task);
  image_tasks_.erase(task);
}

void ImageRasterWorkerPool::DidCompleteRasterTask(
    internal::RasterWorkerPoolTask* task) {
  task->DidComplete();
  task->DispatchCompletionCallback();
}

}  // namespace cc