1 /* 2 * Copyright (C) 2006 Samuel Weinig (sam.weinig (at) 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 "core/platform/graphics/Image.h" 29 30 #include "core/platform/Length.h" 31 #include "core/platform/MIMETypeRegistry.h" 32 #include "core/platform/SharedBuffer.h" 33 #include "core/platform/chromium/TraceEvent.h" 34 #include "core/platform/graphics/BitmapImage.h" 35 #include "core/platform/graphics/FloatPoint.h" 36 #include "core/platform/graphics/FloatRect.h" 37 #include "core/platform/graphics/FloatSize.h" 38 #include "core/platform/graphics/GraphicsContext.h" 39 #include "core/platform/graphics/GraphicsContextStateSaver.h" 40 #include "core/platform/graphics/GraphicsTypes.h" 41 #include "core/platform/graphics/IntRect.h" 42 #include "public/platform/Platform.h" 43 #include "public/platform/WebData.h" 44 #include "wtf/MainThread.h" 45 #include "wtf/StdLibExtras.h" 46 47 #include <math.h> 48 49 namespace WebCore { 50 51 Image::Image(ImageObserver* observer) 52 : m_imageObserver(observer) 53 { 54 } 55 56 Image::~Image() 57 { 58 } 59 60 Image* Image::nullImage() 61 { 62 ASSERT(isMainThread()); 63 DEFINE_STATIC_LOCAL(RefPtr<Image>, nullImage, (BitmapImage::create()));; 64 return nullImage.get(); 65 } 66 67 PassRefPtr<Image> Image::loadPlatformResource(const char *name) 68 { 69 const WebKit::WebData& resource = WebKit::Platform::current()->loadResource(name); 70 if (resource.isEmpty()) 71 return Image::nullImage(); 72 73 RefPtr<Image> image = BitmapImage::create(); 74 image->setData(resource, true); 75 return image.release(); 76 } 77 78 bool Image::supportsType(const String& type) 79 { 80 return MIMETypeRegistry::isSupportedImageResourceMIMEType(type); 81 } 82 83 bool Image::setData(PassRefPtr<SharedBuffer> data, bool allDataReceived) 84 { 85 m_encodedImageData = data; 86 if (!m_encodedImageData.get()) 87 return true; 88 89 int length = m_encodedImageData->size(); 90 if (!length) 91 return true; 92 93 return dataChanged(allDataReceived); 94 } 95 96 void Image::fillWithSolidColor(GraphicsContext* ctxt, const FloatRect& dstRect, const Color& color, CompositeOperator op) 97 { 98 if (!color.alpha()) 99 return; 100 101 CompositeOperator previousOperator = ctxt->compositeOperation(); 102 ctxt->setCompositeOperation(!color.hasAlpha() && op == CompositeSourceOver ? CompositeCopy : op); 103 ctxt->fillRect(dstRect, color); 104 ctxt->setCompositeOperation(previousOperator); 105 } 106 107 FloatRect Image::adjustForNegativeSize(const FloatRect& rect) 108 { 109 FloatRect norm = rect; 110 if (norm.width() < 0) { 111 norm.setX(norm.x() + norm.width()); 112 norm.setWidth(-norm.width()); 113 } 114 if (norm.height() < 0) { 115 norm.setY(norm.y() + norm.height()); 116 norm.setHeight(-norm.height()); 117 } 118 return norm; 119 } 120 121 void Image::draw(GraphicsContext* ctx, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, RespectImageOrientationEnum) 122 { 123 draw(ctx, dstRect, srcRect, op, blendMode); 124 } 125 126 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& destRect, const FloatPoint& srcPoint, const FloatSize& scaledTileSize, CompositeOperator op, BlendMode blendMode) 127 { 128 if (mayFillWithSolidColor()) { 129 fillWithSolidColor(ctxt, destRect, solidColor(), op); 130 return; 131 } 132 133 // See <https://webkit.org/b/59043>. 134 ASSERT(!isBitmapImage() || notSolidColor()); 135 136 FloatSize intrinsicTileSize = size(); 137 if (hasRelativeWidth()) 138 intrinsicTileSize.setWidth(scaledTileSize.width()); 139 if (hasRelativeHeight()) 140 intrinsicTileSize.setHeight(scaledTileSize.height()); 141 142 FloatSize scale(scaledTileSize.width() / intrinsicTileSize.width(), 143 scaledTileSize.height() / intrinsicTileSize.height()); 144 145 FloatRect oneTileRect; 146 oneTileRect.setX(destRect.x() + fmodf(fmodf(-srcPoint.x(), scaledTileSize.width()) - scaledTileSize.width(), scaledTileSize.width())); 147 oneTileRect.setY(destRect.y() + fmodf(fmodf(-srcPoint.y(), scaledTileSize.height()) - scaledTileSize.height(), scaledTileSize.height())); 148 oneTileRect.setSize(scaledTileSize); 149 150 // Check and see if a single draw of the image can cover the entire area we are supposed to tile. 151 if (oneTileRect.contains(destRect)) { 152 FloatRect visibleSrcRect; 153 visibleSrcRect.setX((destRect.x() - oneTileRect.x()) / scale.width()); 154 visibleSrcRect.setY((destRect.y() - oneTileRect.y()) / scale.height()); 155 visibleSrcRect.setWidth(destRect.width() / scale.width()); 156 visibleSrcRect.setHeight(destRect.height() / scale.height()); 157 draw(ctxt, destRect, visibleSrcRect, op, blendMode); 158 return; 159 } 160 161 FloatRect tileRect(FloatPoint(), intrinsicTileSize); 162 drawPattern(ctxt, tileRect, scale, oneTileRect.location(), op, destRect, blendMode); 163 164 startAnimation(); 165 } 166 167 // FIXME: Merge with the other drawTiled eventually, since we need a combination of both for some things. 168 void Image::drawTiled(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, 169 const FloatSize& providedTileScaleFactor, TileRule hRule, TileRule vRule, CompositeOperator op) 170 { 171 if (mayFillWithSolidColor()) { 172 fillWithSolidColor(ctxt, dstRect, solidColor(), op); 173 return; 174 } 175 176 // FIXME: We do not support 'space' yet. For now just map it to 'repeat'. 177 if (hRule == SpaceTile) 178 hRule = RepeatTile; 179 if (vRule == SpaceTile) 180 vRule = RepeatTile; 181 182 // FIXME: if this code is used for background-repeat: round (in addition to 183 // border-image-repeat), then add logic to deal with the background-size: auto 184 // special case. The aspect ratio should be maintained in this case. 185 FloatSize tileScaleFactor = providedTileScaleFactor; 186 bool useLowInterpolationQuality = false; 187 if (hRule == RoundTile) { 188 float hRepetitions = std::max(1.0f, roundf(dstRect.width() / (tileScaleFactor.width() * srcRect.width()))); 189 tileScaleFactor.setWidth(dstRect.width() / (srcRect.width() * hRepetitions)); 190 } 191 if (vRule == RoundTile) { 192 float vRepetitions = std::max(1.0f, roundf(dstRect.height() / (tileScaleFactor.height() * srcRect.height()))); 193 tileScaleFactor.setHeight(dstRect.height() / (srcRect.height() * vRepetitions)); 194 } 195 if (hRule == RoundTile || vRule == RoundTile) { 196 // High interpolation quality rounds the scaled tile to an integer size (see Image::drawPattern). 197 // To avoid causing a visual problem, linear interpolation must be used instead. 198 // FIXME: Allow using high-quality interpolation in this case, too. 199 useLowInterpolationQuality = true; 200 } 201 202 // We want to construct the phase such that the pattern is centered (when stretch is not 203 // set for a particular rule). 204 float hPhase = tileScaleFactor.width() * srcRect.x(); 205 float vPhase = tileScaleFactor.height() * srcRect.y(); 206 float scaledTileWidth = tileScaleFactor.width() * srcRect.width(); 207 float scaledTileHeight = tileScaleFactor.height() * srcRect.height(); 208 if (hRule == Image::RepeatTile) 209 hPhase -= (dstRect.width() - scaledTileWidth) / 2; 210 if (vRule == Image::RepeatTile) 211 vPhase -= (dstRect.height() - scaledTileHeight) / 2; 212 FloatPoint patternPhase(dstRect.x() - hPhase, dstRect.y() - vPhase); 213 214 if (useLowInterpolationQuality) { 215 InterpolationQuality previousInterpolationQuality = ctxt->imageInterpolationQuality(); 216 ctxt->setImageInterpolationQuality(InterpolationLow); 217 drawPattern(ctxt, srcRect, tileScaleFactor, patternPhase, op, dstRect); 218 ctxt->setImageInterpolationQuality(previousInterpolationQuality); 219 } else { 220 drawPattern(ctxt, srcRect, tileScaleFactor, patternPhase, op, dstRect); 221 } 222 223 startAnimation(); 224 } 225 226 void Image::drawPattern(GraphicsContext* context, const FloatRect& floatSrcRect, const FloatSize& scale, 227 const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& destRect, BlendMode blendMode) 228 { 229 TRACE_EVENT0("skia", "Image::drawPattern"); 230 if (RefPtr<NativeImageSkia> bitmap = nativeImageForCurrentFrame()) 231 bitmap->drawPattern(context, adjustForNegativeSize(floatSrcRect), scale, phase, compositeOp, destRect, blendMode); 232 } 233 234 void Image::computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) 235 { 236 intrinsicRatio = size(); 237 intrinsicWidth = Length(intrinsicRatio.width(), Fixed); 238 intrinsicHeight = Length(intrinsicRatio.height(), Fixed); 239 } 240 241 } 242