Tizen 2.1 base
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / WebLayerTreeRenderer.cpp
1 /*
2     Copyright (C) 2012 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
24 #include "WebLayerTreeRenderer.h"
25
26 #include "GraphicsLayerTextureMapper.h"
27 #include "LayerBackingStore.h"
28 #include "LayerTreeCoordinatorProxy.h"
29 #include "MessageID.h"
30 #include "ShareableBitmap.h"
31 #include "TextureMapper.h"
32 #include "TextureMapperBackingStore.h"
33 #include "TextureMapperGL.h"
34 #include "TextureMapperLayer.h"
35 #include "UpdateInfo.h"
36 #if !ENABLE(TIZEN_WEBKIT2_TILED_AC)
37 #include <OpenGLShims.h>
38 #endif
39 #include <WebCore/Logging.h>
40 #include <wtf/Atomics.h>
41 #include <wtf/MainThread.h>
42
43 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
44 #include "LayerTreeCoordinatorMessages.h"
45 #include "TextureMapperGL.h"
46 #include "WebPageProxy.h"
47 #include "WebProcessProxy.h"
48 #endif
49
50 namespace WebKit {
51
52 using namespace WebCore;
53
54 template<class T> class MainThreadGuardedInvoker {
55 public:
56     static void call(PassRefPtr<T> objectToGuard, const Function<void()>& function)
57     {
58         MainThreadGuardedInvoker<T>* invoker = new MainThreadGuardedInvoker<T>(objectToGuard, function);
59         callOnMainThread(invoke, invoker);
60     }
61
62 private:
63     MainThreadGuardedInvoker(PassRefPtr<T> object, const Function<void()>& newFunction)
64         : objectToGuard(object)
65         , function(newFunction)
66     {
67     }
68
69     RefPtr<T> objectToGuard;
70     Function<void()> function;
71     static void invoke(void* data)
72     {
73         MainThreadGuardedInvoker<T>* invoker = static_cast<MainThreadGuardedInvoker<T>*>(data);
74         invoker->function();
75         delete invoker;
76     }
77 };
78
79 void WebLayerTreeRenderer::callOnMainThread(const Function<void()>& function)
80 {
81     if (isMainThread())
82         function();
83     else
84         MainThreadGuardedInvoker<WebLayerTreeRenderer>::call(this, function);
85 }
86
87 static FloatPoint boundedScrollPosition(const FloatPoint& scrollPosition, const FloatRect& visibleContentRect, const FloatSize& contentSize)
88 {
89 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
90     float scrollPositionX = std::max(scrollPosition.x(), 0.0f);
91     scrollPositionX = std::min(scrollPositionX, contentSize.width());
92
93     float scrollPositionY = std::max(scrollPosition.y(), 0.0f);
94     scrollPositionY = std::min(scrollPositionY, contentSize.height());
95
96     // Prevent negative scroll position.
97     scrollPositionX = std::max(scrollPositionX, 0.0f);
98     scrollPositionY = std::max(scrollPositionY, 0.0f);
99 #else
100     float scrollPositionX = std::max(scrollPosition.x(), 0.0f);
101     scrollPositionX = std::min(scrollPositionX, contentSize.width() - visibleContentRect.width());
102
103     float scrollPositionY = std::max(scrollPosition.y(), 0.0f);
104     scrollPositionY = std::min(scrollPositionY, contentSize.height() - visibleContentRect.height());
105 #endif
106
107     return FloatPoint(scrollPositionX, scrollPositionY);
108 }
109
110 WebLayerTreeRenderer::WebLayerTreeRenderer(LayerTreeCoordinatorProxy* layerTreeCoordinatorProxy)
111     : m_contentsScale(1)
112     , m_layerTreeCoordinatorProxy(layerTreeCoordinatorProxy)
113     , m_rootLayerID(InvalidWebLayerID)
114     , m_isActive(false)
115     , m_animationsLocked(false)
116 #if ENABLE(TIZEN_WEBKIT2_DIRECT_RENDERING)
117     , m_angle(0)
118 #endif
119 #if PLATFORM(EFL)
120     , m_updateViewportTimer(RunLoop::current(), this, &WebLayerTreeRenderer::updateViewportFired)
121 #endif
122 {
123 }
124
125 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
126 WebLayerTreeRenderer::WebLayerTreeRenderer(LayerTreeCoordinatorProxy* layerTreeCoordinatorProxy, DrawingAreaProxy* drawingAreaProxy, bool isGLMode)
127     : m_contentsScale(1)
128     , m_layerTreeCoordinatorProxy(layerTreeCoordinatorProxy)
129     , m_rootLayerID(InvalidWebLayerID)
130     , m_isActive(false)
131     , m_animationsLocked(false)
132 #if ENABLE(TIZEN_WEBKIT2_DIRECT_RENDERING)
133     , m_angle(0)
134 #endif
135     , m_drawingAreaProxy(drawingAreaProxy)
136     , m_isGLMode(isGLMode)
137 #if PLATFORM(EFL)
138     , m_updateViewportTimer(RunLoop::current(), this, &WebLayerTreeRenderer::updateViewportFired)
139 #endif
140 {
141 }
142 #endif
143
144 WebLayerTreeRenderer::~WebLayerTreeRenderer()
145 {
146 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE_BACKUP_IMAGE)
147     if (m_backupTexture && m_backupTexture->isValid()) {
148         m_backupTexture.release();
149         m_backupTexture = 0;
150     }
151 #endif
152 }
153
154 PassOwnPtr<GraphicsLayer> WebLayerTreeRenderer::createLayer(WebLayerID layerID)
155 {
156     GraphicsLayer* newLayer = new GraphicsLayerTextureMapper(this);
157     TextureMapperLayer* layer = toTextureMapperLayer(newLayer);
158     layer->setShouldUpdateBackingStoreFromLayer(false);
159
160 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
161     LOG(AcceleratedCompositing, "[UI ] create layer %u @WebLayerTreeRenderer::createLayer \n", layerID);
162 #endif
163
164     return adoptPtr(newLayer);
165 }
166
167 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE_BACKUP_IMAGE)
168 void WebLayerTreeRenderer::paintToBackupTexture(const TransformationMatrix& matrix, float opacity, const FloatRect& clipRect, const WebCore::Color& bgColor)
169 {
170     if (!m_textureMapper)
171         m_textureMapper = TextureMapper::create(TextureMapper::OpenGLMode);
172     ASSERT(m_textureMapper->accelerationMode() == TextureMapper::OpenGLMode);
173
174     adjustPositionForFixedLayers();
175 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION_ON_UI_SIDE)
176     adjustPositionForOverflowLayers();
177 #endif
178
179     GraphicsLayer* currentRootLayer = rootLayer();
180     if (!currentRootLayer)
181         return;
182
183     TextureMapperLayer* layer = toTextureMapperLayer(currentRootLayer);
184     if (!layer)
185         return;
186
187 #if ENABLE(TIZEN_WEBKIT2_DIRECT_RENDERING)
188     static_cast<TextureMapperGL*>(m_textureMapper.get())->setAngle(m_angle);
189 #endif
190
191     if (!m_backupTexture || m_backupTexture->size() != IntSize(clipRect.width(), clipRect.height()))
192         m_backupTexture = m_textureMapper->acquireTextureFromPool(IntSize(clipRect.width(), clipRect.height()));
193
194     layer->setTextureMapper(m_textureMapper.get());
195     if (!m_animationsLocked)
196         layer->applyAnimationsRecursively();
197     m_textureMapper->bindSurface(m_backupTexture.get());
198     m_textureMapper->beginPainting(0);
199     m_textureMapper->beginClip(TransformationMatrix(), clipRect);
200
201     if (currentRootLayer->opacity() != opacity || currentRootLayer->transform() != matrix) {
202         currentRootLayer->setOpacity(opacity);
203         currentRootLayer->setTransform(matrix);
204         currentRootLayer->syncCompositingStateForThisLayerOnly();
205     }
206
207     glClearColor(bgColor.red(), bgColor.green(), bgColor.blue(), bgColor.alpha());
208     glClear(GL_COLOR_BUFFER_BIT);
209
210 #if ENABLE(TIZEN_TEXTURE_MAPPER_CULLER)
211     layer->paintWithCuller(clipRect);
212 #else
213     layer->paint();
214 #endif
215
216     m_textureMapper->endClip();
217     m_textureMapper->endPainting();
218     m_textureMapper->unbindSurface();
219
220     if (layer->descendantsOrSelfHaveRunningAnimations()) {
221 #if PLATFORM(EFL)
222         m_updateViewportTimer.startOneShot(0);
223 #else
224         callOnMainThread(bind(&WebLayerTreeRenderer::updateViewport, this));
225 #endif
226     }
227 }
228 #endif
229
230 void WebLayerTreeRenderer::paintToCurrentGLContext(const TransformationMatrix& matrix, float opacity, const FloatRect& clipRect, const WebCore::Color& backgroundColor, TextureMapper::PaintFlags PaintFlags)
231 {
232     if (!m_textureMapper)
233         m_textureMapper = TextureMapper::create(TextureMapper::OpenGLMode);
234     ASSERT(m_textureMapper->accelerationMode() == TextureMapper::OpenGLMode);
235
236     adjustPositionForFixedLayers();
237 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION_ON_UI_SIDE)
238     adjustPositionForOverflowLayers();
239 #endif
240     GraphicsLayer* currentRootLayer = rootLayer();
241     if (!currentRootLayer) {
242 #if ENABLE(TIZEN_SET_INITIAL_COLOR_OF_WEBVIEW_EVAS_IMAGE)
243         glClearColor(1, 1, 1, 1);
244         glClear(GL_COLOR_BUFFER_BIT);
245 #endif
246         return;
247     }
248
249     TextureMapperLayer* layer = toTextureMapperLayer(currentRootLayer);
250
251     if (!layer) {
252 #if ENABLE(TIZEN_SET_INITIAL_COLOR_OF_WEBVIEW_EVAS_IMAGE)
253         glClearColor(1, 1, 1, 1);
254         glClear(GL_COLOR_BUFFER_BIT);
255 #endif
256         return;
257     }
258
259 #if ENABLE(TIZEN_WEBKIT2_DIRECT_RENDERING)
260     static_cast<TextureMapperGL*>(m_textureMapper.get())->setAngle(m_angle);
261 #endif
262
263     layer->setTextureMapper(m_textureMapper.get());
264     if (!m_animationsLocked)
265         layer->applyAnimationsRecursively();
266     m_textureMapper->beginPainting(PaintFlags);
267     m_textureMapper->beginClip(TransformationMatrix(), clipRect);
268
269     m_textureMapper->drawSolidColor(clipRect, TransformationMatrix(), backgroundColor);
270
271     if (currentRootLayer->opacity() != opacity || currentRootLayer->transform() != matrix) {
272         currentRootLayer->setOpacity(opacity);
273         currentRootLayer->setTransform(matrix);
274         currentRootLayer->syncCompositingStateForThisLayerOnly();
275     }
276
277 #if ENABLE(TIZEN_TEXTURE_MAPPER_CULLER)
278     layer->paintWithCuller(clipRect);
279 #else
280     layer->paint();
281 #endif
282
283     m_textureMapper->endClip();
284     m_textureMapper->endPainting();
285     if (layer->descendantsOrSelfHaveRunningAnimations()) {
286 #if PLATFORM(EFL)
287         m_updateViewportTimer.startOneShot(0);
288 #else
289         callOnMainThread(bind(&WebLayerTreeRenderer::updateViewport, this));
290 #endif
291     }
292 }
293
294 #if PLATFORM(QT) || PLATFORM(EFL)
295 void WebLayerTreeRenderer::paintToGraphicsContext(BackingStore::PlatformGraphicsContext painter)
296 {
297     if (!m_textureMapper)
298         m_textureMapper = TextureMapper::create();
299     ASSERT(m_textureMapper->accelerationMode() == TextureMapper::SoftwareMode);
300     syncRemoteContent();
301     TextureMapperLayer* layer = toTextureMapperLayer(rootLayer());
302
303     if (!layer)
304         return;
305
306     GraphicsContext graphicsContext(painter);
307     m_textureMapper->setGraphicsContext(&graphicsContext);
308     m_textureMapper->beginPainting();
309     layer->paint();
310     m_textureMapper->endPainting();
311     m_textureMapper->setGraphicsContext(0);
312 }
313 #endif
314
315 void WebLayerTreeRenderer::setContentsSize(const WebCore::FloatSize& contentsSize)
316 {
317     m_contentsSize = contentsSize;
318 }
319
320 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
321 void WebLayerTreeRenderer::setAccurateVisibleContentsPosition(const WebCore::FloatPoint& accurateVisibleContentsPosition)
322 {
323     m_accurateVisibleContentsPosition = accurateVisibleContentsPosition;
324 }
325 #endif
326
327 void WebLayerTreeRenderer::setVisibleContentsRect(const IntRect& rect, float scale, const WebCore::FloatPoint& accurateVisibleContentsPosition)
328 {
329     m_visibleContentsRect = rect;
330     m_contentsScale = scale;
331     m_accurateVisibleContentsPosition = accurateVisibleContentsPosition;
332 }
333
334 #if PLATFORM(EFL)
335 void WebLayerTreeRenderer::updateViewportFired()
336 {
337     callOnMainThread(bind(&WebLayerTreeRenderer::updateViewport, this));
338 }
339 #endif
340
341 void WebLayerTreeRenderer::updateViewport()
342 {
343     if (m_layerTreeCoordinatorProxy)
344         m_layerTreeCoordinatorProxy->updateViewport();
345 }
346
347 void WebLayerTreeRenderer::adjustPositionForFixedLayers()
348 {
349     if (m_fixedLayers.isEmpty())
350         return;
351
352     // Fixed layer positions are updated by the web process when we update the visible contents rect / scroll position.
353     // If we want those layers to follow accurately the viewport when we move between the web process updates, we have to offset
354     // them by the delta between the current position and the position of the viewport used for the last layout.
355     FloatPoint scrollPosition = boundedScrollPosition(FloatPoint(m_accurateVisibleContentsPosition.x() / m_contentsScale, m_accurateVisibleContentsPosition.y() / m_contentsScale), m_visibleContentsRect, m_contentsSize);
356     FloatPoint renderedScrollPosition = boundedScrollPosition(m_renderedContentsScrollPosition, m_visibleContentsRect, m_contentsSize);
357     FloatSize delta = scrollPosition - renderedScrollPosition;
358
359     LayerMap::iterator end = m_fixedLayers.end();
360     for (LayerMap::iterator it = m_fixedLayers.begin(); it != end; ++it)
361         toTextureMapperLayer(it->second)->setScrollPositionDeltaIfNeeded(delta);
362 }
363
364 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION_ON_UI_SIDE)
365 void WebLayerTreeRenderer::adjustPositionForOverflowLayers()
366 {
367     LayerMap::iterator end = m_scrollingContentsLayers.end();
368     for (LayerMap::iterator it = m_scrollingContentsLayers.begin(); it != end; ++it) {
369         GraphicsLayer* contentsLayer = it->second;
370         TextureMapperLayer* textureMapperLayer = contentsLayer ? toTextureMapperLayer(contentsLayer) : 0;
371         if (!textureMapperLayer)
372             continue;
373
374 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
375         const Vector<GraphicsLayer*>& childLayers = contentsLayer->children();
376         for (size_t i = 0; i < childLayers.size(); ++i)
377             if (childLayers[i]->isScrollbar())
378                 toTextureMapperLayer(childLayers[i])->setScrollPositionDeltaIfNeeded(FloatSize(contentsLayer->boundsOrigin().x(), contentsLayer->boundsOrigin().y()));
379 #endif
380         textureMapperLayer->setScrollPositionDeltaIfNeeded(FloatSize(-contentsLayer->boundsOrigin().x(), -contentsLayer->boundsOrigin().y()));
381     }
382 }
383
384 bool WebLayerTreeRenderer::setOffset(const WebLayerID id, const FloatPoint& offset)
385 {
386     LayerMap::iterator it = m_scrollingContentsLayers.find(id);
387     if (it == m_scrollingContentsLayers.end())
388         return false;
389
390     GraphicsLayer* contentsLayer = it->second;
391     GraphicsLayer* scrollingLayer = contentsLayer ? contentsLayer->parent() : 0;
392     if (!scrollingLayer)
393         return false;
394
395     const IntSize contentLayerSize(contentsLayer->size().width(), contentsLayer->size().height());
396     const IntRect boundaryRect(FloatRect(scrollingLayer->position(), scrollingLayer->size()));
397     const IntRect visibleRect(FloatRect(contentsLayer->boundsOrigin(), scrollingLayer->size()));
398
399     IntRect newVisibleRect = visibleRect;
400     newVisibleRect.moveBy(flooredIntPoint(offset));
401
402     if (newVisibleRect.x() < boundaryRect.x())
403         newVisibleRect.setX(boundaryRect.x());
404     if (contentLayerSize.width() - newVisibleRect.x() < boundaryRect.maxX())
405         newVisibleRect.setX(contentLayerSize.width() - boundaryRect.width());
406     if (newVisibleRect.y() < boundaryRect.y())
407         newVisibleRect.setY(boundaryRect.y());
408     if (contentLayerSize.height() - newVisibleRect.y() < boundaryRect.maxY())
409         newVisibleRect.setY(contentLayerSize.height() - boundaryRect.height());
410
411     if (visibleRect == newVisibleRect)
412         return false;
413
414     contentsLayer->setBoundsOrigin(newVisibleRect.location());
415
416     m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::SetVisibleContentsRectAndTrajectoryVectorForLayer(id, visibleRect, offset), m_drawingAreaProxy->page()->pageID());
417
418     return true;
419 }
420
421 void WebLayerTreeRenderer::setVisibleContentsRectForScrollingContentsLayers(const WebCore::IntRect& visibleRect)
422 {
423     LayerMap::iterator end = m_scrollingContentsLayers.end();
424     for (LayerMap::iterator it = m_scrollingContentsLayers.begin(); it != end; ++it) {
425         GraphicsLayer* contentsLayer = it->second;
426         GraphicsLayer* scrollingLayer = contentsLayer ? contentsLayer->parent() : 0;
427         if (!scrollingLayer)
428             continue;
429
430         // FIXME: We might need to check if the each content layer is in viewport.
431         // Simply, we should find out a intersected rect between visibleRect and each scrollingLayer here.
432         const IntRect newVisibleRect(FloatRect(contentsLayer->boundsOrigin(), scrollingLayer->size()));
433
434         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::SetVisibleContentsRectAndTrajectoryVectorForLayer(it->first, newVisibleRect, WebCore::FloatPoint()), m_drawingAreaProxy->page()->pageID());
435     }
436 }
437 #endif
438
439 void WebLayerTreeRenderer::didChangeScrollPosition(const IntPoint& position)
440 {
441     m_pendingRenderedContentsScrollPosition = position;
442 }
443
444 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
445 void WebLayerTreeRenderer::syncCanvas(WebLayerID id, const WebCore::IntSize& canvasSize, uint64_t graphicsSurfaceToken, uint32_t frontBuffer, int flags)
446 #else
447 void WebLayerTreeRenderer::syncCanvas(WebLayerID id, const WebCore::IntSize& canvasSize, uint64_t graphicsSurfaceToken, uint32_t frontBuffer)
448 #endif
449 {
450     if (canvasSize.isEmpty() || !m_textureMapper)
451         return;
452
453 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
454     ensureLayer(id);
455     GraphicsLayer* layer = layerByID(id);
456
457     RefPtr<TextureMapperSurfaceBackingStore> canvasBackingStore;
458     SurfaceBackingStoreMap::iterator it = m_surfaceBackingStores.find(id);
459     if (it == m_surfaceBackingStores.end()) {
460         canvasBackingStore = TextureMapperSurfaceBackingStore::create();
461         m_surfaceBackingStores.set(id, canvasBackingStore);
462     } else
463         canvasBackingStore = it->second;
464
465 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
466     canvasBackingStore->setGraphicsSurface(graphicsSurfaceToken, canvasSize, frontBuffer, flags);
467 #else
468     canvasBackingStore->setGraphicsSurface(graphicsSurfaceToken, canvasSize, frontBuffer);
469 #endif
470     layer->setContentsToMedia(canvasBackingStore.get());
471 #endif
472 }
473
474 void WebLayerTreeRenderer::setLayerChildren(WebLayerID id, const Vector<WebLayerID>& childIDs)
475 {
476     ensureLayer(id);
477     LayerMap::iterator it = m_layers.find(id);
478     GraphicsLayer* layer = it->second;
479     Vector<GraphicsLayer*> children;
480
481     for (size_t i = 0; i < childIDs.size(); ++i) {
482         WebLayerID childID = childIDs[i];
483         GraphicsLayer* child = layerByID(childID);
484         if (!child) {
485             child = createLayer(childID).leakPtr();
486             m_layers.add(childID, child);
487         }
488         children.append(child);
489     }
490     layer->setChildren(children);
491 }
492
493 #if ENABLE(CSS_FILTERS)
494 void WebLayerTreeRenderer::setLayerFilters(WebLayerID id, const FilterOperations& filters)
495 {
496     ensureLayer(id);
497     LayerMap::iterator it = m_layers.find(id);
498     ASSERT(it != m_layers.end());
499
500     GraphicsLayer* layer = it->second;
501     layer->setFilters(filters);
502 }
503 #endif
504
505 void WebLayerTreeRenderer::setLayerState(WebLayerID id, const WebLayerInfo& layerInfo)
506 {
507     ensureLayer(id);
508     LayerMap::iterator it = m_layers.find(id);
509     ASSERT(it != m_layers.end());
510
511     GraphicsLayer* layer = it->second;
512
513     layer->setReplicatedByLayer(layerByID(layerInfo.replica));
514     layer->setMaskLayer(layerByID(layerInfo.mask));
515
516     layer->setPosition(layerInfo.pos);
517     layer->setSize(layerInfo.size);
518     layer->setTransform(layerInfo.transform);
519     layer->setAnchorPoint(layerInfo.anchorPoint);
520     layer->setChildrenTransform(layerInfo.childrenTransform);
521     layer->setBackfaceVisibility(layerInfo.backfaceVisible);
522     layer->setContentsOpaque(layerInfo.contentsOpaque);
523     layer->setContentsRect(layerInfo.contentsRect);
524     layer->setDrawsContent(layerInfo.drawsContent);
525     layer->setContentsVisible(layerInfo.contentsVisible);
526     toGraphicsLayerTextureMapper(layer)->setFixedToViewport(layerInfo.fixedToViewport);
527
528     if (layerInfo.fixedToViewport)
529         m_fixedLayers.add(id, layer);
530     else
531         m_fixedLayers.remove(id);
532
533 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION_ON_UI_SIDE)
534     if (layerInfo.isScrollingContentsLayer) {
535         LayerMap::iterator it = m_scrollingContentsLayers.find(id);
536         if (it == m_scrollingContentsLayers.end() || (layer->boundsOrigin() != layerInfo.boundsOrigin)) {
537             layer->setBoundsOrigin(layerInfo.boundsOrigin);
538             GraphicsLayer* scrollingLayer = layerByID(layerInfo.parent);
539             if (scrollingLayer) {
540                 const IntRect newVisibleRect(FloatRect(layer->boundsOrigin(), scrollingLayer->size()));
541                 m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::SetVisibleContentsRectAndTrajectoryVectorForLayer(id, newVisibleRect, WebCore::FloatPoint()), m_drawingAreaProxy->page()->pageID());
542             }
543         }
544         m_scrollingContentsLayers.add(id, layer);
545     }
546     else
547         m_scrollingContentsLayers.remove(id);
548 #endif
549 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
550     layer->setIsScrollbar(layerInfo.isScrollbar);
551 #endif
552
553     assignImageToLayer(layer, layerInfo.imageBackingStoreID);
554
555     // Never make the root layer clip.
556     layer->setMasksToBounds(layerInfo.isRootLayer ? false : layerInfo.masksToBounds);
557     layer->setOpacity(layerInfo.opacity);
558     layer->setPreserves3D(layerInfo.preserves3D);
559     if (layerInfo.isRootLayer && m_rootLayerID != id)
560         setRootLayerID(id);
561 }
562
563 void WebLayerTreeRenderer::deleteLayer(WebLayerID layerID)
564 {
565     GraphicsLayer* layer = layerByID(layerID);
566     if (!layer)
567         return;
568
569 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
570     LOG(AcceleratedCompositing, "[UI ] delete layer %u @WebLayerTreeRenderer::deleteLayer \n", layerID);
571 #endif
572
573     layer->removeFromParent();
574     m_layers.remove(layerID);
575     m_fixedLayers.remove(layerID);
576 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
577     m_scrollingContentsLayers.remove(layerID);
578 #endif
579 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
580     m_surfaceBackingStores.remove(layerID);
581 #endif
582     delete layer;
583 }
584
585
586 void WebLayerTreeRenderer::ensureLayer(WebLayerID id)
587 {
588     // We have to leak the new layer's pointer and manage it ourselves,
589     // because OwnPtr is not copyable.
590     if (m_layers.find(id) == m_layers.end())
591         m_layers.add(id, createLayer(id).leakPtr());
592 }
593
594 void WebLayerTreeRenderer::setRootLayerID(WebLayerID layerID)
595 {
596     if (layerID == m_rootLayerID)
597         return;
598
599     m_rootLayerID = layerID;
600
601     m_rootLayer->removeAllChildren();
602
603     if (!layerID)
604         return;
605
606     GraphicsLayer* layer = layerByID(layerID);
607     if (!layer)
608         return;
609
610     m_rootLayer->addChild(layer);
611 }
612
613 PassRefPtr<LayerBackingStore> WebLayerTreeRenderer::getBackingStore(WebLayerID id)
614 {
615     TextureMapperLayer* layer = toTextureMapperLayer(layerByID(id));
616     ASSERT(layer);
617     RefPtr<LayerBackingStore> backingStore = static_cast<LayerBackingStore*>(layer->backingStore().get());
618     if (!backingStore) {
619         backingStore = LayerBackingStore::create();
620         layer->setBackingStore(backingStore.get());
621     }
622     ASSERT(backingStore);
623     return backingStore;
624 }
625
626 void WebLayerTreeRenderer::createTile(WebLayerID layerID, int tileID, float scale)
627 {
628 #if OS(TIZEN)
629     if (!layerByID(layerID))
630         return;
631 #endif
632
633     getBackingStore(layerID)->createTile(tileID, scale);
634 }
635
636 void WebLayerTreeRenderer::removeBackingStoreIfNeeded(WebLayerID layerID)
637 {
638     TextureMapperLayer* layer = toTextureMapperLayer(layerByID(layerID));
639     ASSERT(layer);
640     RefPtr<LayerBackingStore> backingStore = static_cast<LayerBackingStore*>(layer->backingStore().get());
641     ASSERT(backingStore);
642     if (backingStore->isEmpty())
643         layer->setBackingStore(0);
644 }
645
646 void WebLayerTreeRenderer::removeTile(WebLayerID layerID, int tileID)
647 {
648 #if OS(TIZEN)
649     // Check whether composited graphics layer already been detached
650     GraphicsLayer* pGraphicsLayer = layerByID(layerID);
651     if (!pGraphicsLayer)
652         return;
653 #endif
654     getBackingStore(layerID)->removeTile(tileID);
655     removeBackingStoreIfNeeded(layerID);
656 }
657
658 #if ENABLE(TIZEN_RUNTIME_BACKEND_SELECTION)
659 void WebLayerTreeRenderer::updateTileWithUpdateInfo(WebLayerID layerID, int tileID, const TileUpdate& update)
660 {
661     updateTile(layerID, tileID, update);
662 }
663 #endif
664
665 void WebLayerTreeRenderer::updateTile(WebLayerID layerID, int tileID, const TileUpdate& update)
666 {
667 #if OS(TIZEN)
668     if (!layerByID(layerID))
669         return;
670 #endif
671
672     RefPtr<LayerBackingStore> backingStore = getBackingStore(layerID);
673     backingStore->updateTile(tileID, update.sourceRect, update.targetRect, update.surface, update.offset);
674     m_backingStoresWithPendingBuffers.add(backingStore);
675 }
676
677 void WebLayerTreeRenderer::createImage(int64_t imageID, PassRefPtr<ShareableBitmap> weakBitmap)
678 {
679     RefPtr<ShareableBitmap> bitmap = weakBitmap;
680     RefPtr<TextureMapperTiledBackingStore> backingStore = TextureMapperTiledBackingStore::create();
681     m_directlyCompositedImages.set(imageID, backingStore);
682     backingStore->updateContents(m_textureMapper.get(), bitmap->createImage().get());
683 }
684
685 void WebLayerTreeRenderer::destroyImage(int64_t imageID)
686 {
687     m_directlyCompositedImages.remove(imageID);
688 }
689
690 void WebLayerTreeRenderer::assignImageToLayer(GraphicsLayer* layer, int64_t imageID)
691 {
692     if (!imageID) {
693         layer->setContentsToMedia(0);
694         return;
695     }
696
697     HashMap<int64_t, RefPtr<TextureMapperBackingStore> >::iterator it = m_directlyCompositedImages.find(imageID);
698     ASSERT(it != m_directlyCompositedImages.end());
699     layer->setContentsToMedia(it->second.get());
700 }
701
702 void WebLayerTreeRenderer::commitTileOperations()
703 {
704     HashSet<RefPtr<LayerBackingStore> >::iterator end = m_backingStoresWithPendingBuffers.end();
705     for (HashSet<RefPtr<LayerBackingStore> >::iterator it = m_backingStoresWithPendingBuffers.begin(); it != end; ++it)
706         (*it)->commitTileOperations(m_textureMapper.get());
707
708     m_backingStoresWithPendingBuffers.clear();
709 }
710
711 void WebLayerTreeRenderer::flushLayerChanges()
712 {
713     m_renderedContentsScrollPosition = m_pendingRenderedContentsScrollPosition;
714
715     // Since the frame has now been rendered, we can safely unlock the animations until the next layout.
716     setAnimationsLocked(false);
717
718     m_rootLayer->syncCompositingState(FloatRect());
719
720     commitTileOperations();
721
722     // The pending tiles state is on its way for the screen, tell the web process to render the next one.
723     callOnMainThread(bind(&WebLayerTreeRenderer::renderNextFrame, this));
724 }
725
726 void WebLayerTreeRenderer::renderNextFrame()
727 {
728     if (m_layerTreeCoordinatorProxy)
729         m_layerTreeCoordinatorProxy->renderNextFrame();
730 }
731
732 void WebLayerTreeRenderer::ensureRootLayer()
733 {
734     if (m_rootLayer)
735         return;
736
737 #if ENABLE(TIZEN_RUNTIME_BACKEND_SELECTION)
738     createTextureMapper();
739 #else
740     if (!m_textureMapper) {
741         m_textureMapper = TextureMapper::create(TextureMapper::OpenGLMode);
742         static_cast<TextureMapperGL*>(m_textureMapper.get())->setEnableEdgeDistanceAntialiasing(true);
743     }
744 #endif
745
746     m_rootLayer = createLayer(InvalidWebLayerID);
747     m_rootLayer->setMasksToBounds(false);
748     m_rootLayer->setDrawsContent(false);
749     m_rootLayer->setAnchorPoint(FloatPoint3D(0, 0, 0));
750
751     // The root layer should not have zero size, or it would be optimized out.
752     m_rootLayer->setSize(FloatSize(1.0, 1.0));
753     toTextureMapperLayer(m_rootLayer.get())->setTextureMapper(m_textureMapper.get());
754 }
755
756 void WebLayerTreeRenderer::syncRemoteContent()
757 {
758     // We enqueue messages and execute them during paint, as they require an active GL context.
759     ensureRootLayer();
760
761     for (size_t i = 0; i < m_renderQueue.size(); ++i)
762         m_renderQueue[i]();
763
764     m_renderQueue.clear();
765 }
766
767 void WebLayerTreeRenderer::purgeGLResources()
768 {
769     TextureMapperLayer* layer = toTextureMapperLayer(rootLayer());
770
771     if (layer)
772         layer->clearBackingStoresRecursive();
773
774     m_directlyCompositedImages.clear();
775 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
776     m_surfaceBackingStores.clear();
777 #endif
778
779     m_rootLayer->removeAllChildren();
780     m_rootLayer.clear();
781     m_rootLayerID = InvalidWebLayerID;
782     m_layers.clear();
783     m_fixedLayers.clear();
784 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
785     m_scrollingContentsLayers.clear();
786 #endif
787     m_textureMapper.clear();
788     m_backingStoresWithPendingBuffers.clear();
789
790     setActive(false);
791
792     callOnMainThread(bind(&WebLayerTreeRenderer::purgeBackingStores, this));
793 }
794
795 void WebLayerTreeRenderer::setLayerAnimations(WebLayerID id, const GraphicsLayerAnimations& animations)
796 {
797     GraphicsLayerTextureMapper* layer = toGraphicsLayerTextureMapper(layerByID(id));
798     if (!layer)
799         return;
800     layer->setAnimations(animations);
801 }
802
803 void WebLayerTreeRenderer::setAnimationsLocked(bool locked)
804 {
805     m_animationsLocked = locked;
806 }
807
808 void WebLayerTreeRenderer::purgeBackingStores()
809 {
810     if (m_layerTreeCoordinatorProxy)
811         m_layerTreeCoordinatorProxy->purgeBackingStores();
812 }
813
814 void WebLayerTreeRenderer::detach()
815 {
816 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
817     m_renderQueue.clear();
818 #endif
819     m_layerTreeCoordinatorProxy = 0;
820 }
821
822 void WebLayerTreeRenderer::appendUpdate(const Function<void()>& function)
823 {
824     if (!m_isActive)
825         return;
826
827     m_renderQueue.append(function);
828 }
829
830 void WebLayerTreeRenderer::setActive(bool active)
831 {
832     if (m_isActive == active)
833         return;
834
835     // Have to clear render queue in both cases.
836     // If there are some updates in queue during activation then those updates are from previous instance of paint node
837     // and cannot be applied to the newly created instance.
838 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
839     if (!active)
840 #endif
841     m_renderQueue.clear();
842     m_isActive = active;
843 }
844
845 #if ENABLE(TIZEN_WEBKIT2_TILED_AC)
846 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
847 void WebLayerTreeRenderer::platformLayerChanged(WebCore::GraphicsLayer*, WebCore::PlatformLayer* oldPlatformLayer, WebCore::PlatformLayer* newPlatformLayer)
848 {
849     if (m_isActive)
850         renderNextFrame();
851 }
852 #endif
853
854 void WebLayerTreeRenderer::clearBackingStores()
855 {
856     LayerMap::iterator end = m_layers.end();
857     for(LayerMap::iterator it = m_layers.begin(); it != end; ++it)
858         toTextureMapperLayer(it->second)->clearBackingStore();
859
860     m_directlyCompositedImages.clear();
861     m_backingStoresWithPendingBuffers.clear();
862 }
863 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC)
864
865 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE_BACKUP_IMAGE)
866 void WebLayerTreeRenderer::showBackupTexture(const TransformationMatrix& matrix, float opacity, const FloatRect& clipRect)
867 {
868     if (!m_textureMapper)
869         m_textureMapper = TextureMapper::create(TextureMapper::OpenGLMode);
870     ASSERT(m_textureMapper->accelerationMode() == TextureMapper::OpenGLMode);
871
872     if (!m_backupTexture
873         || !(m_backupTexture->isValid())
874         || m_backupTexture->size().width() != clipRect.width()
875         || m_backupTexture->size().height() != clipRect.height())
876         return;
877
878     m_textureMapper->beginPainting(1);
879     m_textureMapper->beginClip(TransformationMatrix(), clipRect);
880
881     IntSize viewportSize(clipRect.width(), clipRect.height());
882     const BitmapTextureGL* textureGL = static_cast<const BitmapTextureGL*>(m_backupTexture.get());
883     m_textureMapper->drawTexture(textureGL->id(), 0, viewportSize, clipRect, matrix, 1.0, 0, viewportSize, false);
884
885     m_textureMapper->endClip();
886     m_textureMapper->endPainting();
887 }
888 #endif
889
890 #if ENABLE(TIZEN_RUNTIME_BACKEND_SELECTION)
891 void WebLayerTreeRenderer::createTextureMapper()
892 {
893     if (!m_textureMapper)
894         m_textureMapper = m_isGLMode ? TextureMapper::create(TextureMapper::OpenGLMode) : TextureMapper::create();
895
896     if (m_isGLMode)
897         ASSERT(m_textureMapper->accelerationMode() == TextureMapper::OpenGLMode);
898     else
899         ASSERT(m_textureMapper->accelerationMode() == TextureMapper::SoftwareMode);
900 }
901 #endif
902
903 #if ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
904 WebLayerTreeRendererTizen::WebLayerTreeRendererTizen(LayerTreeCoordinatorProxy* layerTreeCoordinatorProxy, DrawingAreaProxy* drawingAreaProxy)
905     : WebLayerTreeRenderer(layerTreeCoordinatorProxy, drawingAreaProxy)
906 {
907 }
908
909 void WebLayerTreeRendererTizen::purgeGLResources()
910 {
911     TextureMapperLayer* layer = toTextureMapperLayer(rootLayer());
912
913     if (layer)
914         layer->clearBackingStoresRecursive();
915
916     clearPlatformLayerPlatformSurfaces();
917
918     m_directlyCompositedImages.clear();
919 #if USE(GRAPHICS_SURFACE)
920     m_surfaceBackingStores.clear();
921 #endif
922
923     m_rootLayer->removeAllChildren();
924     m_rootLayer.clear();
925     m_rootLayerID = InvalidWebLayerID;
926     m_layers.clear();
927     m_fixedLayers.clear();
928 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
929     m_scrollingContentsLayers.clear();
930 #endif
931     m_textureMapper.clear();
932     m_backingStoresWithPendingBuffers.clear();
933
934     setActive(false);
935
936     callOnMainThread(bind(&WebLayerTreeRendererTizen::purgeBackingStores, this));
937 }
938
939 void WebLayerTreeRendererTizen::syncCanvas(WebLayerID id, const WebCore::IntSize& canvasSize, uint64_t graphicsSurfaceToken, uint32_t frontBuffer, int flags)
940 {
941     if (canvasSize.isEmpty() || !m_textureMapper)
942         return;
943
944 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
945     ensureLayer(id);
946     GraphicsLayer* layer = layerByID(id);
947
948     RefPtr<TextureMapperSurfaceBackingStore> canvasBackingStore;
949     SurfaceBackingStoreMap::iterator it = m_surfaceBackingStores.find(id);
950     if (it == m_surfaceBackingStores.end()) {
951         canvasBackingStore = TextureMapperSurfaceBackingStore::create();
952         m_surfaceBackingStores.set(id, canvasBackingStore);
953     } else {
954         canvasBackingStore = it->second;
955
956         if ((canvasBackingStore->graphicsSurfaceFrontBuffer() > 0 && canvasBackingStore->graphicsSurfaceFrontBuffer() != frontBuffer) || flags & GraphicsSurface::Is2D) {
957             FreePlatformSurfaceData data;
958             data.platformSurfaceId = canvasBackingStore->graphicsSurfaceFrontBuffer();
959             data.layerID = id;
960             m_freePlatformSurfaces.append(data);
961         }
962     }
963     m_platformLayerPlatformSurfaces.set(frontBuffer, id);
964     canvasBackingStore->setGraphicsSurface(graphicsSurfaceToken, canvasSize, frontBuffer, flags);
965     layer->setContentsOpaque(!(flags & GraphicsSurface::Alpha));
966     layer->setContentsToMedia(canvasBackingStore.get());
967 #endif
968 }
969
970 void WebLayerTreeRendererTizen::detach()
971 {
972     clearBackingStores();
973     WebLayerTreeRenderer::detach();
974 }
975
976 void WebLayerTreeRendererTizen::appendUpdate(const Function<void()>& function)
977 {
978     m_renderQueue.append(function);
979 }
980
981 void WebLayerTreeRendererTizen::setActive(bool active)
982 {
983     if (m_isActive == active)
984         return;
985
986     // Have to clear render queue in both cases.
987     // If there are some updates in queue during activation then those updates are from previous instance of paint node
988     // and cannot be applied to the newly created instance.
989     if(!active)
990         m_renderQueue.clear();
991     m_isActive = active;
992 }
993
994 void WebLayerTreeRendererTizen::deleteLayer(WebLayerID layerID)
995 {
996     GraphicsLayer* layer = layerByID(layerID);
997     if (!layer)
998         return;
999
1000     LOG(AcceleratedCompositing, "[UI ] delete layer %u @WebLayerTreeRenderer::deleteLayer \n", layerID);
1001     layer->removeFromParent();
1002     m_layers.remove(layerID);
1003     m_fixedLayers.remove(layerID);
1004 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
1005     m_scrollingContentsLayers.remove(layerID);
1006 #endif
1007
1008 #if USE(GRAPHICS_SURFACE) || ENABLE(TIZEN_CANVAS_GRAPHICS_SURFACE)
1009     SurfaceBackingStoreMap::iterator it = m_surfaceBackingStores.find(layerID);
1010     if (it != m_surfaceBackingStores.end())
1011         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::FreePlatformSurface(layerID, it->second->graphicsSurfaceFrontBuffer()), m_drawingAreaProxy->page()->pageID());
1012
1013     m_surfaceBackingStores.remove(layerID);
1014 #endif
1015     delete layer;
1016 }
1017
1018 void WebLayerTreeRendererTizen::setLayerState(WebLayerID id, const WebLayerInfo& layerInfo)
1019 {
1020     ensureLayer(id);
1021     LayerMap::iterator it = m_layers.find(id);
1022     ASSERT(it != m_layers.end());
1023
1024     GraphicsLayer* layer = it->second;
1025
1026     layer->setReplicatedByLayer(layerByID(layerInfo.replica));
1027     layer->setMaskLayer(layerByID(layerInfo.mask));
1028
1029     layer->setPosition(layerInfo.pos);
1030     layer->setSize(layerInfo.size);
1031     layer->setTransform(layerInfo.transform);
1032     layer->setAnchorPoint(layerInfo.anchorPoint);
1033     layer->setChildrenTransform(layerInfo.childrenTransform);
1034     layer->setBackfaceVisibility(layerInfo.backfaceVisible);
1035     layer->setContentsOpaque(layerInfo.contentsOpaque);
1036     layer->setContentsRect(layerInfo.contentsRect);
1037     layer->setDrawsContent(layerInfo.drawsContent);
1038     layer->setContentsVisible(layerInfo.contentsVisible);
1039     toGraphicsLayerTextureMapper(layer)->setFixedToViewport(layerInfo.fixedToViewport);
1040
1041     if (layerInfo.fixedToViewport)
1042         m_fixedLayers.add(id, layer);
1043     else
1044         m_fixedLayers.remove(id);
1045
1046 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION_ON_UI_SIDE)
1047     if (layerInfo.isScrollingContentsLayer) {
1048         LayerMap::iterator it = m_scrollingContentsLayers.find(id);
1049         if (it == m_scrollingContentsLayers.end() || (layer->boundsOrigin() != layerInfo.boundsOrigin)) {
1050             layer->setBoundsOrigin(layerInfo.boundsOrigin);
1051             GraphicsLayer* scrollingLayer = layerByID(layerInfo.parent);
1052             if (scrollingLayer) {
1053                 const IntRect newVisibleRect(FloatRect(layer->boundsOrigin(), scrollingLayer->size()));
1054                 m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::SetVisibleContentsRectAndTrajectoryVectorForLayer(id, newVisibleRect, WebCore::FloatPoint()), m_drawingAreaProxy->page()->pageID());
1055             }
1056         }
1057         m_scrollingContentsLayers.add(id, layer);
1058     }
1059     else
1060         m_scrollingContentsLayers.remove(id);
1061 #endif
1062 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_SCROLLBAR)
1063     layer->setIsScrollbar(layerInfo.isScrollbar);
1064 #endif
1065
1066     assignImageToLayer(layer, layerInfo.imageBackingStoreID);
1067
1068     // Never make the root layer clip.
1069     layer->setMasksToBounds(layerInfo.isRootLayer ? false : layerInfo.masksToBounds);
1070     layer->setOpacity(layerInfo.opacity);
1071     layer->setPreserves3D(layerInfo.preserves3D);
1072     if (layerInfo.isRootLayer && m_rootLayerID != id)
1073         setRootLayerID(id);
1074 }
1075
1076 void WebLayerTreeRendererTizen::removeTile(WebLayerID layerID, int tileID)
1077 {
1078 #if OS(TIZEN)
1079     // Check whether composited graphics layer already been detached
1080     GraphicsLayer* pGraphicsLayer = layerByID(layerID);
1081     if (!pGraphicsLayer) {
1082         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::FreePlatformSurfaceByTileID(tileID), m_drawingAreaProxy->page()->pageID());
1083         LOG(TiledAC, "layerID %d already has been deleted.", layerID);
1084         return;
1085     }
1086 #endif
1087
1088     LayerBackingStoreTizen* backingStore = static_cast<LayerBackingStoreTizen*>(getBackingStore(layerID).get());
1089     if (!backingStore)
1090         return;
1091
1092     if (backingStore->tilePlatformSurfaceId(tileID) > 0)
1093         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::FreePlatformSurface(layerID, backingStore->tilePlatformSurfaceId(tileID)), m_drawingAreaProxy->page()->pageID());
1094
1095     backingStore->removeTile(tileID);
1096 }
1097
1098 #if ENABLE(TIZEN_RUNTIME_BACKEND_SELECTION)
1099 void WebLayerTreeRendererTizen::createTextureMapper()
1100 {
1101     if (!m_textureMapper)
1102         m_textureMapper = TextureMapper::create(TextureMapper::OpenGLMode);
1103     ASSERT(m_textureMapper->accelerationMode() == TextureMapper::OpenGLMode);
1104 }
1105
1106 void WebLayerTreeRendererTizen::updateTileWithUpdateInfo(WebLayerID layerID, int tileID, const TileUpdate& updateInfo)
1107 {
1108     updatePlatformSurfaceTile(layerID, tileID, updateInfo.sourceRect, updateInfo.platformSurfaceID, updateInfo.platformSurfaceSize);
1109 }
1110 #endif
1111
1112 void WebLayerTreeRendererTizen::flushLayerChanges()
1113 {
1114     m_renderedContentsScrollPosition = m_pendingRenderedContentsScrollPosition;
1115
1116     // Since the frame has now been rendered, we can safely unlock the animations until the next layout.
1117     setAnimationsLocked(false);
1118
1119     m_rootLayer->syncCompositingState(FloatRect());
1120
1121     freePlatformSurface();
1122     commitTileOperations();
1123
1124     // The pending tiles state is on its way for the screen, tell the web process to render the next one.
1125     callOnMainThread(bind(&WebLayerTreeRendererTizen::renderNextFrame, this));
1126 }
1127
1128 void WebLayerTreeRendererTizen::clearBackingStores()
1129 {
1130     LayerMap::iterator end = m_layers.end();
1131     for(LayerMap::iterator it = m_layers.begin(); it != end; ++it)
1132         toTextureMapperLayer(it->second)->clearBackingStore();
1133
1134     clearPlatformLayerPlatformSurfaces();
1135
1136     m_directlyCompositedImages.clear();
1137     m_backingStoresWithPendingBuffers.clear();
1138
1139     freePlatformSurfacesAfterClearingBackingStores();
1140 }
1141
1142 PassRefPtr<LayerBackingStore> WebLayerTreeRendererTizen::getBackingStore(WebLayerID id)
1143 {
1144     TextureMapperLayer* layer = toTextureMapperLayer(layerByID(id));
1145     ASSERT(layer);
1146     RefPtr<LayerBackingStoreTizen> backingStore = static_cast<LayerBackingStoreTizen*>(layer->backingStore().get());
1147     if (!backingStore) {
1148         backingStore = LayerBackingStoreTizen::create();
1149         layer->setBackingStore(backingStore.get());
1150         backingStore->setPlatformSurfaceTexturePool(m_drawingAreaProxy);
1151     }
1152     ASSERT(backingStore);
1153     return backingStore;
1154 }
1155
1156 void WebLayerTreeRendererTizen::freePlatformSurface()
1157 {
1158     if (!hasPlatformSurfaceToFree())
1159         return;
1160
1161     HashSet<RefPtr<LayerBackingStore> >::iterator end = m_backingStoresWithPendingBuffers.end();
1162     for (HashSet<RefPtr<LayerBackingStore> >::iterator it = m_backingStoresWithPendingBuffers.begin(); it != end; ++it) {
1163         LayerBackingStoreTizen* backingStore = static_cast<LayerBackingStoreTizen*>((*it).get());
1164         int layerId = backingStore->layerId();
1165         Vector<int> freePlatformSurface = *(backingStore->freePlatformSurfaceTiles());
1166
1167         for (unsigned n = 0; n < backingStore->freePlatformSurfaceTiles()->size(); ++n)
1168             m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::FreePlatformSurface(layerId, freePlatformSurface[n]), m_drawingAreaProxy->page()->pageID());
1169         backingStore->clearFreePlatformSurfaceTiles();
1170     }
1171     m_backingStoresWithPendingBuffers.clear();
1172
1173     // m_freePlatformSurfaces is for platformSurfaces which are not corresponding to platformSurface tiles, like TextureMapperPlatformLayer's.
1174     for (unsigned n = 0; n < m_freePlatformSurfaces.size(); ++n)
1175         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::FreePlatformSurface(m_freePlatformSurfaces[n].layerID, m_freePlatformSurfaces[n].platformSurfaceId), m_drawingAreaProxy->page()->pageID());
1176
1177     m_freePlatformSurfaces.clear();
1178 }
1179
1180 bool WebLayerTreeRendererTizen::hasPlatformSurfaceToFree()
1181 {
1182     if (m_backingStoresWithPendingBuffers.size() > 0)
1183         return true;
1184
1185     if (m_freePlatformSurfaces.isEmpty())
1186         return false;
1187
1188     return true;
1189 }
1190
1191 void WebLayerTreeRendererTizen::updatePlatformSurfaceTile(WebLayerID layerID, int tileID, const IntRect& sourceRect, int platformSurfaceId, const IntSize& platformSurfaceSize)
1192 {
1193     if (!platformSurfaceId)
1194         return;
1195
1196     m_drawingAreaProxy->page()->makeContextCurrent();
1197
1198     RefPtr<LayerBackingStoreTizen> backingStore = static_cast<LayerBackingStoreTizen*>(getBackingStore(layerID).get());
1199     backingStore->updatePlatformSurfaceTile(tileID, sourceRect, sourceRect, layerID, platformSurfaceId, platformSurfaceSize, m_textureMapper.get());
1200     if (!m_backingStoresWithPendingBuffers.contains(backingStore))
1201         m_backingStoresWithPendingBuffers.add(backingStore);
1202 }
1203
1204 void WebLayerTreeRendererTizen::freePlatformSurfacesAfterClearingBackingStores()
1205 {
1206     // Before calling this function, destroying BackingStores should be done,
1207     // because platformSurfacesToFree is filled in ~LayerBackingStore().
1208
1209     RefPtr<PlatformSurfaceTexturePool> platformSurfaceTexturePool = m_drawingAreaProxy->page()->process()->platformSurfaceTexturePool();
1210     Vector<int> platformSurfacesToFree = platformSurfaceTexturePool->platformSurfacesToFree();
1211
1212     for (unsigned n = 0; n < platformSurfacesToFree.size(); ++n)
1213         // Actually, m_rootLayerID is not correct for all platformSurfaces.
1214         // But LayerTreeCoordinator::freePlatformSurface() doesn't care layerID for HTML contents layers
1215         // so any dummy layerID is okay for now.
1216         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::FreePlatformSurface(m_rootLayerID, platformSurfacesToFree[n]), m_drawingAreaProxy->page()->pageID());
1217
1218     platformSurfaceTexturePool->clearPlatformSurfacesToFree();
1219 }
1220
1221 void WebLayerTreeRendererTizen::clearPlatformLayerPlatformSurfaces()
1222 {
1223     PlatformLayerPlatformSurfaceMap::iterator end = m_platformLayerPlatformSurfaces.end();
1224     for (PlatformLayerPlatformSurfaceMap::iterator it = m_platformLayerPlatformSurfaces.begin(); it != end; ++it)
1225         m_drawingAreaProxy->page()->process()->send(Messages::LayerTreeCoordinator::RemovePlatformSurface(it->second, it->first), m_drawingAreaProxy->page()->pageID());
1226
1227     m_platformLayerPlatformSurfaces.clear();
1228 }
1229
1230 void WebLayerTreeRendererTizen::renderNextFrame()
1231 {
1232     WebLayerTreeRenderer::renderNextFrame();
1233 }
1234
1235 void WebLayerTreeRendererTizen::purgeBackingStores()
1236 {
1237     WebLayerTreeRenderer::purgeBackingStores();
1238 }
1239 #endif // ENABLE(TIZEN_WEBKIT2_TILED_AC_SHARED_PLATFORM_SURFACE)
1240
1241 } // namespace WebKit
1242
1243 #endif // USE(UI_SIDE_COMPOSITING)