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.
5 #include "cc/resources/bitmap_content_layer_updater.h"
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"
17 BitmapContentLayerUpdater::Resource::Resource(
18 BitmapContentLayerUpdater* updater,
19 scoped_ptr<PrioritizedResource> texture)
20 : LayerUpdater::Resource(texture.Pass()), updater_(updater) {}
22 BitmapContentLayerUpdater::Resource::~Resource() {}
24 void 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);
32 scoped_refptr<BitmapContentLayerUpdater> BitmapContentLayerUpdater::Create(
33 scoped_ptr<LayerPainter> painter,
34 RenderingStatsInstrumentation* stats_instrumentation,
36 return make_scoped_refptr(
37 new BitmapContentLayerUpdater(painter.Pass(),
38 stats_instrumentation,
42 BitmapContentLayerUpdater::BitmapContentLayerUpdater(
43 scoped_ptr<LayerPainter> painter,
44 RenderingStatsInstrumentation* stats_instrumentation,
46 : ContentLayerUpdater(painter.Pass(), stats_instrumentation, layer_id) {}
48 BitmapContentLayerUpdater::~BitmapContentLayerUpdater() {}
50 scoped_ptr<LayerUpdater::Resource> BitmapContentLayerUpdater::CreateResource(
51 PrioritizedResourceManager* manager) {
52 return scoped_ptr<LayerUpdater::Resource>(
53 new Resource(this, PrioritizedResource::Create(manager)));
56 void BitmapContentLayerUpdater::PrepareToUpdate(
57 gfx::Rect content_rect,
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_));
73 base::TimeTicks start_time =
74 rendering_stats_instrumentation_->StartRecording();
75 PaintContents(canvas_.get(),
78 contents_height_scale,
79 resulting_opaque_rect);
80 base::TimeDelta duration =
81 rendering_stats_instrumentation_->EndRecording(start_time);
82 rendering_stats_instrumentation_->AddPaint(
84 content_rect.width() * content_rect.height());
87 void BitmapContentLayerUpdater::UpdateTexture(ResourceUpdateQueue* queue,
88 PrioritizedResource* texture,
89 gfx::Rect source_rect,
90 gfx::Vector2d dest_offset,
91 bool partial_update) {
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);
98 queue->AppendPartialUpload(upload);
100 queue->AppendFullUpload(upload);
103 void BitmapContentLayerUpdater::ReduceMemoryUsage() {
105 canvas_size_ = gfx::Size();
108 void BitmapContentLayerUpdater::SetOpaque(bool opaque) {
109 if (opaque != layer_is_opaque_) {
111 canvas_size_ = gfx::Size();
114 ContentLayerUpdater::SetOpaque(opaque);