InRegionScroller.cpp 15.4 KB
Newer Older
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
/*
 * Copyright (C) 2011, 2012 Research In Motion Limited. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include "InRegionScroller.h"

#include "BackingStoreClient.h"
#include "Frame.h"
#include "HTMLFrameOwnerElement.h"
#include "HitTestResult.h"
#include "InRegionScrollableArea.h"
27 28
#include "InRegionScroller_p.h"
#include "LayerCompositingThread.h"
29
#include "LayerWebKitThread.h"
30
#include "Page.h"
31 32
#include "RenderBox.h"
#include "RenderLayer.h"
33
#include "RenderLayerBacking.h"
34
#include "RenderLayerCompositor.h"
35 36
#include "RenderObject.h"
#include "RenderView.h"
37
#include "SelectionHandler.h"
38 39 40 41 42 43 44
#include "WebPage_p.h"

using namespace WebCore;

namespace BlackBerry {
namespace WebKit {

45 46 47
static bool canScrollInnerFrame(Frame*);
static RenderLayer* parentLayer(RenderLayer*);
static bool isNonRenderViewFixedPositionedContainer(RenderLayer*);
48

49 50
InRegionScroller::InRegionScroller(WebPagePrivate* webPagePrivate)
    : d(new InRegionScrollerPrivate(webPagePrivate))
51
{
52
    ASSERT(webPagePrivate);
53 54
}

55
InRegionScroller::~InRegionScroller()
56
{
57
    delete d;
58 59
}

60
bool InRegionScroller::setScrollPositionCompositingThread(unsigned camouflagedLayer, const Platform::IntPoint& scrollPosition)
61
{
62
    ASSERT(Platform::userInterfaceThreadMessageClient()->isCurrentThread());
63 64 65 66 67

    // FIXME: Negative values won't work with map{To,From}Transform methods.
    return d->setScrollPositionCompositingThread(camouflagedLayer, d->m_webPage->mapFromTransformed(scrollPosition));
}

68 69
bool InRegionScroller::setScrollPositionWebKitThread(unsigned camouflagedLayer, const Platform::IntPoint& scrollPosition,
    bool supportsAcceleratedScrolling, Platform::ScrollViewBase::ScrollTarget scrollTarget)
70 71 72 73
{
    ASSERT(Platform::webKitThreadMessageClient()->isCurrentThread());

    // FIXME: Negative values won't work with map{To,From}Transform methods.
74
    return d->setScrollPositionWebKitThread(camouflagedLayer, d->m_webPage->mapFromTransformed(scrollPosition), supportsAcceleratedScrolling, scrollTarget);
75 76
}

77
InRegionScrollerPrivate::InRegionScrollerPrivate(WebPagePrivate* webPagePrivate)
78
    : m_webPage(webPagePrivate)
79
    , m_needsActiveScrollableAreaCalculation(false)
80 81 82
{
}

83
void InRegionScrollerPrivate::reset()
84
{
85
    m_needsActiveScrollableAreaCalculation = false;
86 87 88
    for (size_t i = 0; i < m_activeInRegionScrollableAreas.size(); ++i)
        delete m_activeInRegionScrollableAreas[i];
    m_activeInRegionScrollableAreas.clear();
89 90
}

91
bool InRegionScrollerPrivate::isActive() const
92
{
93
    return m_activeInRegionScrollableAreas.size() > 0;
94 95
}

96
void InRegionScrollerPrivate::clearDocumentData(const Document* documentGoingAway)
97
{
98 99 100 101 102 103 104 105 106 107
    if (m_needsActiveScrollableAreaCalculation) {
        reset();
        return;
    }

    InRegionScrollableArea* scrollableArea = static_cast<InRegionScrollableArea*>(m_activeInRegionScrollableAreas[0]);
    ASSERT(scrollableArea);
    Node* node = scrollableArea->layer()->enclosingElement();
    if (node && node->document() == documentGoingAway)
        reset();
108 109
}

110
bool InRegionScrollerPrivate::setScrollPositionCompositingThread(unsigned camouflagedLayer, const WebCore::IntPoint& scrollPosition)
111
{
112
    LayerCompositingThread* scrollLayer = reinterpret_cast<LayerWebKitThread*>(camouflagedLayer)->layerCompositingThread();
113

114
    // FIXME: Clamp maximum and minimum scroll positions as a last attempt to fix round errors.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    FloatPoint anchor;
    if (scrollLayer->override()->isAnchorPointSet())
        anchor = scrollLayer->override()->anchorPoint();
    else
        anchor = scrollLayer->anchorPoint();

    FloatSize bounds;
    if (scrollLayer->override()->isBoundsSet())
        bounds = scrollLayer->override()->bounds();
    else
        bounds = scrollLayer->bounds();

    // Position is offset on the layer by the layer anchor point.
    FloatPoint layerPosition(-scrollPosition.x() + anchor.x() * bounds.width(),
                             -scrollPosition.y() + anchor.y() * bounds.height());

    scrollLayer->override()->setPosition(FloatPoint(layerPosition.x(), layerPosition.y()));
132 133

    // The client is going to blitVisibleContens, which allow us benefit from "defer blits" technique.
134 135 136
    return true;
}

137 138
bool InRegionScrollerPrivate::setScrollPositionWebKitThread(unsigned camouflagedLayer, const WebCore::IntPoint& scrollPosition,
    bool supportsAcceleratedScrolling, Platform::ScrollViewBase::ScrollTarget scrollTarget)
139 140 141
{
    RenderLayer* layer = 0;

142 143 144 145 146
    if (supportsAcceleratedScrolling) {
        LayerWebKitThread* layerWebKitThread = reinterpret_cast<LayerWebKitThread*>(camouflagedLayer);
        ASSERT(layerWebKitThread);
        if (layerWebKitThread->owner()) {
            GraphicsLayer* graphicsLayer = layerWebKitThread->owner();
147 148 149 150 151 152 153 154

            if (scrollTarget == Platform::ScrollViewBase::BlockElement) {
                RenderLayerBacking* backing = static_cast<RenderLayerBacking*>(graphicsLayer->client());
                layer = backing->owningLayer();
            } else {
                RenderLayerCompositor* compositor = static_cast<RenderLayerCompositor*>(graphicsLayer->client());
                layer = compositor->rootRenderLayer();
            }
155 156 157 158 159 160 161 162
        }
    } else {
        Node* node = reinterpret_cast<Node*>(camouflagedLayer);
        ASSERT(node);
        if (!node->renderer())
            return false;

        layer = node->renderer()->enclosingLayer();
163 164 165 166 167
    }

    if (!layer)
        return false;

168 169
    calculateActiveAndShrinkCachedScrollableAreas(layer);

170 171 172 173
    // FIXME: Clamp maximum and minimum scroll positions as a last attempt to fix round errors.
    return setLayerScrollPosition(layer, scrollPosition);
}

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
void InRegionScrollerPrivate::calculateActiveAndShrinkCachedScrollableAreas(RenderLayer* layer)
{
    if (!m_needsActiveScrollableAreaCalculation)
        return;

    ASSERT(layer);
    std::vector<Platform::ScrollViewBase*>::iterator end = m_activeInRegionScrollableAreas.end();
    std::vector<Platform::ScrollViewBase*>::iterator it = m_activeInRegionScrollableAreas.begin();
    while (it != end) {
        InRegionScrollableArea* curr = static_cast<InRegionScrollableArea*>(*it);

        if (layer == curr->layer()) {
            ++it;
            continue;
        }

        delete *it;
        it = m_activeInRegionScrollableAreas.erase(it);
        // ::erase invalidates the iterators.
        end = m_activeInRegionScrollableAreas.end();
    }

    ASSERT(m_activeInRegionScrollableAreas.size() == 1);
    m_needsActiveScrollableAreaCalculation = false;
}

200
void InRegionScrollerPrivate::calculateInRegionScrollableAreasForPoint(const WebCore::IntPoint& point)
201
{
202
    ASSERT(m_activeInRegionScrollableAreas.empty());
203
    m_needsActiveScrollableAreaCalculation = false;
204 205 206 207

    HitTestResult result = m_webPage->m_mainFrame->eventHandler()->hitTestResultAtPoint(m_webPage->mapFromViewportToContents(point), false /*allowShadowContent*/);
    Node* node = result.innerNonSharedNode();
    if (!node || !node->renderer())
