1 /* 2 * Copyright (C) 2008, 2009, 2010, 2012 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 INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "core/platform/graphics/GeneratorGeneratedImage.h" 28 29 #include "core/platform/graphics/FloatRect.h" 30 #include "core/platform/graphics/GraphicsContextStateSaver.h" 31 32 #define SKIA_MAX_PATTERN_SIZE 32767 33 34 namespace WebCore { 35 36 void GeneratorGeneratedImage::draw(GraphicsContext* destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator compositeOp, BlendMode blendMode) 37 { 38 GraphicsContextStateSaver stateSaver(*destContext); 39 destContext->setCompositeOperation(compositeOp, blendMode); 40 destContext->clip(destRect); 41 destContext->translate(destRect.x(), destRect.y()); 42 if (destRect.size() != srcRect.size()) 43 destContext->scale(FloatSize(destRect.width() / srcRect.width(), destRect.height() / srcRect.height())); 44 destContext->translate(-srcRect.x(), -srcRect.y()); 45 destContext->setFillGradient(m_gradient); 46 destContext->fillRect(FloatRect(FloatPoint(), m_size)); 47 } 48 49 void GeneratorGeneratedImage::drawPatternWithoutCache(GraphicsContext* destContext, const FloatRect& srcRect, const FloatSize& scale, 50 const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& destRect, BlendMode blendMode) 51 { 52 int firstColumn = static_cast<int>(floorf((((destRect.x() - phase.x()) / scale.width()) - srcRect.x()) / srcRect.width())); 53 int firstRow = static_cast<int>(floorf((((destRect.y() - phase.y()) / scale.height()) - srcRect.y()) / srcRect.height())); 54 for (int i = firstColumn; ; ++i) { 55 float dstX = (srcRect.x() + i * srcRect.width()) * scale.width() + phase.x(); 56 // assert that first column encroaches left edge of dstRect. 57 ASSERT(i > firstColumn || dstX <= destRect.x()); 58 ASSERT(i == firstColumn || dstX > destRect.x()); 59 60 if (dstX >= destRect.maxX()) 61 break; 62 float dstMaxX = dstX + srcRect.width() * scale.width(); 63 if (dstX < destRect.x()) 64 dstX = destRect.x(); 65 if (dstMaxX > destRect.maxX()) 66 dstMaxX = destRect.maxX(); 67 if (dstX >= dstMaxX) 68 continue; 69 70 FloatRect visibleSrcRect; 71 FloatRect tileDstRect; 72 tileDstRect.setX(dstX); 73 tileDstRect.setWidth(dstMaxX - dstX); 74 visibleSrcRect.setX((tileDstRect.x() - phase.x()) / scale.width() - i * srcRect.width()); 75 visibleSrcRect.setWidth(tileDstRect.width() / scale.width()); 76 77 for (int j = firstRow; ; j++) { 78 float dstY = (srcRect.y() + j * srcRect.height()) * scale.height() + phase.y(); 79 // assert that first row encroaches top edge of dstRect. 80 ASSERT(j > firstRow || dstY <= destRect.y()); 81 ASSERT(j == firstRow || dstY > destRect.y()); 82 83 if (dstY >= destRect.maxY()) 84 break; 85 float dstMaxY = dstY + srcRect.height() * scale.height(); 86 if (dstY < destRect.y()) 87 dstY = destRect.y(); 88 if (dstMaxY > destRect.maxY()) 89 dstMaxY = destRect.maxY(); 90 if (dstY >= dstMaxY) 91 continue; 92 93 tileDstRect.setY(dstY); 94 tileDstRect.setHeight(dstMaxY - dstY); 95 visibleSrcRect.setY((tileDstRect.y() - phase.y()) / scale.height() - j * srcRect.height()); 96 visibleSrcRect.setHeight(tileDstRect.height() / scale.height()); 97 draw(destContext, tileDstRect, visibleSrcRect, compositeOp, blendMode); 98 } 99 } 100 } 101 102 void GeneratorGeneratedImage::drawPattern(GraphicsContext* destContext, const FloatRect& srcRect, const FloatSize& scale, 103 const FloatPoint& phase, CompositeOperator compositeOp, const FloatRect& destRect, BlendMode blendMode) 104 { 105 // Allow the generator to provide visually-equivalent tiling parameters for better performance. 106 IntSize adjustedSize = m_size; 107 FloatRect adjustedSrcRect = srcRect; 108 109 m_gradient->adjustParametersForTiledDrawing(adjustedSize, adjustedSrcRect); 110 111 if (adjustedSize.width() > SKIA_MAX_PATTERN_SIZE || adjustedSize.height() > SKIA_MAX_PATTERN_SIZE) { 112 // Workaround to fix crbug.com/241486 113 // SkBitmapProcShader is unable to handle cached tiles >= 32k pixels high or wide. 114 drawPatternWithoutCache(destContext, srcRect, scale, phase, compositeOp, destRect, blendMode); 115 return; 116 } 117 118 // Factor in the destination context's scale to generate at the best resolution. 119 // FIXME: No need to get the full CTM here, we just need the scale. 120 AffineTransform destContextCTM = destContext->getCTM(GraphicsContext::DefinitelyIncludeDeviceScale); 121 float xScale = fabs(destContextCTM.xScale()); 122 float yScale = fabs(destContextCTM.yScale()); 123 FloatSize scaleWithoutCTM(scale.width() / xScale, scale.height() / yScale); 124 adjustedSrcRect.scale(xScale, yScale); 125 126 unsigned generatorHash = m_gradient->hash(); 127 128 if (!m_cachedImageBuffer || m_cachedGeneratorHash != generatorHash || m_cachedAdjustedSize != adjustedSize || !destContext->isCompatibleWithBuffer(m_cachedImageBuffer.get())) { 129 m_cachedImageBuffer = destContext->createCompatibleBuffer(adjustedSize, m_gradient->hasAlpha()); 130 if (!m_cachedImageBuffer) 131 return; 132 133 // Fill with the generated image. 134 m_cachedImageBuffer->context()->setFillGradient(m_gradient); 135 m_cachedImageBuffer->context()->fillRect(FloatRect(FloatPoint(), adjustedSize)); 136 137 m_cachedGeneratorHash = generatorHash; 138 m_cachedAdjustedSize = adjustedSize; 139 } 140 141 // Tile the image buffer into the context. 142 m_cachedImageBuffer->drawPattern(destContext, adjustedSrcRect, scaleWithoutCTM, phase, compositeOp, destRect, blendMode); 143 m_cacheTimer.restart(); 144 } 145 146 void GeneratorGeneratedImage::invalidateCacheTimerFired(DeferrableOneShotTimer<GeneratorGeneratedImage>*) 147 { 148 m_cachedImageBuffer.clear(); 149 m_cachedAdjustedSize = IntSize(); 150 } 151 152 } 153