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