Unreviewed, rolling out r126169.
[WebKit-https.git] / Source / WebCore / platform / graphics / chromium / GraphicsLayerChromium.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2009 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32
33 /** FIXME
34  * This file borrows code heavily from platform/graphics/win/GraphicsLayerCACF.cpp
35  * (and hence it includes both copyrights)
36  * Ideally the common code (mostly the code that keeps track of the layer hierarchy)
37  * should be kept separate and shared between platforms. It would be a well worthwhile
38  * effort once the Windows implementation (binaries and headers) of CoreAnimation is
39  * checked in to the WebKit repository. Until then only Apple can make this happen.
40  */
41
42 #include "config.h"
43
44 #if USE(ACCELERATED_COMPOSITING)
45
46 #include "GraphicsLayerChromium.h"
47
48 #include "AnimationIdVendor.h"
49 #include "AnimationTranslationUtil.h"
50 #include "ContentLayerChromium.h"
51 #include "FloatConversion.h"
52 #include "FloatRect.h"
53 #include "GraphicsContext.h"
54 #include "Image.h"
55 #include "LinkHighlight.h"
56 #include "NativeImageSkia.h"
57 #include "PlatformContextSkia.h"
58 #include "PlatformString.h"
59 #include "SkMatrix44.h"
60 #include "SystemTime.h"
61
62 #include <public/WebAnimation.h>
63 #include <public/WebFilterOperation.h>
64 #include <public/WebFilterOperations.h>
65 #include <public/WebFloatPoint.h>
66 #include <public/WebFloatRect.h>
67 #include <public/WebImageLayer.h>
68 #include <public/WebSize.h>
69 #include <public/WebTransformationMatrix.h>
70 #include <wtf/CurrentTime.h>
71 #include <wtf/StringExtras.h>
72 #include <wtf/text/CString.h>
73
74 using namespace std;
75 using namespace WebKit;
76
77 namespace WebCore {
78
79 PassOwnPtr<GraphicsLayer> GraphicsLayer::create(GraphicsLayerClient* client)
80 {
81     return adoptPtr(new GraphicsLayerChromium(client));
82 }
83
84 GraphicsLayerChromium::GraphicsLayerChromium(GraphicsLayerClient* client)
85     : GraphicsLayer(client)
86     , m_contentsLayer(0)
87     , m_contentsLayerId(0)
88     , m_contentsLayerPurpose(NoContentsLayer)
89     , m_contentsLayerHasBackgroundColor(false)
90     , m_inSetChildren(false)
91     , m_pageScaleChanged(false)
92 {
93     m_opaqueRectTrackingContentLayerDelegate = adoptPtr(new OpaqueRectTrackingContentLayerDelegate(this));
94     m_layer = adoptPtr(WebContentLayer::create(m_opaqueRectTrackingContentLayerDelegate.get()));
95     m_layer->layer()->setDrawsContent(m_drawsContent && m_contentsVisible);
96     if (client)
97         deviceOrPageScaleFactorChanged();
98     updateDebugIndicators();
99 }
100
101 GraphicsLayerChromium::~GraphicsLayerChromium()
102 {
103     willBeDestroyed();
104 }
105
106 void GraphicsLayerChromium::setName(const String& inName)
107 {
108     m_nameBase = inName;
109     String name = String::format("GraphicsLayer(%p) ", this) + inName;
110     GraphicsLayer::setName(name);
111     updateNames();
112 }
113
114 void GraphicsLayerChromium::updateNames()
115 {
116     String debugName = "Layer for " + m_nameBase;
117     m_layer->layer()->setDebugName(debugName);
118
119     if (m_transformLayer) {
120         String debugName = "TransformLayer for " + m_nameBase;
121         m_transformLayer->setDebugName(debugName);
122     }
123     if (m_contentsLayer) {
124         String debugName = "ContentsLayer for " + m_nameBase;
125         m_contentsLayer->setDebugName(debugName);
126     }
127 }
128
129 bool GraphicsLayerChromium::setChildren(const Vector<GraphicsLayer*>& children)
130 {
131     m_inSetChildren = true;
132     bool childrenChanged = GraphicsLayer::setChildren(children);
133
134     if (childrenChanged)
135         updateChildList();
136     m_inSetChildren = false;
137
138     return childrenChanged;
139 }
140
141 void GraphicsLayerChromium::addChild(GraphicsLayer* childLayer)
142 {
143     GraphicsLayer::addChild(childLayer);
144     if (!m_inSetChildren)
145         updateChildList();
146 }
147
148 void GraphicsLayerChromium::addChildAtIndex(GraphicsLayer* childLayer, int index)
149 {
150     GraphicsLayer::addChildAtIndex(childLayer, index);
151     updateChildList();
152 }
153
154 void GraphicsLayerChromium::addChildBelow(GraphicsLayer* childLayer, GraphicsLayer* sibling)
155 {
156     GraphicsLayer::addChildBelow(childLayer, sibling);
157     updateChildList();
158 }
159
160 void GraphicsLayerChromium::addChildAbove(GraphicsLayer* childLayer, GraphicsLayer *sibling)
161 {
162     GraphicsLayer::addChildAbove(childLayer, sibling);
163     updateChildList();
164 }
165
166 bool GraphicsLayerChromium::replaceChild(GraphicsLayer* oldChild, GraphicsLayer* newChild)
167 {
168     if (GraphicsLayer::replaceChild(oldChild, newChild)) {
169         updateChildList();
170         return true;
171     }
172     return false;
173 }
174
175 void GraphicsLayerChromium::removeFromParent()
176 {
177     GraphicsLayer::removeFromParent();
178     platformLayer()->removeFromParent();
179 }
180
181 void GraphicsLayerChromium::setPosition(const FloatPoint& point)
182 {
183     GraphicsLayer::setPosition(point);
184     updateLayerPosition();
185 }
186
187 void GraphicsLayerChromium::setAnchorPoint(const FloatPoint3D& point)
188 {
189     GraphicsLayer::setAnchorPoint(point);
190     updateAnchorPoint();
191 }
192
193 void GraphicsLayerChromium::setSize(const FloatSize& size)
194 {
195     // We are receiving negative sizes here that cause assertions to fail in the compositor. Clamp them to 0 to
196     // avoid those assertions.
197     // FIXME: This should be an ASSERT instead, as negative sizes should not exist in WebCore.
198     FloatSize clampedSize = size;
199     if (clampedSize.width() < 0 || clampedSize.height() < 0)
200         clampedSize = FloatSize();
201
202     if (clampedSize == m_size)
203         return;
204
205     GraphicsLayer::setSize(clampedSize);
206     updateLayerSize();
207
208     if (m_pageScaleChanged)
209         m_layer->layer()->invalidate();
210     m_pageScaleChanged = false;
211 }
212
213 void GraphicsLayerChromium::setTransform(const TransformationMatrix& transform)
214 {
215     // Call this method first to assign contents scale to our layer so the painter can apply the scale transform.
216     updateContentsScale();
217
218     GraphicsLayer::setTransform(transform);
219     updateTransform();
220 }
221
222 void GraphicsLayerChromium::setChildrenTransform(const TransformationMatrix& transform)
223 {
224     GraphicsLayer::setChildrenTransform(transform);
225     updateChildrenTransform();
226 }
227
228 void GraphicsLayerChromium::setPreserves3D(bool preserves3D)
229 {
230     if (preserves3D == m_preserves3D)
231         return;
232
233     GraphicsLayer::setPreserves3D(preserves3D);
234     updateLayerPreserves3D();
235 }
236
237 void GraphicsLayerChromium::setMasksToBounds(bool masksToBounds)
238 {
239     GraphicsLayer::setMasksToBounds(masksToBounds);
240     updateMasksToBounds();
241 }
242
243 void GraphicsLayerChromium::setDrawsContent(bool drawsContent)
244 {
245     // Note carefully this early-exit is only correct because we also properly call
246     // WebLayer::setDrawsContent whenever m_contentsLayer is set to a new layer in setupContentsLayer().
247     if (drawsContent == m_drawsContent)
248         return;
249
250     GraphicsLayer::setDrawsContent(drawsContent);
251     updateLayerIsDrawable();
252 }
253
254 void GraphicsLayerChromium::setContentsVisible(bool contentsVisible)
255 {
256     // Note carefully this early-exit is only correct because we also properly call
257     // WebLayer::setDrawsContent whenever m_contentsLayer is set to a new layer in setupContentsLayer().
258     if (contentsVisible == m_contentsVisible)
259         return;
260
261     GraphicsLayer::setContentsVisible(contentsVisible);
262     updateLayerIsDrawable();
263 }
264
265 void GraphicsLayerChromium::setBackgroundColor(const Color& color)
266 {
267     GraphicsLayer::setBackgroundColor(color.rgb());
268
269     m_contentsLayerHasBackgroundColor = true;
270     updateLayerBackgroundColor();
271 }
272
273 void GraphicsLayerChromium::clearBackgroundColor()
274 {
275     GraphicsLayer::clearBackgroundColor();
276     m_contentsLayer->setBackgroundColor(static_cast<RGBA32>(0));
277 }
278
279 void GraphicsLayerChromium::setContentsOpaque(bool opaque)
280 {
281     GraphicsLayer::setContentsOpaque(opaque);
282     m_layer->layer()->setOpaque(m_contentsOpaque);
283 }
284
285 static bool copyWebCoreFilterOperationsToWebFilterOperations(const FilterOperations& filters, WebFilterOperations& webFilters)
286 {
287     for (size_t i = 0; i < filters.size(); ++i) {
288         const FilterOperation& op = *filters.at(i);
289         switch (op.getOperationType()) {
290         case FilterOperation::REFERENCE:
291             return false; // Not supported.
292         case FilterOperation::GRAYSCALE:
293         case FilterOperation::SEPIA:
294         case FilterOperation::SATURATE:
295         case FilterOperation::HUE_ROTATE: {
296             float amount = static_cast<const BasicColorMatrixFilterOperation*>(&op)->amount();
297             switch (op.getOperationType()) {
298             case FilterOperation::GRAYSCALE:
299                 webFilters.append(WebFilterOperation::createGrayscaleFilter(amount));
300                 break;
301             case FilterOperation::SEPIA:
302                 webFilters.append(WebFilterOperation::createSepiaFilter(amount));
303                 break;
304             case FilterOperation::SATURATE:
305                 webFilters.append(WebFilterOperation::createSaturateFilter(amount));
306                 break;
307             case FilterOperation::HUE_ROTATE:
308                 webFilters.append(WebFilterOperation::createHueRotateFilter(amount));
309                 break;
310             default:
311                 ASSERT_NOT_REACHED();
312             }
313             break;
314         }
315         case FilterOperation::INVERT:
316         case FilterOperation::OPACITY:
317         case FilterOperation::BRIGHTNESS:
318         case FilterOperation::CONTRAST: {
319             float amount = static_cast<const BasicComponentTransferFilterOperation*>(&op)->amount();
320             switch (op.getOperationType()) {
321             case FilterOperation::INVERT:
322                 webFilters.append(WebFilterOperation::createInvertFilter(amount));
323                 break;
324             case FilterOperation::OPACITY:
325                 webFilters.append(WebFilterOperation::createOpacityFilter(amount));
326                 break;
327             case FilterOperation::BRIGHTNESS:
328                 webFilters.append(WebFilterOperation::createBrightnessFilter(amount));
329                 break;
330             case FilterOperation::CONTRAST:
331                 webFilters.append(WebFilterOperation::createContrastFilter(amount));
332                 break;
333             default:
334                 ASSERT_NOT_REACHED();
335             }
336             break;
337         }
338         case FilterOperation::BLUR: {
339             float pixelRadius = static_cast<const BlurFilterOperation*>(&op)->stdDeviation().getFloatValue();
340             webFilters.append(WebFilterOperation::createBlurFilter(pixelRadius));
341             break;
342         }
343         case FilterOperation::DROP_SHADOW: {
344             const DropShadowFilterOperation& dropShadowOp = *static_cast<const DropShadowFilterOperation*>(&op);
345             webFilters.append(WebFilterOperation::createDropShadowFilter(WebPoint(dropShadowOp.x(), dropShadowOp.y()), dropShadowOp.stdDeviation(), dropShadowOp.color().rgb()));
346             break;
347         }
348 #if ENABLE(CSS_SHADERS)
349         case FilterOperation::CUSTOM:
350             return false; // Not supported.
351 #endif
352         case FilterOperation::PASSTHROUGH:
353         case FilterOperation::NONE:
354             break;
355         }
356     }
357     return true;
358 }
359
360 bool GraphicsLayerChromium::setFilters(const FilterOperations& filters)
361 {
362     WebFilterOperations webFilters;
363     if (!copyWebCoreFilterOperationsToWebFilterOperations(filters, webFilters)) {
364         // Make sure the filters are removed from the platform layer, as they are
365         // going to fallback to software mode.
366         m_layer->layer()->setFilters(WebFilterOperations());
367         GraphicsLayer::setFilters(FilterOperations());
368         return false;
369     }
370     m_layer->layer()->setFilters(webFilters);
371     return GraphicsLayer::setFilters(filters);
372 }
373
374 void GraphicsLayerChromium::setBackgroundFilters(const FilterOperations& filters)
375 {
376     WebFilterOperations webFilters;
377     if (!copyWebCoreFilterOperationsToWebFilterOperations(filters, webFilters))
378         return;
379     m_layer->layer()->setBackgroundFilters(webFilters);
380 }
381
382 void GraphicsLayerChromium::setMaskLayer(GraphicsLayer* maskLayer)
383 {
384     if (maskLayer == m_maskLayer)
385         return;
386
387     GraphicsLayer::setMaskLayer(maskLayer);
388
389     WebLayer* maskWebLayer = m_maskLayer ? m_maskLayer->platformLayer() : 0;
390     m_layer->layer()->setMaskLayer(maskWebLayer);
391 }
392
393 void GraphicsLayerChromium::setBackfaceVisibility(bool visible)
394 {
395     GraphicsLayer::setBackfaceVisibility(visible);
396     m_layer->setDoubleSided(m_backfaceVisibility);
397 }
398
399 void GraphicsLayerChromium::setOpacity(float opacity)
400 {
401     float clampedOpacity = max(min(opacity, 1.0f), 0.0f);
402     GraphicsLayer::setOpacity(clampedOpacity);
403     platformLayer()->setOpacity(opacity);
404 }
405
406 void GraphicsLayerChromium::setReplicatedByLayer(GraphicsLayer* layer)
407 {
408     GraphicsLayerChromium* layerChromium = static_cast<GraphicsLayerChromium*>(layer);
409     GraphicsLayer::setReplicatedByLayer(layer);
410
411     WebLayer* webReplicaLayer = layerChromium ? layerChromium->platformLayer() : 0;
412     platformLayer()->setReplicaLayer(webReplicaLayer);
413 }
414
415
416 void GraphicsLayerChromium::setContentsNeedsDisplay()
417 {
418     if (m_contentsLayer)
419         m_contentsLayer->invalidate();
420 }
421
422 void GraphicsLayerChromium::setNeedsDisplay()
423 {
424     if (drawsContent())
425         m_layer->layer()->invalidate();
426 }
427
428 void GraphicsLayerChromium::setNeedsDisplayInRect(const FloatRect& rect)
429 {
430     if (drawsContent())
431         m_layer->layer()->invalidateRect(rect);
432 }
433
434 void GraphicsLayerChromium::setContentsRect(const IntRect& rect)
435 {
436     if (rect == m_contentsRect)
437         return;
438
439     GraphicsLayer::setContentsRect(rect);
440     updateContentsRect();
441 }
442
443 void GraphicsLayerChromium::setContentsToImage(Image* image)
444 {
445     bool childrenChanged = false;
446     if (image) {
447         if (m_contentsLayerPurpose != ContentsLayerForImage) {
448             m_imageLayer = adoptPtr(WebImageLayer::create());
449             setupContentsLayer(m_imageLayer->layer());
450             m_contentsLayerPurpose = ContentsLayerForImage;
451             childrenChanged = true;
452         }
453         NativeImageSkia* nativeImage = image->nativeImageForCurrentFrame();
454         m_imageLayer->setBitmap(nativeImage->bitmap());
455         m_imageLayer->layer()->setOpaque(image->isBitmapImage() && !image->currentFrameHasAlpha());
456         updateContentsRect();
457     } else {
458         if (m_imageLayer) {
459             childrenChanged = true;
460
461             m_imageLayer.clear();
462         }
463         // The old contents layer will be removed via updateChildList.
464         m_contentsLayer = 0;
465     }
466
467     if (childrenChanged)
468         updateChildList();
469 }
470
471 void GraphicsLayerChromium::setContentsToCanvas(PlatformLayer* layer)
472 {
473     setContentsTo(ContentsLayerForCanvas, layer);
474 }
475
476 void GraphicsLayerChromium::setContentsToMedia(PlatformLayer* layer)
477 {
478     setContentsTo(ContentsLayerForVideo, layer);
479 }
480
481 void GraphicsLayerChromium::setContentsTo(ContentsLayerPurpose purpose, WebKit::WebLayer* layer)
482 {
483     bool childrenChanged = false;
484     if (layer) {
485         if (m_contentsLayerId != layer->id()) {
486             setupContentsLayer(layer);
487             m_contentsLayerPurpose = purpose;
488             childrenChanged = true;
489         }
490         updateContentsRect();
491     } else {
492         if (m_contentsLayer) {
493             childrenChanged = true;
494
495             // The old contents layer will be removed via updateChildList.
496             m_contentsLayer = 0;
497         }
498     }
499
500     if (childrenChanged)
501         updateChildList();
502 }
503
504 bool GraphicsLayerChromium::addAnimation(const KeyframeValueList& values, const IntSize& boxSize, const Animation* animation, const String& animationName, double timeOffset)
505 {
506     platformLayer()->setAnimationDelegate(this);
507
508     int animationId = mapAnimationNameToId(animationName);
509     int groupId = AnimationIdVendor::getNextGroupId();
510
511     OwnPtr<WebKit::WebAnimation> toAdd(createWebAnimation(values, animation, animationId, groupId, timeOffset, boxSize));
512
513     if (toAdd) {
514         // Remove any existing animations with the same animation id and target property.
515         platformLayer()->removeAnimation(animationId, toAdd->targetProperty());
516         return platformLayer()->addAnimation(toAdd.get());
517     }
518
519     return false;
520 }
521
522 void GraphicsLayerChromium::pauseAnimation(const String& animationName, double timeOffset)
523 {
524     platformLayer()->pauseAnimation(mapAnimationNameToId(animationName), timeOffset);
525 }
526
527 void GraphicsLayerChromium::removeAnimation(const String& animationName)
528 {
529     platformLayer()->removeAnimation(mapAnimationNameToId(animationName));
530 }
531
532 void GraphicsLayerChromium::suspendAnimations(double wallClockTime)
533 {
534     // |wallClockTime| is in the wrong time base. Need to convert here.
535     // FIXME: find a more reliable way to do this.
536     double monotonicTime = wallClockTime + monotonicallyIncreasingTime() - currentTime();
537     platformLayer()->suspendAnimations(monotonicTime);
538 }
539
540 void GraphicsLayerChromium::resumeAnimations()
541 {
542     platformLayer()->resumeAnimations(monotonicallyIncreasingTime());
543 }
544
545 void GraphicsLayerChromium::addLinkHighlight(const Path&)
546 {
547 }
548
549 void GraphicsLayerChromium::didFinishLinkHighlight()
550 {
551 }
552
553 PlatformLayer* GraphicsLayerChromium::platformLayer() const
554 {
555     return m_transformLayer ? m_transformLayer.get() : m_layer->layer();
556 }
557
558 void GraphicsLayerChromium::setDebugBackgroundColor(const Color& color)
559 {
560     if (color.isValid())
561         m_layer->layer()->setBackgroundColor(color.rgb());
562     else
563         m_layer->layer()->setBackgroundColor(static_cast<RGBA32>(0));
564 }
565
566 void GraphicsLayerChromium::setDebugBorder(const Color& color, float borderWidth)
567 {
568     if (color.isValid()) {
569         m_layer->layer()->setDebugBorderColor(color.rgb());
570         m_layer->layer()->setDebugBorderWidth(borderWidth);
571     } else {
572         m_layer->layer()->setDebugBorderColor(static_cast<RGBA32>(0));
573         m_layer->layer()->setDebugBorderWidth(0);
574     }
575 }
576
577 void GraphicsLayerChromium::updateChildList()
578 {
579     Vector<WebLayer*> newChildren;
580
581     if (m_transformLayer) {
582         // Add the primary layer first. Even if we have negative z-order children, the primary layer always comes behind.
583         newChildren.append(m_layer->layer());
584     } else if (m_contentsLayer) {
585         // FIXME: add the contents layer in the correct order with negative z-order children.
586         // This does not cause visible rendering issues because currently contents layers are only used
587         // for replaced elements that don't have children.
588         newChildren.append(m_contentsLayer);
589     }
590
591     const Vector<GraphicsLayer*>& childLayers = children();
592     size_t numChildren = childLayers.size();
593     for (size_t i = 0; i < numChildren; ++i) {
594         GraphicsLayerChromium* curChild = static_cast<GraphicsLayerChromium*>(childLayers[i]);
595
596         newChildren.append(curChild->platformLayer());
597     }
598
599     for (size_t i = 0; i < newChildren.size(); ++i)
600         newChildren[i]->removeFromParent();
601
602     WebVector<WebLayer*> newWebChildren;
603     newWebChildren.assign(newChildren.data(), newChildren.size());
604
605     if (m_transformLayer) {
606         m_transformLayer->setChildren(newWebChildren);
607
608         if (m_contentsLayer) {
609             // If we have a transform layer, then the contents layer is parented in the
610             // primary layer (which is itself a child of the transform layer).
611             m_layer->layer()->removeAllChildren();
612             m_layer->layer()->addChild(m_contentsLayer);
613         }
614     } else
615         m_layer->layer()->setChildren(newWebChildren);
616 }
617
618 void GraphicsLayerChromium::updateLayerPosition()
619 {
620     platformLayer()->setPosition(m_position);
621 }
622
623 void GraphicsLayerChromium::updateLayerSize()
624 {
625     IntSize layerSize(m_size.width(), m_size.height());
626     if (m_transformLayer) {
627         m_transformLayer->setBounds(layerSize);
628         m_layer->layer()->setPosition(FloatPoint());
629     }
630
631     m_layer->layer()->setBounds(layerSize);
632
633     // Note that we don't resize m_contentsLayer-> It's up the caller to do that.
634 }
635
636 void GraphicsLayerChromium::updateAnchorPoint()
637 {
638     platformLayer()->setAnchorPoint(FloatPoint(m_anchorPoint.x(), m_anchorPoint.y()));
639     platformLayer()->setAnchorPointZ(m_anchorPoint.z());
640 }
641
642 void GraphicsLayerChromium::updateTransform()
643 {
644     platformLayer()->setTransform(WebTransformationMatrix(m_transform));
645 }
646
647 void GraphicsLayerChromium::updateChildrenTransform()
648 {
649     platformLayer()->setSublayerTransform(WebTransformationMatrix(m_childrenTransform));
650 }
651
652 void GraphicsLayerChromium::updateMasksToBounds()
653 {
654     m_layer->layer()->setMasksToBounds(m_masksToBounds);
655     updateDebugIndicators();
656 }
657
658 void GraphicsLayerChromium::updateLayerPreserves3D()
659 {
660     if (m_preserves3D && !m_transformLayer) {
661         // Create the transform layer.
662         m_transformLayer = adoptPtr(WebLayer::create());
663         m_transformLayer->setPreserves3D(true);
664         m_transformLayer->setAnimationDelegate(this);
665         m_layer->layer()->transferAnimationsTo(m_transformLayer.get());
666
667         // Copy the position from this layer.
668         updateLayerPosition();
669         updateLayerSize();
670         updateAnchorPoint();
671         updateTransform();
672         updateChildrenTransform();
673
674         m_layer->layer()->setPosition(FloatPoint::zero());
675
676         m_layer->layer()->setAnchorPoint(FloatPoint(0.5f, 0.5f));
677         m_layer->layer()->setTransform(SkMatrix44());
678
679         // Set the old layer to opacity of 1. Further down we will set the opacity on the transform layer.
680         m_layer->layer()->setOpacity(1);
681
682         m_layer->setContentsScale(contentsScale());
683
684         // Move this layer to be a child of the transform layer.
685         if (parent())
686             parent()->platformLayer()->replaceChild(m_layer->layer(), m_transformLayer.get());
687         m_transformLayer->addChild(m_layer->layer());
688
689         updateChildList();
690     } else if (m_preserves3D && !m_transformLayer) {
691         // Relace the transformLayer in the parent with this layer.
692         m_layer->layer()->removeFromParent();
693         if (parent())
694             parent()->platformLayer()->replaceChild(m_transformLayer.get(), m_layer->layer());
695
696         m_layer->layer()->setAnimationDelegate(this);
697         m_transformLayer->transferAnimationsTo(m_layer->layer());
698
699         // Release the transform layer.
700         m_transformLayer->setAnimationDelegate(0);
701         m_transformLayer.clear();
702
703         updateLayerPosition();
704         updateLayerSize();
705         updateAnchorPoint();
706         updateTransform();
707         updateChildrenTransform();
708
709         updateChildList();
710     }
711
712     m_layer->layer()->setPreserves3D(m_preserves3D);
713     platformLayer()->setOpacity(m_opacity);
714     updateNames();
715 }
716
717 void GraphicsLayerChromium::updateLayerIsDrawable()
718 {
719     // For the rest of the accelerated compositor code, there is no reason to make a
720     // distinction between drawsContent and contentsVisible. So, for m_layer->layer(), these two
721     // flags are combined here. m_contentsLayer shouldn't receive the drawsContent flag
722     // so it is only given contentsVisible.
723
724     m_layer->layer()->setDrawsContent(m_drawsContent && m_contentsVisible);
725
726     if (m_contentsLayer)
727         m_contentsLayer->setDrawsContent(m_contentsVisible);
728
729     if (m_drawsContent)
730         m_layer->layer()->invalidate();
731
732     updateDebugIndicators();
733 }
734
735 void GraphicsLayerChromium::updateLayerBackgroundColor()
736 {
737     if (!m_contentsLayer)
738         return;
739
740     // We never create the contents layer just for background color yet.
741     if (m_backgroundColorSet)
742         m_contentsLayer->setBackgroundColor(m_backgroundColor.rgb());
743     else
744         m_contentsLayer->setBackgroundColor(static_cast<RGBA32>(0));
745 }
746
747 void GraphicsLayerChromium::updateContentsVideo()
748 {
749     // FIXME: Implement
750 }
751
752 void GraphicsLayerChromium::updateContentsRect()
753 {
754     if (!m_contentsLayer)
755         return;
756
757     m_contentsLayer->setPosition(FloatPoint(m_contentsRect.x(), m_contentsRect.y()));
758     m_contentsLayer->setBounds(IntSize(m_contentsRect.width(), m_contentsRect.height()));
759 }
760
761 void GraphicsLayerChromium::updateContentsScale()
762 {
763     // If page scale is already applied then there's no need to apply it again.
764     if (appliesPageScale())
765         return;
766
767     m_layer->setContentsScale(contentsScale());
768 }
769
770 void GraphicsLayerChromium::setupContentsLayer(WebLayer* contentsLayer)
771 {
772     m_contentsLayer = contentsLayer;
773     m_contentsLayerId = m_contentsLayer->id();
774
775     if (m_contentsLayer) {
776         m_contentsLayer->setAnchorPoint(FloatPoint(0, 0));
777         m_contentsLayer->setUseParentBackfaceVisibility(true);
778
779         // It is necessary to call setDrawsContent as soon as we receive the new contentsLayer, for
780         // the correctness of early exit conditions in setDrawsContent() and setContentsVisible().
781         m_contentsLayer->setDrawsContent(m_contentsVisible);
782
783         // Insert the content layer first. Video elements require this, because they have
784         // shadow content that must display in front of the video.
785         m_layer->layer()->insertChild(m_contentsLayer, 0);
786
787         if (showDebugBorders()) {
788             m_contentsLayer->setDebugBorderColor(Color(0, 0, 128, 180).rgb());
789             m_contentsLayer->setDebugBorderWidth(1);
790         }
791     }
792     updateDebugIndicators();
793     updateNames();
794 }
795
796 float GraphicsLayerChromium::contentsScale() const
797 {
798     if (!appliesPageScale())
799         return pageScaleFactor() * deviceScaleFactor();
800     return 1;
801 }
802
803 void GraphicsLayerChromium::deviceOrPageScaleFactorChanged()
804 {
805     updateContentsScale();
806     // Invalidations are clamped to the layer's bounds but we receive the scale changed notification before receiving
807     // the new layer bounds. When the scale changes, we really want to invalidate the post-scale layer bounds, so we
808     // remember that the scale has changed and then invalidate the full layer bounds when we receive the new size.
809     m_pageScaleChanged = true;
810 }
811
812 void GraphicsLayerChromium::paint(GraphicsContext& context, const IntRect& clip)
813 {
814     context.platformContext()->setDrawingToImageBuffer(true);
815     paintGraphicsLayerContents(context, clip);
816 }
817
818 int GraphicsLayerChromium::mapAnimationNameToId(const String& animationName)
819 {
820     if (animationName.isEmpty())
821         return 0;
822
823     if (!m_animationIdMap.contains(animationName))
824         m_animationIdMap.add(animationName, AnimationIdVendor::getNextAnimationId());
825
826     return m_animationIdMap.find(animationName)->second;
827 }
828
829 void GraphicsLayerChromium::notifyAnimationStarted(double startTime)
830 {
831     if (m_client)
832         m_client->notifyAnimationStarted(this, startTime);
833 }
834
835 void GraphicsLayerChromium::notifyAnimationFinished(double)
836 {
837     // Do nothing.
838 }
839
840 } // namespace WebCore
841
842 #endif // USE(ACCELERATED_COMPOSITING)