1 | /* |
2 | * Copyright (C) 2008-2016 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 "GradientImage.h" |
28 | |
29 | #include "GraphicsContext.h" |
30 | #include "ImageBuffer.h" |
31 | |
32 | namespace WebCore { |
33 | |
34 | GradientImage::GradientImage(Gradient& generator, const FloatSize& size) |
35 | : m_gradient(generator) |
36 | { |
37 | setContainerSize(size); |
38 | } |
39 | |
40 | GradientImage::~GradientImage() = default; |
41 | |
42 | ImageDrawResult GradientImage::draw(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator compositeOp, BlendMode blendMode, DecodingMode, ImageOrientationDescription) |
43 | { |
44 | GraphicsContextStateSaver stateSaver(destContext); |
45 | destContext.setCompositeOperation(compositeOp, blendMode); |
46 | destContext.clip(destRect); |
47 | destContext.translate(destRect.location()); |
48 | if (destRect.size() != srcRect.size()) |
49 | destContext.scale(destRect.size() / srcRect.size()); |
50 | destContext.translate(-srcRect.location()); |
51 | destContext.fillRect(FloatRect(FloatPoint(), size()), m_gradient.get()); |
52 | return ImageDrawResult::DidDraw; |
53 | } |
54 | |
55 | void GradientImage::drawPattern(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, const AffineTransform& patternTransform, |
56 | const FloatPoint& phase, const FloatSize& spacing, CompositeOperator compositeOp, BlendMode blendMode) |
57 | { |
58 | // Allow the generator to provide visually-equivalent tiling parameters for better performance. |
59 | FloatSize adjustedSize = size(); |
60 | FloatRect adjustedSrcRect = srcRect; |
61 | m_gradient->adjustParametersForTiledDrawing(adjustedSize, adjustedSrcRect, spacing); |
62 | |
63 | // Factor in the destination context's scale to generate at the best resolution |
64 | AffineTransform destContextCTM = destContext.getCTM(GraphicsContext::DefinitelyIncludeDeviceScale); |
65 | double xScale = fabs(destContextCTM.xScale()); |
66 | double yScale = fabs(destContextCTM.yScale()); |
67 | AffineTransform adjustedPatternCTM = patternTransform; |
68 | adjustedPatternCTM.scale(1.0 / xScale, 1.0 / yScale); |
69 | adjustedSrcRect.scale(xScale, yScale); |
70 | |
71 | unsigned generatorHash = m_gradient->hash(); |
72 | |
73 | if (!m_cachedImage || m_cachedGeneratorHash != generatorHash || m_cachedAdjustedSize != adjustedSize || !areEssentiallyEqual(destContext.scaleFactor(), m_cachedScaleFactor)) { |
74 | auto imageBuffer = ImageBuffer::createCompatibleBuffer(adjustedSize, ColorSpaceSRGB, destContext); |
75 | if (!imageBuffer) |
76 | return; |
77 | |
78 | // Fill with the generated image. |
79 | imageBuffer->context().fillRect(FloatRect(FloatPoint(), adjustedSize), m_gradient.get()); |
80 | |
81 | m_cachedGeneratorHash = generatorHash; |
82 | m_cachedAdjustedSize = adjustedSize; |
83 | m_cachedScaleFactor = destContext.scaleFactor(); |
84 | |
85 | if (destContext.drawLuminanceMask()) |
86 | imageBuffer->convertToLuminanceMask(); |
87 | |
88 | m_cachedImage = ImageBuffer::sinkIntoImage(WTFMove(imageBuffer), PreserveResolution::Yes); |
89 | } |
90 | |
91 | destContext.setDrawLuminanceMask(false); |
92 | |
93 | // Tile the image buffer into the context. |
94 | m_cachedImage->drawPattern(destContext, destRect, adjustedSrcRect, adjustedPatternCTM, phase, spacing, compositeOp, blendMode); |
95 | |
96 | } |
97 | |
98 | void GradientImage::dump(WTF::TextStream& ts) const |
99 | { |
100 | GeneratedImage::dump(ts); |
101 | // FIXME: dump the gradient. |
102 | } |
103 | |
104 | } |
105 | |