This source file includes following definitions.
- m_resources
- resourceContainsCycles
- resolveCycles
- breakCycle
#include "config.h"
#include "core/rendering/svg/SVGResourcesCycleSolver.h"
#define DEBUG_CYCLE_DETECTION 0
#include "core/rendering/svg/RenderSVGResourceClipper.h"
#include "core/rendering/svg/RenderSVGResourceFilter.h"
#include "core/rendering/svg/RenderSVGResourceMarker.h"
#include "core/rendering/svg/RenderSVGResourceMasker.h"
#include "core/rendering/svg/SVGResources.h"
#include "core/rendering/svg/SVGResourcesCache.h"
#include "core/svg/SVGFilterElement.h"
#include "core/svg/SVGGradientElement.h"
#include "core/svg/SVGPatternElement.h"
namespace WebCore {
SVGResourcesCycleSolver::SVGResourcesCycleSolver(RenderObject* renderer, SVGResources* resources)
: m_renderer(renderer)
, m_resources(resources)
{
ASSERT(m_renderer);
ASSERT(m_resources);
}
SVGResourcesCycleSolver::~SVGResourcesCycleSolver()
{
}
bool SVGResourcesCycleSolver::resourceContainsCycles(RenderObject* renderer) const
{
ASSERT(renderer);
if (SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(renderer)) {
HashSet<RenderSVGResourceContainer*> resourceSet;
resources->buildSetOfResources(resourceSet);
HashSet<RenderSVGResourceContainer*>::iterator end = resourceSet.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = resourceSet.begin(); it != end; ++it) {
if (m_allResources.contains(*it))
return true;
}
}
for (RenderObject* child = renderer->firstChild(); child; child = child->nextSibling()) {
SVGResources* childResources = SVGResourcesCache::cachedResourcesForRenderObject(child);
if (!childResources)
continue;
HashSet<RenderSVGResourceContainer*> childSet;
childResources->buildSetOfResources(childSet);
HashSet<RenderSVGResourceContainer*>::iterator end = childSet.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = childSet.begin(); it != end; ++it) {
if (m_allResources.contains(*it))
return true;
}
if (resourceContainsCycles(child))
return true;
}
return false;
}
void SVGResourcesCycleSolver::resolveCycles()
{
ASSERT(m_allResources.isEmpty());
#if DEBUG_CYCLE_DETECTION > 0
fprintf(stderr, "\nBefore cycle detection:\n");
m_resources->dump(m_renderer);
#endif
HashSet<RenderSVGResourceContainer*> localResources;
m_resources->buildSetOfResources(localResources);
ASSERT(!localResources.isEmpty());
HashSet<RenderSVGResourceContainer*> parentResources;
RenderObject* parent = m_renderer->parent();
while (parent) {
if (parent->isSVGResourceContainer())
parentResources.add(toRenderSVGResourceContainer(parent));
parent = parent->parent();
}
#if DEBUG_CYCLE_DETECTION > 0
fprintf(stderr, "\nDetecting wheter any resources references any of following objects:\n");
{
fprintf(stderr, "Local resources:\n");
HashSet<RenderSVGResourceContainer*>::iterator end = localResources.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = localResources.begin(); it != end; ++it)
fprintf(stderr, "|> %s: object=%p (node=%p)\n", (*it)->renderName(), *it, (*it)->node());
fprintf(stderr, "Parent resources:\n");
end = parentResources.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = parentResources.begin(); it != end; ++it)
fprintf(stderr, "|> %s: object=%p (node=%p)\n", (*it)->renderName(), *it, (*it)->node());
}
#endif
m_allResources = localResources;
HashSet<RenderSVGResourceContainer*>::iterator end = parentResources.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = parentResources.begin(); it != end; ++it)
m_allResources.add(*it);
if (m_renderer->isSVGResourceContainer())
m_allResources.add(toRenderSVGResourceContainer(m_renderer));
ASSERT(!m_allResources.isEmpty());
end = localResources.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = localResources.begin(); it != end; ++it) {
RenderSVGResourceContainer* resource = *it;
if (parentResources.contains(resource) || resourceContainsCycles(resource))
breakCycle(resource);
}
#if DEBUG_CYCLE_DETECTION > 0
fprintf(stderr, "\nAfter cycle detection:\n");
m_resources->dump(m_renderer);
#endif
m_allResources.clear();
}
void SVGResourcesCycleSolver::breakCycle(RenderSVGResourceContainer* resourceLeadingToCycle)
{
ASSERT(resourceLeadingToCycle);
if (resourceLeadingToCycle == m_resources->linkedResource()) {
m_resources->resetLinkedResource();
return;
}
switch (resourceLeadingToCycle->resourceType()) {
case MaskerResourceType:
ASSERT(resourceLeadingToCycle == m_resources->masker());
m_resources->resetMasker();
break;
case MarkerResourceType:
ASSERT(resourceLeadingToCycle == m_resources->markerStart() || resourceLeadingToCycle == m_resources->markerMid() || resourceLeadingToCycle == m_resources->markerEnd());
if (m_resources->markerStart() == resourceLeadingToCycle)
m_resources->resetMarkerStart();
if (m_resources->markerMid() == resourceLeadingToCycle)
m_resources->resetMarkerMid();
if (m_resources->markerEnd() == resourceLeadingToCycle)
m_resources->resetMarkerEnd();
break;
case PatternResourceType:
case LinearGradientResourceType:
case RadialGradientResourceType:
ASSERT(resourceLeadingToCycle == m_resources->fill() || resourceLeadingToCycle == m_resources->stroke());
if (m_resources->fill() == resourceLeadingToCycle)
m_resources->resetFill();
if (m_resources->stroke() == resourceLeadingToCycle)
m_resources->resetStroke();
break;
case FilterResourceType:
ASSERT(resourceLeadingToCycle == m_resources->filter());
m_resources->resetFilter();
break;
case ClipperResourceType:
ASSERT(resourceLeadingToCycle == m_resources->clipper());
m_resources->resetClipper();
break;
case SolidColorResourceType:
ASSERT_NOT_REACHED();
break;
}
}
}