OSDN Git Service

Merge "Limit the number of buckets in a PictureSet"
[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->markAsDirty(m_dirtyRegion);
634         m_dirtyRegion.setEmpty();
635         m_contentLayer->needsRepaint();
636         m_newImage = false;
637         m_needsRepaint = false;
638         return true;
639     }
640     return false;
641 }
642
643 bool GraphicsLayerAndroid::paintContext(SkPicture* context,
644                                         const IntRect& rect)
645 {
646     SkAutoPictureRecord arp(context, rect.width(), rect.height());
647     SkCanvas* canvas = arp.getRecordingCanvas();
648
649     if (!canvas)
650         return false;
651
652     PlatformGraphicsContext platformContext(canvas, 0);
653     GraphicsContext graphicsContext(&platformContext);
654
655     paintGraphicsLayerContents(graphicsContext, rect);
656     return true;
657 }
658
659 void GraphicsLayerAndroid::setNeedsDisplayInRect(const FloatRect& rect)
660 {
661     // rect is in the render object coordinates
662
663     if (!m_haveImage && !drawsContent()) {
664         LOG("(%x) setNeedsDisplay(%.2f,%.2f,%.2f,%.2f) doesn't have content, bypass...",
665             this, rect.x(), rect.y(), rect.width(), rect.height());
666         return;
667     }
668
669     SkRegion region;
670     region.setRect(rect.x(), rect.y(),
671                    rect.x() + rect.width(),
672                    rect.y() + rect.height());
673     m_dirtyRegion.op(region, SkRegion::kUnion_Op);
674
675     m_needsRepaint = true;
676     askForSync();
677 }
678
679 void GraphicsLayerAndroid::pauseDisplay(bool state)
680 {
681     gPaused = state;
682     if (gPaused)
683         gPausedDelay = WTF::currentTime() + 1;
684 }
685
686 bool GraphicsLayerAndroid::addAnimation(const KeyframeValueList& valueList,
687                                         const IntSize& boxSize,
688                                         const Animation* anim,
689                                         const String& keyframesName,
690                                         double beginTime)
691 {
692     // For now, let webkit deals with the animations -- the current UI-side
693     // animation code has some annoying bugs, and we improved communication
694     // between webkit and UI enough that performance-wise it's not so much
695     // a problem to let webkit do everything.
696     // TODO: re-enable UI-side animations
697     return false;
698
699     if (!anim || anim->isEmptyOrZeroDuration() || valueList.size() < 2)
700         return false;
701
702     bool createdAnimations = false;
703     if (valueList.property() == AnimatedPropertyWebkitTransform) {
704         createdAnimations = createTransformAnimationsFromKeyframes(valueList,
705                                                                    anim,
706                                                                    keyframesName,
707                                                                    beginTime,
708                                                                    boxSize);
709     } else {
710         createdAnimations = createAnimationFromKeyframes(valueList,
711                                                          anim,
712                                                          keyframesName,
713                                                          beginTime);
714     }
715     if (createdAnimations)
716         askForSync();
717     return createdAnimations;
718 }
719
720 bool GraphicsLayerAndroid::createAnimationFromKeyframes(const KeyframeValueList& valueList,
721      const Animation* animation, const String& keyframesName, double beginTime)
722 {
723     bool isKeyframe = valueList.size() > 2;
724     TLOG("createAnimationFromKeyframes(%d), name(%s) beginTime(%.2f)",
725         isKeyframe, keyframesName.latin1().data(), beginTime);
726
727     switch (valueList.property()) {
728     case AnimatedPropertyInvalid: break;
729     case AnimatedPropertyWebkitTransform: break;
730     case AnimatedPropertyBackgroundColor: break;
731     case AnimatedPropertyOpacity: {
732         MLOG("ANIMATEDPROPERTYOPACITY");
733
734         KeyframeValueList* operationsList = new KeyframeValueList(AnimatedPropertyOpacity);
735         for (unsigned int i = 0; i < valueList.size(); i++) {
736             FloatAnimationValue* originalValue = (FloatAnimationValue*)valueList.at(i);
737             PassRefPtr<TimingFunction> timingFunction(const_cast<TimingFunction*>(originalValue->timingFunction()));
738             FloatAnimationValue* value = new FloatAnimationValue(originalValue->keyTime(),
739                                                                  originalValue->value(),
740                                                                  timingFunction);
741             operationsList->insert(value);
742         }
743
744         RefPtr<AndroidOpacityAnimation> anim = AndroidOpacityAnimation::create(animation,
745                                                                                operationsList,
746                                                                                beginTime);
747         if (keyframesName.isEmpty())
748             anim->setName(propertyIdToString(valueList.property()));
749         else
750             anim->setName(keyframesName);
751
752         m_contentLayer->addAnimation(anim.release());
753         needsNotifyClient();
754         return true;
755     } break;
756     }
757     return false;
758 }
759
760 void GraphicsLayerAndroid::needsNotifyClient()
761 {
762     m_needsNotifyClient = true;
763     askForSync();
764 }
765
766 bool GraphicsLayerAndroid::createTransformAnimationsFromKeyframes(const KeyframeValueList& valueList,
767                                                                   const Animation* animation,
768                                                                   const String& keyframesName,
769                                                                   double beginTime,
770                                                                   const IntSize& boxSize)
771 {
772     ASSERT(valueList.property() == AnimatedPropertyWebkitTransform);
773     TLOG("createTransformAnimationFromKeyframes, name(%s) beginTime(%.2f)",
774         keyframesName.latin1().data(), beginTime);
775
776     KeyframeValueList* operationsList = new KeyframeValueList(AnimatedPropertyWebkitTransform);
777     for (unsigned int i = 0; i < valueList.size(); i++) {
778         TransformAnimationValue* originalValue = (TransformAnimationValue*)valueList.at(i);
779         PassRefPtr<TimingFunction> timingFunction(const_cast<TimingFunction*>(originalValue->timingFunction()));
780         TransformAnimationValue* value = new TransformAnimationValue(originalValue->keyTime(),
781                                                                      originalValue->value(),
782                                                                      timingFunction);
783         operationsList->insert(value);
784     }
785
786     RefPtr<AndroidTransformAnimation> anim = AndroidTransformAnimation::create(animation,
787                                                                                operationsList,
788                                                                                beginTime);
789
790     if (keyframesName.isEmpty())
791         anim->setName(propertyIdToString(valueList.property()));
792     else
793         anim->setName(keyframesName);
794
795
796     m_contentLayer->addAnimation(anim.release());
797
798     needsNotifyClient();
799     return true;
800 }
801
802 void GraphicsLayerAndroid::removeAnimationsForProperty(AnimatedPropertyID anID)
803 {
804     TLOG("NRO removeAnimationsForProperty(%d)", anID);
805     m_contentLayer->removeAnimationsForProperty(anID);
806     askForSync();
807 }
808
809 void GraphicsLayerAndroid::removeAnimationsForKeyframes(const String& keyframesName)
810 {
811     TLOG("NRO removeAnimationsForKeyframes(%s)", keyframesName.latin1().data());
812     m_contentLayer->removeAnimationsForKeyframes(keyframesName);
813     askForSync();
814 }
815
816 void GraphicsLayerAndroid::pauseAnimation(const String& keyframesName)
817 {
818     TLOG("NRO pauseAnimation(%s)", keyframesName.latin1().data());
819 }
820
821 void GraphicsLayerAndroid::suspendAnimations(double time)
822 {
823     TLOG("NRO suspendAnimations(%.2f)", time);
824 }
825
826 void GraphicsLayerAndroid::resumeAnimations()
827 {
828     TLOG("NRO resumeAnimations()");
829 }
830
831 void GraphicsLayerAndroid::setContentsToImage(Image* image)
832 {
833     TLOG("(%x) setContentsToImage", this, image);
834     if (image) {
835         m_haveContents = true;
836         m_haveImage = true;
837         // Only pass the new image if it's a different one
838         if (image->nativeImageForCurrentFrame() != m_imageRef) {
839             m_newImage = true;
840             m_contentLayer->setContentsImage(image->nativeImageForCurrentFrame());
841             // remember the passed image.
842             m_imageRef = image->nativeImageForCurrentFrame();
843             setNeedsDisplay();
844             askForSync();
845         }
846     }
847     if (m_haveImage && !image) {
848         m_contentLayer->setContentsImage(0);
849         m_imageRef = 0;
850         setNeedsDisplay();
851         askForSync();
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)