summaryrefslogtreecommitdiffstats
path: root/cc/resources/image_raster_worker_pool.cc
blob: c2fe39506fa34cfaebe81a4448a8e67a54136fdd (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
// 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,
                          uint8_t* buffer,
                          int stride,
                          const Reply& reply)
      : task_(task),
        buffer_(buffer),
        stride_(stride),
        reply_(reply) {
  }

  // Overridden from internal::WorkerPoolTask:
  virtual void RunOnThread(unsigned thread_index) OVERRIDE {
    if (!buffer_)
      return;

    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) {
}

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

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

  RasterWorkerPool::SetRasterTasks(queue);

  RasterTaskGraph graph;
  for (RasterTaskVector::const_iterator it = raster_tasks().begin();
       it != raster_tasks().end(); ++it) {
    internal::RasterWorkerPoolTask* task = it->get();

    TaskMap::iterator image_it = image_tasks_.find(task);
    if (image_it != image_tasks_.end()) {
      internal::WorkerPoolTask* image_task = image_it->second.get();
      graph.InsertRasterTask(image_task, task->dependencies());
      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());

    scoped_refptr<internal::WorkerPoolTask> new_image_task(
        new ImageWorkerPoolTaskImpl(
            task,
            buffer,
            stride,
            base::Bind(&ImageRasterWorkerPool::OnRasterTaskCompleted,
                       base::Unretained(this),
                       make_scoped_refptr(task))));
    image_tasks_[task] = new_image_task;
    graph.InsertRasterTask(new_image_task.get(), task->dependencies());
  }

  SetRasterTaskGraph(&graph);
}

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.get()) != 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());

  task->DidRun(was_canceled);
  task->DidComplete();
  task->DispatchCompletionCallback();

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

}  // namespace cc