8b8558ce7f4e59654cd85c257b19755ec04c469e
[platform/framework/web/crosswalk.git] / src / cc / layers / picture_layer.cc
1 // Copyright 2012 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/picture_layer.h"
6
7 #include "base/auto_reset.h"
8 #include "cc/layers/content_layer_client.h"
9 #include "cc/layers/picture_layer_impl.h"
10 #include "cc/trees/layer_tree_impl.h"
11 #include "third_party/skia/include/core/SkPictureRecorder.h"
12 #include "ui/gfx/rect_conversions.h"
13
14 namespace cc {
15
16 scoped_refptr<PictureLayer> PictureLayer::Create(ContentLayerClient* client) {
17   return make_scoped_refptr(new PictureLayer(client));
18 }
19
20 PictureLayer::PictureLayer(ContentLayerClient* client)
21     : client_(client),
22       pile_(make_scoped_refptr(new PicturePile())),
23       instrumentation_object_tracker_(id()),
24       is_mask_(false),
25       update_source_frame_number_(-1),
26       can_use_lcd_text_last_frame_(can_use_lcd_text()) {
27 }
28
29 PictureLayer::~PictureLayer() {
30 }
31
32 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
33   return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
34 }
35
36 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
37   Layer::PushPropertiesTo(base_layer);
38   PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
39
40   if (layer_impl->bounds().IsEmpty()) {
41     // Update may not get called for an empty layer, so resize here instead.
42     // Using layer_impl because either bounds() or paint_properties().bounds
43     // may disagree and either one could have been pushed to layer_impl.
44     pile_->SetEmptyBounds();
45   } else if (update_source_frame_number_ ==
46              layer_tree_host()->source_frame_number()) {
47     // TODO(ernstm): This DCHECK is only valid as long as the pile's tiling_rect
48     // is identical to the layer_rect.
49     // If update called, then pile size must match bounds pushed to impl layer.
50     DCHECK_EQ(layer_impl->bounds().ToString(), pile_->tiling_size().ToString());
51   }
52
53   layer_impl->SetIsMask(is_mask_);
54
55   // Unlike other properties, invalidation must always be set on layer_impl.
56   // See PictureLayerImpl::PushPropertiesTo for more details.
57   layer_impl->invalidation_.Clear();
58   layer_impl->invalidation_.Swap(&pile_invalidation_);
59   layer_impl->pile_ = PicturePileImpl::CreateFromOther(pile_.get());
60 }
61
62 void PictureLayer::SetLayerTreeHost(LayerTreeHost* host) {
63   Layer::SetLayerTreeHost(host);
64   if (host) {
65     pile_->SetMinContentsScale(host->settings().minimum_contents_scale);
66     pile_->SetTileGridSize(host->settings().default_tile_size);
67     pile_->set_slow_down_raster_scale_factor(
68         host->debug_state().slow_down_raster_scale_factor);
69     pile_->set_show_debug_picture_borders(
70         host->debug_state().show_picture_borders);
71   }
72 }
73
74 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
75   gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
76   if (!rect.IsEmpty()) {
77     // Clamp invalidation to the layer bounds.
78     rect.Intersect(gfx::Rect(bounds()));
79     pending_invalidation_.Union(rect);
80   }
81   Layer::SetNeedsDisplayRect(layer_rect);
82 }
83
84 bool PictureLayer::Update(ResourceUpdateQueue* queue,
85                           const OcclusionTracker<Layer>* occlusion) {
86   update_source_frame_number_ = layer_tree_host()->source_frame_number();
87   bool updated = Layer::Update(queue, occlusion);
88
89   {
90     base::AutoReset<bool> ignore_set_needs_commit(&ignore_set_needs_commit_,
91                                                   true);
92     UpdateCanUseLCDText();
93   }
94
95   gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
96       visible_content_rect(), 1.f / contents_scale_x());
97   gfx::Size layer_size = paint_properties().bounds;
98
99   if (last_updated_visible_content_rect_ == visible_content_rect() &&
100       pile_->tiling_size() == layer_size && pending_invalidation_.IsEmpty()) {
101     // Only early out if the visible content rect of this layer hasn't changed.
102     return updated;
103   }
104
105   TRACE_EVENT1("cc", "PictureLayer::Update",
106                "source_frame_number",
107                layer_tree_host()->source_frame_number());
108   devtools_instrumentation::ScopedLayerTreeTask update_layer(
109       devtools_instrumentation::kUpdateLayer, id(), layer_tree_host()->id());
110
111   // Calling paint in WebKit can sometimes cause invalidations, so save
112   // off the invalidation prior to calling update.
113   pending_invalidation_.Swap(&pile_invalidation_);
114   pending_invalidation_.Clear();
115
116   if (layer_tree_host()->settings().record_full_layer) {
117     // Workaround for http://crbug.com/235910 - to retain backwards compat
118     // the full page content must always be provided in the picture layer.
119     visible_layer_rect = gfx::Rect(layer_size);
120   }
121
122   // UpdateAndExpandInvalidation will give us an invalidation that covers
123   // anything not explicitly recorded in this frame. We give this region
124   // to the impl side so that it drops tiles that may not have a recording
125   // for them.
126   DCHECK(client_);
127   updated |=
128       pile_->UpdateAndExpandInvalidation(client_,
129                                          &pile_invalidation_,
130                                          SafeOpaqueBackgroundColor(),
131                                          contents_opaque(),
132                                          client_->FillsBoundsCompletely(),
133                                          layer_size,
134                                          visible_layer_rect,
135                                          update_source_frame_number_,
136                                          RecordingMode(),
137                                          rendering_stats_instrumentation());
138   last_updated_visible_content_rect_ = visible_content_rect();
139
140   if (updated) {
141     SetNeedsPushProperties();
142   } else {
143     // If this invalidation did not affect the pile, then it can be cleared as
144     // an optimization.
145     pile_invalidation_.Clear();
146   }
147
148   return updated;
149 }
150
151 void PictureLayer::SetIsMask(bool is_mask) {
152   is_mask_ = is_mask;
153 }
154
155 Picture::RecordingMode PictureLayer::RecordingMode() const {
156   switch (layer_tree_host()->settings().recording_mode) {
157     case LayerTreeSettings::RecordNormally:
158       return Picture::RECORD_NORMALLY;
159     case LayerTreeSettings::RecordWithSkRecord:
160       return Picture::RECORD_WITH_SKRECORD;
161   }
162   NOTREACHED();
163   return Picture::RECORD_NORMALLY;
164 }
165
166 bool PictureLayer::SupportsLCDText() const {
167   return true;
168 }
169
170 void PictureLayer::UpdateCanUseLCDText() {
171   if (can_use_lcd_text_last_frame_ == can_use_lcd_text())
172     return;
173
174   can_use_lcd_text_last_frame_ = can_use_lcd_text();
175   if (client_)
176     client_->DidChangeLayerCanUseLCDText();
177 }
178
179 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
180   // We could either flatten the PicturePile into a single SkPicture,
181   // or paint a fresh one depending on what we intend to do with the
182   // picture. For now we just paint a fresh one to get consistent results.
183   if (!DrawsContent())
184     return skia::RefPtr<SkPicture>();
185
186   int width = bounds().width();
187   int height = bounds().height();
188   gfx::RectF opaque;
189
190   SkPictureRecorder recorder;
191   SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
192   client_->PaintContents(canvas,
193                          gfx::Rect(width, height),
194                          &opaque,
195                          ContentLayerClient::GRAPHICS_CONTEXT_ENABLED);
196   skia::RefPtr<SkPicture> picture = skia::AdoptRef(recorder.endRecording());
197   return picture;
198 }
199
200 bool PictureLayer::IsSuitableForGpuRasterization() const {
201   return pile_->is_suitable_for_gpu_rasterization();
202 }
203
204 void PictureLayer::ClearClient() {
205   client_ = NULL;
206   UpdateDrawsContent(HasDrawableContent());
207 }
208
209 bool PictureLayer::HasDrawableContent() const {
210   return client_ && Layer::HasDrawableContent();
211 }
212
213 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
214   benchmark->RunOnLayer(this);
215 }
216
217 }  // namespace cc