Fix the issue that accelerated overflow scrolling become invisible
[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     adjustVisibleRect();
261 #endif
262 }
263
264 void WebGraphicsLayer::setTransform(const TransformationMatrix& t)
265 {
266     if (transform() == t)
267         return;
268
269     GraphicsLayer::setTransform(t);
270     didChangeGeometry();
271 }
272
273 void WebGraphicsLayer::setChildrenTransform(const TransformationMatrix& t)
274 {
275     if (childrenTransform() == t)
276         return;
277
278     GraphicsLayer::setChildrenTransform(t);
279     didChangeGeometry();
280 }
281
282 void WebGraphicsLayer::setPreserves3D(bool b)
283 {
284     if (preserves3D() == b)
285         return;
286
287     GraphicsLayer::setPreserves3D(b);
288     didChangeGeometry();
289 }
290
291 void WebGraphicsLayer::setMasksToBounds(bool b)
292 {
293     if (masksToBounds() == b)
294         return;
295     GraphicsLayer::setMasksToBounds(b);
296     didChangeGeometry();
297 }
298
299 void WebGraphicsLayer::setDrawsContent(bool b)
300 {
301     if (drawsContent() == b)
302         return;
303     GraphicsLayer::setDrawsContent(b);
304
305 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
306     if (b)
307         setNeedsDisplay();
308 #endif
309     didChangeLayerState();
310 }
311
312 void WebGraphicsLayer::setContentsVisible(bool b)
313 {
314     if (contentsAreVisible() == b)
315         return;
316     GraphicsLayer::setContentsVisible(b);
317
318     didChangeLayerState();
319 }
320
321 void WebGraphicsLayer::setContentsOpaque(bool b)
322 {
323     if (contentsOpaque() == b)
324         return;
325     if (m_mainBackingStore)
326         m_mainBackingStore->setSupportsAlpha(!b);
327     GraphicsLayer::setContentsOpaque(b);
328     didChangeLayerState();
329 }
330
331 void WebGraphicsLayer::setBackfaceVisibility(bool b)
332 {
333     if (backfaceVisibility() == b)
334         return;
335
336     GraphicsLayer::setBackfaceVisibility(b);
337     didChangeLayerState();
338 }
339
340 void WebGraphicsLayer::setOpacity(float opacity)
341 {
342     if (this->opacity() == opacity)
343         return;
344
345     GraphicsLayer::setOpacity(opacity);
346     didChangeLayerState();
347 }
348
349 void WebGraphicsLayer::setContentsRect(const IntRect& r)
350 {
351     if (contentsRect() == r)
352         return;
353
354     GraphicsLayer::setContentsRect(r);
355     didChangeLayerState();
356 }
357
358 void WebGraphicsLayer::setContentsNeedsDisplay()
359 {
360     RefPtr<Image> image = m_image;
361     setContentsToImage(0);
362     setContentsToImage(image.get());
363     m_canvasNeedsDisplay = true;
364     if (client())
365         client()->notifySyncRequired(this);
366 }
367
368
369 #if ENABLE(CSS_FILTERS)
370 bool WebGraphicsLayer::setFilters(const FilterOperations& newFilters)
371 {
372     if (filters() == newFilters)
373         return true;
374     didChangeFilters();
375     return GraphicsLayer::setFilters(newFilters);
376 }
377 #endif
378
379 void WebGraphicsLayer::setContentsToBackgroundColor(const Color& color)
380 {
381     if (m_layerInfo.backgroundColor == color)
382         return;
383     m_layerInfo.backgroundColor = color;
384
385     // This is in line with what CA does.
386     setBackgroundColor(color);
387     didChangeLayerState();
388 }
389
390 void WebGraphicsLayer::setContentsToImage(Image* image)
391 {
392     if (image == m_image)
393         return;
394     int64_t newID = 0;
395     if (m_webGraphicsLayerClient) {
396         // We adopt first, in case this is the same frame - that way we avoid destroying and recreating the image.
397         newID = m_webGraphicsLayerClient->adoptImageBackingStore(image);
398         m_webGraphicsLayerClient->releaseImageBackingStore(m_layerInfo.imageBackingStoreID);
399         didChangeLayerState();
400         if (m_layerInfo.imageBackingStoreID && newID == m_layerInfo.imageBackingStoreID)
401             return;
402     } else {
403         // If m_webGraphicsLayerClient is not set yet there should be no backing store ID.
404         ASSERT(!m_layerInfo.imageBackingStoreID);
405         didChangeLayerState();
406     }
407
408     m_layerInfo.imageBackingStoreID = newID;
409     m_image = image;
410     GraphicsLayer::setContentsToImage(image);
411 }
412
413 void WebGraphicsLayer::setContentsToCanvas(PlatformLayer* platformLayer)
414 {
415     m_canvasPlatformLayer = platformLayer;
416     m_canvasNeedsDisplay = true;
417 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
418     if (!m_canvasPlatformLayer)
419         return;
420
421     if (static_cast<TextureMapperPlatformLayer*>(m_canvasPlatformLayer)->contentType() == WebKit::WebLayerInfo::Canvas2DContentType)
422         m_layerInfo.contentType = WebKit::WebLayerInfo::Canvas2DContentType;
423     else
424         m_layerInfo.contentType = WebKit::WebLayerInfo::Canvas3DContentType;
425 #endif
426
427     if (client())
428        client()->notifySyncRequired(this);
429 }
430
431 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
432 void WebGraphicsLayer::setContentsToMedia(PlatformLayer* platformLayer)
433 {
434     m_canvasPlatformLayer = platformLayer;
435     m_canvasNeedsDisplay = true;
436     m_layerInfo.contentType = WebKit::WebLayerInfo::MediaContentType;
437
438     if (client())
439         client()->notifySyncRequired(this);
440 }
441 #endif
442
443 void WebGraphicsLayer::setMaskLayer(GraphicsLayer* layer)
444 {
445     if (layer == maskLayer())
446         return;
447
448     GraphicsLayer::setMaskLayer(layer);
449
450     if (!layer)
451         return;
452
453     layer->setSize(size());
454     WebGraphicsLayer* webGraphicsLayer = toWebGraphicsLayer(layer);
455     webGraphicsLayer->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
456     webGraphicsLayer->setMaskTarget(this);
457     webGraphicsLayer->didChangeLayerState();
458     didChangeLayerState();
459
460 }
461
462 void WebGraphicsLayer::setReplicatedByLayer(GraphicsLayer* layer)
463 {
464     if (layer == replicaLayer())
465         return;
466
467     if (layer)
468         toWebGraphicsLayer(layer)->setWebGraphicsLayerClient(m_webGraphicsLayerClient);
469
470     GraphicsLayer::setReplicatedByLayer(layer);
471     didChangeLayerState();
472 }
473
474 void WebGraphicsLayer::setNeedsDisplay()
475 {
476     setNeedsDisplayInRect(IntRect(IntPoint::zero(), IntSize(size().width(), size().height())));
477 }
478
479 void WebGraphicsLayer::setNeedsDisplayInRect(const FloatRect& rect)
480 {
481     if (m_mainBackingStore)
482         m_mainBackingStore->invalidate(IntRect(rect));
483     didChangeLayerState();
484 }
485
486 WebLayerID WebGraphicsLayer::id() const
487 {
488     return m_id;
489 }
490
491 void WebGraphicsLayer::syncCompositingState(const FloatRect& rect)
492 {
493 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
494     if (!m_webGraphicsLayerClient)
495         return;
496 #endif
497
498     if (WebGraphicsLayer* mask = toWebGraphicsLayer(maskLayer()))
499         mask->syncCompositingStateForThisLayerOnly();
500
501     if (WebGraphicsLayer* replica = toWebGraphicsLayer(replicaLayer()))
502         replica->syncCompositingStateForThisLayerOnly();
503
504     m_webGraphicsLayerClient->syncFixedLayers();
505
506     syncCompositingStateForThisLayerOnly();
507
508     for (size_t i = 0; i < children().size(); ++i)
509         children()[i]->syncCompositingState(rect);
510 }
511
512 WebGraphicsLayer* toWebGraphicsLayer(GraphicsLayer* layer)
513 {
514     return static_cast<WebGraphicsLayer*>(layer);
515 }
516
517 void WebGraphicsLayer::syncChildren()
518 {
519     if (!m_shouldSyncChildren)
520         return;
521     m_shouldSyncChildren = false;
522     Vector<WebLayerID> childIDs;
523     for (size_t i = 0; i < children().size(); ++i)
524         childIDs.append(toWebLayerID(children()[i]));
525
526 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
527     if (m_webGraphicsLayerClient)
528 #endif
529     m_webGraphicsLayerClient->syncLayerChildren(m_id, childIDs);
530 }
531
532 #if ENABLE(CSS_FILTERS)
533 void WebGraphicsLayer::syncFilters()
534 {
535     if (!m_shouldSyncFilters)
536         return;
537     m_shouldSyncFilters = false;
538     m_webGraphicsLayerClient->syncLayerFilters(m_id, filters());
539 }
540 #endif
541
542 void WebGraphicsLayer::syncLayerState()
543 {
544     if (!m_shouldSyncLayerState)
545         return;
546
547     m_shouldSyncLayerState = false;
548     m_layerInfo.fixedToViewport = fixedToViewport();
549 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
550     m_layerInfo.isScrollbar = isScrollbar();
551 #endif
552 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
553     m_layerInfo.isScrollingContentsLayer = isOverflow();
554     if (m_layerInfo.isScrollingContentsLayer)
555         m_layerInfo.boundsOrigin = parent() ? parent()->boundsOrigin() : boundsOrigin();
556 #endif
557
558     m_layerInfo.anchorPoint = anchorPoint();
559     m_layerInfo.backfaceVisible = backfaceVisibility();
560     m_layerInfo.childrenTransform = childrenTransform();
561     m_layerInfo.contentsOpaque = contentsOpaque();
562     m_layerInfo.contentsRect = contentsRect();
563     m_layerInfo.drawsContent = drawsContent();
564     m_layerInfo.contentsVisible = contentsAreVisible();
565     m_layerInfo.mask = toWebLayerID(maskLayer());
566     m_layerInfo.masksToBounds = masksToBounds();
567     m_layerInfo.opacity = opacity();
568     m_layerInfo.parent = toWebLayerID(parent());
569     m_layerInfo.pos = position();
570     m_layerInfo.preserves3D = preserves3D();
571     m_layerInfo.replica = toWebLayerID(replicaLayer());
572     m_layerInfo.size = size();
573     m_layerInfo.transform = transform();
574
575 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
576     if (m_webGraphicsLayerClient)
577 #endif
578     m_webGraphicsLayerClient->syncLayerState(m_id, m_layerInfo);
579
580 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
581     syncBackingStoreIfNeeded();
582 #endif
583 }
584
585 void WebGraphicsLayer::syncAnimations()
586 {
587     if (!m_shouldSyncAnimations)
588         return;
589
590     m_shouldSyncAnimations = false;
591
592     m_webGraphicsLayerClient->setLayerAnimations(m_id, m_animations);
593 }
594
595 void WebGraphicsLayer::syncCanvas()
596 {
597     if (!m_canvasNeedsDisplay)
598         return;
599
600     if (!m_canvasPlatformLayer)
601         return;
602
603 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
604     uint64_t token = m_canvasPlatformLayer->graphicsSurfaceToken();
605     if (!token)
606         return;
607     uint32_t frontBuffer = m_canvasPlatformLayer->copyToGraphicsSurface();
608 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
609     int flags = m_canvasPlatformLayer->graphicsSurfaceFlags();
610     if (m_webGraphicsLayerClient)
611         m_webGraphicsLayerClient->syncCanvas(m_id, IntSize(contentsRect().width(), contentsRect().height()), token, frontBuffer, flags);
612 #else
613     m_webGraphicsLayerClient->syncCanvas(m_id, IntSize(size().width(), size().height()), token, frontBuffer);
614 #endif
615 #endif
616     m_canvasNeedsDisplay = false;
617 }
618
619 void WebGraphicsLayer::ensureImageBackingStore()
620 {
621     if (!m_image)
622         return;
623
624 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
625     if (!m_webGraphicsLayerClient)
626         return;
627 #endif
628
629     if (!m_layerInfo.imageBackingStoreID)
630         m_layerInfo.imageBackingStoreID = m_webGraphicsLayerClient->adoptImageBackingStore(m_image.get());
631 }
632
633 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
634 void WebGraphicsLayer::syncBackingStoreIfNeeded()
635 {
636     if (m_shouldSyncBackingStore) {
637         if (m_mainBackingStore)
638             m_mainBackingStore->reviewTiles();
639         m_shouldSyncBackingStore = false;
640     }
641 }
642 #endif
643
644 void WebGraphicsLayer::syncCompositingStateForThisLayerOnly()
645 {
646     // When we have a transform animation, we need to update visible rect every frame to adjust the visible rect of a backing store.
647     bool hasActiveTransformAnimation = selfOrAncestorHasActiveTransformAnimation();
648     if (hasActiveTransformAnimation)
649         m_movingVisibleRect = true;
650
651     // The remote image might have been released by purgeBackingStores.
652     ensureImageBackingStore();
653     syncLayerState();
654     syncAnimations();
655
656     computeTransformedVisibleRect();
657     syncChildren();
658 #if ENABLE(CSS_FILTERS)
659     syncFilters();
660 #endif
661     updateContentBuffers();
662     syncCanvas();
663     // Only unset m_movingVisibleRect after we have updated the visible rect after the animation stopped.
664     if (!hasActiveTransformAnimation)
665         m_movingVisibleRect = false;
666 }
667
668 void WebGraphicsLayer::tiledBackingStorePaintBegin()
669 {
670 }
671
672 void WebGraphicsLayer::setRootLayer(bool isRoot)
673 {
674     m_layerInfo.isRootLayer = isRoot;
675     didChangeLayerState();
676 }
677
678 void WebGraphicsLayer::setVisibleContentRectTrajectoryVector(const FloatPoint& trajectoryVector)
679 {
680     if (m_mainBackingStore)
681         m_mainBackingStore->coverWithTilesIfNeeded(trajectoryVector);
682 }
683
684 void WebGraphicsLayer::setContentsScale(float scale)
685 {
686 #if ENABLE(TIZEN_USE_FIXED_SCALE_ANIMATION)
687     if (!m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
688         m_fixedAnimationScale = scale;
689 #endif
690
691     m_contentsScale = scale;
692     adjustContentsScale();
693 }
694
695 float WebGraphicsLayer::effectiveContentsScale()
696 {
697 #if ENABLE(TIZEN_USE_FIXED_SCALE_ANIMATION)
698     if (m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
699         return m_fixedAnimationScale;
700 #endif
701
702     return selfOrAncestorHaveNonAffineTransforms() ? 1 : m_contentsScale;
703 }
704
705 void WebGraphicsLayer::adjustContentsScale()
706 {
707     if (!drawsContent())
708         return;
709
710     if (!m_mainBackingStore || m_mainBackingStore->contentsScale() == effectiveContentsScale())
711         return;
712
713     // Between creating the new backing store and painting the content,
714     // we do not want to drop the previous one as that might result in
715     // briefly seeing flickering as the old tiles may be dropped before
716     // something replaces them.
717     m_previousBackingStore = m_mainBackingStore.release();
718
719     // No reason to save the previous backing store for non-visible areas.
720     m_previousBackingStore->removeAllNonVisibleTiles();
721
722     createBackingStore();
723 }
724
725 void WebGraphicsLayer::createBackingStore()
726 {
727 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
728 #if ENABLE(TIZEN_RUNTIME_BACKEND_SELECTION)
729     if (!m_webGraphicsLayerClient->isGLAccelerationMode() || !SharedPlatformSurfaceTizen::supportsLockSurfaceExtension())
730         m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackend::create(this)));
731     else
732 #endif
733     m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackendTizen::create(this)));
734 #else
735     m_mainBackingStore = adoptPtr(new TiledBackingStore(this, TiledBackingStoreRemoteTileBackend::create(this)));
736 #endif
737     m_mainBackingStore->setSupportsAlpha(!contentsOpaque());
738     m_mainBackingStore->setContentsScale(effectiveContentsScale());
739 }
740
741 void WebGraphicsLayer::tiledBackingStorePaint(GraphicsContext* context, const IntRect& rect)
742 {
743     if (rect.isEmpty())
744         return;
745
746     paintGraphicsLayerContents(*context, rect);
747 }
748
749 void WebGraphicsLayer::tiledBackingStorePaintEnd(const Vector<IntRect>& updatedRects)
750 {
751 }
752
753 bool WebGraphicsLayer::tiledBackingStoreUpdatesAllowed() const
754 {
755     if (!m_inUpdateMode)
756         return false;
757
758 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
759     if (!m_webGraphicsLayerClient)
760         return false;
761 #endif
762
763     return m_webGraphicsLayerClient->layerTreeTileUpdatesAllowed();
764 }
765
766 IntRect WebGraphicsLayer::tiledBackingStoreContentsRect()
767 {
768     return IntRect(0, 0, size().width(), size().height());
769 }
770
771 IntRect WebGraphicsLayer::tiledBackingStoreVisibleRect()
772 {
773     // Non-invertible layers are not visible.
774     if (!m_layerTransform.combined().isInvertible())
775         return IntRect();
776
777 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
778     if (!m_webGraphicsLayerClient)
779         return IntRect();
780 #endif
781
782     FloatRect perspectiveRect = m_webGraphicsLayerClient->visibleContentsRect();
783
784 #if ENABLE(TIZEN_CSS_OVERFLOW_CLIPPING_BACKING_STORE)
785     if (GraphicsLayer::overflowClipping() && m_mainBackingStore) {
786         WebGraphicsLayer* parentLayer = toWebGraphicsLayer(parent());
787         if (parentLayer)
788             perspectiveRect = parentLayer->clippingBounds();
789     }
790 #endif
791
792     if (m_movingVisibleRect) {
793         IntRect startAnimationRect = enclosingIntRect(m_layerTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
794         IntRect endAnimationRect = enclosingIntRect(m_layerSettledTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
795         endAnimationRect.unite(startAnimationRect);
796         return endAnimationRect;
797     }
798
799 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
800     if (isScrollbar()) {
801         WebGraphicsLayer* parentLayer = toWebGraphicsLayer(parent());
802         if (parentLayer && parentLayer->isOverflow()) {
803             GraphicsLayerTransform adjustedTransform = m_layerTransform;
804             adjustedTransform.setPosition(FloatPoint(position().x() - parentLayer->position().x(),
805                     position().y() - parentLayer->position().y()));
806             adjustedTransform.combineTransforms(parent() ? toWebGraphicsLayer(parent())->m_layerTransform.combinedForChildren() : TransformationMatrix());
807             return enclosingIntRect(adjustedTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
808         }
809     }
810 #endif
811
812     // Return a projection of the visible rect (surface coordinates) onto the layer's plane (layer coordinates).
813     // The resulting quad might be squewed and the visible rect is the bounding box of this quad,
814     // so it might spread further than the real visible area (and then even more amplified by the cover rect multiplier).
815     return enclosingIntRect(m_layerTransform.combined().inverse().clampedBoundsOfProjectedQuad(FloatQuad(perspectiveRect)));
816 }
817
818 Color WebGraphicsLayer::tiledBackingStoreBackgroundColor() const
819 {
820     return contentsOpaque() ? Color::white : Color::transparent;
821 }
822
823 PassOwnPtr<WebCore::GraphicsContext> WebGraphicsLayer::beginContentUpdate(const WebCore::IntSize& size, ShareableSurface::Handle& handle, WebCore::IntPoint& offset)
824 {
825 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
826     if (!m_webGraphicsLayerClient)
827         return PassOwnPtr<WebCore::GraphicsContext>();
828 #endif
829
830     return m_webGraphicsLayerClient->beginContentUpdate(size, contentsOpaque() ? 0 : ShareableBitmap::SupportsAlpha, handle, offset);
831 }
832
833 void WebGraphicsLayer::createTile(int tileID, const SurfaceUpdateInfo& updateInfo, const IntRect& targetRect)
834 {
835 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
836     if (m_webGraphicsLayerClient)
837 #endif
838     m_webGraphicsLayerClient->createTile(id(), tileID, updateInfo, targetRect);
839 }
840
841 void WebGraphicsLayer::updateTile(int tileID, const SurfaceUpdateInfo& updateInfo, const IntRect& targetRect)
842 {
843 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
844     if (m_webGraphicsLayerClient)
845 #endif
846     m_webGraphicsLayerClient->updateTile(id(), tileID, updateInfo, targetRect);
847 }
848
849 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
850 void WebGraphicsLayer::freePlatformSurface(int platformSurfaceID)
851 {
852     if (m_canvasPlatformLayer)
853         m_canvasPlatformLayer->freePlatformSurface(platformSurfaceID);
854 }
855
856 void WebGraphicsLayer::removePlatformSurface(int platformSurfaceID)
857 {
858     if (m_canvasPlatformLayer)
859         m_canvasPlatformLayer->removePlatformSurface(platformSurfaceID);
860 #if ENABLE(TIZEN_CANVAS_CAIRO_GLES_RENDERING) || ENABLE(TIZEN_CANVAS_SURFACE_LOCKING)
861     if (m_canvasPlatformLayer && m_layerInfo.contentType == WebKit::WebLayerInfo::Canvas2DContentType)
862         m_canvasNeedsDisplay = true;
863 #endif
864 #if ENABLE(WEBGL)
865     if (m_canvasPlatformLayer && m_layerInfo.contentType == WebKit::WebLayerInfo::Canvas3DContentType)
866         m_canvasNeedsDisplay = true;
867 #endif
868 }
869
870 bool WebGraphicsLayer::swapPlatformSurfaces()
871 {
872     if (m_canvasPlatformLayer)
873         return m_canvasPlatformLayer->swapPlatformSurfaces();
874     return true;
875 }
876 #endif
877
878 #if ENABLE(TIZEN_ACCELERATED_2D_CANVAS_EFL)
879 void WebGraphicsLayer::flushPlatformSurfaces()
880 {
881     if (m_canvasPlatformLayer)
882         return m_canvasPlatformLayer->flushPlatformSurfaces();
883 }
884 #endif
885
886 void WebGraphicsLayer::removeTile(int tileID)
887 {
888 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
889     if (m_webGraphicsLayerClient)
890 #endif
891     m_webGraphicsLayerClient->removeTile(id(), tileID);
892 }
893
894 void WebGraphicsLayer::updateContentBuffers()
895 {
896     if (!drawsContent()) {
897         m_mainBackingStore.clear();
898         m_previousBackingStore.clear();
899         return;
900     }
901
902     m_inUpdateMode = true;
903     // This is the only place we (re)create the main tiled backing store, once we
904     // have a remote client and we are ready to send our data to the UI process.
905     if (!m_mainBackingStore)
906         createBackingStore();
907
908     m_mainBackingStore->updateTileBuffers();
909     m_inUpdateMode = false;
910
911     // The previous backing store is kept around to avoid flickering between
912     // removing the existing tiles and painting the new ones. The first time
913     // the visibleRect is full painted we remove the previous backing store.
914     if (m_mainBackingStore->visibleAreaIsCovered())
915         m_previousBackingStore.clear();
916 }
917
918 void WebGraphicsLayer::purgeBackingStores()
919 {
920     m_mainBackingStore.clear();
921     m_previousBackingStore.clear();
922
923 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
924     if (m_webGraphicsLayerClient)
925 #endif
926     if (m_layerInfo.imageBackingStoreID) {
927         m_webGraphicsLayerClient->releaseImageBackingStore(m_layerInfo.imageBackingStoreID);
928         m_layerInfo.imageBackingStoreID = 0;
929     }
930
931     didChangeLayerState();
932     didChangeChildren();
933 }
934
935 void WebGraphicsLayer::setWebGraphicsLayerClient(WebKit::WebGraphicsLayerClient* client)
936 {
937     if (m_webGraphicsLayerClient == client)
938         return;
939
940     if (WebGraphicsLayer* replica = toWebGraphicsLayer(replicaLayer()))
941         replica->setWebGraphicsLayerClient(client);
942     if (WebGraphicsLayer* mask = toWebGraphicsLayer(maskLayer()))
943         mask->setWebGraphicsLayerClient(client);
944     for (size_t i = 0; i < children().size(); ++i) {
945         WebGraphicsLayer* layer = toWebGraphicsLayer(this->children()[i]);
946         layer->setWebGraphicsLayerClient(client);
947     }
948
949     // We have to release resources on the UI process here if the remote client has changed or is removed.
950     if (m_webGraphicsLayerClient) {
951 #if ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
952         didChangeLayerState();
953         didChangeChildren();
954         m_shouldSyncBackingStore = true;
955 #else
956         purgeBackingStores();
957 #endif // ENABLE(TIZEN_DONT_PURGE_PLATFORM_SURFACE_BACKINGSTORE_ON_NODE_DETACH_FROM_WEBLAYER_TREE)
958         m_webGraphicsLayerClient->detachLayer(this);
959     }
960     m_webGraphicsLayerClient = client;
961     if (client)
962         client->attachLayer(this);
963 }
964
965 void WebGraphicsLayer::adjustVisibleRect()
966 {
967     if (m_mainBackingStore)
968         m_mainBackingStore->coverWithTilesIfNeeded();
969 }
970
971 bool WebGraphicsLayer::hasPendingVisibleChanges()
972 {
973     if (opacity() < 0.01 && !m_animations.hasActiveAnimationsOfType(AnimatedPropertyOpacity))
974         return false;
975
976     for (size_t i = 0; i < children().size(); ++i) {
977         if (toWebGraphicsLayer(children()[i])->hasPendingVisibleChanges())
978             return true;
979     }
980
981     if (!m_shouldSyncLayerState && !m_shouldSyncChildren && !m_shouldSyncFilters && !m_shouldSyncAnimations && !m_canvasNeedsDisplay)
982         return false;
983
984     return tiledBackingStoreVisibleRect().intersects(tiledBackingStoreContentsRect());
985 }
986
987 void WebGraphicsLayer::computeTransformedVisibleRect()
988 {
989     // When we have a transform animation, we need to update visible rect every frame to adjust the visible rect of a backing store.
990     if (!m_shouldUpdateVisibleRect && !m_movingVisibleRect)
991         return;
992
993     m_shouldUpdateVisibleRect = false;
994     TransformationMatrix currentTransform = transform();
995     if (m_movingVisibleRect) {
996         m_layerSettledTransform.setLocalTransform(currentTransform);
997         m_layerSettledTransform.setPosition(position());
998         m_layerSettledTransform.setAnchorPoint(anchorPoint());
999         m_layerSettledTransform.setSize(size());
1000         m_layerSettledTransform.setFlattening(!preserves3D());
1001         m_layerSettledTransform.setChildrenTransform(childrenTransform());
1002         m_layerSettledTransform.combineTransforms(parent() ? toWebGraphicsLayer(parent())->m_layerTransform.combinedForChildren() : TransformationMatrix());
1003
1004         client()->getCurrentTransform(this, currentTransform);
1005     }
1006
1007     m_layerTransform.setLocalTransform(currentTransform);
1008     m_layerTransform.setPosition(position());
1009     m_layerTransform.setAnchorPoint(anchorPoint());
1010     m_layerTransform.setSize(size());
1011     m_layerTransform.setFlattening(!preserves3D());
1012     m_layerTransform.setChildrenTransform(childrenTransform());
1013     m_layerTransform.combineTransforms(parent() ? toWebGraphicsLayer(parent())->m_layerTransform.combinedForChildren() : TransformationMatrix());
1014
1015     // The combined transform will be used in tiledBackingStoreVisibleRect.
1016     adjustVisibleRect();
1017     adjustContentsScale();
1018 }
1019
1020 static PassOwnPtr<GraphicsLayer> createWebGraphicsLayer(GraphicsLayerClient* client)
1021 {
1022     return adoptPtr(new WebGraphicsLayer(client));
1023 }
1024
1025 void WebGraphicsLayer::initFactory()
1026 {
1027     GraphicsLayer::setGraphicsLayerFactory(createWebGraphicsLayer);
1028 }
1029
1030 bool WebGraphicsLayer::selfOrAncestorHasActiveTransformAnimation()
1031 {
1032     if (m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
1033         return true;
1034
1035     if (!parent())
1036         return false;
1037
1038     return toWebGraphicsLayer(parent())->selfOrAncestorHasActiveTransformAnimation();
1039 }
1040
1041 bool WebGraphicsLayer::selfOrAncestorHaveNonAffineTransforms()
1042 {
1043     if (m_animations.hasActiveAnimationsOfType(AnimatedPropertyWebkitTransform))
1044         return true;
1045
1046     if (!m_layerTransform.combined().isAffine())
1047         return true;
1048
1049     return false;
1050 }
1051
1052 bool WebGraphicsLayer::addAnimation(const KeyframeValueList& valueList, const IntSize& boxSize, const Animation* anim, const String& keyframesName, double timeOffset)
1053 {
1054     ASSERT(!keyframesName.isEmpty());
1055
1056     if (!anim || anim->isEmptyOrZeroDuration() || valueList.size() < 2 || (valueList.property() != AnimatedPropertyWebkitTransform && valueList.property() != AnimatedPropertyOpacity))
1057         return false;
1058
1059     bool listsMatch = false;
1060     bool ignoredHasBigRotation;
1061
1062     if (valueList.property() == AnimatedPropertyWebkitTransform)
1063         listsMatch = validateTransformOperations(valueList, ignoredHasBigRotation) >= 0;
1064
1065     m_animations.add(GraphicsLayerAnimation(keyframesName, valueList, boxSize, anim, WTF::currentTime() - timeOffset, listsMatch));
1066     m_animationStartedTimer.startOneShot(0);
1067 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
1068     didChangeAnimations();
1069 #endif
1070     didChangeLayerState();
1071     return true;
1072 }
1073
1074 void WebGraphicsLayer::pauseAnimation(const String& animationName, double timeOffset)
1075 {
1076     m_animations.pause(animationName, timeOffset);
1077     didChangeAnimations();
1078 }
1079
1080 void WebGraphicsLayer::removeAnimation(const String& animationName)
1081 {
1082     m_animations.remove(animationName);
1083     didChangeAnimations();
1084 }
1085
1086 void WebGraphicsLayer::animationStartedTimerFired(Timer<WebGraphicsLayer>*)
1087 {
1088     client()->notifyAnimationStarted(this, /* DOM time */ WTF::currentTime());
1089 }
1090
1091 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
1092 void WebGraphicsLayer::setIsOverflow(const bool b)
1093 {
1094     if (m_isOverflow == b)
1095         return;
1096
1097     m_isOverflow = b;
1098     didChangeLayerState();
1099 }
1100 #endif
1101
1102 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
1103 void WebGraphicsLayer::startAnimation()
1104 {
1105     setOpacity(1.0);
1106     if (m_animationTimer.isActive())
1107         m_animationTimer.stop();
1108     m_animationTimer.startOneShot(1.0);
1109 }
1110
1111 void WebGraphicsLayer::animationTimerFired(Timer<WebGraphicsLayer>*)
1112 {
1113     setOpacity(0.0);
1114 }
1115 #endif
1116
1117 #if ENABLE(TIZEN_CUTOFF_TILES_OVER_MEMORY_LIMIT)
1118 Vector<TileCutOffInfo> WebGraphicsLayer::getCutOffInfoList()
1119 {
1120     if (!m_mainBackingStore)
1121         return Vector<TileCutOffInfo>();
1122
1123     return m_mainBackingStore->getCutOffInfoList();
1124 }
1125
1126 void WebGraphicsLayer::setCutOffDistance(double distance)
1127 {
1128     if (!m_mainBackingStore)
1129         return;
1130
1131     m_mainBackingStore->setCutOffDistance(distance);
1132 }
1133 #endif
1134
1135 #if ENABLE(TIZEN_CSS_OVERFLOW_CLIPPING_BACKING_STORE)
1136 FloatRect WebGraphicsLayer::clippingBounds()
1137 {
1138     LayoutRect layerRect = LayoutRect(GraphicsLayer::position(),GraphicsLayer::size());
1139     return enclosingIntRect(m_layerTransform.combined().clampedBoundsOfProjectedQuad(FloatQuad(FloatRect(pixelSnappedIntRect(layerRect)))));
1140 }
1141 #endif
1142
1143 #if ENABLE(TIZEN_WEBKIT2_MEMORY_SAVING_MODE)
1144 bool WebGraphicsLayer::memorySavingModeEnabled()
1145 {
1146     return WebProcess::shared().memorySavingModeEnabled();
1147 }
1148 #endif
1149
1150 #if ENABLE(TIZEN_WEBKIT2_DEBUG_BORDERS)
1151 bool WebGraphicsLayer::drawTileInfo() const
1152 {
1153     static bool drawTileInfo = String(getenv("TIZEN_WEBKIT_SHOW_COMPOSITING_DEBUG_VISUALS")) == "1";
1154     return drawTileInfo;
1155 }
1156 #endif
1157 }
1158 #endif