/*
 * Copyright (C) 2006-2020 Apple Inc. All rights reserved.
 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
 * Copyright (C) 2008 Torch Mobile, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#pragma once

#include "AffineTransform.h"
#include "Color.h"
#include "FloatPoint.h"
#include "GraphicsTypes.h"
#include <wtf/OwnPtr.h>
#include <wtf/PassOwnPtr.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>

#if USE(CG)

typedef struct CGContext* CGContextRef;

#define USE_CG_SHADING (PLATFORM(MAC) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1050)

#if USE_CG_SHADING
typedef struct CGShading* CGShadingRef;
typedef CGShadingRef PlatformGradient;
#else
typedef struct CGGradient* CGGradientRef;
typedef CGGradientRef PlatformGradient;
#endif

#elif PLATFORM(QT)
QT_BEGIN_NAMESPACE
class QGradient;
QT_END_NAMESPACE
typedef QGradient* PlatformGradient;
#elif USE(CAIRO)
typedef struct _cairo_pattern cairo_pattern_t;
typedef cairo_pattern_t* PlatformGradient;
#elif USE(OPENVG)
namespace WebCore {
class GradientOpenVG;
}
typedef WebCore::GradientOpenVG* PlatformGradient;
#elif USE(SKIA)
class SkShader;
typedef class SkShader* PlatformGradient;
typedef class SkShader* PlatformPattern;
#elif PLATFORM(WX)
class wxGraphicsBrush;
typedef wxGraphicsBrush* PlatformGradient;
#else
typedef void* PlatformGradient;
#endif

namespace WebCore {

class FloatRect;
class GraphicsContext;

class Gradient : public RefCounted<Gradient> {
public:
    // FIXME: ExtendedColor - A color stop needs a notion of color space.
    struct ColorStop {
    	WTF_MAKE_FAST_ALLOCATED;
    public:
        float offset { 0 };
        Color color;

        ColorStop() = default;
        ColorStop(float offset, const Color& color)
            : offset(offset)
            , color(color)
        {
        }
    };

    using ColorStopVector = Vector<ColorStop, 2>;

    struct LinearData {
    	WTF_MAKE_FAST_ALLOCATED;
    public:
        FloatPoint point0;
        FloatPoint point1;
    };

    struct RadialData {
    	WTF_MAKE_FAST_ALLOCATED;
    public:
        FloatPoint point0;
        FloatPoint point1;
        float startRadius;
        float endRadius;
        float aspectRatio; // For elliptical gradient, width / height.
    };

    struct ConicData {
    	WTF_MAKE_FAST_ALLOCATED;
    public:
        FloatPoint point0;
        float angleRadians;
    };

    enum class Type { Linear, Radial, Conic };

    static Ref<Gradient> create(const LinearData&);
    static Ref<Gradient> create(const RadialData&);
    static Ref<Gradient> create(const ConicData&);

    Type type() const;

    bool hasAlpha() const;
    bool isZeroSize() const;

    const LinearData* linearData() const { return m_linearData.get(); }
    const RadialData* radialData() const { return m_radialData.get(); }

    WEBCORE_EXPORT void addColorStop(const ColorStop&);
    WEBCORE_EXPORT void addColorStop(float, const Color&);
    void setSortedColorStops(ColorStopVector&&);

    const ColorStopVector& stops() const { return m_stops; }

    void setSpreadMethod(GradientSpreadMethod);
    GradientSpreadMethod spreadMethod() const { return m_spreadMethod; }

	bool canRender(const FloatSize&) const;

    void fill(GraphicsContext*, const FloatRect&);
	void adjustParametersForTiledDrawing(FloatSize&, FloatRect&, const FloatSize& spacing);

	unsigned hash() const;
	void invalidateHash() { m_cachedHash = 0; }

	double cacheDeletionInterval() const;
	bool shouldCache(int width, int height) const;

#if USE(CG)
    void paint(GraphicsContext&);
    void paint(CGContextRef);
#elif USE(CAIRO)
    PlatformGradient createPlatformGradient(float globalAlpha, const AffineTransform&);
#endif

private:
    Gradient(const LinearData&);
    Gradient(const RadialData&);
    Gradient(const ConicData&);

    PlatformGradient platformGradient();

	void sortStopsIfNecessary();

    OwnPtr<LinearData> m_linearData;
    OwnPtr<RadialData> m_radialData;
    OwnPtr<ConicData> m_conicData;

    mutable ColorStopVector m_stops;
    mutable bool m_stopsSorted { false };
    GradientSpreadMethod m_spreadMethod { SpreadMethodPad };

    mutable unsigned m_cachedHash { 0 };

#if USE(CG)
    RetainPtr<CGGradientRef> m_gradient;
#endif
};

} //namespace
