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