208
        return;
209 210 211 212 213 214 215 216

    RenderLayer* layer = node->renderer()->enclosingLayer();
    do {
        RenderObject* renderer = layer->renderer();

        if (renderer->isRenderView()) {
            if (RenderView* renderView = toRenderView(renderer)) {
                FrameView* view = renderView->frameView();
217 218 219 220
                if (!view) {
                    reset();
                    return;
                }
221 222

                if (canScrollInnerFrame(view->frame())) {
223
                    pushBackInRegionScrollable(new InRegionScrollableArea(m_webPage, layer));
224 225 226 227
                    continue;
                }
            }
        } else if (canScrollRenderBox(layer->renderBox())) {
228
            pushBackInRegionScrollable(new InRegionScrollableArea(m_webPage, layer));
229 230 231 232 233
            continue;
        }

        // If we run into a fix positioned layer, set the last scrollable in-region object
        // as not able to propagate scroll to its parent scrollable.
234 235
        if (isNonRenderViewFixedPositionedContainer(layer) && m_activeInRegionScrollableAreas.size()) {
            Platform::ScrollViewBase* end = m_activeInRegionScrollableAreas.back();
236 237 238 239 240
            end->setCanPropagateScrollingToEnclosingScrollable(false);
        }

    } while (layer = parentLayer(layer));

