blob: d22459a9c54785fc6be3fd635bcf7a07354f191a [file] [log] [blame]
* Copyright (C) 1999 Lars Knoll (
* (C) 1999 Antti Koivisto (
* (C) 2000 Dirk Mueller (
* (C) 2006 Allan Sandfeld Jensen (
* (C) 2006 Samuel Weinig (
* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) 2010 Google Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2011-2012. 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
* 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.
#include "config.h"
#include "core/rendering/RenderImage.h"
#include "core/HTMLNames.h"
#include "core/editing/FrameSelection.h"
#include "core/fetch/ImageResource.h"
#include "core/fetch/ResourceLoadPriorityOptimizer.h"
#include "core/fetch/ResourceLoader.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLAreaElement.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/HTMLInputElement.h"
#include "core/html/HTMLMapElement.h"
#include "core/paint/ImagePainter.h"
#include "core/rendering/HitTestResult.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/RenderView.h"
#include "core/rendering/TextRunConstructor.h"
#include "core/svg/graphics/SVGImage.h"
#include "platform/fonts/Font.h"
#include "platform/fonts/FontCache.h"
namespace blink {
float deviceScaleFactor(LocalFrame*);
using namespace HTMLNames;
RenderImage::RenderImage(Element* element)
: RenderReplaced(element, IntSize())
, m_didIncrementVisuallyNonEmptyPixelCount(false)
, m_isGeneratedContent(false)
, m_imageDevicePixelRatio(1.0f)
RenderImage* RenderImage::createAnonymous(Document* document)
RenderImage* image = new RenderImage(0);
return image;
void RenderImage::destroy()
void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource)
m_imageResource = imageResource;
// Alt text is restricted to this maximum size, in pixels. These are
// signed integers because they are compared with other signed values.
static const float maxAltTextWidth = 1024;
static const int maxAltTextHeight = 256;
IntSize RenderImage::imageSizeForError(ImageResource* newImage) const
ASSERT_ARG(newImage, newImage);
ASSERT_ARG(newImage, newImage->imageForRenderer(this));
IntSize imageSize;
if (newImage->willPaintBrokenImage()) {
float deviceScaleFactor = blink::deviceScaleFactor(frame());
pair<Image*, float> brokenImageAndImageScaleFactor = ImageResource::brokenImage(deviceScaleFactor);
imageSize = brokenImageAndImageScaleFactor.first->size();
imageSize.scale(1 / brokenImageAndImageScaleFactor.second);
} else
imageSize = newImage->imageForRenderer(this)->size();
// imageSize() returns 0 for the error image. We need the true size of the
// error image, so we have to get it by grabbing image() directly.
return IntSize(paddingWidth + imageSize.width() * style()->effectiveZoom(), paddingHeight + imageSize.height() * style()->effectiveZoom());
// Sets the image height and width to fit the alt text. Returns true if the
// image size changed.
bool RenderImage::setImageSizeForAltText(ImageResource* newImage /* = 0 */)
IntSize imageSize;
if (newImage && newImage->imageForRenderer(this))
imageSize = imageSizeForError(newImage);
else if (!m_altText.isEmpty() || newImage) {
// If we'll be displaying either text or an image, add a little padding.
imageSize = IntSize(paddingWidth, paddingHeight);
// we have an alt and the user meant it (its not a text we invented)
if (!m_altText.isEmpty()) {
FontCachePurgePreventer fontCachePurgePreventer;
const Font& font = style()->font();
IntSize paddedTextSize(paddingWidth + std::min(ceilf(font.width(constructTextRun(this, font, m_altText, style()))), maxAltTextWidth), paddingHeight + std::min(font.fontMetrics().height(), maxAltTextHeight));
imageSize = imageSize.expandedTo(paddedTextSize);
if (imageSize == intrinsicSize())
return false;
return true;
void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect)
if (documentBeingDestroyed())
if (hasBoxDecorationBackground() || hasMask() || hasShapeOutside())
RenderReplaced::imageChanged(newImage, rect);
if (!m_imageResource)
if (newImage != m_imageResource->imagePtr())
// Per the spec, we let the server-sent header override srcset/other sources of dpr.
if (m_imageResource->cachedImage() && m_imageResource->cachedImage()->hasDevicePixelRatioHeaderValue())
m_imageDevicePixelRatio = 1 / m_imageResource->cachedImage()->devicePixelRatioHeaderValue();
if (!m_didIncrementVisuallyNonEmptyPixelCount) {
// At a zoom level of 1 the image is guaranteed to have an integer size.
m_didIncrementVisuallyNonEmptyPixelCount = true;
bool imageSizeChanged = false;
// Set image dimensions, taking into account the size of the alt text.
if (m_imageResource->errorOccurred() || !newImage)
imageSizeChanged = setImageSizeForAltText(m_imageResource->cachedImage());
paintInvalidationOrMarkForLayout(imageSizeChanged, rect);
void RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize)
if (m_imageResource->errorOccurred() || !m_imageResource->hasImage())
void RenderImage::updateInnerContentRect()
// Propagate container size to the image resource.
LayoutRect containerRect = replacedContentRect();
IntSize containerSize(containerRect.width(), containerRect.height());
if (!containerSize.isEmpty())
void RenderImage::paintInvalidationOrMarkForLayout(bool imageSizeChangedToAccomodateAltText, const IntRect* rect)
LayoutSize oldIntrinsicSize = intrinsicSize();
LayoutSize newIntrinsicSize = m_imageResource->intrinsicSize(style()->effectiveZoom());
// In the case of generated image content using :before/:after/content, we might not be
// in the render tree yet. In that case, we just need to update our intrinsic size.
// layout() will be called after we are inserted in the tree which will take care of
// what we are doing here.
if (!containingBlock())
bool imageSourceHasChangedSize = oldIntrinsicSize != newIntrinsicSize || imageSizeChangedToAccomodateAltText;
if (imageSourceHasChangedSize)
// If the actual area occupied by the image has changed and it is not constrained by style then a layout is required.
bool imageSizeIsConstrained = style()->logicalWidth().isSpecified() && style()->logicalHeight().isSpecified();
// FIXME: We only need to recompute the containing block's preferred size if the containing block's size
// depends on the image's size (i.e., the container uses shrink-to-fit sizing).
// There's no easy way to detect that shrink-to-fit is needed, always force a layout.
bool containingBlockNeedsToRecomputePreferredSize = style()->logicalWidth().isPercent() || style()->logicalMaxWidth().isPercent() || style()->logicalMinWidth().isPercent();
if (imageSourceHasChangedSize && (!imageSizeIsConstrained || containingBlockNeedsToRecomputePreferredSize)) {
// The image hasn't changed in size or its style constrains its size, so a paint invalidation will suffice.
if (everHadLayout() && !selfNeedsLayout()) {
// The inner content rectangle is calculated during layout, but may need an update now
// (unless the box has already been scheduled for layout). In order to calculate it, we
// may need values from the containing block, though, so make sure that we're not too
// early. It may be that layout hasn't even taken place once yet.
LayoutRect paintInvalidationRect;
if (rect) {
// The image changed rect is in source image coordinates (without zoom),
// so map from the bounds of the image to the contentsBox.
const LayoutSize imageSizeWithoutZoom = m_imageResource->imageSize(1 / style()->effectiveZoom());
paintInvalidationRect = enclosingIntRect(mapRect(*rect, FloatRect(FloatPoint(), imageSizeWithoutZoom), contentBoxRect()));
// Guard against too-large changed rects.
} else {
paintInvalidationRect = contentBoxRect();
// FIXME: We should not be allowing paint invalidations during layout.
AllowPaintInvalidationScope scoper(frameView());
DisableCompositingQueryAsserts disabler;
// Tell any potential compositing layers that the image needs updating.
void RenderImage::notifyFinished(Resource* newImage)
if (!m_imageResource)
if (documentBeingDestroyed())
if (newImage == m_imageResource->cachedImage()) {
// tell any potential compositing layers
// that the image is done and they can reference it directly.
void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
ImagePainter(*this).paintReplaced(paintInfo, paintOffset);
void RenderImage::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
ImagePainter(*this).paint(paintInfo, paintOffset);
void RenderImage::paintBoxDecorationBackground(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
ImagePainter(*this).paintBoxDecorationBackground(paintInfo, paintOffset);
void RenderImage::areaElementFocusChanged(HTMLAreaElement* areaElement)
ASSERT(areaElement->imageElement() == node());
Path path = areaElement->computePath(this);
if (path.isEmpty())
RenderStyle* areaElementStyle = areaElement->computedStyle();
unsigned short outlineWidth = areaElementStyle->outlineWidth();
IntRect paintInvalidationRect = enclosingIntRect(path.boundingRect());
paintInvalidationOrMarkForLayout(false, &paintInvalidationRect);
bool RenderImage::boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance bleedAvoidance, InlineFlowBox*) const
if (!RenderBoxModelObject::boxShadowShouldBeAppliedToBackground(bleedAvoidance))
return false;
return !const_cast<RenderImage*>(this)->boxDecorationBackgroundIsKnownToBeObscured();
bool RenderImage::foregroundIsKnownToBeOpaqueInRect(const LayoutRect& localRect, unsigned) const
if (!m_imageResource->hasImage() || m_imageResource->errorOccurred())
return false;
if (m_imageResource->cachedImage() && !m_imageResource->cachedImage()->isLoaded())
return false;
if (!contentBoxRect().contains(localRect))
return false;
EFillBox backgroundClip = style()->backgroundClip();
// Background paints under borders.
if (backgroundClip == BorderFillBox && style()->hasBorder() && !style()->borderObscuresBackground())
return false;
// Background shows in padding area.
if ((backgroundClip == BorderFillBox || backgroundClip == PaddingFillBox) && style()->hasPadding())
return false;
// Object-position may leave parts of the content box empty, regardless of the value of object-fit.
if (style()->objectPosition() != RenderStyle::initialObjectPosition())
return false;
// Object-fit may leave parts of the content box empty.
ObjectFit objectFit = style()->objectFit();
if (objectFit != ObjectFitFill && objectFit != ObjectFitCover)
return false;
// Check for image with alpha.
return m_imageResource->cachedImage() && m_imageResource->cachedImage()->currentFrameKnownToBeOpaque(this);
bool RenderImage::computeBackgroundIsKnownToBeObscured()
if (!hasBackground())
return false;
LayoutRect paintedExtent;
if (!getBackgroundPaintedExtent(paintedExtent))
return false;
return foregroundIsKnownToBeOpaqueInRect(paintedExtent, 0);
LayoutUnit RenderImage::minimumReplacedHeight() const
return m_imageResource->errorOccurred() ? intrinsicSize().height() : LayoutUnit();
HTMLMapElement* RenderImage::imageMap() const
HTMLImageElement* i = isHTMLImageElement(node()) ? toHTMLImageElement(node()) : 0;
return i ? i->treeScope().getImageMap(i->fastGetAttribute(usemapAttr)) : 0;
bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
HitTestResult tempResult(result.hitTestLocation());
bool inside = RenderReplaced::nodeAtPoint(request, tempResult, locationInContainer, accumulatedOffset, hitTestAction);
if (tempResult.innerNode() && node()) {
if (HTMLMapElement* map = imageMap()) {
LayoutRect contentBox = contentBoxRect();
float scaleFactor = 1 / style()->effectiveZoom();
LayoutPoint mapLocation = locationInContainer.point() - toLayoutSize(accumulatedOffset) - locationOffset() - toLayoutSize(contentBox.location());
mapLocation.scale(scaleFactor, scaleFactor);
if (map->mapMouseEvent(mapLocation, contentBox.size(), tempResult))
if (!inside && result.isRectBasedTest())
if (inside)
result = tempResult;
return inside;
void RenderImage::updateAltText()
if (!node())
if (isHTMLInputElement(*node()))
m_altText = toHTMLInputElement(node())->altText();
else if (isHTMLImageElement(*node()))
m_altText = toHTMLImageElement(node())->altText();
void RenderImage::layout()
bool RenderImage::updateImageLoadingPriorities()
if (!m_imageResource || !m_imageResource->cachedImage() || m_imageResource->cachedImage()->isLoaded())
return false;
LayoutRect viewBounds = viewRect();
LayoutRect objectBounds = absoluteContentBox();
// The object bounds might be empty right now, so intersects will fail since it doesn't deal
// with empty rects. Use LayoutRect::contains in that case.
bool isVisible;
if (!objectBounds.isEmpty())
isVisible = viewBounds.intersects(objectBounds);
isVisible = viewBounds.contains(objectBounds);
ResourceLoadPriorityOptimizer::VisibilityStatus status = isVisible ?
ResourceLoadPriorityOptimizer::Visible : ResourceLoadPriorityOptimizer::NotVisible;
LayoutRect screenArea;
if (!objectBounds.isEmpty()) {
screenArea = viewBounds;
ResourceLoadPriorityOptimizer::resourceLoadPriorityOptimizer()->notifyImageResourceVisibility(m_imageResource->cachedImage(), status, screenArea);
return true;
void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
RenderReplaced::computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
// Our intrinsicSize is empty if we're rendering generated images with relative width/height. Figure out the right intrinsic size to use.
if (intrinsicSize.isEmpty() && (m_imageResource->imageHasRelativeWidth() || m_imageResource->imageHasRelativeHeight())) {
RenderObject* containingBlock = isOutOfFlowPositioned() ? container() : this->containingBlock();
if (containingBlock->isBox()) {
RenderBox* box = toRenderBox(containingBlock);
// Don't compute an intrinsic ratio to preserve historical WebKit behavior if we're painting alt text and/or a broken image.
// Video is excluded from this behavior because video elements have a default aspect ratio that a failed poster image load should not override.
if (m_imageResource && m_imageResource->errorOccurred() && !isVideo()) {
intrinsicRatio = 1;
bool RenderImage::needsPreferredWidthsRecalculation() const
if (RenderReplaced::needsPreferredWidthsRecalculation())
return true;
return embeddedContentBox();
RenderBox* RenderImage::embeddedContentBox() const
if (!m_imageResource)
return 0;
ImageResource* cachedImage = m_imageResource->cachedImage();
if (cachedImage && cachedImage->image() && cachedImage->image()->isSVGImage())
return toSVGImage(cachedImage->image())->embeddedContentBox();
return 0;
} // namespace blink