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"
18 ContentLayerPainter::ContentLayerPainter(ContentLayerClient
* 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
,
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(),
45 UMA_HISTOGRAM_CUSTOM_COUNTS("Renderer4.AccelContentPaintMegapixPerSecond",
46 pixels_per_sec
/ 1000000,
52 scoped_refptr
<ContentLayer
> ContentLayer::Create(ContentLayerClient
* client
) {
53 return make_scoped_refptr(new ContentLayer(client
));
56 ContentLayer::ContentLayer(ContentLayerClient
* 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
);
75 updater_
->set_rendering_stats_instrumentation(
76 host
->rendering_stats_instrumentation());
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_
,
96 CreateUpdaterIfNeeded();
97 UpdateCanUseLCDText();
100 bool updated
= TiledLayer::Update(queue
, occlusion
);
104 bool ContentLayer::NeedMoreUpdates() {
105 return NeedsIdlePaint();
108 LayerUpdater
* ContentLayer::Updater() const {
109 return updater_
.get();
112 void ContentLayer::CreateUpdaterIfNeeded() {
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(
120 rendering_stats_instrumentation(),
123 updater_
= BitmapContentLayerUpdater::Create(
125 rendering_stats_instrumentation(),
128 updater_
->SetOpaque(contents_opaque());
131 layer_tree_host()->GetRendererCapabilities().best_texture_format
);
134 void ContentLayer::SetContentsOpaque(bool opaque
) {
135 Layer::SetContentsOpaque(opaque
);
137 updater_
->SetOpaque(opaque
);
140 void ContentLayer::UpdateCanUseLCDText() {
141 if (can_use_lcd_text_last_frame_
== can_use_lcd_text())
144 can_use_lcd_text_last_frame_
= can_use_lcd_text();
146 client_
->DidChangeLayerCanUseLCDText();
149 bool ContentLayer::SupportsLCDText() const {
153 skia::RefPtr
<SkPicture
> ContentLayer::GetPicture() const {
155 return skia::RefPtr
<SkPicture
>();
157 int width
= bounds().width();
158 int height
= bounds().height();
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();
168 void ContentLayer::OnOutputSurfaceCreated() {
170 layer_tree_host()->GetRendererCapabilities().best_texture_format
);
171 TiledLayer::OnOutputSurfaceCreated();