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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "web/LinkHighlight.h"
#include "SkMatrix44.h"
#include "core/dom/Node.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/RenderLayerModelObject.h"
#include "core/rendering/RenderObject.h"
#include "core/rendering/RenderView.h"
#include "core/rendering/compositing/CompositedLayerMapping.h"
#include "core/rendering/style/ShadowData.h"
#include "platform/graphics/Color.h"
#include "public/platform/Platform.h"
#include "public/platform/WebAnimationCurve.h"
#include "public/platform/WebCompositorSupport.h"
#include "public/platform/WebFloatAnimationCurve.h"
#include "public/platform/WebFloatPoint.h"
#include "public/platform/WebRect.h"
#include "public/platform/WebSize.h"
#include "public/web/WebKit.h"
#include "web/WebLocalFrameImpl.h"
#include "web/WebViewImpl.h"
#include "wtf/CurrentTime.h"
using namespace WebCore;
namespace blink {
class WebViewImpl;
PassOwnPtr<LinkHighlight> LinkHighlight::create(Node* node, WebViewImpl* owningWebViewImpl)
{
return adoptPtr(new LinkHighlight(node, owningWebViewImpl));
}
LinkHighlight::LinkHighlight(Node* node, WebViewImpl* owningWebViewImpl)
: m_node(node)
, m_owningWebViewImpl(owningWebViewImpl)
, m_currentGraphicsLayer(0)
, m_geometryNeedsUpdate(false)
, m_isAnimating(false)
, m_startTime(monotonicallyIncreasingTime())
{
ASSERT(m_node);
ASSERT(owningWebViewImpl);
WebCompositorSupport* compositorSupport = Platform::current()->compositorSupport();
m_contentLayer = adoptPtr(compositorSupport->createContentLayer(this));
m_clipLayer = adoptPtr(compositorSupport->createLayer());
m_clipLayer->setTransformOrigin(WebFloatPoint3D());
m_clipLayer->addChild(m_contentLayer->layer());
m_contentLayer->layer()->setAnimationDelegate(this);
m_contentLayer->layer()->setDrawsContent(true);
m_contentLayer->layer()->setOpacity(1);
m_geometryNeedsUpdate = true;
updateGeometry();
}
LinkHighlight::~LinkHighlight()
{
clearGraphicsLayerLinkHighlightPointer();
releaseResources();
}
WebContentLayer* LinkHighlight::contentLayer()
{
return m_contentLayer.get();
}
WebLayer* LinkHighlight::clipLayer()
{
return m_clipLayer.get();
}
void LinkHighlight::releaseResources()
{
m_node.clear();
}
RenderLayer* LinkHighlight::computeEnclosingCompositingLayer()
{
if (!m_node || !m_node->renderer())
return 0;
// Find the nearest enclosing composited layer and attach to it. We may need to cross frame boundaries
// to find a suitable layer.
RenderObject* renderer = m_node->renderer();
RenderLayer* renderLayer;
do {
renderLayer = renderer->enclosingLayer()->enclosingCompositingLayerForRepaint();
if (!renderLayer) {
renderer = renderer->frame()->ownerRenderer();
if (!renderer)
return 0;
}
} while (!renderLayer);
CompositedLayerMappingPtr compositedLayerMapping = renderLayer->compositingState() == PaintsIntoGroupedBacking ? renderLayer->groupedMapping() : renderLayer->compositedLayerMapping();
GraphicsLayer* newGraphicsLayer = renderLayer->compositingState() == PaintsIntoGroupedBacking ? compositedLayerMapping->squashingLayer() : compositedLayerMapping->mainGraphicsLayer();
m_clipLayer->setTransform(SkMatrix44(SkMatrix44::kIdentity_Constructor));
if (!newGraphicsLayer->drawsContent()) {
if (renderLayer->scrollableArea() && renderLayer->scrollableArea()->usesCompositedScrolling()) {
ASSERT(renderLayer->hasCompositedLayerMapping() && renderLayer->compositedLayerMapping()->scrollingContentsLayer());
newGraphicsLayer = compositedLayerMapping->scrollingContentsLayer();
}
}
if (m_currentGraphicsLayer != newGraphicsLayer) {
if (m_currentGraphicsLayer)
clearGraphicsLayerLinkHighlightPointer();
m_currentGraphicsLayer = newGraphicsLayer;
m_currentGraphicsLayer->addLinkHighlight(this);
}
return renderLayer;
}
static void convertTargetSpaceQuadToCompositedLayer(const FloatQuad& targetSpaceQuad, RenderObject* targetRenderer, RenderObject* compositedRenderer, FloatQuad& compositedSpaceQuad)
{
ASSERT(targetRenderer);
ASSERT(compositedRenderer);
for (unsigned i = 0; i < 4; ++i) {
IntPoint point;
switch (i) {
case 0: point = roundedIntPoint(targetSpaceQuad.p1()); break;
case 1: point = roundedIntPoint(targetSpaceQuad.p2()); break;
case 2: point = roundedIntPoint(targetSpaceQuad.p3()); break;
case 3: point = roundedIntPoint(targetSpaceQuad.p4()); break;
}
point = targetRenderer->frame()->view()->contentsToWindow(point);
point = compositedRenderer->frame()->view()->windowToContents(point);
FloatPoint floatPoint = compositedRenderer->absoluteToLocal(point, UseTransforms);
switch (i) {
case 0: compositedSpaceQuad.setP1(floatPoint); break;
case 1: compositedSpaceQuad.setP2(floatPoint); break;
case 2: compositedSpaceQuad.setP3(floatPoint); break;
case 3: compositedSpaceQuad.setP4(floatPoint); break;
}
}
}
static void addQuadToPath(const FloatQuad& quad, Path& path)
{
// FIXME: Make this create rounded quad-paths, just like the axis-aligned case.
path.moveTo(quad.p1());
path.addLineTo(quad.p2());
path.addLineTo(quad.p3());
path.addLineTo(quad.p4());
path.closeSubpath();
}
void LinkHighlight::computeQuads(Node* node, Vector<FloatQuad>& outQuads) const
{
if (!node || !node->renderer())
return;
RenderObject* renderer = node->renderer();
// For inline elements, absoluteQuads will return a line box based on the line-height
// and font metrics, which is technically incorrect as replaced elements like images
// should use their intristic height and expand the linebox as needed. To get an
// appropriately sized highlight we descend into the children and have them add their
// boxes.
if (renderer->isRenderInline()) {
for (Node* child = node->firstChild(); child; child = child->nextSibling())
computeQuads(child, outQuads);
} else {
renderer->absoluteQuads(outQuads);
}
}
bool LinkHighlight::computeHighlightLayerPathAndPosition(RenderLayer* compositingLayer)
{
if (!m_node || !m_node->renderer() || !m_currentGraphicsLayer)
return false;
ASSERT(compositingLayer);
// Get quads for node in absolute coordinates.
Vector<FloatQuad> quads;
computeQuads(m_node.get(), quads);
ASSERT(quads.size());
// Adjust for offset between target graphics layer and the node's renderer.
FloatPoint positionAdjust = IntPoint(m_currentGraphicsLayer->offsetFromRenderer());
Path newPath;
for (size_t quadIndex = 0; quadIndex < quads.size(); ++quadIndex) {
FloatQuad absoluteQuad = quads[quadIndex];
absoluteQuad.move(-positionAdjust.x(), -positionAdjust.y());
// Transform node quads in target absolute coords to local coordinates in the compositor layer.
FloatQuad transformedQuad;
convertTargetSpaceQuadToCompositedLayer(absoluteQuad, m_node->renderer(), compositingLayer->renderer(), transformedQuad);
// FIXME: for now, we'll only use rounded paths if we have a single node quad. The reason for this is that
// we may sometimes get a chain of adjacent boxes (e.g. for text nodes) which end up looking like sausage
// links: these should ideally be merged into a single rect before creating the path, but that's
// another CL.
if (quads.size() == 1 && transformedQuad.isRectilinear()) {
FloatSize rectRoundingRadii(3, 3);
newPath.addRoundedRect(transformedQuad.boundingBox(), rectRoundingRadii);
} else
addQuadToPath(transformedQuad, newPath);
}
FloatRect boundingRect = newPath.boundingRect();
newPath.translate(-toFloatSize(boundingRect.location()));
bool pathHasChanged = !(newPath == m_path);
if (pathHasChanged) {
m_path = newPath;
m_contentLayer->layer()->setBounds(enclosingIntRect(boundingRect).size());
}
m_contentLayer->layer()->setPosition(boundingRect.location());
return pathHasChanged;
}
void LinkHighlight::paintContents(WebCanvas* canvas, const WebRect& webClipRect, bool, WebFloatRect&,
WebContentLayerClient::GraphicsContextStatus contextStatus)
{
if (!m_node || !m_node->renderer())
return;
GraphicsContext gc(canvas,
contextStatus == WebContentLayerClient::GraphicsContextEnabled ? GraphicsContext::NothingDisabled : GraphicsContext::FullyDisabled);
IntRect clipRect(IntPoint(webClipRect.x, webClipRect.y), IntSize(webClipRect.width, webClipRect.height));
gc.clip(clipRect);
gc.setFillColor(m_node->renderer()->style()->tapHighlightColor());
gc.fillPath(m_path);
}
void LinkHighlight::startHighlightAnimationIfNeeded()
{
if (m_isAnimating)
return;
m_isAnimating = true;
const float startOpacity = 1;
// FIXME: Should duration be configurable?
const float fadeDuration = 0.1f;
const float minPreFadeDuration = 0.1f;
m_contentLayer->layer()->setOpacity(startOpacity);
WebCompositorSupport* compositorSupport = Platform::current()->compositorSupport();
OwnPtr<WebFloatAnimationCurve> curve = adoptPtr(compositorSupport->createFloatAnimationCurve());
curve->add(WebFloatKeyframe(0, startOpacity));
// Make sure we have displayed for at least minPreFadeDuration before starting to fade out.
float extraDurationRequired = std::max(0.f, minPreFadeDuration - static_cast<float>(monotonicallyIncreasingTime() - m_startTime));
if (extraDurationRequired)
curve->add(WebFloatKeyframe(extraDurationRequired, startOpacity));
// For layout tests we don't fade out.
curve->add(WebFloatKeyframe(fadeDuration + extraDurationRequired, blink::layoutTestMode() ? startOpacity : 0));
OwnPtr<WebAnimation> animation = adoptPtr(compositorSupport->createAnimation(*curve, WebAnimation::TargetPropertyOpacity));
m_contentLayer->layer()->setDrawsContent(true);
m_contentLayer->layer()->addAnimation(animation.leakPtr());
invalidate();
m_owningWebViewImpl->scheduleAnimation();
}
void LinkHighlight::clearGraphicsLayerLinkHighlightPointer()
{
if (m_currentGraphicsLayer) {
m_currentGraphicsLayer->removeLinkHighlight(this);
m_currentGraphicsLayer = 0;
}
}
void LinkHighlight::notifyAnimationStarted(double, blink::WebAnimation::TargetProperty)
{
}
void LinkHighlight::notifyAnimationFinished(double, blink::WebAnimation::TargetProperty)
{
// Since WebViewImpl may hang on to us for a while, make sure we
// release resources as soon as possible.
clearGraphicsLayerLinkHighlightPointer();
releaseResources();
}
void LinkHighlight::updateGeometry()
{
// To avoid unnecessary updates (e.g. other entities have requested animations from our WebViewImpl),
// only proceed if we actually requested an update.
if (!m_geometryNeedsUpdate)
return;
m_geometryNeedsUpdate = false;
RenderLayer* compositingLayer = computeEnclosingCompositingLayer();
if (compositingLayer && computeHighlightLayerPathAndPosition(compositingLayer)) {
// We only need to invalidate the layer if the highlight size has changed, otherwise
// we can just re-position the layer without needing to repaint.
m_contentLayer->layer()->invalidate();
if (m_currentGraphicsLayer)
m_currentGraphicsLayer->addRepaintRect(FloatRect(layer()->position().x, layer()->position().y, layer()->bounds().width, layer()->bounds().height));
} else if (!m_node || !m_node->renderer()) {
clearGraphicsLayerLinkHighlightPointer();
releaseResources();
}
}
void LinkHighlight::clearCurrentGraphicsLayer()
{
m_currentGraphicsLayer = 0;
m_geometryNeedsUpdate = true;
}
void LinkHighlight::invalidate()
{
// Make sure we update geometry on the next callback from WebViewImpl::layout().
m_geometryNeedsUpdate = true;
}
WebLayer* LinkHighlight::layer()
{
return clipLayer();
}
} // namespace WeKit
|