~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/page/scrolling/mac/ScrollingTreeScrollingNodeMac.mm

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 
14
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
15
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 
17
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
19
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
20
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
21
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
22
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
23
 * THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "ScrollingTreeScrollingNodeMac.h"
 
28
 
 
29
#if ENABLE(THREADED_SCROLLING)
 
30
 
 
31
#include "PlatformWheelEvent.h"
 
32
#include "ScrollingCoordinator.h"
 
33
#include "ScrollingTree.h"
 
34
#include "ScrollingStateTree.h"
 
35
#include "Settings.h"
 
36
#include "TileCache.h"
 
37
#include "WebTileLayer.h"
 
38
 
 
39
#include <wtf/CurrentTime.h>
 
40
#include <wtf/Deque.h>
 
41
#include <wtf/text/StringBuilder.h>
 
42
#include <wtf/text/CString.h>
 
43
 
 
44
namespace WebCore {
 
45
 
 
46
static void logThreadedScrollingMode(unsigned mainThreadScrollingReasons);
 
47
 
 
48
PassOwnPtr<ScrollingTreeScrollingNode> ScrollingTreeScrollingNode::create(ScrollingTree* scrollingTree)
 
49
{
 
50
    return adoptPtr(new ScrollingTreeScrollingNodeMac(scrollingTree));
 
51
}
 
52
 
 
53
ScrollingTreeScrollingNodeMac::ScrollingTreeScrollingNodeMac(ScrollingTree* scrollingTree)
 
54
    : ScrollingTreeScrollingNode(scrollingTree)
 
55
    , m_scrollElasticityController(this)
 
56
{
 
57
}
 
58
 
 
59
ScrollingTreeScrollingNodeMac::~ScrollingTreeScrollingNodeMac()
 
60
{
 
61
    if (m_snapRubberbandTimer)
 
62
        CFRunLoopTimerInvalidate(m_snapRubberbandTimer.get());
 
63
}
 
64
 
 
65
void ScrollingTreeScrollingNodeMac::update(ScrollingStateNode* stateNode)
 
66
{
 
67
    ScrollingTreeScrollingNode::update(stateNode);
 
68
    ScrollingStateScrollingNode* state = toScrollingStateScrollingNode(stateNode);
 
69
 
 
70
    if (state->scrollLayerDidChange())
 
71
        m_scrollLayer = state->platformScrollLayer();
 
72
 
 
73
    if (state->changedProperties() & ScrollingStateScrollingNode::RequestedScrollPosition)
 
74
        setScrollPosition(state->requestedScrollPosition());
 
75
 
 
76
    if (state->scrollLayerDidChange() || state->changedProperties() & (ScrollingStateScrollingNode::ContentsSize | ScrollingStateScrollingNode::ViewportRect))
 
77
        updateMainFramePinState(scrollPosition());
 
78
 
 
79
    if ((state->changedProperties() & ScrollingStateScrollingNode::ShouldUpdateScrollLayerPositionOnMainThread)) {
 
80
        unsigned mainThreadScrollingReasons = this->shouldUpdateScrollLayerPositionOnMainThread();
 
81
 
 
82
        if (mainThreadScrollingReasons) {
 
83
            // We're transitioning to the slow "update scroll layer position on the main thread" mode.
 
84
            // Initialize the probable main thread scroll position with the current scroll layer position.
 
85
            if (state->changedProperties() & ScrollingStateScrollingNode::RequestedScrollPosition)
 
86
                m_probableMainThreadScrollPosition = state->requestedScrollPosition();
 
87
            else {
 
88
                CGPoint scrollLayerPosition = m_scrollLayer.get().position;
 
89
                m_probableMainThreadScrollPosition = IntPoint(-scrollLayerPosition.x, -scrollLayerPosition.y);
 
90
            }
 
91
        }
 
92
 
 
93
        if (scrollingTree()->scrollingPerformanceLoggingEnabled())
 
94
            logThreadedScrollingMode(mainThreadScrollingReasons);
 
95
    }
 
96
}
 
97
 
 
98
void ScrollingTreeScrollingNodeMac::handleWheelEvent(const PlatformWheelEvent& wheelEvent)
 
99
{
 
100
    if (!canHaveScrollbars())
 
101
        return;
 
102
 
 
103
    m_scrollElasticityController.handleWheelEvent(wheelEvent);
 
104
    scrollingTree()->handleWheelEventPhase(wheelEvent.phase());
 
105
}
 
106
 
 
107
bool ScrollingTreeScrollingNodeMac::allowsHorizontalStretching()
 
108
{
 
109
    switch (horizontalScrollElasticity()) {
 
110
    case ScrollElasticityAutomatic:
 
111
        return hasEnabledHorizontalScrollbar() || !hasEnabledVerticalScrollbar();
 
112
    case ScrollElasticityNone:
 
113
        return false;
 
114
    case ScrollElasticityAllowed:
 
115
        return true;
 
116
    }
 
117
 
 
118
    ASSERT_NOT_REACHED();
 
119
    return false;
 
120
}
 
121
 
 
122
bool ScrollingTreeScrollingNodeMac::allowsVerticalStretching()
 
123
{
 
124
    switch (verticalScrollElasticity()) {
 
125
    case ScrollElasticityAutomatic:
 
126
        return hasEnabledVerticalScrollbar() || !hasEnabledHorizontalScrollbar();
 
127
    case ScrollElasticityNone:
 
128
        return false;
 
129
    case ScrollElasticityAllowed:
 
130
        return true;
 
131
    }
 
132
 
 
133
    ASSERT_NOT_REACHED();
 
134
    return false;
 
135
}
 
136
 
 
137
IntSize ScrollingTreeScrollingNodeMac::stretchAmount()
 
138
{
 
139
    IntSize stretch;
 
140
 
 
141
    if (scrollPosition().y() < minimumScrollPosition().y())
 
142
        stretch.setHeight(scrollPosition().y() - minimumScrollPosition().y());
 
143
    else if (scrollPosition().y() > maximumScrollPosition().y())
 
144
        stretch.setHeight(scrollPosition().y() - maximumScrollPosition().y());
 
145
 
 
146
    if (scrollPosition().x() < minimumScrollPosition().x())
 
147
        stretch.setWidth(scrollPosition().x() - minimumScrollPosition().x());
 
148
    else if (scrollPosition().x() > maximumScrollPosition().x())
 
149
        stretch.setWidth(scrollPosition().x() - maximumScrollPosition().x());
 
150
 
 
151
    return stretch;
 
152
}
 
153
 
 
154
bool ScrollingTreeScrollingNodeMac::pinnedInDirection(const FloatSize& delta)
 
155
{
 
156
    FloatSize limitDelta;
 
157
 
 
158
    if (fabsf(delta.height()) >= fabsf(delta.width())) {
 
159
        if (delta.height() < 0) {
 
160
            // We are trying to scroll up.  Make sure we are not pinned to the top
 
161
            limitDelta.setHeight(scrollPosition().y() - minimumScrollPosition().y());
 
162
        } else {
 
163
            // We are trying to scroll down.  Make sure we are not pinned to the bottom
 
164
            limitDelta.setHeight(maximumScrollPosition().y() - scrollPosition().y());
 
165
        }
 
166
    } else if (delta.width()) {
 
167
        if (delta.width() < 0) {
 
168
            // We are trying to scroll left.  Make sure we are not pinned to the left
 
169
            limitDelta.setHeight(scrollPosition().x() - minimumScrollPosition().x());
 
170
        } else {
 
171
            // We are trying to scroll right.  Make sure we are not pinned to the right
 
172
            limitDelta.setHeight(maximumScrollPosition().x() - scrollPosition().x());
 
173
        }
 
174
    }
 
175
 
 
176
    if ((delta.width() || delta.height()) && (limitDelta.width() < 1 && limitDelta.height() < 1))        
 
177
        return true;
 
178
 
 
179
    return false;
 
180
}
 
181
 
 
182
bool ScrollingTreeScrollingNodeMac::canScrollHorizontally()
 
183
{
 
184
    return hasEnabledHorizontalScrollbar();
 
185
}
 
186
 
 
187
bool ScrollingTreeScrollingNodeMac::canScrollVertically()
 
188
{
 
189
    return hasEnabledVerticalScrollbar();
 
190
}
 
191
 
 
192
bool ScrollingTreeScrollingNodeMac::shouldRubberBandInDirection(ScrollDirection direction)
 
193
{
 
194
    if (direction == ScrollLeft)
 
195
        return !scrollingTree()->canGoBack();
 
196
    if (direction == ScrollRight)
 
197
        return !scrollingTree()->canGoForward();
 
198
 
 
199
    ASSERT_NOT_REACHED();
 
200
    return false;
 
201
}
 
202
 
 
203
IntPoint ScrollingTreeScrollingNodeMac::absoluteScrollPosition()
 
204
{
 
205
    return scrollPosition();
 
206
}
 
207
 
 
208
void ScrollingTreeScrollingNodeMac::immediateScrollBy(const FloatSize& offset)
 
209
{
 
210
    scrollBy(roundedIntSize(offset));
 
211
}
 
212
 
 
213
void ScrollingTreeScrollingNodeMac::immediateScrollByWithoutContentEdgeConstraints(const FloatSize& offset)
 
214
{
 
215
    scrollByWithoutContentEdgeConstraints(roundedIntSize(offset));
 
216
}
 
217
 
 
218
void ScrollingTreeScrollingNodeMac::startSnapRubberbandTimer()
 
219
{
 
220
    ASSERT(!m_snapRubberbandTimer);
 
221
 
 
222
    CFTimeInterval timerInterval = 1.0 / 60.0;
 
223
 
 
224
    m_snapRubberbandTimer = adoptCF(CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent() + timerInterval, timerInterval, 0, 0, ^(CFRunLoopTimerRef) {
 
225
        m_scrollElasticityController.snapRubberBandTimerFired();
 
226
    }));
 
227
    CFRunLoopAddTimer(CFRunLoopGetCurrent(), m_snapRubberbandTimer.get(), kCFRunLoopDefaultMode);
 
228
}
 
