OSDN Git Service

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