Pass gfx::Rect and gfx::RectF by const ref.
[chromium-blink-merge.git] / cc / layers / content_layer.cc
blob858597771774f93bea2b87f5c582b6527d9b6ca5
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.
5 #include "cc/layers/content_layer.h"
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"
16 namespace cc {
18 ContentLayerPainter::ContentLayerPainter(ContentLayerClient* client)
19 : client_(client) {}
21 scoped_ptr<ContentLayerPainter> ContentLayerPainter::Create(
22 ContentLayerClient* client) {
23 return make_scoped_ptr(new ContentLayerPainter(client));
26 void ContentLayerPainter::Paint(SkCanvas* canvas,
27 const gfx::Rect& content_rect,
28 gfx::RectF* opaque) {
29 base::TimeTicks paint_start = base::TimeTicks::HighResNow();
30 client_->PaintContents(canvas, content_rect, opaque);
31 base::TimeTicks paint_end = base::TimeTicks::HighResNow();
32 // The start and end times might be the same if the paint was very fast or if
33 // our timer granularity is poor. Treat this as a very short time duration
34 // instead of none to avoid dividing by zero.
35 if (paint_end == paint_start)
36 paint_end += base::TimeDelta::FromMicroseconds(1);
38 double pixels_per_sec = (content_rect.width() * content_rect.height()) /
39 (paint_end - paint_start).InSecondsF();
40 UMA_HISTOGRAM_CUSTOM_COUNTS("Renderer4.AccelContentPaintDurationMS",
41 (paint_end - paint_start).InMilliseconds(),
43 120,
44 30);
45 UMA_HISTOGRAM_CUSTOM_COUNTS("Renderer4.AccelContentPaintMegapixPerSecond",
46 pixels_per_sec / 1000000,
47 10,
48 210,
49 30);
52 scoped_refptr<ContentLayer> ContentLayer::Create(ContentLayerClient* client) {
53 return make_scoped_refptr(new ContentLayer(client));
56 ContentLayer::ContentLayer(ContentLayerClient* client)
57 : TiledLayer(),
58 client_(client),
59 can_use_lcd_text_last_frame_(can_use_lcd_text()) {
62 ContentLayer::~ContentLayer() {}
64 bool ContentLayer::DrawsContent() const {
65 return TiledLayer::DrawsContent() && client_;
68 void ContentLayer::SetLayerTreeHost(LayerTreeHost* host) {
69 TiledLayer::SetLayerTreeHost(host);
71 if (!updater_.get())
72 return;
74 if (host) {
75 updater_->set_rendering_stats_instrumentation(
76 host->rendering_stats_instrumentation());
77 } else {
78 updater_->set_rendering_stats_instrumentation(NULL);
82 void ContentLayer::SetTexturePriorities(
83 const PriorityCalculator& priority_calc) {
84 // Update the tile data before creating all the layer's tiles.
85 UpdateTileSizeAndTilingOption();
87 TiledLayer::SetTexturePriorities(priority_calc);
90 bool ContentLayer::Update(ResourceUpdateQueue* queue,
91 const OcclusionTracker* occlusion) {
93 base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
94 true);
96 CreateUpdaterIfNeeded();
97 UpdateCanUseLCDText();
100 bool updated = TiledLayer::Update(queue, occlusion);
101 return updated;
104 bool ContentLayer::NeedMoreUpdates() {
105 return NeedsIdlePaint();
108 LayerUpdater* ContentLayer::Updater() const {
109 return updater_.get();
112 void ContentLayer::CreateUpdaterIfNeeded() {
113 if (updater_.get())
114 return;
115 scoped_ptr<LayerPainter> painter =
116 ContentLayerPainter::Create(client_).PassAs<LayerPainter>();
117 if (layer_tree_host()->settings().per_tile_painting_enabled) {
118 updater_ = BitmapSkPictureContentLayerUpdater::Create(
119 painter.Pass(),
120 rendering_stats_instrumentation(),
121 id());
122 } else {
123 updater_ = BitmapContentLayerUpdater::Create(
124 painter.Pass(),
125 rendering_stats_instrumentation(),
126 id());
128 updater_->SetOpaque(contents_opaque());
130 SetTextureFormat(
131 layer_tree_host()->GetRendererCapabilities().best_texture_format);
134 void ContentLayer::SetContentsOpaque(bool opaque) {
135 Layer::SetContentsOpaque(opaque);
136 if (updater_.get())
137 updater_->SetOpaque(opaque);
140 void ContentLayer::UpdateCanUseLCDText() {
141 if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
142 return;
144 can_use_lcd_text_last_frame_ = can_use_lcd_text();
145 if (client_)
146 client_->DidChangeLayerCanUseLCDText();
149 bool ContentLayer::SupportsLCDText() const {
150 return true;
153 skia::RefPtr<SkPicture> ContentLayer::GetPicture() const {
154 if (!DrawsContent())
155 return skia::RefPtr<SkPicture>();
157 int width = bounds().width();
158 int height = bounds().height();
159 gfx::RectF opaque;
161 skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
162 SkCanvas* canvas = picture->beginRecording(width, height);
163 client_->PaintContents(canvas, gfx::Rect(width, height), &opaque);
164 picture->endRecording();
165 return picture;
168 void ContentLayer::OnOutputSurfaceCreated() {
169 SetTextureFormat(
170 layer_tree_host()->GetRendererCapabilities().best_texture_format);
171 TiledLayer::OnOutputSurfaceCreated();
174 } // namespace cc