• 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,const gfx::Rect & source_rect,const gfx::Vector2d & dest_offset,bool partial_update)24 void BitmapContentLayerUpdater::Resource::Update(
25     ResourceUpdateQueue* queue,
26     const gfx::Rect& source_rect,
27     const gfx::Vector2d& dest_offset,
28     bool partial_update) {
29   updater_->UpdateTexture(
30       queue, texture(), source_rect, dest_offset, partial_update);
31 }
32 
Create(scoped_ptr<LayerPainter> painter,RenderingStatsInstrumentation * stats_instrumentation,int layer_id)33 scoped_refptr<BitmapContentLayerUpdater> BitmapContentLayerUpdater::Create(
34     scoped_ptr<LayerPainter> painter,
35     RenderingStatsInstrumentation* stats_instrumentation,
36     int layer_id) {
37   return make_scoped_refptr(
38       new BitmapContentLayerUpdater(painter.Pass(),
39                                     stats_instrumentation,
40                                     layer_id));
41 }
42 
BitmapContentLayerUpdater(scoped_ptr<LayerPainter> painter,RenderingStatsInstrumentation * stats_instrumentation,int layer_id)43 BitmapContentLayerUpdater::BitmapContentLayerUpdater(
44     scoped_ptr<LayerPainter> painter,
45     RenderingStatsInstrumentation* stats_instrumentation,
46     int layer_id)
47     : ContentLayerUpdater(painter.Pass(), stats_instrumentation, layer_id) {}
48 
~BitmapContentLayerUpdater()49 BitmapContentLayerUpdater::~BitmapContentLayerUpdater() {}
50 
CreateResource(PrioritizedResourceManager * manager)51 scoped_ptr<LayerUpdater::Resource> BitmapContentLayerUpdater::CreateResource(
52     PrioritizedResourceManager* manager) {
53   return scoped_ptr<LayerUpdater::Resource>(
54       new Resource(this, PrioritizedResource::Create(manager)));
55 }
56 
PrepareToUpdate(const gfx::Rect & content_rect,const gfx::Size & tile_size,float contents_width_scale,float contents_height_scale,gfx::Rect * resulting_opaque_rect)57 void BitmapContentLayerUpdater::PrepareToUpdate(
58     const gfx::Rect& content_rect,
59     const gfx::Size& tile_size,
60     float contents_width_scale,
61     float contents_height_scale,
62     gfx::Rect* resulting_opaque_rect) {
63   if (canvas_size_ != content_rect.size()) {
64     devtools_instrumentation::ScopedLayerTask paint_setup(
65         devtools_instrumentation::kPaintSetup, layer_id_);
66     canvas_size_ = content_rect.size();
67     bool alloc = bitmap_backing_.allocN32Pixels(
68         canvas_size_.width(), canvas_size_.height(), layer_is_opaque_);
69     // TODO(danak): Remove when skia does the check for us: crbug.com/360384
70     CHECK(alloc);
71     canvas_ = skia::AdoptRef(new SkCanvas(bitmap_backing_));
72     DCHECK_EQ(content_rect.width(), canvas_->getBaseLayerSize().width());
73     DCHECK_EQ(content_rect.height(), canvas_->getBaseLayerSize().height());
74   }
75 
76   base::TimeTicks start_time =
77       rendering_stats_instrumentation_->StartRecording();
78   PaintContents(canvas_.get(),
79                 content_rect,
80                 contents_width_scale,
81                 contents_height_scale,
82                 resulting_opaque_rect);
83   base::TimeDelta duration =
84       rendering_stats_instrumentation_->EndRecording(start_time);
85   rendering_stats_instrumentation_->AddPaint(
86       duration,
87       content_rect.width() * content_rect.height());
88 }
89 
UpdateTexture(ResourceUpdateQueue * queue,PrioritizedResource * texture,const gfx::Rect & source_rect,const gfx::Vector2d & dest_offset,bool partial_update)90 void BitmapContentLayerUpdater::UpdateTexture(ResourceUpdateQueue* queue,
91                                               PrioritizedResource* texture,
92                                               const gfx::Rect& source_rect,
93                                               const gfx::Vector2d& dest_offset,
94                                               bool partial_update) {
95   CHECK(canvas_);
96   ResourceUpdate upload = ResourceUpdate::Create(texture,
97                                                  &bitmap_backing_,
98                                                  content_rect(),
99                                                  source_rect,
100                                                  dest_offset);
101   if (partial_update)
102     queue->AppendPartialUpload(upload);
103   else
104     queue->AppendFullUpload(upload);
105 }
106 
ReduceMemoryUsage()107 void BitmapContentLayerUpdater::ReduceMemoryUsage() {
108   canvas_.clear();
109   canvas_size_ = gfx::Size();
110 }
111 
SetOpaque(bool opaque)112 void BitmapContentLayerUpdater::SetOpaque(bool opaque) {
113   if (opaque != layer_is_opaque_) {
114     canvas_.clear();
115     canvas_size_ = gfx::Size();
116   }
117 
118   ContentLayerUpdater::SetOpaque(opaque);
119 }
120 
121 }  // namespace cc
122