1/*
2 * Copyright (C) 2011 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#import "config.h"
27#import "TiledCoreAnimationDrawingAreaProxy.h"
28
29#if !PLATFORM(IOS)
30
31#import "ColorSpaceData.h"
32#import "DrawingAreaMessages.h"
33#import "DrawingAreaProxyMessages.h"
34#import "LayerTreeContext.h"
35#import "WebPageProxy.h"
36#import "WebProcessProxy.h"
37
38using namespace IPC;
39using namespace WebCore;
40
41namespace WebKit {
42
43TiledCoreAnimationDrawingAreaProxy::TiledCoreAnimationDrawingAreaProxy(WebPageProxy* webPageProxy)
44    : DrawingAreaProxy(DrawingAreaTypeTiledCoreAnimation, webPageProxy)
45    , m_isWaitingForDidUpdateGeometry(false)
46{
47}
48
49TiledCoreAnimationDrawingAreaProxy::~TiledCoreAnimationDrawingAreaProxy()
50{
51}
52
53void TiledCoreAnimationDrawingAreaProxy::deviceScaleFactorDidChange()
54{
55    m_webPageProxy->process().send(Messages::DrawingArea::SetDeviceScaleFactor(m_webPageProxy->deviceScaleFactor()), m_webPageProxy->pageID());
56}
57
58void TiledCoreAnimationDrawingAreaProxy::sizeDidChange()
59{
60    if (!m_webPageProxy->isValid())
61        return;
62
63    // We only want one UpdateGeometry message in flight at once, so if we've already sent one but
64    // haven't yet received the reply we'll just return early here.
65    if (m_isWaitingForDidUpdateGeometry)
66        return;
67
68    sendUpdateGeometry();
69}
70
71void TiledCoreAnimationDrawingAreaProxy::waitForPossibleGeometryUpdate(std::chrono::milliseconds timeout)
72{
73    if (!m_isWaitingForDidUpdateGeometry)
74        return;
75
76    if (m_webPageProxy->process().state() != WebProcessProxy::State::Running)
77        return;
78
79    m_webPageProxy->process().connection()->waitForAndDispatchImmediately<Messages::DrawingAreaProxy::DidUpdateGeometry>(m_webPageProxy->pageID(), timeout);
80}
81
82void TiledCoreAnimationDrawingAreaProxy::colorSpaceDidChange()
83{
84    m_webPageProxy->process().send(Messages::DrawingArea::SetColorSpace(m_webPageProxy->colorSpace()), m_webPageProxy->pageID());
85}
86
87void TiledCoreAnimationDrawingAreaProxy::minimumLayoutSizeDidChange()
88{
89    if (!m_webPageProxy->isValid())
90        return;
91
92    // We only want one UpdateGeometry message in flight at once, so if we've already sent one but
93    // haven't yet received the reply we'll just return early here.
94    if (m_isWaitingForDidUpdateGeometry)
95        return;
96
97    sendUpdateGeometry();
98}
99
100void TiledCoreAnimationDrawingAreaProxy::enterAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext& layerTreeContext)
101{
102    m_webPageProxy->enterAcceleratedCompositingMode(layerTreeContext);
103}
104
105void TiledCoreAnimationDrawingAreaProxy::exitAcceleratedCompositingMode(uint64_t backingStoreStateID, const UpdateInfo&)
106{
107    // This should never be called.
108    ASSERT_NOT_REACHED();
109}
110
111void TiledCoreAnimationDrawingAreaProxy::updateAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext& layerTreeContext)
112{
113    m_webPageProxy->updateAcceleratedCompositingMode(layerTreeContext);
114}
115
116void TiledCoreAnimationDrawingAreaProxy::didUpdateGeometry()
117{
118    ASSERT(m_isWaitingForDidUpdateGeometry);
119
120    m_isWaitingForDidUpdateGeometry = false;
121
122    IntSize minimumLayoutSize = m_webPageProxy->minimumLayoutSize();
123
124    // If the WKView was resized while we were waiting for a DidUpdateGeometry reply from the web process,
125    // we need to resend the new size here.
126    if (m_lastSentSize != m_size || m_lastSentLayerPosition != m_layerPosition || m_lastSentMinimumLayoutSize != minimumLayoutSize)
127        sendUpdateGeometry();
128}
129
130void TiledCoreAnimationDrawingAreaProxy::waitForDidUpdateViewState()
131{
132    auto viewStateUpdateTimeout = std::chrono::milliseconds(250);
133    m_webPageProxy->process().connection()->waitForAndDispatchImmediately<Messages::WebPageProxy::DidUpdateViewState>(m_webPageProxy->pageID(), viewStateUpdateTimeout, InterruptWaitingIfSyncMessageArrives);
134}
135
136void TiledCoreAnimationDrawingAreaProxy::intrinsicContentSizeDidChange(const IntSize& newIntrinsicContentSize)
137{
138    if (m_webPageProxy->minimumLayoutSize().width() > 0)
139        m_webPageProxy->intrinsicContentSizeDidChange(newIntrinsicContentSize);
140}
141
142void TiledCoreAnimationDrawingAreaProxy::sendUpdateGeometry()
143{
144    ASSERT(!m_isWaitingForDidUpdateGeometry);
145
146    m_lastSentMinimumLayoutSize = m_webPageProxy->minimumLayoutSize();
147    m_lastSentSize = m_size;
148    m_lastSentLayerPosition = m_layerPosition;
149    m_webPageProxy->process().send(Messages::DrawingArea::UpdateGeometry(m_size, m_layerPosition), m_webPageProxy->pageID());
150    m_isWaitingForDidUpdateGeometry = true;
151}
152
153void TiledCoreAnimationDrawingAreaProxy::adjustTransientZoom(double scale, FloatPoint origin)
154{
155    m_webPageProxy->process().send(Messages::DrawingArea::AdjustTransientZoom(scale, origin), m_webPageProxy->pageID());
156}
157
158void TiledCoreAnimationDrawingAreaProxy::commitTransientZoom(double scale, FloatPoint origin)
159{
160    m_webPageProxy->process().send(Messages::DrawingArea::CommitTransientZoom(scale, origin), m_webPageProxy->pageID());
161}
162
163} // namespace WebKit
164
165#endif // !PLATFORM(IOS)
166