2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "web/LinkHighlight.h"
30 #include "SkMatrix44.h"
31 #include "core/dom/Node.h"
32 #include "core/frame/FrameView.h"
33 #include "core/frame/LocalFrame.h"
34 #include "core/rendering/RenderLayer.h"
35 #include "core/rendering/RenderLayerModelObject.h"
36 #include "core/rendering/RenderObject.h"
37 #include "core/rendering/RenderPart.h"
38 #include "core/rendering/RenderView.h"
39 #include "core/rendering/compositing/CompositedLayerMapping.h"
40 #include "core/rendering/style/ShadowData.h"
41 #include "platform/graphics/Color.h"
42 #include "public/platform/Platform.h"
43 #include "public/platform/WebCompositorAnimationCurve.h"
44 #include "public/platform/WebCompositorSupport.h"
45 #include "public/platform/WebFloatAnimationCurve.h"
46 #include "public/platform/WebFloatPoint.h"
47 #include "public/platform/WebRect.h"
48 #include "public/platform/WebSize.h"
49 #include "public/web/WebKit.h"
50 #include "web/WebLocalFrameImpl.h"
51 #include "web/WebSettingsImpl.h"
52 #include "web/WebViewImpl.h"
53 #include "wtf/CurrentTime.h"
59 PassOwnPtr<LinkHighlight> LinkHighlight::create(Node* node, WebViewImpl* owningWebViewImpl)
61 return adoptPtr(new LinkHighlight(node, owningWebViewImpl));
64 LinkHighlight::LinkHighlight(Node* node, WebViewImpl* owningWebViewImpl)
66 , m_owningWebViewImpl(owningWebViewImpl)
67 , m_currentGraphicsLayer(0)
68 , m_geometryNeedsUpdate(false)
69 , m_isAnimating(false)
70 , m_startTime(monotonicallyIncreasingTime())
73 ASSERT(owningWebViewImpl);
74 WebCompositorSupport* compositorSupport = Platform::current()->compositorSupport();
75 m_contentLayer = adoptPtr(compositorSupport->createContentLayer(this));
76 m_clipLayer = adoptPtr(compositorSupport->createLayer());
77 m_clipLayer->setTransformOrigin(WebFloatPoint3D());
78 m_clipLayer->addChild(m_contentLayer->layer());
79 m_contentLayer->layer()->setAnimationDelegate(this);
80 m_contentLayer->layer()->setDrawsContent(true);
81 m_contentLayer->layer()->setOpacity(1);
82 m_geometryNeedsUpdate = true;
86 LinkHighlight::~LinkHighlight()
88 clearGraphicsLayerLinkHighlightPointer();
92 WebContentLayer* LinkHighlight::contentLayer()
94 return m_contentLayer.get();
97 WebLayer* LinkHighlight::clipLayer()
99 return m_clipLayer.get();
102 void LinkHighlight::releaseResources()
107 RenderLayer* LinkHighlight::computeEnclosingCompositingLayer()
109 if (!m_node || !m_node->renderer())
112 // Find the nearest enclosing composited layer and attach to it. We may need to cross frame boundaries
113 // to find a suitable layer.
114 RenderObject* renderer = m_node->renderer();
115 RenderLayer* renderLayer;
117 renderLayer = renderer->enclosingLayer()->enclosingLayerForPaintInvalidation();
119 renderer = renderer->frame()->ownerRenderer();
123 } while (!renderLayer);
125 ASSERT(renderLayer->compositingState() != NotComposited);
127 GraphicsLayer* newGraphicsLayer = renderLayer->graphicsLayerBacking();
128 if (!newGraphicsLayer->drawsContent()) {
129 newGraphicsLayer = renderLayer->graphicsLayerBackingForScrolling();
132 m_clipLayer->setTransform(SkMatrix44(SkMatrix44::kIdentity_Constructor));
134 if (m_currentGraphicsLayer != newGraphicsLayer) {
135 if (m_currentGraphicsLayer)
136 clearGraphicsLayerLinkHighlightPointer();
138 m_currentGraphicsLayer = newGraphicsLayer;
139 m_currentGraphicsLayer->addLinkHighlight(this);
145 static void convertTargetSpaceQuadToCompositedLayer(const FloatQuad& targetSpaceQuad, RenderObject* targetRenderer, RenderObject* compositedRenderer, FloatQuad& compositedSpaceQuad)
147 ASSERT(targetRenderer);
148 ASSERT(compositedRenderer);
150 for (unsigned i = 0; i < 4; ++i) {
153 case 0: point = roundedIntPoint(targetSpaceQuad.p1()); break;
154 case 1: point = roundedIntPoint(targetSpaceQuad.p2()); break;
155 case 2: point = roundedIntPoint(targetSpaceQuad.p3()); break;
156 case 3: point = roundedIntPoint(targetSpaceQuad.p4()); break;
159 point = targetRenderer->frame()->view()->contentsToWindow(point);
160 point = compositedRenderer->frame()->view()->windowToContents(point);
161 FloatPoint floatPoint = compositedRenderer->absoluteToLocal(point, UseTransforms);
164 case 0: compositedSpaceQuad.setP1(floatPoint); break;
165 case 1: compositedSpaceQuad.setP2(floatPoint); break;
166 case 2: compositedSpaceQuad.setP3(floatPoint); break;
167 case 3: compositedSpaceQuad.setP4(floatPoint); break;
172 static void addQuadToPath(const FloatQuad& quad, Path& path)
174 // FIXME: Make this create rounded quad-paths, just like the axis-aligned case.
175 path.moveTo(quad.p1());
176 path.addLineTo(quad.p2());
177 path.addLineTo(quad.p3());
178 path.addLineTo(quad.p4());
182 void LinkHighlight::computeQuads(RenderObject& renderer, Vector<FloatQuad>& outQuads) const
184 // For inline elements, absoluteQuads will return a line box based on the line-height
185 // and font metrics, which is technically incorrect as replaced elements like images
186 // should use their intristic height and expand the linebox as needed. To get an
187 // appropriately sized highlight we descend into the children and have them add their
189 if (renderer.isRenderInline()) {
190 for (RenderObject* child = renderer.slowFirstChild(); child; child = child->nextSibling())
191 computeQuads(*child, outQuads);
193 renderer.absoluteQuads(outQuads);
197 bool LinkHighlight::computeHighlightLayerPathAndPosition(RenderLayer* compositingLayer)
199 if (!m_node || !m_node->renderer() || !m_currentGraphicsLayer)
202 ASSERT(compositingLayer);
204 // Get quads for node in absolute coordinates.
205 Vector<FloatQuad> quads;
206 computeQuads(*m_node->renderer(), quads);
207 ASSERT(quads.size());
209 // Adjust for offset between target graphics layer and the node's renderer.
210 FloatPoint positionAdjust = IntPoint(m_currentGraphicsLayer->offsetFromRenderer());
213 for (size_t quadIndex = 0; quadIndex < quads.size(); ++quadIndex) {
214 FloatQuad absoluteQuad = quads[quadIndex];
215 absoluteQuad.move(-positionAdjust.x(), -positionAdjust.y());
217 // Transform node quads in target absolute coords to local coordinates in the compositor layer.
218 FloatQuad transformedQuad;
219 convertTargetSpaceQuadToCompositedLayer(absoluteQuad, m_node->renderer(), compositingLayer->renderer(), transformedQuad);
221 // FIXME: for now, we'll only use rounded paths if we have a single node quad. The reason for this is that
222 // we may sometimes get a chain of adjacent boxes (e.g. for text nodes) which end up looking like sausage
223 // links: these should ideally be merged into a single rect before creating the path, but that's
225 if (quads.size() == 1 && transformedQuad.isRectilinear()
226 && !m_owningWebViewImpl->settingsImpl()->mockGestureTapHighlightsEnabled()) {
227 FloatSize rectRoundingRadii(3, 3);
228 newPath.addRoundedRect(transformedQuad.boundingBox(), rectRoundingRadii);
230 addQuadToPath(transformedQuad, newPath);
233 FloatRect boundingRect = newPath.boundingRect();
234 newPath.translate(-toFloatSize(boundingRect.location()));
236 bool pathHasChanged = !(newPath == m_path);
237 if (pathHasChanged) {
239 m_contentLayer->layer()->setBounds(enclosingIntRect(boundingRect).size());
242 m_contentLayer->layer()->setPosition(boundingRect.location());
244 return pathHasChanged;
247 void LinkHighlight::paintContents(WebCanvas* canvas, const WebRect& webClipRect, bool, WebContentLayerClient::GraphicsContextStatus contextStatus)
249 if (!m_node || !m_node->renderer())
252 GraphicsContext gc(canvas,
253 contextStatus == WebContentLayerClient::GraphicsContextEnabled ? GraphicsContext::NothingDisabled : GraphicsContext::FullyDisabled);
254 IntRect clipRect(IntPoint(webClipRect.x, webClipRect.y), IntSize(webClipRect.width, webClipRect.height));
256 gc.setFillColor(m_node->renderer()->style()->tapHighlightColor());
260 void LinkHighlight::startHighlightAnimationIfNeeded()
265 m_isAnimating = true;
266 const float startOpacity = 1;
267 // FIXME: Should duration be configurable?
268 const float fadeDuration = 0.1f;
269 const float minPreFadeDuration = 0.1f;
271 m_contentLayer->layer()->setOpacity(startOpacity);
273 WebCompositorSupport* compositorSupport = Platform::current()->compositorSupport();
275 OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(compositorSupport->createFloatAnimationCurve());
277 curve->add(WebFloatKeyframe(0, startOpacity));
278 // Make sure we have displayed for at least minPreFadeDuration before starting to fade out.
279 float extraDurationRequired = std::max(0.f, minPreFadeDuration - static_cast<float>(monotonicallyIncreasingTime() - m_startTime));
280 if (extraDurationRequired)
281 curve->add(WebFloatKeyframe(extraDurationRequired, startOpacity));
282 // For layout tests we don't fade out.
283 curve->add(WebFloatKeyframe(fadeDuration + extraDurationRequired, layoutTestMode() ? startOpacity : 0));
285 OwnPtr<WebCompositorAnimation> animation = adoptPtr(compositorSupport->createAnimation(*curve, WebCompositorAnimation::TargetPropertyOpacity));
287 m_contentLayer->layer()->setDrawsContent(true);
288 m_contentLayer->layer()->addAnimation(animation.leakPtr());
291 m_owningWebViewImpl->scheduleAnimation();
294 void LinkHighlight::clearGraphicsLayerLinkHighlightPointer()
296 if (m_currentGraphicsLayer) {
297 m_currentGraphicsLayer->removeLinkHighlight(this);
298 m_currentGraphicsLayer = 0;
302 void LinkHighlight::notifyAnimationStarted(double, WebCompositorAnimation::TargetProperty)
306 void LinkHighlight::notifyAnimationFinished(double, WebCompositorAnimation::TargetProperty)
308 // Since WebViewImpl may hang on to us for a while, make sure we
309 // release resources as soon as possible.
310 clearGraphicsLayerLinkHighlightPointer();
314 void LinkHighlight::updateGeometry()
316 // To avoid unnecessary updates (e.g. other entities have requested animations from our WebViewImpl),
317 // only proceed if we actually requested an update.
318 if (!m_geometryNeedsUpdate)
321 m_geometryNeedsUpdate = false;
323 RenderLayer* compositingLayer = computeEnclosingCompositingLayer();
324 if (compositingLayer && computeHighlightLayerPathAndPosition(compositingLayer)) {
325 // We only need to invalidate the layer if the highlight size has changed, otherwise
326 // we can just re-position the layer without needing to repaint.
327 m_contentLayer->layer()->invalidate();
329 if (m_currentGraphicsLayer)
330 m_currentGraphicsLayer->addRepaintRect(FloatRect(layer()->position().x, layer()->position().y, layer()->bounds().width, layer()->bounds().height));
331 } else if (!m_node || !m_node->renderer()) {
332 clearGraphicsLayerLinkHighlightPointer();
337 void LinkHighlight::clearCurrentGraphicsLayer()
339 m_currentGraphicsLayer = 0;
340 m_geometryNeedsUpdate = true;
343 void LinkHighlight::invalidate()
345 // Make sure we update geometry on the next callback from WebViewImpl::layout().
346 m_geometryNeedsUpdate = true;
349 WebLayer* LinkHighlight::layer()