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