bitmap_content_layer_updater.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
1// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/resources/bitmap_content_layer_updater.h"
6
7#include "cc/debug/devtools_instrumentation.h"
8#include "cc/debug/rendering_stats_instrumentation.h"
9#include "cc/resources/layer_painter.h"
10#include "cc/resources/prioritized_resource.h"
11#include "cc/resources/resource_update.h"
12#include "cc/resources/resource_update_queue.h"
13#include "skia/ext/platform_canvas.h"
14
15namespace cc {
16
17BitmapContentLayerUpdater::Resource::Resource(
18    BitmapContentLayerUpdater* updater,
19    scoped_ptr<PrioritizedResource> texture)
20    : LayerUpdater::Resource(texture.Pass()), updater_(updater) {}
21
22BitmapContentLayerUpdater::Resource::~Resource() {}
23
24void BitmapContentLayerUpdater::Resource::Update(ResourceUpdateQueue* queue,
25                                                 gfx::Rect source_rect,
26                                                 gfx::Vector2d dest_offset,
27                                                 bool partial_update) {
28  updater_->UpdateTexture(
29      queue, texture(), source_rect, dest_offset, partial_update);
30}
31
32scoped_refptr<BitmapContentLayerUpdater> BitmapContentLayerUpdater::Create(
33    scoped_ptr<LayerPainter> painter,
34    RenderingStatsInstrumentation* stats_instrumentation,
35    int layer_id) {
36  return make_scoped_refptr(
37      new BitmapContentLayerUpdater(painter.Pass(),
38                                    stats_instrumentation,
39                                    layer_id));
40}
41
42BitmapContentLayerUpdater::BitmapContentLayerUpdater(
43    scoped_ptr<LayerPainter> painter,
44    RenderingStatsInstrumentation* stats_instrumentation,
45    int layer_id)
46    : ContentLayerUpdater(painter.Pass(), stats_instrumentation, layer_id) {}
47
48BitmapContentLayerUpdater::~BitmapContentLayerUpdater() {}
49
50scoped_ptr<LayerUpdater::Resource> BitmapContentLayerUpdater::CreateResource(
51    PrioritizedResourceManager* manager) {
52  return scoped_ptr<LayerUpdater::Resource>(
53      new Resource(this, PrioritizedResource::Create(manager)));
54}
55
56void BitmapContentLayerUpdater::PrepareToUpdate(
57    gfx::Rect content_rect,
58    gfx::Size tile_size,
59    float contents_width_scale,
60    float contents_height_scale,
61    gfx::Rect* resulting_opaque_rect) {
62  devtools_instrumentation::ScopedLayerTask paint_layer(
63      devtools_instrumentation::kPaintLayer, layer_id_);
64  if (canvas_size_.width() < content_rect.size().width() ||
65      canvas_size_.height() < content_rect.size().height()) {
66    devtools_instrumentation::ScopedLayerTask paint_setup(
67        devtools_instrumentation::kPaintSetup, layer_id_);
68    canvas_size_ = content_rect.size();
69    canvas_ = skia::AdoptRef(skia::CreateBitmapCanvas(
70        canvas_size_.width(), canvas_size_.height(), layer_is_opaque_));
71  }
72
73  base::TimeTicks start_time =
74      rendering_stats_instrumentation_->StartRecording();
75  PaintContents(canvas_.get(),
76                content_rect,
77                contents_width_scale,
78                contents_height_scale,
79                resulting_opaque_rect);
80  base::TimeDelta duration =
81      rendering_stats_instrumentation_->EndRecording(start_time);
82  rendering_stats_instrumentation_->AddPaint(
83      duration,
84      content_rect.width() * content_rect.height());
85}
86
87void BitmapContentLayerUpdater::UpdateTexture(ResourceUpdateQueue* queue,
88                                              PrioritizedResource* texture,
89                                              gfx::Rect source_rect,
90                                              gfx::Vector2d dest_offset,
91                                              bool partial_update) {
92  CHECK(canvas_);
93  gfx::Rect canvas_rect = content_rect();
94  canvas_rect.set_size(canvas_size_);
95  ResourceUpdate upload = ResourceUpdate::CreateFromCanvas(
96      texture, canvas_, canvas_rect, source_rect, dest_offset);
97  if (partial_update)
98    queue->AppendPartialUpload(upload);
99  else
100    queue->AppendFullUpload(upload);
101}
102
103void BitmapContentLayerUpdater::ReduceMemoryUsage() {
104  canvas_.clear();
105  canvas_size_ = gfx::Size();
106}
107
108void BitmapContentLayerUpdater::SetOpaque(bool opaque) {
109  if (opaque != layer_is_opaque_) {
110    canvas_.clear();
111    canvas_size_ = gfx::Size();
112  }
113
114  ContentLayerUpdater::SetOpaque(opaque);
115}
116
117}  // namespace cc
118