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