Fix the bug that fixed element has wrong size
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebPage / LayerTreeCoordinator / WebGraphicsLayer.cpp
1 /*
2  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Library General Public License for more details.
13
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB.  If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21
22 #if USE(UI_SIDE_COMPOSITING)
23 #include "WebGraphicsLayer.h"
24
25 #include "BackingStore.h"
26 #include "FloatQuad.h"
27 #include "Frame.h"
28 #include "FrameView.h"
29 #include "GraphicsContext.h"
30 #include "GraphicsLayer.h"
31 #include "LayerTreeCoordinatorProxyMessages.h"
32 #include "Page.h"
33 #include "TextureMapperPlatformLayer.h"
34 #include "TiledBackingStoreRemoteTile.h"
35 #include "SharedPlatformSurfaceTizen.h"
36 #include "WebPage.h"
37 #include <wtf/CurrentTime.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/text/CString.h>
40
41 using namespace WebKit;
42
43 namespace WebCore {
44
45 static HashMap<WebLayerID, WebGraphicsLayer*>& layerByIDMap()
46 {
47     static HashMap<WebLayerID, WebGraphicsLayer*> globalMap;
48     return globalMap;
49 }
50
51 WebGraphicsLayer* WebGraphicsLayer::layerByID(WebKit::WebLayerID id)
52 {
53     HashMap<WebLayerID, WebGraphicsLayer*>& table = layerByIDMap();
54     HashMap<WebLayerID, WebGraphicsLayer*>::iterator it = table.find(id);
55     if (it == table.end())
56         return 0;
57     return it->second;
58 }
59
60 static WebLayerID toWebLayerID(GraphicsLayer* layer)
61 {
62     return layer ? toWebGraphicsLayer(layer)->id() : 0;
63 }
64
65 void WebGraphicsLayer::didChangeLayerState()
66 {
67     m_shouldSyncLayerState = true;
68     if (client())
69         client()->notifySyncRequired(this);
70 }
71
72 void WebGraphicsLayer::didChangeAnimations()
73 {
74     m_shouldSyncAnimations = true;
75     if (client())
76         client()->notifySyncRequired(this);
77 }
78
79 void WebGraphicsLayer::didChangeChildren()
80 {
81     m_shouldSyncChildren = true;
82     if (client())
83         client()->notifySyncRequired(this);
84 }
85
86 #if ENABLE(CSS_FILTERS)
87 void WebGraphicsLayer::didChangeFilters()
88 {
89     m_shouldSyncFilters = true;
90     if (client())
91         client()->notifySyncRequired(this);
92 }
93 #endif
94
95 void WebGraphicsLayer::setShouldUpdateVisibleRect()
96 {
97     m_shouldUpdateVisibleRect = true;
98     for (size_t i = 0; i < children().size(); ++i)
99         toWebGraphicsLayer(children()[i])->setShouldUpdateVisibleRect();
100     if (replicaLayer())
101         toWebGraphicsLayer(replicaLayer())->setShouldUpdateVisibleRect();
102 }
103
104 void WebGraphicsLayer::didChangeGeometry()
105 {
106     didChangeLayerState();
107     setShouldUpdateVisibleRect();
108 }
109
110 WebGraphicsLayer::WebGraphicsLayer(GraphicsLayerClient* client)
111     : GraphicsLayer(client)
112     , m_maskTarget(0)
113     , m_inUpdateMode(false)
114     , m_shouldUpdateVisibleRect(true)
115     , m_shouldSyncLayerState(true)
116     , m_shouldSyncChildren(true)
117     , m_shouldSyncAnimations(true)
118     , m_fixedToViewport(false)
119     , m_movingVisibleRect(false)
120 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
121     , m_isOverflow(false)
122 #endif
123     , m_canvasNeedsDisplay(false)
124 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
125     , m_shouldSyncBackingStore(false)
126 #endif
127     , m_webGraphicsLayerClient(0)
128     , m_contentsScale(1)
129 #if ENABLE(TIZEN_USE_FIXED_SCALE_ANIMATION)
130     , m_fixedAnimationScale(1)
131 #endif
132     , m_canvasPlatformLayer(0)
133     , m_animationStartedTimer(this, &WebGraphicsLayer::animationStartedTimerFired)
134 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
135     , m_animationTimer(this, &WebGraphicsLayer::animationTimerFired)
136 #endif
137 {
138     static WebLayerID nextLayerID = 1;
139     m_id = nextLayerID++;
140     layerByIDMap().add(id(), this);
141
142 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
143     m_layerInfo.isRootLayer = false;
144     m_layerInfo.contentType = WebKit::WebLayerInfo::HTMLContentType;
145 #endif
146 }
147
148 WebGraphicsLayer::~WebGraphicsLayer()
149 {
150     layerByIDMap().remove(id());
151
152     if (m_webGraphicsLayerClient) {
153         purgeBackingStores();
154         m_webGraphicsLayerClient->detachLayer(this);
155     }
156     willBeDestroyed();
157 }
158
159 void WebGraphicsLayer::willBeDestroyed()
160 {
161     GraphicsLayer::willBeDestroyed();
162 }
163
164 bool WebGraphicsLayer::setChildren(const Vector<GraphicsLayer*>& children)
165 {
166     bool ok = GraphicsLayer::setChildren(children);
167     if (!ok)
168         return false;
169     for (size_t i = 0; i < children.size(); ++i) {
170         WebGraphicsLayer* child = toWebGraphicsLayer(children[i]);
171         child->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
172         child->didChangeLayerState();
173     }
174     didChangeChildren();
175     return true;
176 }
177
178 void WebGraphicsLayer::addChild(GraphicsLayer* layer)
179 {
180     GraphicsLayer::addChild(layer);
181     toWebGraphicsLayer(layer)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
182     toWebGraphicsLayer(layer)->didChangeLayerState();
183     didChangeChildren();
184 }
185
186 void WebGraphicsLayer::addChildAtIndex(GraphicsLayer* layer, int index)
187 {
188     GraphicsLayer::addChildAtIndex(layer, index);
189     toWebGraphicsLayer(layer)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
190     toWebGraphicsLayer(layer)->didChangeLayerState();
191     didChangeChildren();
192 }
193
194 void WebGraphicsLayer::addChildAbove(GraphicsLayer* layer, GraphicsLayer* sibling)
195 {
196     GraphicsLayer::addChildAbove(layer, sibling);
197     toWebGraphicsLayer(layer)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
198     toWebGraphicsLayer(layer)->didChangeLayerState();
199     didChangeChildren();
200 }
201
202 void WebGraphicsLayer::addChildBelow(GraphicsLayer* layer, GraphicsLayer* sibling)
203 {
204     GraphicsLayer::addChildBelow(layer, sibling);
205     toWebGraphicsLayer(layer)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
206     toWebGraphicsLayer(layer)->didChangeLayerState();
207     didChangeChildren();
208 }
209
210 bool WebGraphicsLayer::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
211 {
212     bool ok = GraphicsLayer::replaceChild(oldChild, newChild);
213     if (!ok)
214         return false;
215     didChangeChildren();
216     toWebGraphicsLayer(oldChild)->didChangeLayerState();
217     toWebGraphicsLayer(newChild)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
218     toWebGraphicsLayer(newChild)->didChangeLayerState();
219     return true;
220 }
221
222 void WebGraphicsLayer::removeFromParent()
223 {
224     if (WebGraphicsLayer* parentLayer = toWebGraphicsLayer(parent()))
225         parentLayer->didChangeChildren();
226     GraphicsLayer::removeFromParent();
227
228     didChangeLayerState();
229 }
230
231 void WebGraphicsLayer::setPosition(const FloatPoint& p)
232 {
233     if (position() == p)
234         return;
235
236     GraphicsLayer::setPosition(p);
237     didChangeGeometry();
238 }
239
240 void WebGraphicsLayer::setAnchorPoint(const FloatPoint3D& p)
241 {
242     if (anchorPoint() == p)
243         return;
244
245     GraphicsLayer::setAnchorPoint(p);
246     didChangeGeometry();
247 }
248
249 void WebGraphicsLayer::setSize(const FloatSize& size)
250 {
251     if (this->size() == size)
252         return;
253
254     GraphicsLayer::setSize(size);
255     setNeedsDisplay();
256     if (maskLayer())
257         maskLayer()->setSize(size);
258     didChangeGeometry();
259 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
260     if (!fixedToViewport())
261         adjustVisibleRect();
262 #endif
263 }
264
265 void WebGraphicsLayer::setTransform(const TransformationMatrix& t)
266 {
267     if (transform() == t)
268         return;
269
270     GraphicsLayer::setTransform(t);
271     didChangeGeometry();
272 }
273
274 void WebGraphicsLayer::setChildrenTransform(const TransformationMatrix& t)
275 {
276     if (childrenTransform() == t)
277         return;
278
279     GraphicsLayer::setChildrenTransform(t);
280     didChangeGeometry();
281 }
282
283 void WebGraphicsLayer::setPreserves3D(bool b)
284 {
285     if (preserves3D() == b)
286         return;
287
288     GraphicsLayer::setPreserves3D(b);
289     didChangeGeometry();
290 }
291
292 void WebGraphicsLayer::setMasksToBounds(bool b)
293 {
294     if (masksToBounds() == b)
295         return;
296     GraphicsLayer::setMasksToBounds(b);
297     didChangeGeometry();
298 }
299
300 void WebGraphicsLayer::setDrawsContent(bool b)
301 {
302     if (drawsContent() == b)
303         return;
304     GraphicsLayer::setDrawsContent(b);
305
306 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
307     if (b)
308         setNeedsDisplay();
309 #endif
310     didChangeLayerState();
311 }
312
313 void WebGraphicsLayer::setContentsVisible(bool b)
314 {
315     if (contentsAreVisible() == b)
316         return;
317     GraphicsLayer::setContentsVisible(b);
318
319     didChangeLayerState();
320 }
321
322 void WebGraphicsLayer::setContentsOpaque(bool b)
323 {
324     if (contentsOpaque() == b)
325         return;
326     if (m_mainBackingStore)
327         m_mainBackingStore->setSupportsAlpha(!b);
328     GraphicsLayer::setContentsOpaque(b);
329     didChangeLayerState();
330 }
331
332 void WebGraphicsLayer::setBackfaceVisibility(bool b)
333 {
334     if (backfaceVisibility() == b)
335         return;
336
337     GraphicsLayer::setBackfaceVisibility(b);
338     didChangeLayerState();
339 }
340
341 void WebGraphicsLayer::setOpacity(float opacity)
342 {
343     if (this->opacity() == opacity)
344         return;
345
346     GraphicsLayer::setOpacity(opacity);
347     didChangeLayerState();
348 }
349
350 void WebGraphicsLayer::setContentsRect(const IntRect& r)
351 {
352     if (contentsRect() == r)
353         return;
354
355     GraphicsLayer::setContentsRect(r);
356     didChangeLayerState();
357 }
358
359 void WebGraphicsLayer::setContentsNeedsDisplay()
360 {
361     RefPtr<Image> image = m_image;
362     setContentsToImage(0);
363     setContentsToImage(image.get());
364     m_canvasNeedsDisplay = true;
365     if (client())
366         client()->notifySyncRequired(this);
367 }
368
369
370 #if ENABLE(CSS_FILTERS)
371 bool WebGraphicsLayer::setFilters(const FilterOperations& newFilters)
372 {
373     if (filters() == newFilters)
374         return true;
375     didChangeFilters();
376     return GraphicsLayer::setFilters(newFilters);
377 }
378 #endif
379
380 void WebGraphicsLayer::setContentsToBackgroundColor(const Color& color)
381 {
382     if (m_layerInfo.backgroundColor == color)
383         return;
384     m_layerInfo.backgroundColor = color;
385
386     // This is in line with what CA does.
387     setBackgroundColor(color);
388     didChangeLayerState();
389 }
390
391 void WebGraphicsLayer::setContentsToImage(Image* image)
392 {
393     if (image == m_image)
394         return;
395     int64_t newID = 0;
396     if (m_webGraphicsLayerClient) {
397         // We adopt first, in case this is the same frame - that way we avoid destroying and recreating the image.
398         newID = m_webGraphicsLayerClient->adoptImageBackingStore(image);
399         m_webGraphicsLayerClient->releaseImageBackingStore(m_layerInfo.imageBackingStoreID);
400         didChangeLayerState();
401         if (m_layerInfo.imageBackingStoreID && newID == m_layerInfo.imageBackingStoreID)
402             return;
403     } else {
404         // If m_webGraphicsLayerClient is not set yet there should be no backing store ID.
405         ASSERT(!m_layerInfo.imageBackingStoreID);
406         didChangeLayerState();
407     }
408
409     m_layerInfo.imageBackingStoreID = newID;
410     m_image = image;
411     GraphicsLayer::setContentsToImage(image);
412 }
413
414 void WebGraphicsLayer::setContentsToCanvas(PlatformLayer* platformLayer)
415 {
416     m_canvasPlatformLayer = platformLayer;
417     m_canvasNeedsDisplay = true;
418 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
419     if (!m_canvasPlatformLayer)
420         return;
421
422     if (static_cast<TextureMapperPlatformLayer*>(m_canvasPlatformLayer)->contentType() == WebKit::WebLayerInfo::Canvas2DContentType)
423         m_layerInfo.contentType = WebKit::WebLayerInfo::Canvas2DContentType;
424     else
425         m_layerInfo.contentType = WebKit::WebLayerInfo::Canvas3DContentType;
426 #endif
427
428     if (client())
429        client()->notifySyncRequired(this);
430 }
431
432 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
433 void WebGraphicsLayer::setContentsToMedia(PlatformLayer* platformLayer)
434 {
435     m_canvasPlatformLayer = platformLayer;
436     m_canvasNeedsDisplay = true;
437     m_layerInfo.contentType = WebKit::WebLayerInfo::MediaContentType;
438
439     if (client())
440         client()->notifySyncRequired(this);
441 }
442 #endif
443
444 void WebGraphicsLayer::setMaskLayer(GraphicsLayer* layer)
445 {
446     if (layer == maskLayer())
447         return;
448
449     GraphicsLayer::setMaskLayer(layer);
450
451     if (!layer)
452         return;
453
454     layer->setSize(size());
455     WebGraphicsLayer* webGraphicsLayer = toWebGraphicsLayer(layer);
456     webGraphicsLayer->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
457     webGraphicsLayer->setMaskTarget(this);
458     webGraphicsLayer->didChangeLayerState();
459     didChangeLayerState();
460
461 }
462
463 void WebGraphicsLayer::setReplicatedByLayer(GraphicsLayer* layer)
464 {
465     if (layer == replicaLayer())
466         return;
467
468     if (layer)
469         toWebGraphicsLayer(layer)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
470
471     GraphicsLayer::setReplicatedByLayer(layer);
472     didChangeLayerState();
473 }
474
475 void WebGraphicsLayer::setNeedsDisplay()
476 {
477     setNeedsDisplayInRect(IntRect(IntPoint::zero(), IntSize(size().width(), size().height())));
478 }
479
480 void WebGraphicsLayer::setNeedsDisplayInRect(const FloatRect& rect)
481 {
482     if (m_mainBackingStore)
483         m_mainBackingStore->invalidate(IntRect(rect));
484     didChangeLayerState();
485 }
486
487 WebLayerID WebGraphicsLayer::id() const
488 {
489     return m_id;
490 }
491
492 void WebGraphicsLayer::syncCompositingState(const FloatRect& rect)
493 {
494 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
495     if (!m_webGraphicsLayerClient)
496         return;
497 #endif
498
499     if (WebGraphicsLayer* mask = toWebGraphicsLayer(maskLayer()))
500         mask->syncCompositingStateForThisLayerOnly();
501
502     if (WebGraphicsLayer* replica = toWebGraphicsLayer(replicaLayer()))
503         replica->syncCompositingStateForThisLayerOnly();
504
505 #if !ENABLE(TIZEN_CSS_FIXED_ACCELERATION)
506     m_webGraphicsLayerClient->syncFixedLayers();
507 #endif
508
509     syncCompositingStateForThisLayerOnly();
510
511     for (size_t i = 0; i < children().size(); ++i)
512         children()[i]->syncCompositingState(rect);
513 }
514
515 WebGraphicsLayer* toWebGraphicsLayer(GraphicsLayer* layer)
516 {
517     return static_cast<WebGraphicsLayer*>(layer);
518 }
519
520 void WebGraphicsLayer::syncChildren()
521 {
522     if (!m_shouldSyncChildren)
523         return;
524     m_shouldSyncChildren = false;
525     Vector<WebLayerID> childIDs;
526     for (size_t i = 0; i < children().size(); ++i)
527         childIDs.append(toWebLayerID(children()[i]));
528
529 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
530     if (m_webGraphicsLayerClient)
531 #endif
532     m_webGraphicsLayerClient->syncLayerChildren(m_id, childIDs);
533 }
534
535 #if ENABLE(CSS_FILTERS)
536 void WebGraphicsLayer::syncFilters()
537 {
538     if (!m_shouldSyncFilters)
539         return;
540     m_shouldSyncFilters = false;
541     m_webGraphicsLayerClient->syncLayerFilters(m_id, filters());
542 }
543 #endif
544
545 void WebGraphicsLayer::syncLayerState()
546 {
547     if (!m_shouldSyncLayerState)
548         return;
549
550     m_shouldSyncLayerState = false;
551     m_layerInfo.fixedToViewport = fixedToViewport();
552 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
553     m_layerInfo.isScrollbar = isScrollbar();
554 #endif
555 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
556     m_layerInfo.isScrollingContentsLayer = isOverflow();
557     if (m_layerInfo.isScrollingContentsLayer)
558         m_layerInfo.boundsOrigin = parent() ? parent()->boundsOrigin() : boundsOrigin();
559 #endif
560
561     m_layerInfo.anchorPoint = anchorPoint();
562     m_layerInfo.backfaceVisible = backfaceVisibility();
563     m_layerInfo.childrenTransform = childrenTransform();
564     m_layerInfo.contentsOpaque = contentsOpaque();
565     m_layerInfo.contentsRect = contentsRect();
566     m_layerInfo.drawsContent = drawsContent();
567     m_layerInfo.contentsVisible = contentsAreVisible();
568     m_layerInfo.mask = toWebLayerID(maskLayer());
569     m_layerInfo.masksToBounds = masksToBounds();
570     m_layerInfo.opacity = opacity();
571     m_layerInfo.parent = toWebLayerID(parent());
572     m_layerInfo.pos = position();
573     m_layerInfo.preserves3D = preserves3D();
574     m_layerInfo.replica = toWebLayerID(replicaLayer());
575     m_layerInfo.size = size();
576     m_layerInfo.transform = transform();
577
578 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
579     if (m_webGraphicsLayerClient)
580 #endif
581     m_webGraphicsLayerClient->syncLayerState(m_id, m_layerInfo);
582
583 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
584     syncBackingStoreIfNeeded();
585 #endif
586 }
587
588 void WebGraphicsLayer::syncAnimations()
589 {
590     if (!m_shouldSyncAnimations)
591         return;
592
593     m_shouldSyncAnimations = false;
594
595     m_webGraphicsLayerClient->setLayerAnimations(m_id, m_animations);
596 }
597
598 void WebGraphicsLayer::syncCanvas()
599 {
600     if (!m_canvasNeedsDisplay)
601         return;
602
603     if (!m_canvasPlatformLayer)
604         return;
605
606 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
607     uint64_t token = m_canvasPlatformLayer->graphicsSurfaceToken();
608     if (!token)
609         return;
610     uint32_t frontBuffer = m_canvasPlatformLayer->copyToGraphicsSurface();
611 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
612     int flags = m_canvasPlatformLayer->graphicsSurfaceFlags();
613     if (m_webGraphicsLayerClient)
614         m_webGraphicsLayerClient->syncCanvas(m_id, IntSize(contentsRect().width(), contentsRect().height()), token, frontBuffer, flags);
615 #else
616     m_webGraphicsLayerClient->syncCanvas(m_id, IntSize(size().width(), size().height()), token, frontBuffer);
617 #endif
618 #endif
619     m_canvasNeedsDisplay = false;
620 }
621
622 void WebGraphicsLayer::ensureImageBackingStore()
623 {
624     if (!m_image)
625         return;
626
627 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
628     if (!m_webGraphicsLayerClient)
629         return;
630 #endif
631
632     if (!m_layerInfo.imageBackingStoreID)
633         m_layerInfo.imageBackingStoreID = m_webGraphicsLayerClient->adoptImageBackingStore(m_image.get());
634 }
635
636 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
637 void WebGraphicsLayer::syncBackingStoreIfNeeded()
638 {
639     if (m_shouldSyncBackingStore) {
640         if (m_mainBackingStore)
641             m_mainBackingStore->reviewTiles();
642         m_shouldSyncBackingStore = false;
643     }
644 }
645 #endif
646
647 void WebGraphicsLayer::syncCompositingStateForThisLayerOnly()
648 {
649     // When we have a transform animation, we need to update visible rect every frame to adjust the visible rect of a backing store.
650     bool hasActiveTransformAnimation = selfOrAncestorHasActiveTransformAnimation();
651     if (hasActiveTransformAnimation)
652         m_movingVisibleRect = true;
653
654     // The remote image might have been released by purgeBackingStores.
655     ensureImageBackingStore();
656     syncLayerState();
657     syncAnimations();
658
659     computeTransformedVisibleRect();
660     syncChildren();
661 #if ENABLE(CSS_FILTERS)
662     syncFilters();
663 #endif
664     updateContentBuffers();
665     syncCanvas();
666     // Only unset m_movingVisibleRect after we have updated the visible rect after the animation stopped.
667     if (!hasActiveTransformAnimation)
668         m_movingVisibleRect = false;
669 }
670
671 void WebGraphicsLayer::tiledBackingStorePaintBegin()
672 {
673 }
674
675 void WebGraphicsLayer::setRootLayer(bool isRoot)
676 {
677     m_layerInfo.isRootLayer = isRoot;
678     didChangeLayerState();
679 }
680
681 void WebGraphicsLayer::setVisibleContentRectTrajectoryVector(const FloatPoint& trajectoryVector)
682 {
683     if (m_mainBackingStore)
684         m_mainBackingStore->coverWithTilesIfNeeded(trajectoryVector);
685 }
686
687 void WebGraphicsLayer::setContentsScale(float scale)
688 {
689 #if ENABLE(TIZEN_USE_FIXED_SCALE_ANIMATION)
690     if (!m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
691         m_fixedAnimationScale = scale;
692 #endif
693
694     m_contentsScale = scale;
695     adjustContentsScale();
696 }
697
698 float WebGraphicsLayer::effectiveContentsScale()
699 {
700 #if ENABLE(TIZEN_USE_FIXED_SCALE_ANIMATION)
701     if (m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
702         return m_fixedAnimationScale;
703 #endif
704
705     return selfOrAncestorHaveNonAffineTransforms() ? 1 : m_contentsScale;
706 }
707
708 void WebGraphicsLayer::adjustContentsScale()
709 {
710     if (!drawsContent())
711         return;
712
713     if (!m_mainBackingStore || m_mainBackingStore->contentsScale() == effectiveContentsScale())
714         return;
715
716     // Between creating the new backing store and painting the content,
717     // we do not want to drop the previous one as that might result in
718     // briefly seeing flickering as the old tiles may be dropped before
719     // something replaces them.
720     m_previousBackingStore = m_mainBackingStore.release();
721
722     // No reason to save the previous backing store for non-visible areas.
723     m_previousBackingStore->removeAllNonVisibleTiles();
724
725     createBackingStore();
726 }
727
728 void WebGraphicsLayer::createBackingStore()
729 {
730 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
731 #if ENABLE(TIZEN_RUNTIME_BACKEND_SELECTION)
732     if (!m_webGraphicsLayerClient->isGLAccelerationMode() || !SharedPlatformSurfaceTizen::supportsLockSurfaceExtension())
733         m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackend::create(this)));
734     else
735 #endif
736     m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackendTizen::create(this)));
737 #else
738     m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackend::create(this)));
739 #endif
740     m_mainBackingStore->setSupportsAlpha(!contentsOpaque());
741     m_mainBackingStore->setContentsScale(effectiveContentsScale());
742 }
743
744 void WebGraphicsLayer::tiledBackingStorePaint(GraphicsContext* context, const IntRect& rect)
745 {
746     if (rect.isEmpty())
747         return;
748
749     paintGraphicsLayerContents(*context, rect);
750 }
751
752 void WebGraphicsLayer::tiledBackingStorePaintEnd(const Vector<IntRect>& updatedRects)
753 {
754 }
755
756 bool WebGraphicsLayer::tiledBackingStoreUpdatesAllowed() const
757 {
758     if (!m_inUpdateMode)
759         return false;
760
761 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
762     if (!m_webGraphicsLayerClient)
763         return false;
764 #endif
765
766     return m_webGraphicsLayerClient->layerTreeTileUpdatesAllowed();
767 }
768
769 IntRect WebGraphicsLayer::tiledBackingStoreContentsRect()
770 {
771     return IntRect(0, 0, size().width(), size().height());
772 }
773
774 IntRect WebGraphicsLayer::tiledBackingStoreVisibleRect()
775 {
776     // Non-invertible layers are not visible.
777     if (!m_layerTransform.combined().isInvertible())
778         return IntRect();
779
780 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
781     if (!m_webGraphicsLayerClient)
782         return IntRect();
783 #endif
784
785     FloatRect perspectiveRect = m_webGraphicsLayerClient->visibleContentsRect();
786
787 #if ENABLE(TIZEN_CSS_OVERFLOW_CLIPPING_BACKING_STORE)
788     if (GraphicsLayer::overflowClipping() && m_mainBackingStore) {
789         WebGraphicsLayer* parentLayer = toWebGraphicsLayer(parent());
790         if (parentLayer)
791             perspectiveRect = parentLayer->clippingBounds();
792     }
793 #endif
794
795     if (m_movingVisibleRect) {
796         IntRect startAnimationRect = enclosingIntRect(m_layerTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
797         IntRect endAnimationRect = enclosingIntRect(m_layerSettledTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
798         endAnimationRect.unite(startAnimationRect);
799         return endAnimationRect;
800     }
801
802 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
803     if (isScrollbar()) {
804         WebGraphicsLayer* parentLayer = toWebGraphicsLayer(parent());
805         if (parentLayer && parentLayer->isOverflow()) {
806             GraphicsLayerTransform adjustedTransform = m_layerTransform;
807             adjustedTransform.setPosition(FloatPoint(position().x() - parentLayer->position().x(),
808                     position().y() - parentLayer->position().y()));
809             adjustedTransform.combineTransforms(parent() ? toWebGraphicsLayer(parent())->m_layerTransform.combinedForChildren() : TransformationMatrix());
810             return enclosingIntRect(adjustedTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
811         }
812     }
813 #endif
814
815     // Return a projection of the visible rect (surface coordinates) onto the layer's plane (layer coordinates).
816     // The resulting quad might be squewed and the visible rect is the bounding box of this quad,
817     // so it might spread further than the real visible area (and then even more amplified by the cover rect multiplier).
818     return enclosingIntRect(m_layerTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
819 }
820
821 Color WebGraphicsLayer::tiledBackingStoreBackgroundColor() const
822 {
823     return contentsOpaque() ? Color::white : Color::transparent;
824 }
825
826 PassOwnPtr<WebCore::GraphicsContext> WebGraphicsLayer::beginContentUpdate(const WebCore::IntSize& size, ShareableSurface::Handle& handle, WebCore::IntPoint& offset)
827 {
828 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
829     if (!m_webGraphicsLayerClient)
830         return nullptr;
831 #endif
832
833     return m_webGraphicsLayerClient->beginContentUpdate(size, contentsOpaque() ? 0 : ShareableBitmap::SupportsAlpha, handle, offset);
834 }
835
836 void WebGraphicsLayer::createTile(int tileID, const SurfaceUpdateInfo& updateInfo, const IntRect& targetRect)
837 {
838 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
839     if (m_webGraphicsLayerClient)
840 #endif
841     m_webGraphicsLayerClient->createTile(id(), tileID, updateInfo, targetRect);
842 }
843
844 void WebGraphicsLayer::updateTile(int tileID, const SurfaceUpdateInfo& updateInfo, const IntRect& targetRect)
845 {
846 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
847     if (m_webGraphicsLayerClient)
848 #endif
849     m_webGraphicsLayerClient->updateTile(id(), tileID, updateInfo, targetRect);
850 }
851
852 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
853 void WebGraphicsLayer::freePlatformSurface(int platformSurfaceID)
854 {
855     if (m_canvasPlatformLayer)
856         m_canvasPlatformLayer->freePlatformSurface(platformSurfaceID);
857 }
858
859 void WebGraphicsLayer::removePlatformSurface(int platformSurfaceID)
860 {
861     if (m_canvasPlatformLayer)
862         m_canvasPlatformLayer->removePlatformSurface(platformSurfaceID);
863 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
864     if (m_canvasPlatformLayer && m_layerInfo.contentType == WebKit::WebLayerInfo::Canvas2DContentType)
865         m_canvasNeedsDisplay = true;
866 #endif
867 #if ENABLE(WEBGL)
868     if (m_canvasPlatformLayer && m_layerInfo.contentType == WebKit::WebLayerInfo::Canvas3DContentType)
869         m_canvasNeedsDisplay = true;
870 #endif
871 }
872
873 bool WebGraphicsLayer::swapPlatformSurfaces()
874 {
875     if (m_canvasPlatformLayer)
876         return m_canvasPlatformLayer->swapPlatformSurfaces();
877     return true;
878 }
879 #endif
880
881 #if ENABLE(TIZEN_ACCELERATED_2D_CANVAS_EFL)
882 void WebGraphicsLayer::flushPlatformSurfaces()
883 {
884     if (m_canvasPlatformLayer)
885         return m_canvasPlatformLayer->flushPlatformSurfaces();
886 }
887 #endif
888
889 void WebGraphicsLayer::removeTile(int tileID)
890 {
891 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
892     if (m_webGraphicsLayerClient)
893 #endif
894     m_webGraphicsLayerClient->removeTile(id(), tileID);
895 }
896
897 void WebGraphicsLayer::updateContentBuffers()
898 {
899     if (!drawsContent()) {
900         m_mainBackingStore.clear();
901         m_previousBackingStore.clear();
902         return;
903     }
904
905     m_inUpdateMode = true;
906     // This is the only place we (re)create the main tiled backing store, once we
907     // have a remote client and we are ready to send our data to the UI process.
908     if (!m_mainBackingStore)
909         createBackingStore();
910
911     m_mainBackingStore->updateTileBuffers();
912     m_inUpdateMode = false;
913
914     // The previous backing store is kept around to avoid flickering between
915     // removing the existing tiles and painting the new ones. The first time
916     // the visibleRect is full painted we remove the previous backing store.
917     if (m_mainBackingStore->visibleAreaIsCovered())
918         m_previousBackingStore.clear();
919 }
920
921 void WebGraphicsLayer::purgeBackingStores()
922 {
923     m_mainBackingStore.clear();
924     m_previousBackingStore.clear();
925
926 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
927     if (m_webGraphicsLayerClient)
928 #endif
929     if (m_layerInfo.imageBackingStoreID) {
930         m_webGraphicsLayerClient->releaseImageBackingStore(m_layerInfo.imageBackingStoreID);
931         m_layerInfo.imageBackingStoreID = 0;
932     }
933
934     didChangeLayerState();
935     didChangeChildren();
936 }
937
938 void WebGraphicsLayer::setWebGraphicsLayerClient(WebKit::WebGraphicsLayerClient* client)
939 {
940     if (m_webGraphicsLayerClient == client)
941         return;
942
943     if (WebGraphicsLayer* replica = toWebGraphicsLayer(replicaLayer()))
944         replica->setWebGraphicsLayerClient(client);
945     if (WebGraphicsLayer* mask = toWebGraphicsLayer(maskLayer()))
946         mask->setWebGraphicsLayerClient(client);
947     for (size_t i = 0; i < children().size(); ++i) {
948         WebGraphicsLayer* layer = toWebGraphicsLayer(this->children()[i]);
949         layer->setWebGraphicsLayerClient(client);
950     }
951
952     // We have to release resources on the UI process here if the remote client has changed or is removed.
953     if (m_webGraphicsLayerClient) {
954 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
955         didChangeLayerState();
956         didChangeChildren();
957         m_shouldSyncBackingStore = true;
958 #else
959         purgeBackingStores();
960 #endif // ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
961         m_webGraphicsLayerClient->detachLayer(this);
962     }
963     m_webGraphicsLayerClient = client;
964     if (client)
965         client->attachLayer(this);
966 }
967
968 void WebGraphicsLayer::adjustVisibleRect()
969 {
970     if (m_mainBackingStore)
971         m_mainBackingStore->coverWithTilesIfNeeded();
972 }
973
974 bool WebGraphicsLayer::hasPendingVisibleChanges()
975 {
976     if (opacity() < 0.01 && !m_animations.hasActiveAnimationsOfType(AnimatedPropertyOpacity))
977         return false;
978
979     for (size_t i = 0; i < children().size(); ++i) {
980         if (toWebGraphicsLayer(children()[i])->hasPendingVisibleChanges())
981             return true;
982     }
983
984     if (!m_shouldSyncLayerState && !m_shouldSyncChildren && !m_shouldSyncFilters && !m_shouldSyncAnimations && !m_canvasNeedsDisplay)
985         return false;
986
987     return tiledBackingStoreVisibleRect().intersects(tiledBackingStoreContentsRect());
988 }
989
990 void WebGraphicsLayer::computeTransformedVisibleRect()
991 {
992     // When we have a transform animation, we need to update visible rect every frame to adjust the visible rect of a backing store.
993     if (!m_shouldUpdateVisibleRect && !m_movingVisibleRect)
994         return;
995
996     m_shouldUpdateVisibleRect = false;
997     TransformationMatrix currentTransform = transform();
998     if (m_movingVisibleRect) {
999         m_layerSettledTransform.setLocalTransform(currentTransform);
1000         m_layerSettledTransform.setPosition(position());
1001         m_layerSettledTransform.setAnchorPoint(anchorPoint());
1002         m_layerSettledTransform.setSize(size());
1003         m_layerSettledTransform.setFlattening(!preserves3D());
1004         m_layerSettledTransform.setChildrenTransform(childrenTransform());
1005         m_layerSettledTransform.combineTransforms(parent() ? toWebGraphicsLayer(parent())->m_layerTransform.combinedForChildren() : TransformationMatrix());
1006
1007         client()->getCurrentTransform(this, currentTransform);
1008     }
1009
1010     m_layerTransform.setLocalTransform(currentTransform);
1011     m_layerTransform.setPosition(position());
1012     m_layerTransform.setAnchorPoint(anchorPoint());
1013     m_layerTransform.setSize(size());
1014     m_layerTransform.setFlattening(!preserves3D());
1015     m_layerTransform.setChildrenTransform(childrenTransform());
1016     m_layerTransform.combineTransforms(parent() ? toWebGraphicsLayer(parent())->m_layerTransform.combinedForChildren() : TransformationMatrix());
1017
1018     // The combined transform will be used in tiledBackingStoreVisibleRect.
1019     adjustVisibleRect();
1020     adjustContentsScale();
1021 }
1022
1023 static PassOwnPtr<GraphicsLayer> createWebGraphicsLayer(GraphicsLayerClient* client)
1024 {
1025     return adoptPtr(new WebGraphicsLayer(client));
1026 }
1027
1028 void WebGraphicsLayer::initFactory()
1029 {
1030     GraphicsLayer::setGraphicsLayerFactory(createWebGraphicsLayer);
1031 }
1032
1033 bool WebGraphicsLayer::selfOrAncestorHasActiveTransformAnimation()
1034 {
1035     if (m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
1036         return true;
1037
1038     if (!parent())
1039         return false;
1040
1041     return toWebGraphicsLayer(parent())->selfOrAncestorHasActiveTransformAnimation();
1042 }
1043
1044 bool WebGraphicsLayer::selfOrAncestorHaveNonAffineTransforms()
1045 {
1046     if (m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
1047         return true;
1048
1049     if (!m_layerTransform.combined().isAffine())
1050         return true;
1051
1052     return false;
1053 }
1054
1055 bool WebGraphicsLayer::addAnimation(const KeyframeValueList& valueList, const IntSize& boxSize, const Animation* anim, const String& keyframesName, double timeOffset)
1056 {
1057     ASSERT(!keyframesName.isEmpty());
1058
1059     if (!anim || anim->isEmptyOrZeroDuration() || valueList.size() < 2 || (valueList.property() != AnimatedPropertyWebkitTransform && valueList.property() != AnimatedPropertyOpacity))
1060         return false;
1061
1062     bool listsMatch = false;
1063     bool ignoredHasBigRotation;
1064
1065     if (valueList.property() == AnimatedPropertyWebkitTransform)
1066         listsMatch = validateTransformOperations(valueList, ignoredHasBigRotation) >= 0;
1067
1068     m_animations.add(GraphicsLayerAnimation(keyframesName, valueList, boxSize, anim, WTF::currentTime() - timeOffset, listsMatch));
1069     m_animationStartedTimer.startOneShot(0);
1070 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
1071     didChangeAnimations();
1072 #endif
1073     didChangeLayerState();
1074     return true;
1075 }
1076
1077 void WebGraphicsLayer::pauseAnimation(const String& animationName, double timeOffset)
1078 {
1079     m_animations.pause(animationName, timeOffset);
1080     didChangeAnimations();
1081 }
1082
1083 void WebGraphicsLayer::removeAnimation(const String& animationName)
1084 {
1085     m_animations.remove(animationName);
1086     didChangeAnimations();
1087 }
1088
1089 void WebGraphicsLayer::animationStartedTimerFired(Timer<WebGraphicsLayer>*)
1090 {
1091     client()->notifyAnimationStarted(this, /* DOM time */ WTF::currentTime());
1092 }
1093
1094 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
1095 void WebGraphicsLayer::setIsOverflow(const bool b)
1096 {
1097     if (m_isOverflow == b)
1098         return;
1099
1100     m_isOverflow = b;
1101     didChangeLayerState();
1102 }
1103 #endif
1104
1105 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
1106 void WebGraphicsLayer::startAnimation()
1107 {
1108     setOpacity(1.0);
1109     if (m_animationTimer.isActive())
1110         m_animationTimer.stop();
1111     m_animationTimer.startOneShot(1.0);
1112 }
1113
1114 void WebGraphicsLayer::animationTimerFired(Timer<WebGraphicsLayer>*)
1115 {
1116     setOpacity(0.0);
1117 }
1118 #endif
1119
1120 #if ENABLE(TIZEN_CUTOFF_TILES_OVER_MEMORY_LIMIT)
1121 Vector<TileCutOffInfo> WebGraphicsLayer::getCutOffInfoList()
1122 {
1123     if (!m_mainBackingStore)
1124         return Vector<TileCutOffInfo>();
1125
1126     return m_mainBackingStore->getCutOffInfoList();
1127 }
1128
1129 void WebGraphicsLayer::setCutOffDistance(double distance)
1130 {
1131     if (!m_mainBackingStore)
1132         return;
1133
1134     m_mainBackingStore->setCutOffDistance(distance);
1135 }
1136 #endif
1137
1138 #if ENABLE(TIZEN_CSS_OVERFLOW_CLIPPING_BACKING_STORE)
1139 FloatRect WebGraphicsLayer::clippingBounds()
1140 {
1141     LayoutRect layerRect = LayoutRect(GraphicsLayer::position(),GraphicsLayer::size());
1142     return enclosingIntRect(m_layerTransform.combined().clampedBoundsOfProjectedQuad(FloatQuad(FloatRect(pixelSnappedIntRect(layerRect)))));
1143 }
1144 #endif
1145
1146 #if ENABLE(TIZEN_WEBKIT2_MEMORY_SAVING_MODE)
1147 bool WebGraphicsLayer::memorySavingModeEnabled()
1148 {
1149     return WebProcess::shared().memorySavingModeEnabled();
1150 }
1151 #endif
1152
1153 #if ENABLE(TIZEN_WEBKIT2_DEBUG_BORDERS)
1154 bool WebGraphicsLayer::drawTileInfo() const
1155 {
1156     static bool drawTileInfo = String(getenv("TIZEN_WEBKIT_SHOW_COMPOSITING_DEBUG_VISUALS")) == "1";
1157     return drawTileInfo;
1158 }
1159 #endif
1160
1161 #if ENABLE(TIZEN_CSS_FIXED_ACCELERATION)
1162 void WebGraphicsLayer::syncFixedLayers()
1163 {
1164     if (m_webGraphicsLayerClient)
1165         m_webGraphicsLayerClient->syncFixedLayers();
1166 }
1167 #endif
1168 }
1169 #endif