241 242
    if (m_activeInRegionScrollableAreas.empty())
        return;
243

244 245
    m_needsActiveScrollableAreaCalculation = true;

246 247 248 249
    // Post-calculate the visible window rects in reverse hit test order so
    // we account for all and any clipping rects.
    WebCore::IntRect recursiveClippingRect(WebCore::IntPoint::zero(), m_webPage->transformedViewportSize());

250 251
    std::vector<Platform::ScrollViewBase*>::reverse_iterator rend = m_activeInRegionScrollableAreas.rend();
    for (std::vector<Platform::ScrollViewBase*>::reverse_iterator rit = m_activeInRegionScrollableAreas.rbegin(); rit != rend; ++rit) {
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

        InRegionScrollableArea* curr = static_cast<InRegionScrollableArea*>(*rit);
        RenderLayer* layer = curr->layer();

        if (layer && layer->renderer()->isRenderView()) { // #document case
            FrameView* view = toRenderView(layer->renderer())->frameView();
            ASSERT(view);
            ASSERT(canScrollInnerFrame(view->frame()));

            WebCore::IntRect frameWindowRect = m_webPage->mapToTransformed(m_webPage->getRecursiveVisibleWindowRect(view));
            frameWindowRect.intersect(recursiveClippingRect);
            curr->setVisibleWindowRect(frameWindowRect);
            recursiveClippingRect = frameWindowRect;

        } else { // RenderBox-based elements case (scrollable boxes (div's, p's, textarea's, etc)).

            RenderBox* box = layer->renderBox();
            ASSERT(box);
            ASSERT(canScrollRenderBox(box));

            WebCore::IntRect visibleWindowRect = enclosingIntRect(box->absoluteClippedOverflowRect());
            visibleWindowRect = box->frame()->view()->contentsToWindow(visibleWindowRect);
            visibleWindowRect = m_webPage->mapToTransformed(visibleWindowRect);
            visibleWindowRect.intersect(recursiveClippingRect);

            curr->setVisibleWindowRect(visibleWindowRect);
            recursiveClippingRect = visibleWindowRect;
        }
    }
281
}
282

283 284 285
const std::vector<Platform::ScrollViewBase*>& InRegionScrollerPrivate::activeInRegionScrollableAreas() const
{
    return m_activeInRegionScrollableAreas;
286 287
}

288 289 290 291 292 293 294 295 296 297 298
bool InRegionScrollerPrivate::setLayerScrollPosition(RenderLayer* layer, const IntPoint& scrollPosition)
{
    RenderObject* layerRenderer = layer->renderer();
    ASSERT(layerRenderer);

    if (layerRenderer->isRenderView()) { // #document case.
        FrameView* view = toRenderView(layerRenderer)->frameView();
        ASSERT(view);

        Frame* frame = view->frame();
        ASSERT_UNUSED(frame, frame);
299 300 301
        ASSERT(canScrollInnerFrame(frame));

        view->setCanBlitOnScroll(false);
302
        view->setScrollPosition(scrollPosition);
303

304
    } else {
305

306 307 308
        // RenderBox-based elements case (scrollable boxes (div's, p's, textarea's, etc)).
        layer->scrollToOffset(toSize(scrollPosition));
    }
309

310 311 312 313 314 315
    m_webPage->m_selectionHandler->selectionPositionChanged();
    // FIXME: We have code in place to handle scrolling and clipping tap highlight
    // on in-region scrolling. As soon as it is fast enough (i.e. we have it backed by
    // a backing store), we can reliably make use of it in the real world.
    // m_touchEventHandler->drawTapHighlight();
    return true;
316 317
}

