Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2007 Alp Toker <alp (at) atoker.com>
      4  * Copyright (C) 2008 Torch Mobile, Inc.
      5  * Copyright (C) 2013 Google Inc. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef Gradient_h
     30 #define Gradient_h
     31 
     32 #include "core/platform/graphics/FloatPoint.h"
     33 #include "core/platform/graphics/GraphicsTypes.h"
     34 #include "core/platform/graphics/transforms/AffineTransform.h"
     35 #include "wtf/PassRefPtr.h"
     36 #include "wtf/RefCounted.h"
     37 #include "wtf/RefPtr.h"
     38 #include "wtf/Vector.h"
     39 
     40 class SkShader;
     41 
     42 namespace WebCore {
     43 
     44 class Color;
     45 class FloatRect;
     46 class IntSize;
     47 
     48 class Gradient : public RefCounted<Gradient> {
     49 public:
     50     static PassRefPtr<Gradient> create(const FloatPoint& p0, const FloatPoint& p1)
     51     {
     52         return adoptRef(new Gradient(p0, p1));
     53     }
     54     static PassRefPtr<Gradient> create(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio = 1)
     55     {
     56         return adoptRef(new Gradient(p0, r0, p1, r1, aspectRatio));
     57     }
     58     ~Gradient();
     59 
     60     struct ColorStop {
     61         float stop;
     62         float red;
     63         float green;
     64         float blue;
     65         float alpha;
     66 
     67         ColorStop() : stop(0), red(0), green(0), blue(0), alpha(0) { }
     68         ColorStop(float s, float r, float g, float b, float a) : stop(s), red(r), green(g), blue(b), alpha(a) { }
     69     };
     70     void addColorStop(const ColorStop&);
     71     void addColorStop(float, const Color&);
     72 
     73     bool hasAlpha() const;
     74 
     75     bool isRadial() const { return m_radial; }
     76     bool isZeroSize() const { return m_p0.x() == m_p1.x() && m_p0.y() == m_p1.y() && (!m_radial || m_r0 == m_r1); }
     77 
     78     const FloatPoint& p0() const { return m_p0; }
     79     const FloatPoint& p1() const { return m_p1; }
     80 
     81     void setP0(const FloatPoint& p)
     82     {
     83         if (m_p0 == p)
     84             return;
     85 
     86         m_p0 = p;
     87         invalidateHash();
     88     }
     89 
     90     void setP1(const FloatPoint& p)
     91     {
     92         if (m_p1 == p)
     93             return;
     94 
     95         m_p1 = p;
     96         invalidateHash();
     97     }
     98 
     99     float startRadius() const { return m_r0; }
    100     float endRadius() const { return m_r1; }
    101 
    102     void setStartRadius(float r)
    103     {
    104         if (m_r0 == r)
    105             return;
    106 
    107         m_r0 = r;
    108         invalidateHash();
    109     }
    110 
    111     void setEndRadius(float r)
    112     {
    113         if (m_r1 == r)
    114             return;
    115 
    116         m_r1 = r;
    117         invalidateHash();
    118     }
    119 
    120     float aspectRatio() const { return m_aspectRatio; }
    121 
    122     SkShader* shader();
    123 
    124     void setStopsSorted(bool s) { m_stopsSorted = s; }
    125 
    126     void setDrawsInPMColorSpace(bool drawInPMColorSpace);
    127 
    128     void setSpreadMethod(GradientSpreadMethod);
    129     GradientSpreadMethod spreadMethod() { return m_spreadMethod; }
    130     void setGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
    131     AffineTransform gradientSpaceTransform() { return m_gradientSpaceTransformation; }
    132 
    133     void adjustParametersForTiledDrawing(IntSize&, FloatRect&);
    134 
    135     unsigned hash() const;
    136     void invalidateHash() { m_cachedHash = 0; }
    137 
    138 private:
    139     Gradient(const FloatPoint& p0, const FloatPoint& p1);
    140     Gradient(const FloatPoint& p0, float r0, const FloatPoint& p1, float r1, float aspectRatio);
    141 
    142     void destroyShader();
    143 
    144     void sortStopsIfNecessary();
    145 
    146     // Keep any parameters relevant to rendering in sync with the structure in Gradient::hash().
    147     bool m_radial;
    148     FloatPoint m_p0;
    149     FloatPoint m_p1;
    150     float m_r0;
    151     float m_r1;
    152     float m_aspectRatio; // For elliptical gradient, width / height.
    153     mutable Vector<ColorStop, 2> m_stops;
    154     mutable bool m_stopsSorted;
    155     GradientSpreadMethod m_spreadMethod;
    156     AffineTransform m_gradientSpaceTransformation;
    157 
    158     bool m_drawInPMColorSpace;
    159 
    160     mutable unsigned m_cachedHash;
    161 
    162     RefPtr<SkShader> m_gradient;
    163 };
    164 
    165 } // namespace WebCore
    166 
    167 #endif
    168