/*
 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#pragma once

#if ENABLE(SVG)
#include "FloatRect.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "IntSize.h"
#include "Path.h"
#include "RenderSVGResourceContainer.h"
#include "SVGClipPathElement.h"
#include "SVGUnitTypes.h"

#include <wtf/HashMap.h>
#include <wtf/OwnPtr.h>

namespace WebCore {

class RenderSVGResourceClipper FINAL : public RenderSVGResourceContainer {
public:
    RenderSVGResourceClipper(SVGClipPathElement&, PassOwnPtr<RenderStyle>);

    SVGClipPathElement& clipPathElement() const { return toSVGClipPathElement(nodeForNonAnonymous()); }

    void removeAllClientsFromCacheIfNeeded(bool markForInvalidation, WeakHashSet<RenderObject>* visitedRenderers) override;
    virtual void removeClientFromCache(RenderElement&, bool markForInvalidation = true) OVERRIDE;

    virtual bool applyResource(RenderElement&, const RenderStyle*, GraphicsContext*&, unsigned short resourceMode) OVERRIDE;
    // clipPath can be clipped too, but don't have a boundingBox or repaintRect. So we can't call
	// applyResource directly and use the rects from the object, since they are empty for RenderSVGResources
	// FIXME: We made applyClippingToContext public because we cannot call applyResource on HTML elements (it asserts on RenderObject::objectBoundingBox)
    // objectBoundingBox ia used to compute clip path geometry when clipPathUnits="objectBoundingBox".
	// clippedContentBounds is the bounds of the content to which clipping is being applied.
	bool applyClippingToContext(GraphicsContext&, RenderElement&, const FloatRect& objectBoundingBox, const FloatRect& clippedContentBounds, float effectiveZoom = 1);
	virtual FloatRect resourceBoundingBox(const RenderObject&) OVERRIDE;

    virtual RenderSVGResourceType resourceType() const OVERRIDE { return ClipperResourceType; }
    
    bool hitTestClipContent(const FloatRect&, const FloatPoint&);

    SVGUnitTypes::SVGUnitType clipPathUnits() const { return toSVGClipPathElement(element()).clipPathUnits(); }

    static RenderSVGResourceType s_resourceType;

protected:
    virtual bool selfNeedsClientInvalidation() const OVERRIDE { return (everHadLayout() || m_clipper.size()) && selfNeedsLayout(); }

private:
    struct ClipperData {
        FloatRect objectBoundingBox;
        FloatRect clippedContentBounds;
        AffineTransform absoluteTransform;
        bool contextPaintingDisabled { false };
        OwnPtr<ImageBuffer> imageBuffer;

        ClipperData() = default;
        ClipperData(PassOwnPtr<ImageBuffer> buffer, const FloatRect& boundingBox, const FloatRect& clippedBounds, const AffineTransform& transform, bool paintingDisabled)
			: objectBoundingBox(boundingBox)
			, clippedContentBounds(clippedBounds)
			, absoluteTransform(transform)
        	, contextPaintingDisabled(paintingDisabled)
			, imageBuffer(buffer)
		{
		}

		bool isValidForGeometry(const FloatRect& boundingBox, const FloatRect& clippedBounds, const AffineTransform& transform, bool paintingDisabled) const
		{
			return imageBuffer && objectBoundingBox == boundingBox && clippedContentBounds == clippedBounds && absoluteTransform == transform && contextPaintingDisabled == paintingDisabled;
		}
    };

    virtual const char* renderName() const OVERRIDE { return "RenderSVGResourceClipper"; }
    virtual bool isSVGResourceClipper() const OVERRIDE { return true; }

    bool pathOnlyClipping(GraphicsContext&, const AffineTransform&, const FloatRect&, float effectiveZoom);
    bool drawContentIntoMaskImage(ImageBuffer&, const FloatRect& objectBoundingBox, float effectiveZoom);
    void calculateClipContentRepaintRect();
    ClipperData& addRendererToClipper(const RenderObject&);

    FloatRect m_clipBoundaries;
    HashMap<const RenderObject*, ClipperData> m_clipper;
};

}

#if 0
SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::RenderSVGResourceClipper)
static bool isType(const WebCore::RenderObject& renderer) { return renderer.isSVGResourceClipper(); }
static bool isType(const WebCore::RenderSVGResource& resource) { return resource.resourceType() == WebCore::ClipperResourceType; }
SPECIALIZE_TYPE_TRAITS_END()
#endif

#endif