318
void InRegionScrollerPrivate::adjustScrollDelta(const WebCore::IntPoint& maxOffset, const WebCore::IntPoint& currentOffset, WebCore::IntSize& delta) const
319 320 321 322 323 324 325 326 327 328 329 330 331 332
{
    if (currentOffset.x() + delta.width() > maxOffset.x())
        delta.setWidth(std::min(maxOffset.x() - currentOffset.x(), delta.width()));

    if (currentOffset.x() + delta.width() < 0)
        delta.setWidth(std::max(-currentOffset.x(), delta.width()));

    if (currentOffset.y() + delta.height() > maxOffset.y())
        delta.setHeight(std::min(maxOffset.y() - currentOffset.y(), delta.height()));

    if (currentOffset.y() + delta.height() < 0)
        delta.setHeight(std::max(-currentOffset.y(), delta.height()));
}

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
static bool canScrollInnerFrame(Frame* frame)
{
    if (!frame || !frame->view())
        return false;

    // Not having an owner element means that we are on the mainframe.
    if (!frame->ownerElement())
        return false;

    ASSERT(frame != frame->page()->mainFrame());

    IntSize visibleSize = frame->view()->visibleContentRect().size();
    IntSize contentsSize = frame->view()->contentsSize();

    bool canBeScrolled = contentsSize.height() > visibleSize.height() || contentsSize.width() > visibleSize.width();

    // Lets also consider the 'overflow-{x,y} property set directly to the {i}frame tag.
    return canBeScrolled && (frame->ownerElement()->scrollingMode() != ScrollbarAlwaysOff);
}

// The RenderBox::canbeScrolledAndHasScrollableArea method returns true for the
// following scenario, for example:
// (1) a div that has a vertical overflow but no horizontal overflow
//     with overflow-y: hidden and overflow-x: auto set.
// The version below fixes it.
// FIXME: Fix RenderBox::canBeScrolledAndHasScrollableArea method instead.
359
bool InRegionScrollerPrivate::canScrollRenderBox(RenderBox* box)
360
{
361 362 363 364 365
    if (!box)
        return false;

    // We use this to make non-overflown contents layers to actually
    // be overscrollable.
366 367
    if (box->layer() && box->layer()->usesCompositedScrolling()) {
        if (box->style()->overflowScrolling() == OSBlackberryTouch)
368 369 370 371
            return true;
    }

    if (!box->hasOverflowClip())
372 373
        return false;

374 375 376
    if (box->scrollHeight() == box->clientHeight() && box->scrollWidth() == box->clientWidth())
        return false;

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    if (box->scrollsOverflowX() && (box->scrollWidth() != box->clientWidth())
        || box->scrollsOverflowY() && (box->scrollHeight() != box->clientHeight()))
        return true;

    Node* node = box->node();
    return node && (node->rendererIsEditable() || node->isDocumentNode());
}

static RenderLayer* parentLayer(RenderLayer* layer)
{
    ASSERT(layer);
    if (layer->parent())
        return layer->parent();

    RenderObject* renderer = layer->renderer();
    if (renderer->document() && renderer->document()->ownerElement() && renderer->document()->ownerElement()->renderer())
        return renderer->document()->ownerElement()->renderer()->enclosingLayer();

    return 0;
}

static bool isNonRenderViewFixedPositionedContainer(RenderLayer* layer)
{
    RenderObject* o = layer->renderer();
    if (o->isRenderView())
        return false;

    return o->isOutOfFlowPositioned() && o->style()->position() == FixedPosition;
}

407
void InRegionScrollerPrivate::pushBackInRegionScrollable(InRegionScrollableArea* scrollableArea)
408 409 410 411
{
    ASSERT(!scrollableArea->isNull());

    scrollableArea->setCanPropagateScrollingToEnclosingScrollable(!isNonRenderViewFixedPositionedContainer(scrollableArea->layer()));
412
    m_activeInRegionScrollableAreas.push_back(scrollableArea);
413 414
}

415 416
}
}