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