2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #if USE(ACCELERATED_COMPOSITING)
34 #include "LayerChromium.h"
36 #include "cc/CCLayerImpl.h"
37 #include "cc/CCLayerTreeHost.h"
39 #include "NativeImageSkia.h"
40 #include "PlatformContextSkia.h"
42 #include "RenderLayerBacking.h"
43 #include "TextStream.h"
44 #include "skia/ext/platform_canvas.h"
50 static int s_nextLayerId = 1;
52 PassRefPtr<LayerChromium> LayerChromium::create(CCLayerDelegate* delegate)
54 return adoptRef(new LayerChromium(delegate));
57 LayerChromium::LayerChromium(CCLayerDelegate* delegate)
58 : m_delegate(delegate)
59 , m_layerId(s_nextLayerId++)
62 , m_anchorPoint(0.5, 0.5)
63 , m_backgroundColor(0, 0, 0, 0)
64 , m_debugBorderWidth(0)
67 , m_masksToBounds(false)
70 , m_usesLayerClipping(false)
71 , m_isNonCompositedContent(false)
72 , m_preserves3D(false)
75 , m_targetRenderSurface(0)
76 , m_contentsScale(1.0)
77 , m_pageScaleDirty(false)
81 LayerChromium::~LayerChromium()
83 // Our parent should be holding a reference to us so there should be no
84 // way for us to be destroyed while we still have a parent.
87 // Remove the parent reference from all children.
91 void LayerChromium::cleanupResources()
95 void LayerChromium::cleanupResourcesRecursive()
97 for (size_t i = 0; i < children().size(); ++i)
98 children()[i]->cleanupResourcesRecursive();
101 maskLayer()->cleanupResourcesRecursive();
103 replicaLayer()->cleanupResourcesRecursive();
108 void LayerChromium::setLayerTreeHost(CCLayerTreeHost* host)
110 // If we're changing layer renderers then we need to free up any resources
111 // allocated by the old renderer.
112 if (layerTreeHost() && layerTreeHost() != host) {
117 m_layerTreeHost = host;
120 void LayerChromium::setNeedsCommit()
122 // Call notifySyncRequired(), which for non-root layers plumbs through to
123 // call setRootLayerNeedsDisplay() on the WebView, which will cause LayerRendererChromium
124 // to render a frame.
125 // This function has no effect on root layers.
127 m_delegate->notifySyncRequired();
130 void LayerChromium::setParent(LayerChromium* layer)
132 ASSERT(!layer || !layer->hasAncestor(this));
136 bool LayerChromium::hasAncestor(LayerChromium* ancestor) const
138 for (LayerChromium* layer = parent(); layer; layer = layer->parent()) {
139 if (layer == ancestor)
145 void LayerChromium::addChild(PassRefPtr<LayerChromium> child)
147 insertChild(child, numChildren());
150 void LayerChromium::insertChild(PassRefPtr<LayerChromium> child, size_t index)
152 index = min(index, m_children.size());
153 child->removeFromParent();
154 child->setParent(this);
155 m_children.insert(index, child);
159 void LayerChromium::removeFromParent()
162 m_parent->removeChild(this);
165 void LayerChromium::removeChild(LayerChromium* child)
167 int foundIndex = indexOfChild(child);
168 if (foundIndex == -1)
172 m_children.remove(foundIndex);
176 void LayerChromium::replaceChild(LayerChromium* reference, PassRefPtr<LayerChromium> newLayer)
178 ASSERT_ARG(reference, reference);
179 ASSERT_ARG(reference, reference->parent() == this);
181 if (reference == newLayer)
184 int referenceIndex = indexOfChild(reference);
185 if (referenceIndex == -1) {
186 ASSERT_NOT_REACHED();
190 reference->removeFromParent();
193 newLayer->removeFromParent();
194 insertChild(newLayer, referenceIndex);
198 int LayerChromium::indexOfChild(const LayerChromium* reference)
200 for (size_t i = 0; i < m_children.size(); i++) {
201 if (m_children[i] == reference)
207 void LayerChromium::setBounds(const IntSize& size)
209 if (bounds() == size)
212 bool firstResize = !bounds().width() && !bounds().height() && size.width() && size.height();
216 if (firstResize || m_pageScaleDirty)
217 setNeedsDisplay(FloatRect(0, 0, bounds().width(), bounds().height()));
221 m_pageScaleDirty = false;
224 const LayerChromium* LayerChromium::rootLayer() const
226 const LayerChromium* layer = this;
227 for (LayerChromium* parent = layer->parent(); parent; layer = parent, parent = parent->parent()) { }
231 void LayerChromium::removeAllChildren()
233 while (m_children.size()) {
234 LayerChromium* layer = m_children[0].get();
235 ASSERT(layer->parent());
236 layer->removeFromParent();
240 void LayerChromium::setChildren(const Vector<RefPtr<LayerChromium> >& children)
242 if (children == m_children)
246 size_t listSize = children.size();
247 for (size_t i = 0; i < listSize; i++)
248 addChild(children[i]);
251 LayerChromium* LayerChromium::parent() const
256 void LayerChromium::setName(const String& name)
261 void LayerChromium::setNeedsDisplay(const FloatRect& dirtyRect)
263 // Simply mark the contents as dirty. For non-root layers, the call to
264 // setNeedsCommit will schedule a fresh compositing pass.
265 // For the root layer, setNeedsCommit has no effect.
266 m_dirtyRect.unite(dirtyRect);
270 void LayerChromium::setNeedsDisplay()
272 m_dirtyRect.setLocation(FloatPoint());
273 m_dirtyRect.setSize(bounds());
277 void LayerChromium::resetNeedsDisplay()
279 m_dirtyRect = FloatRect();
282 void LayerChromium::pushPropertiesTo(CCLayerImpl* layer)
284 layer->setAnchorPoint(m_anchorPoint);
285 layer->setAnchorPointZ(m_anchorPointZ);
286 layer->setBackgroundColor(m_backgroundColor);
287 layer->setBounds(m_bounds);
288 layer->setContentBounds(contentBounds());
289 layer->setDebugBorderColor(m_debugBorderColor);
290 layer->setDebugBorderWidth(m_debugBorderWidth);
291 layer->setDoubleSided(m_doubleSided);
292 layer->setDrawsContent(drawsContent());
293 layer->setIsNonCompositedContent(m_isNonCompositedContent);
294 layer->setMasksToBounds(m_masksToBounds);
295 layer->setScrollable(m_scrollable);
296 layer->setName(m_name);
297 layer->setOpaque(m_opaque);
298 layer->setOpacity(m_opacity);
299 layer->setPosition(m_position);
300 layer->setPreserves3D(preserves3D());
301 layer->setScrollPosition(m_scrollPosition);
302 layer->setSublayerTransform(m_sublayerTransform);
303 layer->setTransform(m_transform);
304 layer->setUpdateRect(m_updateRect);
305 layer->setSentScrollDelta(IntSize());
308 maskLayer()->pushPropertiesTo(layer->maskLayer());
310 replicaLayer()->pushPropertiesTo(layer->replicaLayer());
312 // Reset any state that should be cleared for the next update.
313 m_updateRect = FloatRect();
316 PassRefPtr<CCLayerImpl> LayerChromium::createCCLayerImpl()
318 return CCLayerImpl::create(m_layerId);
321 void LayerChromium::setDebugBorderColor(const Color& color)
323 m_debugBorderColor = color;
327 void LayerChromium::setDebugBorderWidth(float width)
329 m_debugBorderWidth = width;
333 void LayerChromium::setContentsScale(float contentsScale)
335 if (!needsContentsScale())
337 m_contentsScale = contentsScale;
340 void LayerChromium::createRenderSurface()
342 ASSERT(!m_renderSurface);
343 m_renderSurface = adoptPtr(new RenderSurfaceChromium(this));
346 bool LayerChromium::descendantDrawsContent()
348 for (size_t i = 0; i < m_children.size(); ++i) {
349 if (m_children[i]->drawsContent() || m_children[i]->descendantDrawsContent())
355 void sortLayers(Vector<RefPtr<LayerChromium> >::iterator, Vector<RefPtr<LayerChromium> >::iterator, void*)
357 // Currently we don't use z-order to decide what to paint, so there's no need to actually sort LayerChromiums.
361 #endif // USE(ACCELERATED_COMPOSITING)