This source file includes following definitions.
- resourceLoadPriorityOptimizer
- screenArea
- addRenderObject
- removeRenderObject
- updateAllImageResourcePriorities
- updateImageResourcesWithLoadPriority
- notifyImageResourceVisibility
#include "config.h"
#include "core/fetch/ResourceLoadPriorityOptimizer.h"
#include "core/rendering/RenderObject.h"
#include "wtf/Vector.h"
namespace WebCore {
ResourceLoadPriorityOptimizer* ResourceLoadPriorityOptimizer::resourceLoadPriorityOptimizer()
{
DEFINE_STATIC_LOCAL(ResourceLoadPriorityOptimizer, s_renderLoadOptimizer, ());
return &s_renderLoadOptimizer;
}
ResourceLoadPriorityOptimizer::ResourceAndVisibility::ResourceAndVisibility(ImageResource* image, VisibilityStatus visibilityStatus, uint32_t screenArea)
: imageResource(image)
, status(visibilityStatus)
, screenArea(screenArea)
{
}
ResourceLoadPriorityOptimizer::ResourceAndVisibility::~ResourceAndVisibility()
{
}
ResourceLoadPriorityOptimizer::ResourceLoadPriorityOptimizer()
{
}
ResourceLoadPriorityOptimizer::~ResourceLoadPriorityOptimizer()
{
}
void ResourceLoadPriorityOptimizer::addRenderObject(RenderObject* renderer)
{
m_objects.add(renderer);
renderer->setHasPendingResourceUpdate(true);
}
void ResourceLoadPriorityOptimizer::removeRenderObject(RenderObject* renderer)
{
if (!renderer->hasPendingResourceUpdate())
return;
m_objects.remove(renderer);
renderer->setHasPendingResourceUpdate(false);
}
void ResourceLoadPriorityOptimizer::updateAllImageResourcePriorities()
{
m_imageResources.clear();
Vector<RenderObject*> objectsToRemove;
for (RenderObjectSet::iterator it = m_objects.begin(); it != m_objects.end(); ++it) {
RenderObject* obj = *it;
if (!obj->updateImageLoadingPriorities()) {
objectsToRemove.append(obj);
}
}
for (Vector<RenderObject*>::iterator it = objectsToRemove.begin(); it != objectsToRemove.end(); ++it)
m_objects.remove(*it);
updateImageResourcesWithLoadPriority();
}
void ResourceLoadPriorityOptimizer::updateImageResourcesWithLoadPriority()
{
for (ImageResourceMap::iterator it = m_imageResources.begin(); it != m_imageResources.end(); ++it) {
ResourceLoadPriority priority = it->value->status == Visible ?
ResourceLoadPriorityLow : ResourceLoadPriorityVeryLow;
if (priority != it->value->imageResource->resourceRequest().priority()) {
it->value->imageResource->resourceRequest().setPriority(priority, it->value->screenArea);
it->value->imageResource->didChangePriority(priority, it->value->screenArea);
}
}
m_imageResources.clear();
}
void ResourceLoadPriorityOptimizer::notifyImageResourceVisibility(ImageResource* img, VisibilityStatus status, const LayoutRect& screenRect)
{
if (!img || img->isLoaded())
return;
int screenArea = 0;
if (!screenRect.isEmpty() && status == Visible)
screenArea += static_cast<uint32_t>(screenRect.width() * screenRect.height());
ImageResourceMap::AddResult result = m_imageResources.add(img->identifier(), adoptPtr(new ResourceAndVisibility(img, status, screenArea)));
if (!result.isNewEntry && status == Visible) {
result.storedValue->value->status = status;
result.storedValue->value->screenArea = status;
}
}
}