| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/paint/ImagePainter.h" | 6 #include "core/paint/ImagePainter.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
| 10 #include "core/editing/FrameSelection.h" | 10 #include "core/editing/FrameSelection.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "platform/geometry/LayoutPoint.h" | 22 #include "platform/geometry/LayoutPoint.h" |
| 23 #include "platform/graphics/Path.h" | 23 #include "platform/graphics/Path.h" |
| 24 | 24 |
| 25 namespace blink { | 25 namespace blink { |
| 26 | 26 |
| 27 void ImagePainter::paint(const PaintInfo& paintInfo, const LayoutPoint& paintOff
set) | 27 void ImagePainter::paint(const PaintInfo& paintInfo, const LayoutPoint& paintOff
set) |
| 28 { | 28 { |
| 29 m_layoutImage.LayoutReplaced::paint(paintInfo, paintOffset); | 29 m_layoutImage.LayoutReplaced::paint(paintInfo, paintOffset); |
| 30 | 30 |
| 31 if (paintInfo.phase == PaintPhaseOutline) | 31 if (paintInfo.phase == PaintPhaseOutline) |
| 32 paintAreaElementFocusRing(paintInfo); | 32 paintAreaElementFocusRing(paintInfo, paintOffset); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void ImagePainter::paintAreaElementFocusRing(const PaintInfo& paintInfo) | 35 void ImagePainter::paintAreaElementFocusRing(const PaintInfo& paintInfo, const L
ayoutPoint& paintOffset) |
| 36 { | 36 { |
| 37 // TODO(wangxianzhu): In other places, we just paint focus ring if outline s
tyle is auto. | 37 // TODO(wangxianzhu): In other places, we just paint focus ring if outline s
tyle is auto. |
| 38 // We should also do that here to keep consistency. | 38 // We should also do that here to keep consistency. |
| 39 Document& document = m_layoutImage.document(); | 39 Document& document = m_layoutImage.document(); |
| 40 | 40 |
| 41 if (paintInfo.isPrinting() || !document.frame()->selection().isFocusedAndAct
ive()) | 41 if (paintInfo.isPrinting() || !document.frame()->selection().isFocusedAndAct
ive()) |
| 42 return; | 42 return; |
| 43 | 43 |
| 44 Element* focusedElement = document.focusedElement(); | 44 Element* focusedElement = document.focusedElement(); |
| 45 if (!isHTMLAreaElement(focusedElement)) | 45 if (!isHTMLAreaElement(focusedElement)) |
| 46 return; | 46 return; |
| 47 | 47 |
| 48 HTMLAreaElement& areaElement = toHTMLAreaElement(*focusedElement); | 48 HTMLAreaElement& areaElement = toHTMLAreaElement(*focusedElement); |
| 49 if (areaElement.imageElement() != m_layoutImage.node()) | 49 if (areaElement.imageElement() != m_layoutImage.node()) |
| 50 return; | 50 return; |
| 51 | 51 |
| 52 // Even if the theme handles focus ring drawing for entire elements, it won'
t do it for | 52 // Even if the theme handles focus ring drawing for entire elements, it won'
t do it for |
| 53 // an area within an image, so we don't call LayoutTheme::supportsFocusRing
here. | 53 // an area within an image, so we don't call LayoutTheme::supportsFocusRing
here. |
| 54 | 54 |
| 55 Path path = areaElement.computePath(&m_layoutImage); | 55 Path path = areaElement.computePath(&m_layoutImage); |
| 56 if (path.isEmpty()) | 56 if (path.isEmpty()) |
| 57 return; | 57 return; |
| 58 | 58 |
| 59 const ComputedStyle& areaElementStyle = *areaElement.ensureComputedStyle(); | 59 const ComputedStyle& areaElementStyle = *areaElement.ensureComputedStyle(); |
| 60 int outlineWidth = areaElementStyle.outlineWidth(); | 60 int outlineWidth = areaElementStyle.outlineWidth(); |
| 61 if (!outlineWidth) | 61 if (!outlineWidth) |
| 62 return; | 62 return; |
| 63 | 63 |
| 64 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(*paintInfo.conte
xt, m_layoutImage, paintInfo.phase)) | 64 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(*paintInfo.conte
xt, m_layoutImage, paintInfo.phase, paintOffset)) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 IntRect focusRect = m_layoutImage.absoluteContentBox(); | 67 IntRect focusRect = m_layoutImage.absoluteContentBox(); |
| 68 LayoutObjectDrawingRecorder drawingRecorder(*paintInfo.context, m_layoutImag
e, paintInfo.phase, focusRect); | 68 LayoutObjectDrawingRecorder drawingRecorder(*paintInfo.context, m_layoutImag
e, paintInfo.phase, focusRect, paintOffset); |
| 69 | 69 |
| 70 // FIXME: Clip path instead of context when Skia pathops is ready. | 70 // FIXME: Clip path instead of context when Skia pathops is ready. |
| 71 // https://crbug.com/251206 | 71 // https://crbug.com/251206 |
| 72 | 72 |
| 73 paintInfo.context->save(); | 73 paintInfo.context->save(); |
| 74 paintInfo.context->clip(focusRect); | 74 paintInfo.context->clip(focusRect); |
| 75 paintInfo.context->drawFocusRing(path, outlineWidth, | 75 paintInfo.context->drawFocusRing(path, outlineWidth, |
| 76 areaElementStyle.outlineOffset(), | 76 areaElementStyle.outlineOffset(), |
| 77 m_layoutImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor)); | 77 m_layoutImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor)); |
| 78 paintInfo.context->restore(); | 78 paintInfo.context->restore(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void ImagePainter::paintReplaced(const PaintInfo& paintInfo, const LayoutPoint&
paintOffset) | 81 void ImagePainter::paintReplaced(const PaintInfo& paintInfo, const LayoutPoint&
paintOffset) |
| 82 { | 82 { |
| 83 LayoutUnit cWidth = m_layoutImage.contentWidth(); | 83 LayoutUnit cWidth = m_layoutImage.contentWidth(); |
| 84 LayoutUnit cHeight = m_layoutImage.contentHeight(); | 84 LayoutUnit cHeight = m_layoutImage.contentHeight(); |
| 85 | 85 |
| 86 GraphicsContext* context = paintInfo.context; | 86 GraphicsContext* context = paintInfo.context; |
| 87 | 87 |
| 88 if (!m_layoutImage.imageResource()->hasImage()) { | 88 if (!m_layoutImage.imageResource()->hasImage()) { |
| 89 if (paintInfo.phase == PaintPhaseSelection) | 89 if (paintInfo.phase == PaintPhaseSelection) |
| 90 return; | 90 return; |
| 91 if (cWidth > 2 && cHeight > 2) { | 91 if (cWidth > 2 && cHeight > 2) { |
| 92 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(*context
, m_layoutImage, paintInfo.phase)) | 92 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(*context
, m_layoutImage, paintInfo.phase, paintOffset)) |
| 93 return; | 93 return; |
| 94 // Draw an outline rect where the image should be. | 94 // Draw an outline rect where the image should be. |
| 95 IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() +
m_layoutImage.borderLeft() + m_layoutImage.paddingLeft(), paintOffset.y() + m_l
ayoutImage.borderTop() + m_layoutImage.paddingTop(), cWidth, cHeight)); | 95 IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() +
m_layoutImage.borderLeft() + m_layoutImage.paddingLeft(), paintOffset.y() + m_l
ayoutImage.borderTop() + m_layoutImage.paddingTop(), cWidth, cHeight)); |
| 96 LayoutObjectDrawingRecorder drawingRecorder(*context, m_layoutImage,
paintInfo.phase, paintRect); | 96 LayoutObjectDrawingRecorder drawingRecorder(*context, m_layoutImage,
paintInfo.phase, paintRect, paintOffset); |
| 97 context->setStrokeStyle(SolidStroke); | 97 context->setStrokeStyle(SolidStroke); |
| 98 context->setStrokeColor(Color::lightGray); | 98 context->setStrokeColor(Color::lightGray); |
| 99 context->setFillColor(Color::transparent); | 99 context->setFillColor(Color::transparent); |
| 100 context->drawRect(paintRect); | 100 context->drawRect(paintRect); |
| 101 } | 101 } |
| 102 } else if (cWidth > 0 && cHeight > 0) { | 102 } else if (cWidth > 0 && cHeight > 0) { |
| 103 LayoutRect contentRect = m_layoutImage.contentBoxRect(); | 103 LayoutRect contentRect = m_layoutImage.contentBoxRect(); |
| 104 contentRect.moveBy(paintOffset); | 104 contentRect.moveBy(paintOffset); |
| 105 LayoutRect paintRect = m_layoutImage.replacedContentRect(); | 105 LayoutRect paintRect = m_layoutImage.replacedContentRect(); |
| 106 paintRect.moveBy(paintOffset); | 106 paintRect.moveBy(paintOffset); |
| 107 | 107 |
| 108 Optional<FloatClipRecorder> clipRecorder; | 108 Optional<FloatClipRecorder> clipRecorder; |
| 109 if (!contentRect.contains(paintRect)) { | 109 if (!contentRect.contains(paintRect)) { |
| 110 // TODO(chrishtr): this should be pixel-snapped. | 110 // TODO(chrishtr): this should be pixel-snapped. |
| 111 // TODO(fmalita): can we get rid of this clip and adjust the image s
rc/dst rect instead? | 111 // TODO(fmalita): can we get rid of this clip and adjust the image s
rc/dst rect instead? |
| 112 clipRecorder.emplace(*context, m_layoutImage, paintInfo.phase, Float
Rect(contentRect)); | 112 clipRecorder.emplace(*context, m_layoutImage, paintInfo.phase, Float
Rect(contentRect)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(*context, m_
layoutImage, paintInfo.phase)) | 115 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(*context, m_
layoutImage, paintInfo.phase, paintOffset)) |
| 116 return; | 116 return; |
| 117 | 117 |
| 118 LayoutObjectDrawingRecorder drawingRecorder(*context, m_layoutImage, pai
ntInfo.phase, contentRect); | 118 LayoutObjectDrawingRecorder drawingRecorder(*context, m_layoutImage, pai
ntInfo.phase, contentRect, paintOffset); |
| 119 paintIntoRect(context, paintRect); | 119 paintIntoRect(context, paintRect); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 void ImagePainter::paintIntoRect(GraphicsContext* context, const LayoutRect& rec
t) | 123 void ImagePainter::paintIntoRect(GraphicsContext* context, const LayoutRect& rec
t) |
| 124 { | 124 { |
| 125 if (!m_layoutImage.imageResource()->hasImage() || m_layoutImage.imageResourc
e()->errorOccurred()) | 125 if (!m_layoutImage.imageResource()->hasImage() || m_layoutImage.imageResourc
e()->errorOccurred()) |
| 126 return; // FIXME: should we just ASSERT these conditions? (audit all cal
lers). | 126 return; // FIXME: should we just ASSERT these conditions? (audit all cal
lers). |
| 127 | 127 |
| 128 IntRect alignedRect = pixelSnappedIntRect(rect); | 128 IntRect alignedRect = pixelSnappedIntRect(rect); |
| 129 if (alignedRect.width() <= 0 || alignedRect.height() <= 0) | 129 if (alignedRect.width() <= 0 || alignedRect.height() <= 0) |
| 130 return; | 130 return; |
| 131 | 131 |
| 132 RefPtr<Image> image = m_layoutImage.imageResource()->image(alignedRect.width
(), alignedRect.height()); | 132 RefPtr<Image> image = m_layoutImage.imageResource()->image(alignedRect.width
(), alignedRect.height()); |
| 133 if (!image || image->isNull()) | 133 if (!image || image->isNull()) |
| 134 return; | 134 return; |
| 135 | 135 |
| 136 // FIXME: why is interpolation quality selection not included in the Instrum
entation reported cost of drawing an image? | 136 // FIXME: why is interpolation quality selection not included in the Instrum
entation reported cost of drawing an image? |
| 137 InterpolationQuality interpolationQuality = BoxPainter::chooseInterpolationQ
uality(m_layoutImage, context, image.get(), image.get(), LayoutSize(alignedRect.
size())); | 137 InterpolationQuality interpolationQuality = BoxPainter::chooseInterpolationQ
uality(m_layoutImage, context, image.get(), image.get(), LayoutSize(alignedRect.
size())); |
| 138 | 138 |
| 139 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "PaintImage", "
data", InspectorPaintImageEvent::data(m_layoutImage)); | 139 TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "PaintImage", "
data", InspectorPaintImageEvent::data(m_layoutImage)); |
| 140 | 140 |
| 141 InterpolationQuality previousInterpolationQuality = context->imageInterpolat
ionQuality(); | 141 InterpolationQuality previousInterpolationQuality = context->imageInterpolat
ionQuality(); |
| 142 context->setImageInterpolationQuality(interpolationQuality); | 142 context->setImageInterpolationQuality(interpolationQuality); |
| 143 context->drawImage(image.get(), alignedRect, SkXfermode::kSrcOver_Mode, m_la
youtImage.shouldRespectImageOrientation()); | 143 context->drawImage(image.get(), alignedRect, SkXfermode::kSrcOver_Mode, m_la
youtImage.shouldRespectImageOrientation()); |
| 144 context->setImageInterpolationQuality(previousInterpolationQuality); | 144 context->setImageInterpolationQuality(previousInterpolationQuality); |
| 145 } | 145 } |
| 146 | 146 |
| 147 } // namespace blink | 147 } // namespace blink |
| OLD | NEW |