Home | History | Annotate | Download | only in cg
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple Computer, Inc.  All rights reserved.
      3  * Copyright (C) 2007 Alp Toker <alp (at) atoker.com>
      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 "Gradient.h"
     29 
     30 #include "GraphicsContextCG.h"
     31 #include <ApplicationServices/ApplicationServices.h>
     32 #include <wtf/RetainPtr.h>
     33 
     34 namespace WebCore {
     35 
     36 void Gradient::platformDestroy()
     37 {
     38 #if USE_CG_SHADING
     39     CGShadingRelease(m_gradient);
     40 #else
     41     CGGradientRelease(m_gradient);
     42 #endif
     43     m_gradient = 0;
     44 }
     45 
     46 #if USE_CG_SHADING
     47 static void gradientCallback(void* info, const CGFloat* in, CGFloat* out)
     48 {
     49     float r, g, b, a;
     50     static_cast<const Gradient*>(info)->getColor(*in, &r, &g, &b, &a);
     51     out[0] = r;
     52     out[1] = g;
     53     out[2] = b;
     54     out[3] = a;
     55 }
     56 
     57 CGShadingRef Gradient::platformGradient()
     58 {
     59     if (m_gradient)
     60         return m_gradient;
     61 
     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));
     66 
     67     CGColorSpaceRef colorSpace = deviceRGBColorSpaceRef();
     68 
     69     if (m_radial)
     70         m_gradient = CGShadingCreateRadial(colorSpace, m_p0, m_r0, m_p1, m_r1, colorFunction.get(), true, true);
     71     else
     72         m_gradient = CGShadingCreateAxial(colorSpace, m_p0, m_p1, colorFunction.get(), true, true);
     73 
     74     return m_gradient;
     75 }
     76 #else
     77 CGGradientRef Gradient::platformGradient()
     78 {
     79     if (m_gradient)
     80         return m_gradient;
     81 
     82     sortStopsIfNecessary();
     83 
     84     const int cReservedStops = 3;
     85     Vector<CGFloat, 4 * cReservedStops> colorComponents;
     86     colorComponents.reserveCapacity(m_stops.size() * 4); // RGBA components per stop
     87 
     88     Vector<CGFloat, cReservedStops> locations;
     89     locations.reserveCapacity(m_stops.size());
     90 
     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);
     96 
     97         locations.uncheckedAppend(m_stops[i].stop);
     98     }
     99 
    100     m_gradient = CGGradientCreateWithColorComponents(deviceRGBColorSpaceRef(), colorComponents.data(), locations.data(), m_stops.size());
    101 
    102     return m_gradient;
    103 }
    104 #endif
    105 
    106 void Gradient::fill(GraphicsContext* context, const FloatRect& rect)
    107 {
    108     context->clip(rect);
    109     paint(context);
    110 }
    111 
    112 void Gradient::paint(GraphicsContext* context)
    113 {
    114     CGContextRef ctx = context->platformContext();
    115     paint(ctx);
    116 }
    117 
    118 void Gradient::paint(CGContextRef context)
    119 {
    120 #if USE_CG_SHADING
    121     CGContextDrawShading(context, platformGradient());
    122 #else
    123     CGGradientDrawingOptions extendOptions = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
    124     if (m_radial) {
    125         bool needScaling = aspectRatio() != 1;
    126         if (needScaling) {
    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());
    134         }
    135 
    136         CGContextDrawRadialGradient(context, platformGradient(), m_p0, m_r0, m_p1, m_r1, extendOptions);
    137 
    138         if (needScaling)
    139             CGContextRestoreGState(context);
    140     } else
    141         CGContextDrawLinearGradient(context, platformGradient(), m_p0, m_p1, extendOptions);
    142 #endif
    143 }
    144 
    145 } //namespace
    146