2 * Copyright (C) 2006, 2007, 2008 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "GraphicsContextCG.h"
31 #include <ApplicationServices/ApplicationServices.h>
32 #include <wtf/RetainPtr.h>
36 void Gradient::platformDestroy()
39 CGShadingRelease(m_gradient);
41 CGGradientRelease(m_gradient);
47 static void gradientCallback(void* info, const CGFloat* in, CGFloat* out)
50 static_cast<const Gradient*>(info)->getColor(*in, &r, &g, &b, &a);
57 CGShadingRef Gradient::platformGradient()
62 const CGFloat intervalRanges[2] = { 0, 1 };
63 const CGFloat colorComponentRanges[4 * 2] = { 0, 1, 0, 1, 0, 1, 0, 1 };
64 const CGFunctionCallbacks gradientCallbacks = { 0, gradientCallback, 0 };
65 RetainPtr<CGFunctionRef> colorFunction(AdoptCF, CGFunctionCreate(this, 1, intervalRanges, 4, colorComponentRanges, &gradientCallbacks));
67 CGColorSpaceRef colorSpace = deviceRGBColorSpaceRef();
70 m_gradient = CGShadingCreateRadial(colorSpace, m_p0, m_r0, m_p1, m_r1, colorFunction.get(), true, true);
72 m_gradient = CGShadingCreateAxial(colorSpace, m_p0, m_p1, colorFunction.get(), true, true);
77 CGGradientRef Gradient::platformGradient()
82 sortStopsIfNecessary();
84 const int cReservedStops = 3;
85 Vector<CGFloat, 4 * cReservedStops> colorComponents;
86 colorComponents.reserveCapacity(m_stops.size() * 4); // RGBA components per stop
88 Vector<CGFloat, cReservedStops> locations;
89 locations.reserveCapacity(m_stops.size());
91 for (size_t i = 0; i < m_stops.size(); ++i) {
92 colorComponents.uncheckedAppend(m_stops[i].red);
93 colorComponents.uncheckedAppend(m_stops[i].green);
94 colorComponents.uncheckedAppend(m_stops[i].blue);
95 colorComponents.uncheckedAppend(m_stops[i].alpha);
97 locations.uncheckedAppend(m_stops[i].stop);
100 m_gradient = CGGradientCreateWithColorComponents(deviceRGBColorSpaceRef(), colorComponents.data(), locations.data(), m_stops.size());
106 void Gradient::fill(GraphicsContext* context, const FloatRect& rect)
112 void Gradient::paint(GraphicsContext* context)
114 CGContextRef ctx = context->platformContext();
118 void Gradient::paint(CGContextRef context)
121 CGContextDrawShading(context, platformGradient());
123 CGGradientDrawingOptions extendOptions = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
125 bool needScaling = aspectRatio() != 1;
127 CGContextSaveGState(context);
128 // Scale from the center of the gradient. We only ever scale non-deprecated gradients,
129 // for which m_p0 == m_p1.
130 ASSERT(m_p0 == m_p1);
131 CGContextTranslateCTM(context, m_p0.x(), m_p0.y());
132 CGContextScaleCTM(context, 1, 1 / aspectRatio());
133 CGContextTranslateCTM(context, -m_p0.x(), -m_p0.y());
136 CGContextDrawRadialGradient(context, platformGradient(), m_p0, m_r0, m_p1, m_r1, extendOptions);
139 CGContextRestoreGState(context);
141 CGContextDrawLinearGradient(context, platformGradient(), m_p0, m_p1, extendOptions);