/*
 * Copyright (C) Research In Motion Limited 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 "RenderSVGHiddenContainer.h"
#include "RenderSVGResource.h"
#include <wtf/WeakHashSet.h>

namespace WebCore {

class RenderLayer;

class RenderSVGResourceContainer : public RenderSVGHiddenContainer, public RenderSVGResource {
public:
    RenderSVGResourceContainer(SVGElement&, PassOwnPtr<RenderStyle>);

    virtual void layout() OVERRIDE;
    virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle) final;

    virtual bool isSVGResourceContainer() const final { return true; }

    static bool shouldTransformOnTextPainting(RenderObject&, AffineTransform&);
    static AffineTransform transformOnNonScalingStroke(RenderObject*, const AffineTransform& resourceTransform);

    void idChanged();
    void markAllClientsForRepaint();
    void addClientRenderLayer(RenderLayer&);
    void removeClientRenderLayer(RenderLayer&);
    void markAllClientLayersForInvalidation();

protected:
    enum InvalidationMode {
        LayoutAndBoundariesInvalidation,
        BoundariesInvalidation,
        RepaintInvalidation,
        ParentOnlyInvalidation
    };

    // Used from the invalidateClient/invalidateClients methods from classes, inheriting from us.
    virtual bool selfNeedsClientInvalidation() const { return everHadLayout() && selfNeedsLayout(); }

    void markAllClientsForInvalidation(InvalidationMode);
    void markAllClientsForInvalidationIfNeeded(InvalidationMode, WeakHashSet<RenderObject>* visitedRenderers);
    void markClientForInvalidation(RenderObject&, InvalidationMode);

private:
    friend class SVGResourcesCache;
    void addClient(RenderElement&);
    void removeClient(RenderElement&);

    virtual void willBeDestroyed() FINAL;
    void registerResource();

    AtomicString m_id;
    WeakHashSet<RenderElement> m_clients;
    WeakHashSet<RenderLayer> m_clientLayers;
    bool m_registered { false };
    bool m_isInvalidating { false };
};

template<> inline bool isRendererOfType<const RenderSVGResourceContainer>(const RenderObject& renderer)
{
	return renderer.isSVGResourceContainer();
}
inline bool isRenderSVGResourceContainer(const RenderObject& renderer)
{
	return renderer.isSVGResourceContainer();
}
RENDER_OBJECT_TYPE_CASTS(RenderSVGResourceContainer, isSVGResourceContainer())

inline RenderSVGResourceContainer* getRenderSVGResourceContainerById(TreeScope& treeScope, const AtomicString& id)
{
    if (id.isEmpty())
        return nullptr;

    if (auto* renderResource = treeScope.svgResourceById(id))
        return renderResource;

    return nullptr;
}

template<typename Renderer>
inline Renderer* getRenderSVGResourceById(TreeScope& treeScope, const AtomicString& id)
{
    if (auto* container = getRenderSVGResourceContainerById(treeScope, id))
        return container->cast<Renderer>();

    return nullptr;
}

}

#endif
