• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 namespace cc {
16 
Resource(BitmapContentLayerUpdater * updater,scoped_ptr<PrioritizedResource> texture)17 BitmapContentLayerUpdater::Resource::Resource(
18     BitmapContentLayerUpdater* updater,
19     scoped_ptr<PrioritizedResource> texture)
20     : LayerUpdater::Resource(texture.Pass()), updater_(updater) {}
21 
~Resource()22 BitmapContentLayerUpdater::Resource::~Resource() {}
23 
Update(ResourceUpdateQueue * queue,gfx::Rect source_rect,gfx::Vector2d dest_offset,bool partial_update)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);
30 }
31 
Create(scoped_ptr<LayerPainter> painter,RenderingStatsInstrumentation * stats_instrumentation,int layer_id)32 scoped_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 
BitmapContentLayerUpdater(scoped_ptr<LayerPainter> painter,RenderingStatsInstrumentation * stats_instrumentation,int layer_id)42 BitmapContentLayerUpdater::BitmapContentLayerUpdater(
43     scoped_ptr<LayerPainter> painter,
44     RenderingStatsInstrumentation* stats_instrumentation,
45     int layer_id)
46     : ContentLayerUpdater(painter.Pass(), stats_instrumentation, layer_id) {}
47 
~BitmapContentLayerUpdater()48 BitmapContentLayerUpdater::~BitmapContentLayerUpdater() {}
49 
CreateResource(PrioritizedResourceManager * manager)50 scoped_ptr<LayerUpdater::Resource> BitmapContentLayerUpdater::CreateResource(
51     PrioritizedResourceManager* manager) {
52   return scoped_ptr<LayerUpdater::Resource>(
53       new Resource(this, PrioritizedResource::Create(manager)));
54 }
55 
PrepareToUpdate(gfx::Rect content_rect,gfx::Size tile_size,float contents_width_scale,float contents_height_scale,gfx::Rect * resulting_opaque_rect)56 void 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   if (canvas_size_ != content_rect.size()) {
63     devtools_instrumentation::ScopedLayerTask paint_setup(
64         devtools_instrumentation::kPaintSetup, layer_id_);
65     canvas_size_ = content_rect.size();
66     bitmap_backing_.setConfig(
67         SkBitmap::kARGB_8888_Config,
68         canvas_size_.width(), canvas_size_.height(),
69         0, layer_is_opaque_ ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
70     bitmap_backing_.allocPixels();
71     canvas_ = skia::AdoptRef(new SkCanvas(bitmap_backing_));
72   }
73 
74   base::TimeTicks start_time =
75       rendering_stats_instrumentation_->StartRecording();
76   PaintContents(canvas_.get(),
77                 content_rect.origin(),
78                 contents_width_scale,
79                 contents_height_scale,
80                 resulting_opaque_rect);
81   base::TimeDelta duration =
82       rendering_stats_instrumentation_->EndRecording(start_time);
83   rendering_stats_instrumentation_->AddPaint(
84       duration,
85       content_rect.width() * content_rect.height());
86 }
87 
UpdateTexture(ResourceUpdateQueue * queue,PrioritizedResource * texture,gfx::Rect source_rect,gfx::Vector2d dest_offset,bool partial_update)88 void BitmapContentLayerUpdater::UpdateTexture(ResourceUpdateQueue* queue,
89                                               PrioritizedResource* texture,
90                                               gfx::Rect source_rect,
91                                               gfx::Vector2d dest_offset,
92                                               bool partial_update) {
93   CHECK(canvas_);
94   ResourceUpdate upload =
95       ResourceUpdate::CreateFromCanvas(texture,
96                                        canvas_,
97                                        content_rect(),
98                                        source_rect,
99                                        dest_offset);
100   if (partial_update)
101     queue->AppendPartialUpload(upload);
102   else
103     queue->AppendFullUpload(upload);
104 }
105 
ReduceMemoryUsage()106 void BitmapContentLayerUpdater::ReduceMemoryUsage() {
107   canvas_.clear();
108   canvas_size_ = gfx::Size();
109 }
110 
SetOpaque(bool opaque)111 void BitmapContentLayerUpdater::SetOpaque(bool opaque) {
112   if (opaque != layer_is_opaque_) {
113     canvas_.clear();
114     canvas_size_ = gfx::Size();
115   }
116 
117   ContentLayerUpdater::SetOpaque(opaque);
118 }
119 
120 }  // namespace cc
121