tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / Image.cpp
1 /*
2  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3  * Copyright (C) 2004, 2005, 2006 Apple Computer, 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
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
26
27 #include "config.h"
28 #include "Image.h"
29
30 #include "AffineTransform.h"
31 #include "BitmapImage.h"
32 #include "GraphicsContext.h"
33 #include "IntRect.h"
34 #include "Length.h"
35 #include "MIMETypeRegistry.h"
36 #include "SharedBuffer.h"
37 #include <math.h>
38 #include <wtf/MainThread.h>
39 #include <wtf/StdLibExtras.h>
40
41 #if USE(CG)
42 #include <CoreFoundation/CoreFoundation.h>
43 #endif
44
45 namespace WebCore {
46
47 Image::Image(ImageObserver* observer)
48     : m_imageObserver(observer)
49 {
50 }
51
52 Image::~Image()
53 {
54 }
55
56 Image* Image::nullImage()
57 {
58     ASSERT(isMainThread());
59     DEFINE_STATIC_LOCAL(RefPtr<Image>, nullImage, (BitmapImage::create()));;
60     return nullImage.get();
61 }
62
63 bool Image::supportsType(const String& type)
64 {
65     return MIMETypeRegistry::isSupportedImageResourceMIMEType(type); 
66
67
68 bool Image::setData(PassRefPtr<SharedBuffer> data, bool allDataReceived)
69 {
70     m_data = data;
71     if (!m_data.get())
72         return true;
73
74     int length = m_data->size();
75     if (!length)
76         return true;
77     
78     return dataChanged(allDataReceived);
79 }
80
81 void Image::fillWithSolidColor(GraphicsContext* ctxt, const FloatRect& dstRect, const Color& color, ColorSpace styleColorSpace, CompositeOperator op)
82 {
83     if (color.alpha() <= 0)
84         return;
85     
86     CompositeOperator previousOperator = ctxt->compositeOperation();
87     ctxt->setCompositeOperation(!color.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op);
88     ctxt->fillRect(dstRect, color, styleColorSpace);
89     ctxt->setCompositeOperation(previousOperator);
90 }
91
92 void Image::draw(GraphicsContext* ctx, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator op, RespectImageOrientationEnum)
93 {
94     draw(ctx, dstRect, srcRect, styleColorSpace, op);
95 }
96
97 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& destRect, const FloatPoint& srcPoint, const FloatSize& scaledTileSize, ColorSpace styleColorSpace, CompositeOperator op)
98 {    
99     if (mayFillWithSolidColor()) {
100         fillWithSolidColor(ctxt, destRect, solidColor(), styleColorSpace, op);
101         return;
102     }
103
104     // See <https://webkit.org/b/59043>.
105 #if !PLATFORM(WX)
106     ASSERT(!isBitmapImage() || notSolidColor());
107 #endif
108
109     FloatSize intrinsicTileSize = size();
110     if (hasRelativeWidth())
111         intrinsicTileSize.setWidth(scaledTileSize.width());
112     if (hasRelativeHeight())
113         intrinsicTileSize.setHeight(scaledTileSize.height());
114
115     FloatSize scale(scaledTileSize.width() / intrinsicTileSize.width(),
116                     scaledTileSize.height() / intrinsicTileSize.height());
117
118     FloatRect oneTileRect;
119     oneTileRect.setX(destRect.x() + fmodf(fmodf(-srcPoint.x(), scaledTileSize.width()) - scaledTileSize.width(), scaledTileSize.width()));
120     oneTileRect.setY(destRect.y() + fmodf(fmodf(-srcPoint.y(), scaledTileSize.height()) - scaledTileSize.height(), scaledTileSize.height()));
121     oneTileRect.setSize(scaledTileSize);
122     
123     // Check and see if a single draw of the image can cover the entire area we are supposed to tile.    
124     if (oneTileRect.contains(destRect)) {
125         FloatRect visibleSrcRect;
126         visibleSrcRect.setX((destRect.x() - oneTileRect.x()) / scale.width());
127         visibleSrcRect.setY((destRect.y() - oneTileRect.y()) / scale.height());
128         visibleSrcRect.setWidth(destRect.width() / scale.width());
129         visibleSrcRect.setHeight(destRect.height() / scale.height());
130         draw(ctxt, destRect, visibleSrcRect, styleColorSpace, op);
131         return;
132     }
133
134     AffineTransform patternTransform = AffineTransform().scaleNonUniform(scale.width(), scale.height());
135     FloatRect tileRect(FloatPoint(), intrinsicTileSize);    
136     drawPattern(ctxt, tileRect, patternTransform, oneTileRect.location(), styleColorSpace, op, destRect);
137     
138     startAnimation();
139 }
140
141 // FIXME: Merge with the other drawTiled eventually, since we need a combination of both for some things.
142 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect,
143     const FloatSize& tileScaleFactor, TileRule hRule, TileRule vRule, ColorSpace styleColorSpace, CompositeOperator op)
144 {    
145     if (mayFillWithSolidColor()) {
146         fillWithSolidColor(ctxt, dstRect, solidColor(), styleColorSpace, op);
147         return;
148     }
149     
150     // FIXME: We do not support 'round' or 'space' yet. For now just map them to 'repeat'.
151     if (hRule == RoundTile || hRule == SpaceTile)
152         hRule = RepeatTile;
153     if (vRule == RoundTile || vRule == SpaceTile)
154         vRule = RepeatTile;
155
156     AffineTransform patternTransform = AffineTransform().scaleNonUniform(tileScaleFactor.width(), tileScaleFactor.height());
157
158     // We want to construct the phase such that the pattern is centered (when stretch is not
159     // set for a particular rule).
160     float hPhase = tileScaleFactor.width() * srcRect.x();
161     float vPhase = tileScaleFactor.height() * srcRect.y();
162     float scaledTileWidth = tileScaleFactor.width() * srcRect.width();
163     float scaledTileHeight = tileScaleFactor.height() * srcRect.height();
164     if (hRule == Image::RepeatTile)
165         hPhase -= (dstRect.width() - scaledTileWidth) / 2;
166     if (vRule == Image::RepeatTile)
167         vPhase -= (dstRect.height() - scaledTileHeight) / 2; 
168     FloatPoint patternPhase(dstRect.x() - hPhase, dstRect.y() - vPhase);
169     
170     drawPattern(ctxt, srcRect, patternTransform, patternPhase, styleColorSpace, op, dstRect);
171
172     startAnimation();
173 }
174
175 #if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
176 FloatRect Image::adjustSourceRectForDownSampling(const FloatRect& srcRect, const IntSize& scaledSize) const
177 {
178     const IntSize unscaledSize = size();
179     if (unscaledSize == scaledSize)
180         return srcRect;
181
182     // Image has been down-sampled.
183     float xscale = static_cast<float>(scaledSize.width()) / unscaledSize.width();
184     float yscale = static_cast<float>(scaledSize.height()) / unscaledSize.height();
185     FloatRect scaledSrcRect = srcRect;
186     scaledSrcRect.scale(xscale, yscale);
187
188     return scaledSrcRect;
189 }
190 #endif
191
192 void Image::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio)
193 {
194     intrinsicRatio = size();
195     intrinsicWidth = Length(intrinsicRatio.width(), Fixed);
196     intrinsicHeight = Length(intrinsicRatio.height(), Fixed);
197 }
198
199 }