1 /* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #include "config.h" 32 33 #if USE(ACCELERATED_COMPOSITING) 34 35 #include "ImageLayerChromium.h" 36 37 #include "cc/CCLayerImpl.h" 38 #include "Image.h" 39 #include "LayerRendererChromium.h" 40 #include "LayerTexture.h" 41 42 namespace WebCore { 43 44 PassRefPtr<ImageLayerChromium> ImageLayerChromium::create(GraphicsLayerChromium* owner) 45 { 46 return adoptRef(new ImageLayerChromium(owner)); 47 } 48 49 ImageLayerChromium::ImageLayerChromium(GraphicsLayerChromium* owner) 50 : ContentLayerChromium(owner) 51 , m_imageForCurrentFrame(0) 52 , m_contents(0) 53 { 54 } 55 56 void ImageLayerChromium::setContents(Image* contents) 57 { 58 // setContents() currently gets called whenever there is any 59 // style change that affects the layer even if that change doesn't 60 // affect the actual contents of the image (e.g. a CSS animation). 61 // With this check in place we avoid unecessary texture uploads. 62 if ((m_contents == contents) && (m_contents->nativeImageForCurrentFrame() == m_imageForCurrentFrame)) 63 return; 64 65 m_contents = contents; 66 m_imageForCurrentFrame = m_contents->nativeImageForCurrentFrame(); 67 m_dirtyRect = IntRect(IntPoint(0, 0), bounds()); 68 setNeedsDisplay(); 69 } 70 71 void ImageLayerChromium::paintContentsIfDirty(const IntRect&) 72 { 73 ASSERT(layerRenderer()); 74 75 if (!m_dirtyRect.isEmpty()) { 76 m_decodedImage.updateFromImage(m_contents->nativeImageForCurrentFrame()); 77 updateLayerSize(m_decodedImage.size()); 78 IntRect paintRect(IntPoint(0, 0), m_decodedImage.size()); 79 if (!m_dirtyRect.isEmpty()) { 80 m_tiler->invalidateRect(paintRect); 81 m_dirtyRect = IntRect(); 82 } 83 } 84 } 85 86 void ImageLayerChromium::updateCompositorResources() 87 { 88 IntRect paintRect(IntPoint(0, 0), m_decodedImage.size()); 89 m_tiler->updateFromPixels(paintRect, paintRect, m_decodedImage.pixels()); 90 } 91 92 IntRect ImageLayerChromium::layerBounds() const 93 { 94 return IntRect(IntPoint(0, 0), m_decodedImage.size()); 95 } 96 97 TransformationMatrix ImageLayerChromium::tilingTransform() 98 { 99 // Tiler draws from the upper left corner. The draw transform 100 // specifies the middle of the layer. 101 TransformationMatrix transform = ccLayerImpl()->drawTransform(); 102 const IntRect sourceRect = layerBounds(); 103 const IntSize destSize = bounds(); 104 105 transform.translate(-destSize.width() / 2.0, -destSize.height() / 2.0); 106 107 // Tiler also draws at the original content size, so rescale the original 108 // image dimensions to the bounds that it is meant to be drawn at. 109 float scaleX = destSize.width() / static_cast<float>(sourceRect.size().width()); 110 float scaleY = destSize.height() / static_cast<float>(sourceRect.size().height()); 111 transform.scale3d(scaleX, scaleY, 1.0f); 112 113 return transform; 114 } 115 116 } 117 #endif // USE(ACCELERATED_COMPOSITING) 118