Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / cc / layers / content_layer.cc
1 // Copyright 2010 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/layers/content_layer.h"
6
7 #include "base/auto_reset.h"
8 #include "base/metrics/histogram.h"
9 #include "base/time/time.h"
10 #include "cc/layers/content_layer_client.h"
11 #include "cc/resources/bitmap_content_layer_updater.h"
12 #include "cc/resources/bitmap_skpicture_content_layer_updater.h"
13 #include "cc/resources/layer_painter.h"
14 #include "cc/trees/layer_tree_host.h"
15 #include "third_party/skia/include/core/SkPictureRecorder.h"
16
17 namespace cc {
18
19 ContentLayerPainter::ContentLayerPainter(ContentLayerClient* client)
20     : client_(client) {}
21
22 scoped_ptr<ContentLayerPainter> ContentLayerPainter::Create(
23     ContentLayerClient* client) {
24   return make_scoped_ptr(new ContentLayerPainter(client));
25 }
26
27 void ContentLayerPainter::Paint(SkCanvas* canvas,
28                                 const gfx::Rect& content_rect,
29                                 gfx::RectF* opaque) {
30   client_->PaintContents(canvas,
31                          content_rect,
32                          opaque,
33                          ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
34 }
35
36 scoped_refptr<ContentLayer> ContentLayer::Create(ContentLayerClient* client) {
37   return make_scoped_refptr(new ContentLayer(client));
38 }
39
40 ContentLayer::ContentLayer(ContentLayerClient* client)
41     : TiledLayer(),
42       client_(client),
43       can_use_lcd_text_last_frame_(can_use_lcd_text()) {
44 }
45
46 ContentLayer::~ContentLayer() {}
47
48 void ContentLayer::ClearClient() {
49   client_ = NULL;
50   UpdateDrawsContent(HasDrawableContent());
51 }
52
53 bool ContentLayer::HasDrawableContent() const {
54   return client_ && TiledLayer::HasDrawableContent();
55 }
56
57 void ContentLayer::SetLayerTreeHost(LayerTreeHost* host) {
58   TiledLayer::SetLayerTreeHost(host);
59
60   if (!updater_.get())
61     return;
62
63   if (host) {
64     updater_->set_rendering_stats_instrumentation(
65         host->rendering_stats_instrumentation());
66   } else {
67     updater_->set_rendering_stats_instrumentation(NULL);
68   }
69 }
70
71 void ContentLayer::SetTexturePriorities(
72     const PriorityCalculator& priority_calc) {
73   // Update the tile data before creating all the layer's tiles.
74   UpdateTileSizeAndTilingOption();
75
76   TiledLayer::SetTexturePriorities(priority_calc);
77 }
78
79 bool ContentLayer::Update(ResourceUpdateQueue* queue,
80                           const OcclusionTracker<Layer>* occlusion) {
81   {
82     base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
83                                                   true);
84
85     CreateUpdaterIfNeeded();
86     UpdateCanUseLCDText();
87   }
88
89   bool updated = TiledLayer::Update(queue, occlusion);
90   return updated;
91 }
92
93 bool ContentLayer::NeedMoreUpdates() {
94   return NeedsIdlePaint();
95 }
96
97 LayerUpdater* ContentLayer::Updater() const {
98   return updater_.get();
99 }
100
101 void ContentLayer::CreateUpdaterIfNeeded() {
102   if (updater_.get())
103     return;
104   scoped_ptr<LayerPainter> painter =
105       ContentLayerPainter::Create(client_).PassAs<LayerPainter>();
106   if (layer_tree_host()->settings().per_tile_painting_enabled) {
107     updater_ = BitmapSkPictureContentLayerUpdater::Create(
108         painter.Pass(),
109         rendering_stats_instrumentation(),
110         id());
111   } else {
112     updater_ = BitmapContentLayerUpdater::Create(
113         painter.Pass(),
114         rendering_stats_instrumentation(),
115         id());
116   }
117   updater_->SetOpaque(contents_opaque());
118   if (client_)
119     updater_->SetFillsBoundsCompletely(client_->FillsBoundsCompletely());
120
121   SetTextureFormat(
122       layer_tree_host()->GetRendererCapabilities().best_texture_format);
123 }
124
125 void ContentLayer::SetContentsOpaque(bool opaque) {
126   Layer::SetContentsOpaque(opaque);
127   if (updater_.get())
128     updater_->SetOpaque(opaque);
129 }
130
131 void ContentLayer::UpdateCanUseLCDText() {
132   if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
133     return;
134
135   can_use_lcd_text_last_frame_ = can_use_lcd_text();
136   if (client_)
137     client_->DidChangeLayerCanUseLCDText();
138 }
139
140 bool ContentLayer::SupportsLCDText() const {
141   return true;
142 }
143
144 skia::RefPtr<SkPicture> ContentLayer::GetPicture() const {
145   if (!DrawsContent())
146     return skia::RefPtr<SkPicture>();
147
148   int width = bounds().width();
149   int height = bounds().height();
150   gfx::RectF opaque;
151
152   SkPictureRecorder recorder;
153   SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
154   client_->PaintContents(canvas,
155                          gfx::Rect(width, height),
156                          &opaque,
157                          ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
158   skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
159   return picture;
160 }
161
162 void ContentLayer::OnOutputSurfaceCreated() {
163   SetTextureFormat(
164       layer_tree_host()->GetRendererCapabilities().best_texture_format);
165   TiledLayer::OnOutputSurfaceCreated();
166 }
167
168 }  // namespace cc