- add sources.
[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/debug/devtools_instrumentation.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 "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
27 PictureLayer::~PictureLayer() {
28 }
29
30 bool PictureLayer::DrawsContent() const {
31   return Layer::DrawsContent() && client_;
32 }
33
34 scoped_ptr<LayerImpl> PictureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
35   return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
36 }
37
38 void PictureLayer::PushPropertiesTo(LayerImpl* base_layer) {
39   Layer::PushPropertiesTo(base_layer);
40   PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
41
42   if (layer_impl->bounds().IsEmpty()) {
43     // Update may not get called for an empty layer, so resize here instead.
44     // Using layer_impl because either bounds() or paint_properties().bounds
45     // may disagree and either one could have been pushed to layer_impl.
46     pile_->Resize(gfx::Size());
47     pile_->UpdateRecordedRegion();
48   } else if (update_source_frame_number_ ==
49              layer_tree_host()->source_frame_number()) {
50     // If update called, then pile size must match bounds pushed to impl layer.
51     DCHECK_EQ(layer_impl->bounds().ToString(), pile_->size().ToString());
52   }
53
54   layer_impl->SetIsMask(is_mask_);
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_num_raster_threads(host->settings().num_raster_threads);
68     pile_->set_slow_down_raster_scale_factor(
69         host->debug_state().slow_down_raster_scale_factor);
70     pile_->set_show_debug_picture_borders(
71         host->debug_state().show_picture_borders);
72   }
73 }
74
75 void PictureLayer::SetNeedsDisplayRect(const gfx::RectF& layer_rect) {
76   gfx::Rect rect = gfx::ToEnclosedRect(layer_rect);
77   if (!rect.IsEmpty()) {
78     // Clamp invalidation to the layer bounds.
79     rect.Intersect(gfx::Rect(bounds()));
80     pending_invalidation_.Union(rect);
81   }
82   Layer::SetNeedsDisplayRect(layer_rect);
83 }
84
85 bool PictureLayer::Update(ResourceUpdateQueue* queue,
86                           const OcclusionTracker* occlusion) {
87   // Do not early-out of this function so that PicturePile::Update has a chance
88   // to record pictures due to changing visibility of this layer.
89
90   TRACE_EVENT1("cc", "PictureLayer::Update",
91                "source_frame_number",
92                layer_tree_host()->source_frame_number());
93
94   update_source_frame_number_ = layer_tree_host()->source_frame_number();
95   bool updated = Layer::Update(queue, occlusion);
96
97   pile_->Resize(paint_properties().bounds);
98
99   // Calling paint in WebKit can sometimes cause invalidations, so save
100   // off the invalidation prior to calling update.
101   pending_invalidation_.Swap(&pile_invalidation_);
102   pending_invalidation_.Clear();
103
104   gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
105       visible_content_rect(), 1.f / contents_scale_x());
106   if (layer_tree_host()->settings().using_synchronous_renderer_compositor) {
107     // Workaround for http://crbug.com/235910 - to retain backwards compat
108     // the full page content must always be provided in the picture layer.
109     visible_layer_rect = gfx::Rect(bounds());
110   }
111   devtools_instrumentation::ScopedLayerTask paint_layer(
112       devtools_instrumentation::kPaintLayer, id());
113   updated |= pile_->Update(client_,
114                            SafeOpaqueBackgroundColor(),
115                            contents_opaque(),
116                            pile_invalidation_,
117                            visible_layer_rect,
118                            rendering_stats_instrumentation());
119   if (updated) {
120     SetNeedsPushProperties();
121   } else {
122     // If this invalidation did not affect the pile, then it can be cleared as
123     // an optimization.
124     pile_invalidation_.Clear();
125   }
126
127   return updated;
128 }
129
130 void PictureLayer::SetIsMask(bool is_mask) {
131   is_mask_ = is_mask;
132 }
133
134 bool PictureLayer::SupportsLCDText() const {
135   return true;
136 }
137
138 skia::RefPtr<SkPicture> PictureLayer::GetPicture() const {
139   // We could either flatten the PicturePile into a single SkPicture,
140   // or paint a fresh one depending on what we intend to do with the
141   // picture. For now we just paint a fresh one to get consistent results.
142   if (!DrawsContent())
143     return skia::RefPtr<SkPicture>();
144
145   int width = bounds().width();
146   int height = bounds().height();
147   gfx::RectF opaque;
148
149   skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
150   SkCanvas* canvas = picture->beginRecording(width, height);
151   client_->PaintContents(canvas, gfx::Rect(width, height), &opaque);
152   picture->endRecording();
153   return picture;
154 }
155
156 void PictureLayer::RunMicroBenchmark(MicroBenchmark* benchmark) {
157   benchmark->RunOnLayer(this);
158 }
159
160 }  // namespace cc