229
 
 
230
void ScrollingTreeScrollingNodeMac::stopSnapRubberbandTimer()
 
231
{
 
232
    if (!m_snapRubberbandTimer)
 
233
        return;
 
234
 
 
235
    CFRunLoopTimerInvalidate(m_snapRubberbandTimer.get());
 
236
    m_snapRubberbandTimer = nullptr;
 
237
}
 
238
 
 
239
IntPoint ScrollingTreeScrollingNodeMac::scrollPosition() const
 
240
{
 
241
    if (shouldUpdateScrollLayerPositionOnMainThread())
 
242
        return m_probableMainThreadScrollPosition;
 
243
 
 
244
    CGPoint scrollLayerPosition = m_scrollLayer.get().position;
 
245
    return IntPoint(-scrollLayerPosition.x + scrollOrigin().x(), -scrollLayerPosition.y + scrollOrigin().y());
 
246
}
 
247
 
 
248
void ScrollingTreeScrollingNodeMac::setScrollPosition(const IntPoint& scrollPosition)
 
249
{
 
250
    IntPoint newScrollPosition = scrollPosition;
 
251
    newScrollPosition = newScrollPosition.shrunkTo(maximumScrollPosition());
 
252
    newScrollPosition = newScrollPosition.expandedTo(minimumScrollPosition());
 
253
 
 
254
    setScrollPositionWithoutContentEdgeConstraints(newScrollPosition);
 
255
 
 
256
    if (scrollingTree()->scrollingPerformanceLoggingEnabled())
 
257
        logExposedUnfilledArea();
 
258
}
 
