| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | |
| 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | |
| 4 * (C) 2000 Dirk Mueller (mueller@kde.org) | |
| 5 * (C) 2006 Allan Sandfeld Jensen (kde@carewolf.com) | |
| 6 * (C) 2006 Samuel Weinig (sam.weinig@gmail.com) | |
| 7 * Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Apple Inc. All r
ights reserved. | |
| 8 * Copyright (C) 2010 Google Inc. All rights reserved. | |
| 9 * Copyright (C) Research In Motion Limited 2011-2012. All rights reserved. | |
| 10 * | |
| 11 * This library is free software; you can redistribute it and/or | |
| 12 * modify it under the terms of the GNU Library General Public | |
| 13 * License as published by the Free Software Foundation; either | |
| 14 * version 2 of the License, or (at your option) any later version. | |
| 15 * | |
| 16 * This library is distributed in the hope that it will be useful, | |
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 19 * Library General Public License for more details. | |
| 20 * | |
| 21 * You should have received a copy of the GNU Library General Public License | |
| 22 * along with this library; see the file COPYING.LIB. If not, write to | |
| 23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 24 * Boston, MA 02110-1301, USA. | |
| 25 * | |
| 26 */ | |
| 27 | |
| 28 #include "sky/engine/core/rendering/RenderImage.h" | |
| 29 | |
| 30 #include "gen/sky/core/HTMLNames.h" | |
| 31 #include "sky/engine/core/editing/FrameSelection.h" | |
| 32 #include "sky/engine/core/fetch/ImageResource.h" | |
| 33 #include "sky/engine/core/fetch/ResourceLoader.h" | |
| 34 #include "sky/engine/core/frame/LocalFrame.h" | |
| 35 #include "sky/engine/core/rendering/HitTestResult.h" | |
| 36 #include "sky/engine/core/rendering/PaintInfo.h" | |
| 37 #include "sky/engine/core/rendering/RenderLayer.h" | |
| 38 #include "sky/engine/core/rendering/RenderView.h" | |
| 39 #include "sky/engine/core/rendering/TextRunConstructor.h" | |
| 40 #include "sky/engine/platform/fonts/Font.h" | |
| 41 #include "sky/engine/platform/fonts/FontCache.h" | |
| 42 #include "sky/engine/platform/graphics/GraphicsContext.h" | |
| 43 #include "sky/engine/platform/graphics/GraphicsContextStateSaver.h" | |
| 44 | |
| 45 namespace blink { | |
| 46 | |
| 47 float deviceScaleFactor(LocalFrame*); | |
| 48 | |
| 49 RenderImage::RenderImage(Element* element) | |
| 50 : RenderReplaced(element, IntSize()) | |
| 51 , m_imageDevicePixelRatio(1.0f) | |
| 52 { | |
| 53 } | |
| 54 | |
| 55 RenderImage::~RenderImage() | |
| 56 { | |
| 57 } | |
| 58 | |
| 59 void RenderImage::destroy() | |
| 60 { | |
| 61 ASSERT(m_imageResource); | |
| 62 m_imageResource->shutdown(); | |
| 63 RenderReplaced::destroy(); | |
| 64 } | |
| 65 | |
| 66 void RenderImage::intrinsicSizeChanged() | |
| 67 { | |
| 68 if (m_imageResource) | |
| 69 imageChanged(m_imageResource->imagePtr()); | |
| 70 } | |
| 71 | |
| 72 void RenderImage::setImageResource(PassOwnPtr<RenderImageResource> imageResource
) | |
| 73 { | |
| 74 ASSERT(!m_imageResource); | |
| 75 m_imageResource = imageResource; | |
| 76 m_imageResource->initialize(this); | |
| 77 } | |
| 78 | |
| 79 void RenderImage::imageChanged(WrappedImagePtr newImage, const IntRect* rect) | |
| 80 { | |
| 81 if (documentBeingDestroyed()) | |
| 82 return; | |
| 83 | |
| 84 if (!m_imageResource) | |
| 85 return; | |
| 86 | |
| 87 if (newImage != m_imageResource->imagePtr()) | |
| 88 return; | |
| 89 | |
| 90 // Per the spec, we let the server-sent header override srcset/other sources
of dpr. | |
| 91 // https://github.com/igrigorik/http-client-hints/blob/master/draft-grigorik
-http-client-hints-01.txt#L255 | |
| 92 if (m_imageResource->cachedImage() && m_imageResource->cachedImage()->hasDev
icePixelRatioHeaderValue()) | |
| 93 m_imageDevicePixelRatio = 1 / m_imageResource->cachedImage()->devicePixe
lRatioHeaderValue(); | |
| 94 | |
| 95 // If the RenderImage was just created we don't have style() or a parent() | |
| 96 // yet so all we can do is update our intrinsic size. Once we're inserted | |
| 97 // the resulting layout will do the rest of the work. | |
| 98 if (!parent()) { | |
| 99 updateIntrinsicSizeIfNeeded(m_imageResource->intrinsicSize()); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 RenderReplaced::imageChanged(newImage, rect); | |
| 104 | |
| 105 ASSERT(isRooted()); | |
| 106 | |
| 107 LayoutSize oldIntrinsicSize = intrinsicSize(); | |
| 108 LayoutSize newIntrinsicSize = m_imageResource->intrinsicSize(); | |
| 109 updateIntrinsicSizeIfNeeded(newIntrinsicSize); | |
| 110 | |
| 111 bool imageSourceHasChangedSize = oldIntrinsicSize != newIntrinsicSize; | |
| 112 if (imageSourceHasChangedSize) | |
| 113 setPreferredLogicalWidthsDirty(); | |
| 114 | |
| 115 // If the actual area occupied by the image has changed and it is not constr
ained by style then a layout is required. | |
| 116 bool imageSizeIsConstrained = style()->logicalWidth().isSpecified() && style
()->logicalHeight().isSpecified(); | |
| 117 | |
| 118 // FIXME: We only need to recompute the containing block's preferred size if
the containing block's size | |
| 119 // depends on the image's size (i.e., the container uses shrink-to-fit sizin
g). | |
| 120 // There's no easy way to detect that shrink-to-fit is needed, always force
a layout. | |
| 121 bool containingBlockNeedsToRecomputePreferredSize = style()->logicalWidth().
isPercent() || style()->logicalMaxWidth().isPercent() || style()->logicalMinWid
th().isPercent(); | |
| 122 | |
| 123 if (imageSourceHasChangedSize && (!imageSizeIsConstrained || containingBlock
NeedsToRecomputePreferredSize)) { | |
| 124 setNeedsLayout(); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 // The image hasn't changed in size or its style constrains its size, so a p
aint invalidation will suffice. | |
| 129 if (everHadLayout() && !selfNeedsLayout()) { | |
| 130 // The inner content rectangle is calculated during layout, but may need
an update now | |
| 131 // (unless the box has already been scheduled for layout). In order to c
alculate it, we | |
| 132 // may need values from the containing block, though, so make sure that
we're not too | |
| 133 // early. It may be that layout hasn't even taken place once yet. | |
| 134 updateInnerContentRect(); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void RenderImage::updateIntrinsicSizeIfNeeded(const LayoutSize& newSize) | |
| 139 { | |
| 140 if (m_imageResource->errorOccurred() || !m_imageResource->hasImage()) | |
| 141 return; | |
| 142 setIntrinsicSize(newSize); | |
| 143 } | |
| 144 | |
| 145 void RenderImage::updateInnerContentRect() | |
| 146 { | |
| 147 // Propagate container size to the image resource. | |
| 148 LayoutRect containerRect = replacedContentRect(); | |
| 149 IntSize containerSize(containerRect.width(), containerRect.height()); | |
| 150 if (!containerSize.isEmpty()) | |
| 151 m_imageResource->setContainerSizeForRenderer(containerSize); | |
| 152 } | |
| 153 | |
| 154 void RenderImage::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOf
fset) | |
| 155 { | |
| 156 GraphicsContext* context = paintInfo.context; | |
| 157 | |
| 158 if (m_imageResource->hasImage() && contentWidth() > 0 && contentHeight() > 0
) { | |
| 159 LayoutRect contentRect = contentBoxRect(); | |
| 160 contentRect.moveBy(paintOffset); | |
| 161 LayoutRect paintRect = replacedContentRect(); | |
| 162 paintRect.moveBy(paintOffset); | |
| 163 bool clip = !contentRect.contains(paintRect); | |
| 164 if (clip) { | |
| 165 context->save(); | |
| 166 context->clip(contentRect); | |
| 167 } | |
| 168 | |
| 169 paintIntoRect(context, paintRect); | |
| 170 | |
| 171 if (clip) | |
| 172 context->restore(); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void RenderImage::paintIntoRect(GraphicsContext* context, const LayoutRect& rect
) | |
| 177 { | |
| 178 IntRect alignedRect = pixelSnappedIntRect(rect); | |
| 179 if (!m_imageResource->hasImage() || m_imageResource->errorOccurred() || alig
nedRect.width() <= 0 || alignedRect.height() <= 0) | |
| 180 return; | |
| 181 | |
| 182 RefPtr<Image> img = m_imageResource->image(alignedRect.width(), alignedRect.
height()); | |
| 183 if (!img || img->isNull()) | |
| 184 return; | |
| 185 | |
| 186 Image* image = img.get(); | |
| 187 | |
| 188 InterpolationQuality previousInterpolationQuality = context->imageInterpolat
ionQuality(); | |
| 189 context->setImageInterpolationQuality(InterpolationLow); | |
| 190 context->drawImage(image, alignedRect, CompositeSourceOver, shouldRespectIma
geOrientation()); | |
| 191 context->setImageInterpolationQuality(previousInterpolationQuality); | |
| 192 } | |
| 193 | |
| 194 LayoutUnit RenderImage::minimumReplacedHeight() const | |
| 195 { | |
| 196 return m_imageResource->errorOccurred() ? intrinsicSize().height() : LayoutU
nit(); | |
| 197 } | |
| 198 | |
| 199 bool RenderImage::nodeAtPoint(const HitTestRequest& request, HitTestResult& resu
lt, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOf
fset) | |
| 200 { | |
| 201 HitTestResult tempResult(result.hitTestLocation()); | |
| 202 bool inside = RenderReplaced::nodeAtPoint(request, tempResult, locationInCon
tainer, accumulatedOffset); | |
| 203 | |
| 204 if (!inside && result.isRectBasedTest()) | |
| 205 result.append(tempResult); | |
| 206 if (inside) | |
| 207 result = tempResult; | |
| 208 return inside; | |
| 209 } | |
| 210 | |
| 211 void RenderImage::layout() | |
| 212 { | |
| 213 LayoutRect oldContentRect = replacedContentRect(); | |
| 214 RenderReplaced::layout(); | |
| 215 if (replacedContentRect() != oldContentRect) { | |
| 216 updateInnerContentRect(); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 void RenderImage::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, dou
ble& intrinsicRatio) const | |
| 221 { | |
| 222 RenderReplaced::computeIntrinsicRatioInformation(intrinsicSize, intrinsicRat
io); | |
| 223 | |
| 224 // Our intrinsicSize is empty if we're rendering generated images with relat
ive width/height. Figure out the right intrinsic size to use. | |
| 225 if (intrinsicSize.isEmpty() && (m_imageResource->imageHasRelativeWidth() ||
m_imageResource->imageHasRelativeHeight())) { | |
| 226 RenderObject* containingBlock = isOutOfFlowPositioned() ? container() :
this->containingBlock(); | |
| 227 if (containingBlock->isBox()) { | |
| 228 RenderBox* box = toRenderBox(containingBlock); | |
| 229 intrinsicSize.setWidth(box->availableLogicalWidth().toFloat()); | |
| 230 intrinsicSize.setHeight(box->availableLogicalHeight(IncludeMarginBor
derPadding).toFloat()); | |
| 231 } | |
| 232 } | |
| 233 // Don't compute an intrinsic ratio to preserve historical WebKit behavior i
f we're painting alt text and/or a broken image. | |
| 234 // Video is excluded from this behavior because video elements have a defaul
t aspect ratio that a failed poster image load should not override. | |
| 235 if (m_imageResource && m_imageResource->errorOccurred()) { | |
| 236 intrinsicRatio = 1; | |
| 237 return; | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 bool RenderImage::needsPreferredWidthsRecalculation() const | |
| 242 { | |
| 243 return RenderReplaced::needsPreferredWidthsRecalculation(); | |
| 244 } | |
| 245 | |
| 246 } // namespace blink | |
| OLD | NEW |