1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "core/paint/ImagePainter.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/editing/FrameSelection.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLAreaElement.h"
#include "core/html/HTMLImageElement.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/page/Page.h"
#include "core/paint/BoxPainter.h"
#include "core/paint/RenderDrawingRecorder.h"
#include "core/rendering/PaintInfo.h"
#include "core/rendering/RenderImage.h"
#include "core/rendering/RenderReplaced.h"
#include "core/rendering/TextRunConstructor.h"
#include "platform/geometry/LayoutPoint.h"
#include "platform/graphics/GraphicsContextStateSaver.h"
#include "platform/graphics/Path.h"
namespace blink {
void ImagePainter::paint(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
m_renderImage.RenderReplaced::paint(paintInfo, paintOffset);
if (paintInfo.phase == PaintPhaseOutline)
paintAreaElementFocusRing(paintInfo);
}
void ImagePainter::paintAreaElementFocusRing(const PaintInfo& paintInfo)
{
Document& document = m_renderImage.document();
if (document.printing() || !document.frame()->selection().isFocusedAndActive())
return;
Element* focusedElement = document.focusedElement();
if (!isHTMLAreaElement(focusedElement))
return;
HTMLAreaElement& areaElement = toHTMLAreaElement(*focusedElement);
if (areaElement.imageElement() != m_renderImage.node())
return;
// Even if the theme handles focus ring drawing for entire elements, it won't do it for
// an area within an image, so we don't call RenderTheme::supportsFocusRing here.
Path path = areaElement.computePath(&m_renderImage);
if (path.isEmpty())
return;
RenderStyle* areaElementStyle = areaElement.computedStyle();
unsigned short outlineWidth = areaElementStyle->outlineWidth();
if (!outlineWidth)
return;
// FIXME: Clip path instead of context when Skia pathops is ready.
// https://crbug.com/251206
GraphicsContextStateSaver savedContext(*paintInfo.context);
IntRect focusRect = m_renderImage.absoluteContentBox();
RenderDrawingRecorder recorder(paintInfo.context, m_renderImage, paintInfo.phase, focusRect);
if (recorder.canUseCachedDrawing())
return;
paintInfo.context->clip(focusRect);
paintInfo.context->drawFocusRing(path, outlineWidth,
areaElementStyle->outlineOffset(),
m_renderImage.resolveColor(areaElementStyle, CSSPropertyOutlineColor));
}
void ImagePainter::paintReplaced(const PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
LayoutUnit cWidth = m_renderImage.contentWidth();
LayoutUnit cHeight = m_renderImage.contentHeight();
GraphicsContext* context = paintInfo.context;
if (!m_renderImage.imageResource()->hasImage()) {
if (paintInfo.phase == PaintPhaseSelection)
return;
if (cWidth > 2 && cHeight > 2) {
// Draw an outline rect where the image should be.
IntRect paintRect = pixelSnappedIntRect(LayoutRect(paintOffset.x() + m_renderImage.borderLeft() + m_renderImage.paddingLeft(), paintOffset.y() + m_renderImage.borderTop() + m_renderImage.paddingTop(), cWidth, cHeight));
context->setStrokeStyle(SolidStroke);
context->setStrokeColor(Color::lightGray);
context->setFillColor(Color::transparent);
context->drawRect(paintRect);
}
} else if (cWidth > 0 && cHeight > 0) {
LayoutRect contentRect = m_renderImage.contentBoxRect();
contentRect.moveBy(paintOffset);
LayoutRect paintRect = m_renderImage.replacedContentRect();
paintRect.moveBy(paintOffset);
bool clip = !contentRect.contains(paintRect);
if (clip) {
context->save();
context->clip(contentRect);
}
paintIntoRect(context, paintRect);
if (clip)
context->restore();
}
}
void ImagePainter::paintIntoRect(GraphicsContext* context, const LayoutRect& rect)
{
if (!m_renderImage.imageResource()->hasImage() || m_renderImage.imageResource()->errorOccurred())
return; // FIXME: should we just ASSERT these conditions? (audit all callers).
IntRect alignedRect = pixelSnappedIntRect(rect);
if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
return;
RefPtr<Image> image = m_renderImage.imageResource()->image(alignedRect.width(), alignedRect.height());
if (!image || image->isNull())
return;
// FIXME: why is interpolation quality selection not included in the Instrumentation reported cost of drawing an image?
InterpolationQuality interpolationQuality = BoxPainter::chooseInterpolationQuality(m_renderImage, context, image.get(), image.get(), LayoutSize(alignedRect.size()));
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "PaintImage", "data", InspectorPaintImageEvent::data(m_renderImage));
// FIXME: crbug.com/361045 remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
InspectorInstrumentation::willPaintImage(&m_renderImage);
InterpolationQuality previousInterpolationQuality = context->imageInterpolationQuality();
context->setImageInterpolationQuality(interpolationQuality);
context->drawImage(image.get(), alignedRect, CompositeSourceOver, m_renderImage.shouldRespectImageOrientation());
context->setImageInterpolationQuality(previousInterpolationQuality);
InspectorInstrumentation::didPaintImage(&m_renderImage);
}
} // namespace blink
|