259
 
 
260
void ScrollingTreeScrollingNodeMac::setScrollPositionWithoutContentEdgeConstraints(const IntPoint& scrollPosition)
 
261
{
 
262
    updateMainFramePinState(scrollPosition);
 
263
 
 
264
    if (shouldUpdateScrollLayerPositionOnMainThread()) {
 
265
        m_probableMainThreadScrollPosition = scrollPosition;
 
266
        scrollingTree()->updateMainFrameScrollPosition(scrollPosition, SetScrollingLayerPosition);
 
267
        return;
 
268
    }
 
269
 
 
270
    setScrollLayerPosition(scrollPosition);
 
271
    scrollingTree()->updateMainFrameScrollPosition(scrollPosition);
 
272
}
 
273
 
 
274
void ScrollingTreeScrollingNodeMac::setScrollLayerPosition(const IntPoint& position)
 
275
{
 
276
    ASSERT(!shouldUpdateScrollLayerPositionOnMainThread());
 
277
    m_scrollLayer.get().position = CGPointMake(-position.x() + scrollOrigin().x(), -position.y() + scrollOrigin().y());
 
278
 
 
279
    if (!m_children)
 
280
        return;
 
281
 
 
282
    IntSize scrollOffsetForFixedChildren = WebCore::scrollOffsetForFixedPosition(viewportRect(), contentsSize(), position,
 
283
        scrollOrigin(), 1, false);
 
284
    IntRect viewportRect = this->viewportRect();
 
285
    viewportRect.setLocation(toPoint(scrollOffsetForFixedChildren));
 
286
 
 
287
    size_t size = m_children->size();
 
288
    for (size_t i = 0; i < size; ++i)
 
289
        m_children->at(i)->parentScrollPositionDidChange(viewportRect);
 
290
}
 
291
 
 
292
IntPoint ScrollingTreeScrollingNodeMac::minimumScrollPosition() const
 
293
{
 
294
    return IntPoint(0, 0);
 
295
}
 
296
 
 
297
IntPoint ScrollingTreeScrollingNodeMac::maximumScrollPosition() const
 
298
{
 
299
    IntPoint position(contentsSize().width() - viewportRect().width(),
 
300
                      contentsSize().height() - viewportRect().height());
 
301
 
 
302
    position.clampNegativeToZero();
 
303
 
 
304
    return position;
 
305
}
 
