summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/ZoomManager.cpp
blob: 35100ae37a8282dbe65357507acef0d275ca25fb (plain)
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
/*
 * Copyright 2011, The Android Open Source Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * 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 THE COPYRIGHT HOLDERS ``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 THE COPYRIGHT OWNER OR
 * 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.
 */

#define LOG_TAG "ZoomManager"
#define LOG_NDEBUG 1

#include "config.h"
#include "ZoomManager.h"

#include "AndroidLog.h"
#include "GLWebViewState.h"

#if USE(ACCELERATED_COMPOSITING)

namespace WebCore {

using namespace android;

ZoomManager::ZoomManager(GLWebViewState* state)
    : m_scaleRequestState(kNoScaleRequest)
    , m_currentScale(-1)
    , m_futureScale(-1)
    , m_layersScale(-1)
    , m_updateTime(-1)
    , m_transitionTime(-1)
    , m_glWebViewState(state)
{
}

void ZoomManager::scheduleUpdate(const double& currentTime,
                                 const SkIRect& viewport, float scale)
{
    // if no update time, set it
    if (updateTime() == -1) {
        m_scaleRequestState = kWillScheduleRequest;
        setUpdateTime(currentTime + s_updateInitialDelay);
        setFutureScale(scale);
        m_glWebViewState->setFutureViewport(viewport);
        return;
    }

    if (currentTime < updateTime())
        return;

    // we reached the scheduled update time, check if we can update
    if (futureScale() == scale) {
        // we are still with the previous scale, let's go
        // with the update
        m_scaleRequestState = kRequestNewScale;
        setUpdateTime(-1);
    } else {
        // we reached the update time, but the planned update was for
        // a different scale factor -- meaning the user is still probably
        // in the process of zooming. Let's push the update time a bit.
        setUpdateTime(currentTime + s_updateDelay);
        setFutureScale(scale);
        m_glWebViewState->setFutureViewport(viewport);
    }
}

double ZoomManager::zoomInTransitionTime(double currentTime)
{
    if (m_transitionTime == -1)
        m_transitionTime = currentTime + s_zoomInTransitionDelay;
    return m_transitionTime;
}

double ZoomManager::zoomOutTransitionTime(double currentTime)
{
    if (m_transitionTime == -1)
        m_transitionTime = currentTime + s_zoomOutTransitionDelay;
    return m_transitionTime;
}

float ZoomManager::zoomInTransparency(double currentTime)
{
    float t = zoomInTransitionTime(currentTime) - currentTime;
    t *= s_invZoomInTransitionDelay;
    return fmin(1, fmax(0, t));
}

float ZoomManager::zoomOutTransparency(double currentTime)
{
    float t = zoomOutTransitionTime(currentTime) - currentTime;
    t *= s_invZoomOutTransitionDelay;
    return fmin(1, fmax(0, t));
}

bool ZoomManager::swapPages()
{
    bool reset = m_scaleRequestState != kNoScaleRequest;
    m_scaleRequestState = kNoScaleRequest;
    return reset;
}

void ZoomManager::processNewScale(double currentTime, float scale)
{
    m_prepareNextTiledPage = false;
    m_zooming = false;
    const SkIRect& viewportTileBounds = m_glWebViewState->viewportTileBounds();

    if (scale == m_currentScale
        || m_glWebViewState->preZoomBounds().isEmpty())
      m_glWebViewState->setPreZoomBounds(viewportTileBounds);

    // If we have a different scale than the current one, we have to
    // decide what to do. The current behaviour is to delay an update,
    // so that we do not slow down zooming unnecessarily.
    if ((m_currentScale != scale
        && (m_scaleRequestState == ZoomManager::kNoScaleRequest
            || m_futureScale != scale))
        || m_scaleRequestState == ZoomManager::kWillScheduleRequest) {

        // schedule the new Zoom request
        scheduleUpdate(currentTime, viewportTileBounds, scale);

        // If it's a new request, we will have to prepare the page.
        if (m_scaleRequestState == ZoomManager::kRequestNewScale)
            m_prepareNextTiledPage = true;
    }

    // If the viewport has changed since we scheduled the request, we also need
    // to prepare.
    if ((m_scaleRequestState == ZoomManager::kRequestNewScale
         || m_scaleRequestState == ZoomManager::kReceivedNewScale)
        && m_glWebViewState->futureViewport() != viewportTileBounds)
        m_prepareNextTiledPage = true;

    // Checking if we are zooming...
    if (m_scaleRequestState != ZoomManager::kNoScaleRequest) {
        m_prepareNextTiledPage = true;
        m_zooming = true;
    }

    // Get the current scale; if we are zooming, we don't change the scale
    // factor immediately (see BaseLayerAndroid::drawBasePictureInGL()), but
    // we change the scaleRequestState. When the state is kReceivedNewScale
    // (see setReceivedRequest()), we can use the future scale instead of
    // the current scale to request new textures. After a transition time,
    // the scaleRequestState will be reset and the current scale will be set
    // to the future scale.
    m_layersScale = m_currentScale;
}

void ZoomManager::processTransition(double currentTime, float scale,
                                     bool* doSwap, float* backPageTransparency,
                                     float* frontPageTransparency)
{
    if (scale < m_currentScale)
        *backPageTransparency = 1 - zoomOutTransparency(currentTime);
    else
        *frontPageTransparency = zoomInTransparency(currentTime);

    // The transition between the two page is finished
    if (currentTime > transitionTime(currentTime, scale)) {
        resetTransitionTime();
        *doSwap = true;
    }
}

} // namespace WebCore

#endif // USE(ACCELERATED_COMPOSITING)