1 /*
2 * Copyright (C) 2011 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "platform/graphics/CrossfadeGeneratedImage.h"
28
29 #include "platform/geometry/FloatRect.h"
30 #include "platform/graphics/GraphicsContextStateSaver.h"
31 #include "platform/graphics/ImageBuffer.h"
32
33 namespace blink {
34
CrossfadeGeneratedImage(Image * fromImage,Image * toImage,float percentage,IntSize crossfadeSize,const IntSize & size)35 CrossfadeGeneratedImage::CrossfadeGeneratedImage(Image* fromImage, Image* toImage, float percentage, IntSize crossfadeSize, const IntSize& size)
36 : m_fromImage(fromImage)
37 , m_toImage(toImage)
38 , m_percentage(percentage)
39 , m_crossfadeSize(crossfadeSize)
40 {
41 m_size = size;
42 }
43
drawCrossfade(GraphicsContext * context)44 void CrossfadeGeneratedImage::drawCrossfade(GraphicsContext* context)
45 {
46 float inversePercentage = 1 - m_percentage;
47
48 IntSize fromImageSize = m_fromImage->size();
49 IntSize toImageSize = m_toImage->size();
50
51 // Draw nothing if either of the images hasn't loaded yet.
52 if (m_fromImage == Image::nullImage() || m_toImage == Image::nullImage())
53 return;
54
55 GraphicsContextStateSaver stateSaver(*context);
56
57 context->clip(IntRect(IntPoint(), m_crossfadeSize));
58 context->beginTransparencyLayer(1);
59
60 // Draw the image we're fading away from.
61 context->save();
62 if (m_crossfadeSize != fromImageSize) {
63 context->scale(
64 static_cast<float>(m_crossfadeSize.width()) / fromImageSize.width(),
65 static_cast<float>(m_crossfadeSize.height()) / fromImageSize.height());
66 }
67 context->setAlphaAsFloat(inversePercentage);
68 context->drawImage(m_fromImage, IntPoint());
69 context->restore();
70
71 // Draw the image we're fading towards.
72 context->save();
73 if (m_crossfadeSize != toImageSize) {
74 context->scale(
75 static_cast<float>(m_crossfadeSize.width()) / toImageSize.width(),
76 static_cast<float>(m_crossfadeSize.height()) / toImageSize.height());
77 }
78 context->setAlphaAsFloat(m_percentage);
79 context->drawImage(m_toImage, IntPoint(), CompositePlusLighter);
80 context->restore();
81
82 context->endLayer();
83 }
84
draw(GraphicsContext * context,const FloatRect & dstRect,const FloatRect & srcRect,CompositeOperator compositeOp,WebBlendMode blendMode)85 void CrossfadeGeneratedImage::draw(GraphicsContext* context, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, WebBlendMode blendMode)
86 {
87 GraphicsContextStateSaver stateSaver(*context);
88 context->setCompositeOperation(compositeOp, blendMode);
89 context->clip(dstRect);
90 context->translate(dstRect.x(), dstRect.y());
91 if (dstRect.size() != srcRect.size())
92 context->scale(dstRect.width() / srcRect.width(), dstRect.height() / srcRect.height());
93 context->translate(-srcRect.x(), -srcRect.y());
94
95 drawCrossfade(context);
96 }
97
drawPattern(GraphicsContext * context,const FloatRect & srcRect,const FloatSize & scale,const FloatPoint & phase,CompositeOperator compositeOp,const FloatRect & dstRect,WebBlendMode blendMode,const IntSize & repeatSpacing)98 void CrossfadeGeneratedImage::drawPattern(GraphicsContext* context, const FloatRect& srcRect, const FloatSize& scale, const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& dstRect, WebBlendMode blendMode, const IntSize& repeatSpacing)
99 {
100 OwnPtr<ImageBuffer> imageBuffer = context->createRasterBuffer(m_size);
101 if (!imageBuffer)
102 return;
103
104 // Fill with the cross-faded image.
105 GraphicsContext* graphicsContext = imageBuffer->context();
106 drawCrossfade(graphicsContext);
107
108 // Tile the image buffer into the context.
109 imageBuffer->drawPattern(context, srcRect, scale, phase, compositeOp, dstRect, blendMode, repeatSpacing);
110 }
111
112 } // namespace blink
113