306
 
 
307
void ScrollingTreeScrollingNodeMac::scrollBy(const IntSize& offset)
 
308
{
 
309
    setScrollPosition(scrollPosition() + offset);
 
310
}
 
311
 
 
312
void ScrollingTreeScrollingNodeMac::scrollByWithoutContentEdgeConstraints(const IntSize& offset)
 
313
{
 
314
    setScrollPositionWithoutContentEdgeConstraints(scrollPosition() + offset);
 
315
}
 
316
 
 
317
void ScrollingTreeScrollingNodeMac::updateMainFramePinState(const IntPoint& scrollPosition)
 
318
{
 
319
    bool pinnedToTheLeft = scrollPosition.x() <= minimumScrollPosition().x();
 
320
    bool pinnedToTheRight = scrollPosition.x() >= maximumScrollPosition().x();
 
321
 
 
322
    scrollingTree()->setMainFramePinState(pinnedToTheLeft, pinnedToTheRight);
 
323
}
 
324
 
 
325
void ScrollingTreeScrollingNodeMac::logExposedUnfilledArea()
 
326
{
 
327
    Region paintedVisibleTiles;
 
328
 
 
329
    Deque<CALayer*> layerQueue;
 
330
    layerQueue.append(m_scrollLayer.get());
 
331
    WebTileLayerList tiles;
 
332
 
 
333
    while(!layerQueue.isEmpty() && tiles.isEmpty()) {
 
334
        CALayer* layer = layerQueue.takeFirst();
 
335
        NSArray* sublayers = [[layer sublayers] copy];
 
336
 
 
337
        // If this layer is the parent of a tile, it is the parent of all of the tiles and nothing else.
 
338
        if ([[sublayers objectAtIndex:0] isKindOfClass:[WebTileLayer class]]) {
 
339
            for (CALayer* sublayer in sublayers) {
 
340
                ASSERT([sublayer isKindOfClass:[WebTileLayer class]]);
 
341
                tiles.append(static_cast<WebTileLayer*>(sublayer));
 
342
            }
 
343
        } else {
 
344
            for (CALayer* sublayer in sublayers)
 
345
                layerQueue.append(sublayer);
 
346
        }
 
347
 
 
348
        [sublayers release];
 
349
    }
 
350
 
 
351
    IntPoint scrollPosition = this->scrollPosition();
 
352
    unsigned unfilledArea = TileCache::blankPixelCountForTiles(tiles, viewportRect(), IntPoint(-scrollPosition.x(), -scrollPosition.y()));
 
353
 
 
354
    if (unfilledArea)
 
355
        WTFLogAlways("SCROLLING: Exposed tileless area. Time: %f Unfilled Pixels: %u\n", WTF::monotonicallyIncreasingTime(), unfilledArea);
 
356
}
 
357
 
 
358
static void logThreadedScrollingMode(unsigned mainThreadScrollingReasons)
 
359
{
 
360
    if (mainThreadScrollingReasons) {
 
361
        StringBuilder reasonsDescription;
 
362
 
 
363
        if (mainThreadScrollingReasons & ScrollingCoordinator::ForcedOnMainThread)
 
364
            reasonsDescription.append("forced,");
 
365
        if (mainThreadScrollingReasons & ScrollingCoordinator::HasSlowRepaintObjects)
 
366
            reasonsDescription.append("slow-repaint objects,");
 
367
        if (mainThreadScrollingReasons & ScrollingCoordinator::HasViewportConstrainedObjectsWithoutSupportingFixedLayers)
 
368
            reasonsDescription.append("viewport-constrained objects,");
 
369
        if (mainThreadScrollingReasons & ScrollingCoordinator::HasNonLayerFixedObjects)
 
370
            reasonsDescription.append("non-layer viewport-constrained objects,");
 
371
        if (mainThreadScrollingReasons & ScrollingCoordinator::IsImageDocument)
 
372
            reasonsDescription.append("image document,");
 
373
 
 
374
        // Strip the trailing comma.
 
375
        String reasonsDescriptionTrimmed = reasonsDescription.toString().left(reasonsDescription.length() - 1);
 
376
 
 
377
        WTFLogAlways("SCROLLING: Switching to main-thread scrolling mode. Time: %f Reason(s): %s\n", WTF::monotonicallyIncreasingTime(), reasonsDescriptionTrimmed.ascii().data());
 
378
    } else
 
379
        WTFLogAlways("SCROLLING: Switching to threaded scrolling mode. Time: %f\n", WTF::monotonicallyIncreasingTime());
 
380
}
 
381
 
 
382
} // namespace WebCore
 
383
 
 
384
#endif // ENABLE(THREADED_SCROLLING)