OSDN Git Service

Merge WebKit at r76408: Fix calls to RenderLayer::scrollToOffset()
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / android / GraphicsLayerAndroid.cpp
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "config.h"
18 #include "GraphicsLayerAndroid.h"
19
20 #if USE(ACCELERATED_COMPOSITING)
21
22 #include "AndroidAnimation.h"
23 #include "Animation.h"
24 #include "FloatRect.h"
25 #include "GraphicsContext.h"
26 #include "Image.h"
27 #include "Length.h"
28 #include "MediaLayer.h"
29 #include "PlatformBridge.h"
30 #include "PlatformGraphicsContext.h"
31 #include "RenderLayerBacking.h"
32 #include "RenderView.h"
33 #include "RotateTransformOperation.h"
34 #include "ScaleTransformOperation.h"
35 #include "ScrollableLayerAndroid.h"
36 #include "SkCanvas.h"
37 #include "SkLayer.h"
38 #include "TransformationMatrix.h"
39 #include "TranslateTransformOperation.h"
40
41 #include <cutils/log.h>
42 #include <wtf/CurrentTime.h>
43 #include <wtf/text/CString.h>
44
45 #undef LOG
46 #define LOG(...) android_printLog(ANDROID_LOG_DEBUG, "GraphicsLayer", __VA_ARGS__)
47 #define MLOG(...) android_printLog(ANDROID_LOG_DEBUG, "GraphicsLayer", __VA_ARGS__)
48 #define TLOG(...) android_printLog(ANDROID_LOG_DEBUG, "GraphicsLayer", __VA_ARGS__)
49
50 #undef LOG
51 #define LOG(...)
52 #undef MLOG
53 #define MLOG(...)
54 #undef TLOG
55 #define TLOG(...)
56 #undef LAYER_DEBUG
57
58 using namespace std;
59
60 static bool gPaused;
61 static double gPausedDelay;
62
63 namespace WebCore {
64
65 static int gDebugGraphicsLayerAndroidInstances = 0;
66 inline int GraphicsLayerAndroid::instancesCount()
67 {
68     return gDebugGraphicsLayerAndroidInstances;
69 }
70
71 static String propertyIdToString(AnimatedPropertyID property)
72 {
73     switch (property) {
74     case AnimatedPropertyWebkitTransform:
75         return "transform";
76     case AnimatedPropertyOpacity:
77         return "opacity";
78     case AnimatedPropertyBackgroundColor:
79         return "backgroundColor";
80     case AnimatedPropertyInvalid:
81         ASSERT_NOT_REACHED();
82     }
83     ASSERT_NOT_REACHED();
84     return "";
85 }
86
87 PassOwnPtr<GraphicsLayer> GraphicsLayer::create(GraphicsLayerClient* client)
88 {
89     return new GraphicsLayerAndroid(client);
90 }
91
92 SkLength convertLength(Length len)
93 {
94     SkLength length;
95     length.type = SkLength::Undefined;
96     length.value = 0;
97     if (len.type() == WebCore::Percent) {
98         length.type = SkLength::Percent;
99         length.value = len.percent();
100     }
101     if (len.type() == WebCore::Fixed) {
102         length.type = SkLength::Fixed;
103         length.value = len.value();
104     }
105     return length;
106 }
107
108 static RenderLayer* renderLayerFromClient(GraphicsLayerClient* client)
109 {
110     if (client)
111         return static_cast<RenderLayerBacking*>(client)->owningLayer();
112     return 0;
113 }
114
115 GraphicsLayerAndroid::GraphicsLayerAndroid(GraphicsLayerClient* client) :
116     GraphicsLayer(client),
117     m_needsSyncChildren(false),
118     m_needsSyncMask(false),
119     m_needsRepaint(false),
120     m_needsNotifyClient(false),
121     m_haveContents(false),
122     m_haveImage(false),
123     m_newImage(false),
124     m_imageRef(0),
125     m_foregroundLayer(0),
126     m_foregroundClipLayer(0)
127 {
128     RenderLayer* renderLayer = renderLayerFromClient(m_client);
129     m_contentLayer = new LayerAndroid(renderLayer);
130     gDebugGraphicsLayerAndroidInstances++;
131 }
132
133 GraphicsLayerAndroid::~GraphicsLayerAndroid()
134 {
135     m_contentLayer->unref();
136     SkSafeUnref(m_foregroundLayer);
137     SkSafeUnref(m_foregroundClipLayer);
138     gDebugGraphicsLayerAndroidInstances--;
139 }
140
141 void GraphicsLayerAndroid::setName(const String& name)
142 {
143     GraphicsLayer::setName(name);
144 }
145
146 NativeLayer GraphicsLayerAndroid::nativeLayer() const
147 {
148     LOG("(%x) nativeLayer", this);
149     return 0;
150 }
151
152 bool GraphicsLayerAndroid::setChildren(const Vector<GraphicsLayer*>& children)
153 {
154     bool childrenChanged = GraphicsLayer::setChildren(children);
155     if (childrenChanged) {
156         m_needsSyncChildren = true;
157         askForSync();
158     }
159
160     return childrenChanged;
161 }
162
163 void GraphicsLayerAndroid::addChild(GraphicsLayer* childLayer)
164 {
165 #ifndef NDEBUG
166     const String& name = childLayer->name();
167     LOG("(%x) addChild: %x (%s)", this, childLayer, name.latin1().data());
168 #endif
169     GraphicsLayer::addChild(childLayer);
170     m_needsSyncChildren = true;
171     askForSync();
172 }
173
174 void GraphicsLayerAndroid::addChildAtIndex(GraphicsLayer* childLayer, int index)
175 {
176     LOG("(%x) addChild %x AtIndex %d", this, childLayer, index);
177     GraphicsLayer::addChildAtIndex(childLayer, index);
178     m_needsSyncChildren = true;
179     askForSync();
180 }
181
182 void GraphicsLayerAndroid::addChildBelow(GraphicsLayer* childLayer, GraphicsLayer* sibling)
183 {
184     LOG("(%x) addChild %x Below %x", this, childLayer, sibling);
185     GraphicsLayer::addChildBelow(childLayer, sibling);
186     m_needsSyncChildren = true;
187     askForSync();
188 }
189
190 void GraphicsLayerAndroid::addChildAbove(GraphicsLayer* childLayer, GraphicsLayer* sibling)
191 {
192     LOG("(%x) addChild %x Above %x", this, childLayer, sibling);
193     GraphicsLayer::addChildAbove(childLayer, sibling);
194     m_needsSyncChildren = true;
195     askForSync();
196 }
197
198 bool GraphicsLayerAndroid::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
199 {
200     LOG("(%x) replaceChild %x by %x", this, oldChild, newChild);
201     bool ret = GraphicsLayer::replaceChild(oldChild, newChild);
202     if (ret) {
203         m_needsSyncChildren = true;
204         askForSync();
205     }
206     return ret;
207 }
208
209 void GraphicsLayerAndroid::removeFromParent()
210 {
211     LOG("(%x) removeFromParent()", this);
212     GraphicsLayerAndroid* parent = static_cast<GraphicsLayerAndroid*>(m_parent);
213     GraphicsLayer::removeFromParent();
214     // Update the parent's children.
215     if (parent) {
216         parent->m_needsSyncChildren = true;
217         askForSync();
218     }
219 }
220
221 void GraphicsLayerAndroid::updateFixedPosition()
222 {
223     if (!m_client)
224         return;
225
226     RenderLayer* renderLayer = renderLayerFromClient(m_client);
227     RenderView* view = static_cast<RenderView*>(renderLayer->renderer());
228
229     // We will need the Iframe flag in the LayerAndroid tree for fixed position
230     if (view && view->isRenderIFrame())
231         m_contentLayer->setIsIframe(true);
232     // If we are a fixed position layer, just set it
233     if (view->isPositioned() && view->style()->position() == FixedPosition) {
234         // We need to get the passed CSS properties for the element
235         SkLength left, top, right, bottom;
236         left = convertLength(view->style()->left());
237         top = convertLength(view->style()->top());
238         right = convertLength(view->style()->right());
239         bottom = convertLength(view->style()->bottom());
240
241         // We also need to get the margin...
242         SkLength marginLeft, marginTop, marginRight, marginBottom;
243         marginLeft = convertLength(view->style()->marginLeft());
244         marginTop = convertLength(view->style()->marginTop());
245         marginRight = convertLength(view->style()->marginRight());
246         marginBottom = convertLength(view->style()->marginBottom());
247
248         // In order to compute the fixed element's position, we need the width
249         // and height of the element when bottom or right is defined.
250         // And here we should use the non-overflowed value, that means, the
251         // overflowed content (e.g. outset shadow) will not be counted into the
252         // width and height.
253         int w = view->width();
254         int h = view->height();
255
256         int paintingOffsetX = - offsetFromRenderer().width();
257         int paintingOffsetY = - offsetFromRenderer().height();
258
259         SkRect viewRect;
260         viewRect.set(paintingOffsetX, paintingOffsetY, paintingOffsetX + w, paintingOffsetY + h);
261         IntPoint renderLayerPos(renderLayer->x(), renderLayer->y());
262         m_contentLayer->setFixedPosition(left, top, right, bottom,
263                                          marginLeft, marginTop,
264                                          marginRight, marginBottom,
265                                          renderLayerPos,
266                                          viewRect);
267     }
268 }
269
270 void GraphicsLayerAndroid::setPosition(const FloatPoint& point)
271 {
272     if (point == m_position)
273         return;
274
275     GraphicsLayer::setPosition(point);
276
277 #ifdef LAYER_DEBUG_2
278     LOG("(%x) setPosition(%.2f,%.2f) pos(%.2f, %.2f) anchor(%.2f,%.2f) size(%.2f, %.2f)",
279         this, point.x(), point.y(), m_position.x(), m_position.y(),
280         m_anchorPoint.x(), m_anchorPoint.y(), m_size.width(), m_size.height());
281 #endif
282     m_contentLayer->setPosition(point.x(), point.y());
283     askForSync();
284 }
285
286 void GraphicsLayerAndroid::setPreserves3D(bool preserves3D)
287 {
288     if (preserves3D == m_preserves3D)
289         return;
290
291     GraphicsLayer::setPreserves3D(preserves3D);
292     m_contentLayer->setPreserves3D(preserves3D);
293     askForSync();
294 }
295
296 void GraphicsLayerAndroid::setAnchorPoint(const FloatPoint3D& point)
297 {
298     if (point == m_anchorPoint)
299         return;
300     GraphicsLayer::setAnchorPoint(point);
301     m_contentLayer->setAnchorPoint(point.x(), point.y());
302     m_contentLayer->setAnchorPointZ(point.z());
303     askForSync();
304 }
305
306 void GraphicsLayerAndroid::setSize(const FloatSize& size)
307 {
308     if (size == m_size)
309         return;
310     MLOG("(%x) setSize (%.2f,%.2f)", this, size.width(), size.height());
311     GraphicsLayer::setSize(size);
312
313     // If it is a media layer the size may have changed as a result of the media
314     // element (e.g. plugin) gaining focus. Therefore, we must sync the size of
315     // the focus' outline so that our UI thread can draw accordingly.
316     if (m_contentLayer->isMedia() && m_client) {
317         RenderLayer* layer = renderLayerFromClient(m_client);
318         RenderBox* box = layer->renderBox();
319         int outline = box->view()->maximalOutlineSize();
320         static_cast<MediaLayer*>(m_contentLayer)->setOutlineSize(outline);
321         LOG("Media Outline: %d %p %p %p", outline, m_client, layer, box);
322         LOG("Media Size: %g,%g", size.width(), size.height());
323     }
324
325     m_contentLayer->setSize(size.width(), size.height());
326     askForSync();
327 }
328
329 void GraphicsLayerAndroid::setBackfaceVisibility(bool b)
330 {
331     GraphicsLayer::setBackfaceVisibility(b);
332     m_contentLayer->setBackfaceVisibility(b);
333     askForSync();
334 }
335
336 void GraphicsLayerAndroid::setTransform(const TransformationMatrix& t)
337 {
338     if (t == m_transform)
339         return;
340
341     GraphicsLayer::setTransform(t);
342     m_contentLayer->setTransform(t);
343     askForSync();
344 }
345
346 void GraphicsLayerAndroid::setChildrenTransform(const TransformationMatrix& t)
347 {
348     if (t == m_childrenTransform)
349        return;
350     LOG("(%x) setChildrenTransform", this);
351
352     GraphicsLayer::setChildrenTransform(t);
353     m_contentLayer->setChildrenTransform(t);
354     for (unsigned int i = 0; i < m_children.size(); i++) {
355         GraphicsLayer* layer = m_children[i];
356         layer->setTransform(t);
357         if (layer->children().size())
358             layer->setChildrenTransform(t);
359     }
360     askForSync();
361 }
362
363 void GraphicsLayerAndroid::setMaskLayer(GraphicsLayer* layer)
364 {
365     if (layer == m_maskLayer)
366         return;
367
368     GraphicsLayer::setMaskLayer(layer);
369     m_needsSyncMask = true;
370     askForSync();
371 }
372
373 void GraphicsLayerAndroid::setMasksToBounds(bool masksToBounds)
374 {
375     if (masksToBounds == m_masksToBounds)
376         return;
377     GraphicsLayer::setMasksToBounds(masksToBounds);
378     m_needsSyncMask = true;
379     askForSync();
380 }
381
382 void GraphicsLayerAndroid::setDrawsContent(bool drawsContent)
383 {
384     if (drawsContent == m_drawsContent)
385         return;
386     GraphicsLayer::setDrawsContent(drawsContent);
387     if (m_drawsContent) {
388         m_haveContents = true;
389         setNeedsDisplay();
390     }
391     askForSync();
392 }
393
394 void GraphicsLayerAndroid::setBackgroundColor(const Color& color)
395 {
396     if (color == m_backgroundColor)
397         return;
398     LOG("(%x) setBackgroundColor", this);
399     GraphicsLayer::setBackgroundColor(color);
400     SkColor c = SkColorSetARGB(color.alpha(), color.red(), color.green(), color.blue());
401     m_contentLayer->setBackgroundColor(c);
402     m_haveContents = true;
403     askForSync();
404 }
405
406 void GraphicsLayerAndroid::clearBackgroundColor()
407 {
408     LOG("(%x) clearBackgroundColor", this);
409     GraphicsLayer::clearBackgroundColor();
410     askForSync();
411 }
412
413 void GraphicsLayerAndroid::setContentsOpaque(bool opaque)
414 {
415     if (opaque == m_contentsOpaque)
416         return;
417     LOG("(%x) setContentsOpaque (%d)", this, opaque);
418     GraphicsLayer::setContentsOpaque(opaque);
419     m_haveContents = true;
420     askForSync();
421 }
422
423 void GraphicsLayerAndroid::setOpacity(float opacity)
424 {
425     LOG("(%x) setOpacity: %.2f", this, opacity);
426     float clampedOpacity = max(0.0f, min(opacity, 1.0f));
427
428     if (clampedOpacity == m_opacity)
429         return;
430
431     MLOG("(%x) setFinalOpacity: %.2f=>%.2f (%.2f)", this,
432         opacity, clampedOpacity, m_opacity);
433     GraphicsLayer::setOpacity(clampedOpacity);
434     m_contentLayer->setOpacity(clampedOpacity);
435     askForSync();
436 }
437
438 void GraphicsLayerAndroid::setNeedsDisplay()
439 {
440     LOG("(%x) setNeedsDisplay()", this);
441     FloatRect rect(0, 0, m_size.width(), m_size.height());
442     setNeedsDisplayInRect(rect);
443 }
444
445 // Helper to set and clear the painting phase as well as auto restore the
446 // original phase.
447 class PaintingPhase {
448 public:
449     PaintingPhase(GraphicsLayer* layer)
450         : m_layer(layer)
451         , m_originalPhase(layer->paintingPhase()) {}
452
453     ~PaintingPhase()
454     {
455         m_layer->setPaintingPhase(m_originalPhase);
456     }
457
458     void set(GraphicsLayerPaintingPhase phase)
459     {
460         m_layer->setPaintingPhase(phase);
461     }
462
463     void clear(GraphicsLayerPaintingPhase phase)
464     {
465         m_layer->setPaintingPhase(
466                 (GraphicsLayerPaintingPhase) (m_originalPhase & ~phase));
467     }
468 private:
469     GraphicsLayer* m_layer;
470     GraphicsLayerPaintingPhase m_originalPhase;
471 };
472
473 void GraphicsLayerAndroid::updateScrollingLayers()
474 {
475 #if ENABLE(ANDROID_OVERFLOW_SCROLL)
476     RenderLayer* layer = renderLayerFromClient(m_client);
477     if (!layer || !m_haveContents)
478         return;
479     bool hasOverflowScroll = m_foregroundLayer || m_contentLayer->contentIsScrollable();
480     bool layerNeedsOverflow = layer->hasOverflowScroll();
481     bool iframeNeedsOverflow = layer->isRootLayer() &&
482         layer->renderer()->frame()->ownerRenderer() &&
483         layer->renderer()->frame()->view()->hasOverflowScroll();
484
485     if (hasOverflowScroll && (layerNeedsOverflow || iframeNeedsOverflow)) {
486         // Already has overflow layers.
487         return;
488     }
489     if (!hasOverflowScroll && !layerNeedsOverflow && !iframeNeedsOverflow) {
490         // Does not need overflow layers.
491         return;
492     }
493     if (layerNeedsOverflow || iframeNeedsOverflow) {
494         ASSERT(!hasOverflowScroll);
495         if (layerNeedsOverflow) {
496             ASSERT(!m_foregroundLayer && !m_foregroundClipLayer);
497             m_foregroundLayer = new ScrollableLayerAndroid(layer);
498             m_foregroundClipLayer = new LayerAndroid(layer);
499             m_foregroundClipLayer->setMasksToBounds(true);
500             m_foregroundClipLayer->addChild(m_foregroundLayer);
501             m_contentLayer->addChild(m_foregroundClipLayer);
502         } else {
503             ASSERT(iframeNeedsOverflow && !m_contentLayer->contentIsScrollable());
504             // No need to copy the children as they will be removed and synced.
505             m_contentLayer->removeChildren();
506             // Replace the content layer with a scrollable layer.
507             LayerAndroid* layer = new ScrollableLayerAndroid(*m_contentLayer);
508             m_contentLayer->unref();
509             m_contentLayer = layer;
510             if (m_parent) {
511                 // The content layer has changed so the parent needs to sync
512                 // children.
513                 static_cast<GraphicsLayerAndroid*>(m_parent)->m_needsSyncChildren = true;
514             }
515         }
516         // Need to rebuild our children based on the new structure.
517         m_needsSyncChildren = true;
518     } else {
519         ASSERT(hasOverflowScroll && !layerNeedsOverflow && !iframeNeedsOverflow);
520         ASSERT(m_contentLayer);
521         // Remove the foreground layers.
522         if (m_foregroundLayer) {
523             m_foregroundLayer->unref();
524             m_foregroundLayer = 0;
525             m_foregroundClipLayer->unref();
526             m_foregroundClipLayer = 0;
527         }
528         // No need to copy over children.
529         m_contentLayer->removeChildren();
530         LayerAndroid* layer = new LayerAndroid(*m_contentLayer);
531         m_contentLayer->unref();
532         m_contentLayer = layer;
533         if (m_parent) {
534             // The content layer has changed so the parent needs to sync
535             // children.
536             static_cast<GraphicsLayerAndroid*>(m_parent)->m_needsSyncChildren = true;
537         }
538         // Children are all re-parented.
539         m_needsSyncChildren = true;
540     }
541 #endif
542 }
543
544 bool GraphicsLayerAndroid::repaint()
545 {
546     LOG("(%x) repaint(), gPaused(%d) m_needsRepaint(%d) m_haveContents(%d) ",
547         this, gPaused, m_needsRepaint, m_haveContents);
548
549     if (!gPaused && m_haveContents && m_needsRepaint && !m_haveImage) {
550         // with SkPicture, we request the entire layer's content.
551         IntRect layerBounds(0, 0, m_size.width(), m_size.height());
552
553         RenderLayer* layer = renderLayerFromClient(m_client);
554         if (m_foregroundLayer) {
555             PaintingPhase phase(this);
556             // Paint the background into a separate context.
557             phase.set(GraphicsLayerPaintBackground);
558             if (!paintContext(m_contentLayer->recordContext(), layerBounds))
559                 return false;
560
561             // Construct the foreground layer and draw.
562             RenderBox* box = layer->renderBox();
563             int outline = box->view()->maximalOutlineSize();
564             IntRect contentsRect(0, 0,
565                                  box->borderLeft() + box->borderRight() + layer->scrollWidth(),
566                                  box->borderTop() + box->borderBottom() + layer->scrollHeight());
567             contentsRect.inflate(outline);
568             // Update the foreground layer size.
569             m_foregroundLayer->setSize(contentsRect.width(), contentsRect.height());
570             // Paint everything else into the main recording canvas.
571             phase.clear(GraphicsLayerPaintBackground);
572
573             // Paint at 0,0.
574             IntSize scroll = layer->scrolledContentOffset();
575             layer->scrollToOffset(0, 0);
576             // At this point, it doesn't matter if painting failed.
577             (void) paintContext(m_foregroundLayer->recordContext(), contentsRect);
578             layer->scrollToOffset(scroll.width(), scroll.height());
579
580             // Construct the clip layer for masking the contents.
581             IntRect clip = layer->renderer()->absoluteBoundingBoxRect();
582             // absoluteBoundingBoxRect does not include the outline so we need
583             // to offset the position.
584             int x = box->borderLeft() + outline;
585             int y = box->borderTop() + outline;
586             int width = clip.width() - box->borderLeft() - box->borderRight();
587             int height = clip.height() - box->borderTop() - box->borderBottom();
588             m_foregroundClipLayer->setPosition(x, y);
589             m_foregroundClipLayer->setSize(width, height);
590
591             // Need to offset the foreground layer by the clip layer in order
592             // for the contents to be in the correct position.
593             m_foregroundLayer->setPosition(-x, -y);
594             // Set the scrollable bounds of the layer.
595             m_foregroundLayer->setScrollLimits(-x, -y, m_size.width(), m_size.height());
596             m_foregroundLayer->needsRepaint();
597         } else {
598             // If there is no contents clip, we can draw everything into one
599             // picture.
600             if (!paintContext(m_contentLayer->recordContext(), layerBounds))
601                 return false;
602             // Check for a scrollable iframe and report the scrolling
603             // limits based on the view size.
604             if (m_contentLayer->contentIsScrollable()) {
605                 FrameView* view = layer->renderer()->frame()->view();
606                 static_cast<ScrollableLayerAndroid*>(m_contentLayer)->setScrollLimits(
607                     m_position.x(), m_position.y(), view->layoutWidth(), view->layoutHeight());
608             }
609         }
610
611         LOG("(%x) repaint() on (%.2f,%.2f) contentlayer(%.2f,%.2f,%.2f,%.2f)paintGraphicsLayer called!",
612             this, m_size.width(), m_size.height(),
613             m_contentLayer->getPosition().fX,
614             m_contentLayer->getPosition().fY,
615             m_contentLayer->getSize().width(),
616             m_contentLayer->getSize().height());
617
618         m_contentLayer->needsRepaint();
619         m_needsRepaint = false;
620         m_invalidatedRects.clear();
621
622         return true;
623     }
624     if (m_needsRepaint && m_haveImage && m_newImage) {
625         // We need to tell the GL thread that we will need to repaint the
626         // texture. Only do so if we effectively have a new image!
627         m_contentLayer->needsRepaint();
628         m_newImage = false;
629         m_needsRepaint = false;
630         return true;
631     }
632     return false;
633 }
634
635 bool GraphicsLayerAndroid::paintContext(SkPicture* context,
636                                         const IntRect& rect)
637 {
638     SkAutoPictureRecord arp(context, rect.width(), rect.height());
639     SkCanvas* canvas = arp.getRecordingCanvas();
640
641     if (!canvas)
642         return false;
643
644     PlatformGraphicsContext platformContext(canvas, 0);
645     GraphicsContext graphicsContext(&platformContext);
646
647     paintGraphicsLayerContents(graphicsContext, rect);
648     return true;
649 }
650
651 void GraphicsLayerAndroid::setNeedsDisplayInRect(const FloatRect& rect)
652 {
653     for (unsigned int i = 0; i < m_children.size(); i++) {
654         GraphicsLayer* layer = m_children[i];
655         if (layer) {
656            FloatRect childrenRect = m_transform.mapRect(rect);
657            layer->setNeedsDisplayInRect(childrenRect);
658         }
659     }
660
661     if (!m_haveImage && !drawsContent()) {
662         LOG("(%x) setNeedsDisplay(%.2f,%.2f,%.2f,%.2f) doesn't have content, bypass...",
663             this, rect.x(), rect.y(), rect.width(), rect.height());
664         return;
665     }
666
667     bool addInval = true;
668     const size_t maxDirtyRects = 8;
669     for (size_t i = 0; i < m_invalidatedRects.size(); ++i) {
670         if (m_invalidatedRects[i].contains(rect)) {
671             addInval = false;
672             break;
673         }
674     }
675
676 #ifdef LAYER_DEBUG
677     LOG("(%x) layer %d setNeedsDisplayInRect(%d) - (%.2f, %.2f, %.2f, %.2f)", this,
678         m_contentLayer->uniqueId(), m_needsRepaint, rect.x(), rect.y(), rect.width(), rect.height());
679 #endif
680
681     if (addInval) {
682         if (m_invalidatedRects.size() < maxDirtyRects)
683             m_invalidatedRects.append(rect);
684         else
685             m_invalidatedRects[0].unite(rect);
686     }
687
688     m_needsRepaint = true;
689     askForSync();
690 }
691
692 void GraphicsLayerAndroid::pauseDisplay(bool state)
693 {
694     gPaused = state;
695     if (gPaused)
696         gPausedDelay = WTF::currentTime() + 1;
697 }
698
699 bool GraphicsLayerAndroid::addAnimation(const KeyframeValueList& valueList,
700                                         const IntSize& boxSize,
701                                         const Animation* anim,
702                                         const String& keyframesName,
703                                         double beginTime)
704 {
705     if (!anim || anim->isEmptyOrZeroDuration() || valueList.size() < 2)
706         return false;
707
708     bool createdAnimations = false;
709     if (valueList.property() == AnimatedPropertyWebkitTransform) {
710         createdAnimations = createTransformAnimationsFromKeyframes(valueList,
711                                                                    anim,
712                                                                    keyframesName,
713                                                                    beginTime,
714                                                                    boxSize);
715     } else {
716         createdAnimations = createAnimationFromKeyframes(valueList,
717                                                          anim,
718                                                          keyframesName,
719                                                          beginTime);
720     }
721     if (createdAnimations)
722         askForSync();
723     return createdAnimations;
724 }
725
726 bool GraphicsLayerAndroid::createAnimationFromKeyframes(const KeyframeValueList& valueList,
727      const Animation* animation, const String& keyframesName, double beginTime)
728 {
729     bool isKeyframe = valueList.size() > 2;
730     TLOG("createAnimationFromKeyframes(%d), name(%s) beginTime(%.2f)",
731         isKeyframe, keyframesName.latin1().data(), beginTime);
732
733     switch (valueList.property()) {
734     case AnimatedPropertyInvalid: break;
735     case AnimatedPropertyWebkitTransform: break;
736     case AnimatedPropertyBackgroundColor: break;
737     case AnimatedPropertyOpacity: {
738         MLOG("ANIMATEDPROPERTYOPACITY");
739
740         KeyframeValueList* operationsList = new KeyframeValueList(AnimatedPropertyOpacity);
741         for (unsigned int i = 0; i < valueList.size(); i++) {
742             FloatAnimationValue* originalValue = (FloatAnimationValue*)valueList.at(i);
743             PassRefPtr<TimingFunction> timingFunction(const_cast<TimingFunction*>(originalValue->timingFunction()));
744             FloatAnimationValue* value = new FloatAnimationValue(originalValue->keyTime(),
745                                                                  originalValue->value(),
746                                                                  timingFunction);
747             operationsList->insert(value);
748         }
749
750         RefPtr<AndroidOpacityAnimation> anim = AndroidOpacityAnimation::create(animation,
751                                                                                operationsList,
752                                                                                beginTime);
753         if (keyframesName.isEmpty())
754             anim->setName(propertyIdToString(valueList.property()));
755         else
756             anim->setName(keyframesName);
757
758         m_contentLayer->addAnimation(anim.release());
759         needsNotifyClient();
760         return true;
761     } break;
762     }
763     return false;
764 }
765
766 void GraphicsLayerAndroid::needsNotifyClient()
767 {
768     m_needsNotifyClient = true;
769     askForSync();
770 }
771
772 bool GraphicsLayerAndroid::createTransformAnimationsFromKeyframes(const KeyframeValueList& valueList,
773                                                                   const Animation* animation,
774                                                                   const String& keyframesName,
775                                                                   double beginTime,
776                                                                   const IntSize& boxSize)
777 {
778     ASSERT(valueList.property() == AnimatedPropertyWebkitTransform);
779     TLOG("createTransformAnimationFromKeyframes, name(%s) beginTime(%.2f)",
780         keyframesName.latin1().data(), beginTime);
781
782     KeyframeValueList* operationsList = new KeyframeValueList(AnimatedPropertyWebkitTransform);
783     for (unsigned int i = 0; i < valueList.size(); i++) {
784         TransformAnimationValue* originalValue = (TransformAnimationValue*)valueList.at(i);
785         PassRefPtr<TimingFunction> timingFunction(const_cast<TimingFunction*>(originalValue->timingFunction()));
786         TransformAnimationValue* value = new TransformAnimationValue(originalValue->keyTime(),
787                                                                      originalValue->value(),
788                                                                      timingFunction);
789         operationsList->insert(value);
790     }
791
792     RefPtr<AndroidTransformAnimation> anim = AndroidTransformAnimation::create(animation,
793                                                                                operationsList,
794                                                                                beginTime);
795
796     if (keyframesName.isEmpty())
797         anim->setName(propertyIdToString(valueList.property()));
798     else
799         anim->setName(keyframesName);
800
801
802     m_contentLayer->addAnimation(anim.release());
803
804     needsNotifyClient();
805     return true;
806 }
807
808 void GraphicsLayerAndroid::removeAnimationsForProperty(AnimatedPropertyID anID)
809 {
810     TLOG("NRO removeAnimationsForProperty(%d)", anID);
811     m_contentLayer->removeAnimationsForProperty(anID);
812     askForSync();
813 }
814
815 void GraphicsLayerAndroid::removeAnimationsForKeyframes(const String& keyframesName)
816 {
817     TLOG("NRO removeAnimationsForKeyframes(%s)", keyframesName.latin1().data());
818     m_contentLayer->removeAnimationsForKeyframes(keyframesName);
819     askForSync();
820 }
821
822 void GraphicsLayerAndroid::pauseAnimation(const String& keyframesName)
823 {
824     TLOG("NRO pauseAnimation(%s)", keyframesName.latin1().data());
825 }
826
827 void GraphicsLayerAndroid::suspendAnimations(double time)
828 {
829     TLOG("NRO suspendAnimations(%.2f)", time);
830 }
831
832 void GraphicsLayerAndroid::resumeAnimations()
833 {
834     TLOG("NRO resumeAnimations()");
835 }
836
837 void GraphicsLayerAndroid::setContentsToImage(Image* image)
838 {
839     TLOG("(%x) setContentsToImage", this, image);
840     if (image) {
841         m_haveContents = true;
842         m_haveImage = true;
843         // Only pass the new image if it's a different one
844         if (image->nativeImageForCurrentFrame() != m_imageRef) {
845             m_newImage = true;
846             m_contentLayer->setContentsImage(image->nativeImageForCurrentFrame());
847             // remember the passed image.
848             m_imageRef = image->nativeImageForCurrentFrame();
849             setNeedsDisplay();
850             askForSync();
851         }
852     }
853 }
854
855 void GraphicsLayerAndroid::setContentsToMedia(PlatformLayer* mediaLayer)
856 {
857     // Only fullscreen video on Android, so media doesn't get it's own layer.
858     // We might still have other layers though.
859     if (m_contentLayer != mediaLayer && mediaLayer) {
860
861         // TODO add a copy method to LayerAndroid to sync everything
862         // copy data from the original content layer to the new one
863         mediaLayer->setPosition(m_contentLayer->getPosition().fX,
864                                 m_contentLayer->getPosition().fY);
865         mediaLayer->setSize(m_contentLayer->getWidth(), m_contentLayer->getHeight());
866         mediaLayer->setDrawTransform(m_contentLayer->drawTransform());
867
868         mediaLayer->ref();
869         m_contentLayer->unref();
870         m_contentLayer = mediaLayer;
871
872         // If the parent exists then notify it to re-sync it's children
873         if (m_parent) {
874             GraphicsLayerAndroid* parent = static_cast<GraphicsLayerAndroid*>(m_parent);
875             parent->m_needsSyncChildren = true;
876         }
877         m_needsSyncChildren = true;
878
879         setNeedsDisplay();
880         askForSync();
881     }
882 }
883
884 PlatformLayer* GraphicsLayerAndroid::platformLayer() const
885 {
886     LOG("platformLayer");
887     return m_contentLayer;
888 }
889
890 #ifndef NDEBUG
891 void GraphicsLayerAndroid::setDebugBackgroundColor(const Color& color)
892 {
893 }
894
895 void GraphicsLayerAndroid::setDebugBorder(const Color& color, float borderWidth)
896 {
897 }
898 #endif
899
900 void GraphicsLayerAndroid::setZPosition(float position)
901 {
902     if (position == m_zPosition)
903         return;
904     LOG("(%x) setZPosition: %.2f", this, position);
905     GraphicsLayer::setZPosition(position);
906     askForSync();
907 }
908
909 void GraphicsLayerAndroid::askForSync()
910 {
911     if (!m_client)
912         return;
913
914     if (m_client)
915         m_client->notifySyncRequired(this);
916 }
917
918 void GraphicsLayerAndroid::syncChildren()
919 {
920     if (m_needsSyncChildren) {
921         m_contentLayer->removeChildren();
922         LayerAndroid* layer = m_contentLayer;
923         if (m_foregroundClipLayer) {
924             m_contentLayer->addChild(m_foregroundClipLayer);
925             // Use the scrollable content layer as the parent of the children so
926             // that they move with the content.
927             layer = m_foregroundLayer;
928             layer->removeChildren();
929         }
930         for (unsigned int i = 0; i < m_children.size(); i++)
931             layer->addChild(m_children[i]->platformLayer());
932         m_needsSyncChildren = false;
933     }
934 }
935
936 void GraphicsLayerAndroid::syncMask()
937 {
938     if (m_needsSyncMask) {
939         if (m_maskLayer) {
940             LayerAndroid* mask = m_maskLayer->platformLayer();
941             m_contentLayer->setMaskLayer(mask);
942         } else
943             m_contentLayer->setMaskLayer(0);
944
945         m_contentLayer->setMasksToBounds(m_masksToBounds);
946         m_needsSyncMask = false;
947     }
948 }
949
950 void GraphicsLayerAndroid::syncCompositingState()
951 {
952     for (unsigned int i = 0; i < m_children.size(); i++)
953         m_children[i]->syncCompositingState();
954
955     updateScrollingLayers();
956     updateFixedPosition();
957     syncChildren();
958     syncMask();
959
960     if (!gPaused || WTF::currentTime() >= gPausedDelay)
961         repaint();
962 }
963
964 void GraphicsLayerAndroid::notifyClientAnimationStarted()
965 {
966     for (unsigned int i = 0; i < m_children.size(); i++)
967         static_cast<GraphicsLayerAndroid*>(m_children[i])->notifyClientAnimationStarted();
968
969     if (m_needsNotifyClient) {
970         if (client())
971             client()->notifyAnimationStarted(this, WTF::currentTime());
972         m_needsNotifyClient = false;
973     }
974 }
975
976 } // namespace WebCore
977
978 #endif // USE(ACCELERATED_COMPOSITING)