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