Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / webkit / renderer / compositor_bindings / web_layer_impl.cc
1 // Copyright 2011 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 "webkit/renderer/compositor_bindings/web_layer_impl.h"
6
7 #include "base/bind.h"
8 #include "base/debug/trace_event_impl.h"
9 #include "base/lazy_instance.h"
10 #include "base/strings/string_util.h"
11 #include "base/threading/thread_checker.h"
12 #include "cc/animation/animation.h"
13 #include "cc/base/region.h"
14 #include "cc/base/switches.h"
15 #include "cc/layers/layer.h"
16 #include "cc/layers/layer_position_constraint.h"
17 #include "cc/trees/layer_tree_host.h"
18 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
19 #include "third_party/WebKit/public/platform/WebFloatRect.h"
20 #include "third_party/WebKit/public/platform/WebGraphicsLayerDebugInfo.h"
21 #include "third_party/WebKit/public/platform/WebLayerClient.h"
22 #include "third_party/WebKit/public/platform/WebLayerPositionConstraint.h"
23 #include "third_party/WebKit/public/platform/WebLayerScrollClient.h"
24 #include "third_party/WebKit/public/platform/WebSize.h"
25 #include "third_party/skia/include/utils/SkMatrix44.h"
26 #include "webkit/renderer/compositor_bindings/web_animation_impl.h"
27 #include "webkit/renderer/compositor_bindings/web_blend_mode.h"
28 #include "webkit/renderer/compositor_bindings/web_filter_operations_impl.h"
29 #include "webkit/renderer/compositor_bindings/web_to_cc_animation_delegate_adapter.h"
30
31 using cc::Animation;
32 using cc::Layer;
33 using blink::WebLayer;
34 using blink::WebFloatPoint;
35 using blink::WebVector;
36 using blink::WebRect;
37 using blink::WebSize;
38 using blink::WebColor;
39 using blink::WebFilterOperations;
40
41 namespace webkit {
42 namespace {
43
44 bool g_impl_side_painting_enabled = false;
45
46 }  // namespace
47
48 WebLayerImpl::WebLayerImpl() : layer_(Layer::Create()) {
49   web_layer_client_ = NULL;
50   layer_->SetLayerClient(this);
51 }
52
53 WebLayerImpl::WebLayerImpl(scoped_refptr<Layer> layer) : layer_(layer) {
54   web_layer_client_ = NULL;
55   layer_->SetLayerClient(this);
56 }
57
58 WebLayerImpl::~WebLayerImpl() {
59   layer_->ClearRenderSurface();
60   layer_->set_layer_animation_delegate(NULL);
61   web_layer_client_ = NULL;
62 }
63
64 // static
65 bool WebLayerImpl::UsingPictureLayer() {
66   return g_impl_side_painting_enabled;
67 }
68
69 // static
70 void WebLayerImpl::SetImplSidePaintingEnabled(bool enabled) {
71   g_impl_side_painting_enabled = enabled;
72 }
73
74 int WebLayerImpl::id() const { return layer_->id(); }
75
76 void WebLayerImpl::invalidateRect(const blink::WebFloatRect& rect) {
77   layer_->SetNeedsDisplayRect(rect);
78 }
79
80 void WebLayerImpl::invalidate() { layer_->SetNeedsDisplay(); }
81
82 void WebLayerImpl::addChild(WebLayer* child) {
83   layer_->AddChild(static_cast<WebLayerImpl*>(child)->layer());
84 }
85
86 void WebLayerImpl::insertChild(WebLayer* child, size_t index) {
87   layer_->InsertChild(static_cast<WebLayerImpl*>(child)->layer(), index);
88 }
89
90 void WebLayerImpl::replaceChild(WebLayer* reference, WebLayer* new_layer) {
91   layer_->ReplaceChild(static_cast<WebLayerImpl*>(reference)->layer(),
92                        static_cast<WebLayerImpl*>(new_layer)->layer());
93 }
94
95 void WebLayerImpl::removeFromParent() { layer_->RemoveFromParent(); }
96
97 void WebLayerImpl::removeAllChildren() { layer_->RemoveAllChildren(); }
98
99 void WebLayerImpl::setAnchorPoint(const WebFloatPoint& anchor_point) {
100   layer_->SetAnchorPoint(anchor_point);
101 }
102
103 WebFloatPoint WebLayerImpl::anchorPoint() const {
104   return layer_->anchor_point();
105 }
106
107 void WebLayerImpl::setAnchorPointZ(float anchor_point_z) {
108   layer_->SetAnchorPointZ(anchor_point_z);
109 }
110
111 float WebLayerImpl::anchorPointZ() const { return layer_->anchor_point_z(); }
112
113 void WebLayerImpl::setBounds(const WebSize& size) { layer_->SetBounds(size); }
114
115 WebSize WebLayerImpl::bounds() const { return layer_->bounds(); }
116
117 void WebLayerImpl::setMasksToBounds(bool masks_to_bounds) {
118   layer_->SetMasksToBounds(masks_to_bounds);
119 }
120
121 bool WebLayerImpl::masksToBounds() const { return layer_->masks_to_bounds(); }
122
123 void WebLayerImpl::setMaskLayer(WebLayer* maskLayer) {
124   layer_->SetMaskLayer(
125       maskLayer ? static_cast<WebLayerImpl*>(maskLayer)->layer() : 0);
126 }
127
128 void WebLayerImpl::setReplicaLayer(WebLayer* replica_layer) {
129   layer_->SetReplicaLayer(
130       replica_layer ? static_cast<WebLayerImpl*>(replica_layer)->layer() : 0);
131 }
132
133 void WebLayerImpl::setOpacity(float opacity) { layer_->SetOpacity(opacity); }
134
135 float WebLayerImpl::opacity() const { return layer_->opacity(); }
136
137 void WebLayerImpl::setBlendMode(blink::WebBlendMode blend_mode) {
138   layer_->SetBlendMode(BlendModeToSkia(blend_mode));
139 }
140
141 blink::WebBlendMode WebLayerImpl::blendMode() const {
142   return BlendModeFromSkia(layer_->blend_mode());
143 }
144
145 void WebLayerImpl::setIsRootForIsolatedGroup(bool isolate) {
146   layer_->SetIsRootForIsolatedGroup(isolate);
147 }
148
149 bool WebLayerImpl::isRootForIsolatedGroup() {
150   return layer_->is_root_for_isolated_group();
151 }
152
153 void WebLayerImpl::setOpaque(bool opaque) { layer_->SetContentsOpaque(opaque); }
154
155 bool WebLayerImpl::opaque() const { return layer_->contents_opaque(); }
156
157 void WebLayerImpl::setPosition(const WebFloatPoint& position) {
158   layer_->SetPosition(position);
159 }
160
161 WebFloatPoint WebLayerImpl::position() const { return layer_->position(); }
162
163 void WebLayerImpl::setTransform(const SkMatrix44& matrix) {
164   gfx::Transform transform;
165   transform.matrix() = matrix;
166   layer_->SetTransform(transform);
167 }
168
169 SkMatrix44 WebLayerImpl::transform() const {
170   return layer_->transform().matrix();
171 }
172
173 void WebLayerImpl::setDrawsContent(bool draws_content) {
174   layer_->SetIsDrawable(draws_content);
175 }
176
177 bool WebLayerImpl::drawsContent() const { return layer_->DrawsContent(); }
178
179 void WebLayerImpl::setShouldFlattenTransform(bool flatten) {
180   layer_->SetShouldFlattenTransform(flatten);
181 }
182
183 void WebLayerImpl::setRenderingContext(int context) {
184   layer_->SetIs3dSorted(context != 0);
185 }
186
187 void WebLayerImpl::setUseParentBackfaceVisibility(
188     bool use_parent_backface_visibility) {
189   layer_->set_use_parent_backface_visibility(use_parent_backface_visibility);
190 }
191
192 void WebLayerImpl::setBackgroundColor(WebColor color) {
193   layer_->SetBackgroundColor(color);
194 }
195
196 WebColor WebLayerImpl::backgroundColor() const {
197   return layer_->background_color();
198 }
199
200 void WebLayerImpl::setFilters(const WebFilterOperations& filters) {
201   const WebFilterOperationsImpl& filters_impl =
202       static_cast<const WebFilterOperationsImpl&>(filters);
203   layer_->SetFilters(filters_impl.AsFilterOperations());
204 }
205
206 void WebLayerImpl::setBackgroundFilters(const WebFilterOperations& filters) {
207   const WebFilterOperationsImpl& filters_impl =
208       static_cast<const WebFilterOperationsImpl&>(filters);
209   layer_->SetBackgroundFilters(filters_impl.AsFilterOperations());
210 }
211
212 void WebLayerImpl::setAnimationDelegate(
213       blink::WebAnimationDelegate* delegate) {
214   animation_delegate_adapter_.reset(
215       new WebToCCAnimationDelegateAdapter(delegate));
216   layer_->set_layer_animation_delegate(animation_delegate_adapter_.get());
217 }
218
219 bool WebLayerImpl::addAnimation(blink::WebAnimation* animation) {
220   bool result = layer_->AddAnimation(
221       static_cast<WebAnimationImpl*>(animation)->PassAnimation());
222   delete animation;
223   return result;
224 }
225
226 void WebLayerImpl::removeAnimation(int animation_id) {
227   layer_->RemoveAnimation(animation_id);
228 }
229
230 void WebLayerImpl::removeAnimation(
231     int animation_id,
232     blink::WebAnimation::TargetProperty target_property) {
233   layer_->layer_animation_controller()->RemoveAnimation(
234       animation_id,
235       static_cast<Animation::TargetProperty>(target_property));
236 }
237
238 void WebLayerImpl::pauseAnimation(int animation_id, double time_offset) {
239   layer_->PauseAnimation(animation_id, time_offset);
240 }
241
242 bool WebLayerImpl::hasActiveAnimation() { return layer_->HasActiveAnimation(); }
243
244 void WebLayerImpl::setForceRenderSurface(bool force_render_surface) {
245   layer_->SetForceRenderSurface(force_render_surface);
246 }
247
248 void WebLayerImpl::setScrollPosition(blink::WebPoint position) {
249   layer_->SetScrollOffset(gfx::Point(position).OffsetFromOrigin());
250 }
251
252 blink::WebPoint WebLayerImpl::scrollPosition() const {
253   return gfx::PointAtOffsetFromOrigin(layer_->scroll_offset());
254 }
255
256 void WebLayerImpl::setScrollClipLayer(WebLayer* clip_layer) {
257   if (!clip_layer) {
258     layer_->SetScrollClipLayerId(Layer::INVALID_ID);
259     return;
260   }
261   layer_->SetScrollClipLayerId(clip_layer->id());
262 }
263
264 bool WebLayerImpl::scrollable() const { return layer_->scrollable(); }
265
266 void WebLayerImpl::setUserScrollable(bool horizontal, bool vertical) {
267   layer_->SetUserScrollable(horizontal, vertical);
268 }
269
270 bool WebLayerImpl::userScrollableHorizontal() const {
271   return layer_->user_scrollable_horizontal();
272 }
273
274 bool WebLayerImpl::userScrollableVertical() const {
275   return layer_->user_scrollable_vertical();
276 }
277
278 void WebLayerImpl::setHaveWheelEventHandlers(bool have_wheel_event_handlers) {
279   layer_->SetHaveWheelEventHandlers(have_wheel_event_handlers);
280 }
281
282 bool WebLayerImpl::haveWheelEventHandlers() const {
283   return layer_->have_wheel_event_handlers();
284 }
285
286 void WebLayerImpl::setHaveScrollEventHandlers(bool have_scroll_event_handlers) {
287   layer_->SetHaveScrollEventHandlers(have_scroll_event_handlers);
288 }
289
290 bool WebLayerImpl::haveScrollEventHandlers() const {
291   return layer_->have_scroll_event_handlers();
292 }
293
294 void WebLayerImpl::setShouldScrollOnMainThread(
295     bool should_scroll_on_main_thread) {
296   layer_->SetShouldScrollOnMainThread(should_scroll_on_main_thread);
297 }
298
299 bool WebLayerImpl::shouldScrollOnMainThread() const {
300   return layer_->should_scroll_on_main_thread();
301 }
302
303 void WebLayerImpl::setNonFastScrollableRegion(const WebVector<WebRect>& rects) {
304   cc::Region region;
305   for (size_t i = 0; i < rects.size(); ++i)
306     region.Union(rects[i]);
307   layer_->SetNonFastScrollableRegion(region);
308 }
309
310 WebVector<WebRect> WebLayerImpl::nonFastScrollableRegion() const {
311   size_t num_rects = 0;
312   for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
313        region_rects.has_rect();
314        region_rects.next())
315     ++num_rects;
316
317   WebVector<WebRect> result(num_rects);
318   size_t i = 0;
319   for (cc::Region::Iterator region_rects(layer_->non_fast_scrollable_region());
320        region_rects.has_rect();
321        region_rects.next()) {
322     result[i] = region_rects.rect();
323     ++i;
324   }
325   return result;
326 }
327
328 void WebLayerImpl::setTouchEventHandlerRegion(const WebVector<WebRect>& rects) {
329   cc::Region region;
330   for (size_t i = 0; i < rects.size(); ++i)
331     region.Union(rects[i]);
332   layer_->SetTouchEventHandlerRegion(region);
333 }
334
335 WebVector<WebRect> WebLayerImpl::touchEventHandlerRegion() const {
336   size_t num_rects = 0;
337   for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
338        region_rects.has_rect();
339        region_rects.next())
340     ++num_rects;
341
342   WebVector<WebRect> result(num_rects);
343   size_t i = 0;
344   for (cc::Region::Iterator region_rects(layer_->touch_event_handler_region());
345        region_rects.has_rect();
346        region_rects.next()) {
347     result[i] = region_rects.rect();
348     ++i;
349   }
350   return result;
351 }
352
353 void WebLayerImpl::setIsContainerForFixedPositionLayers(bool enable) {
354   layer_->SetIsContainerForFixedPositionLayers(enable);
355 }
356
357 bool WebLayerImpl::isContainerForFixedPositionLayers() const {
358   return layer_->IsContainerForFixedPositionLayers();
359 }
360
361 static blink::WebLayerPositionConstraint ToWebLayerPositionConstraint(
362     const cc::LayerPositionConstraint& constraint) {
363   blink::WebLayerPositionConstraint web_constraint;
364   web_constraint.isFixedPosition = constraint.is_fixed_position();
365   web_constraint.isFixedToRightEdge = constraint.is_fixed_to_right_edge();
366   web_constraint.isFixedToBottomEdge = constraint.is_fixed_to_bottom_edge();
367   return web_constraint;
368 }
369
370 static cc::LayerPositionConstraint ToLayerPositionConstraint(
371     const blink::WebLayerPositionConstraint& web_constraint) {
372   cc::LayerPositionConstraint constraint;
373   constraint.set_is_fixed_position(web_constraint.isFixedPosition);
374   constraint.set_is_fixed_to_right_edge(web_constraint.isFixedToRightEdge);
375   constraint.set_is_fixed_to_bottom_edge(web_constraint.isFixedToBottomEdge);
376   return constraint;
377 }
378
379 void WebLayerImpl::setPositionConstraint(
380     const blink::WebLayerPositionConstraint& constraint) {
381   layer_->SetPositionConstraint(ToLayerPositionConstraint(constraint));
382 }
383
384 blink::WebLayerPositionConstraint WebLayerImpl::positionConstraint() const {
385   return ToWebLayerPositionConstraint(layer_->position_constraint());
386 }
387
388 void WebLayerImpl::setScrollClient(
389     blink::WebLayerScrollClient* scroll_client) {
390   if (scroll_client) {
391     layer_->set_did_scroll_callback(
392         base::Bind(&blink::WebLayerScrollClient::didScroll,
393                    base::Unretained(scroll_client)));
394   } else {
395     layer_->set_did_scroll_callback(base::Closure());
396   }
397 }
398
399 bool WebLayerImpl::isOrphan() const { return !layer_->layer_tree_host(); }
400
401 void WebLayerImpl::setWebLayerClient(blink::WebLayerClient* client) {
402   web_layer_client_ = client;
403 }
404
405 class TracedDebugInfo : public base::debug::ConvertableToTraceFormat {
406  public:
407   // This object takes ownership of the debug_info object.
408   explicit TracedDebugInfo(blink::WebGraphicsLayerDebugInfo* debug_info) :
409     debug_info_(debug_info) {}
410   virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
411     DCHECK(thread_checker_.CalledOnValidThread());
412     blink::WebString web_string;
413     debug_info_->appendAsTraceFormat(&web_string);
414     out->append(web_string.utf8());
415   }
416  private:
417   virtual ~TracedDebugInfo() {}
418   scoped_ptr<blink::WebGraphicsLayerDebugInfo> debug_info_;
419   base::ThreadChecker thread_checker_;
420 };
421
422 scoped_refptr<base::debug::ConvertableToTraceFormat>
423     WebLayerImpl::TakeDebugInfo() {
424   if (!web_layer_client_)
425     return NULL;
426   blink::WebGraphicsLayerDebugInfo* debug_info =
427       web_layer_client_->takeDebugInfoFor(this);
428
429   if (debug_info)
430     return new TracedDebugInfo(debug_info);
431   else
432     return NULL;
433 }
434
435 void WebLayerImpl::setScrollParent(blink::WebLayer* parent) {
436   cc::Layer* scroll_parent = NULL;
437   if (parent)
438     scroll_parent = static_cast<WebLayerImpl*>(parent)->layer();
439   layer_->SetScrollParent(scroll_parent);
440 }
441
442 void WebLayerImpl::setClipParent(blink::WebLayer* parent) {
443   cc::Layer* clip_parent = NULL;
444   if (parent)
445     clip_parent = static_cast<WebLayerImpl*>(parent)->layer();
446   layer_->SetClipParent(clip_parent);
447 }
448
449 Layer* WebLayerImpl::layer() const { return layer_.get(); }
450
451 }  // namespace webkit