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 "LayerTreeHostCAMac.h"
28
29#import "WebProcess.h"
30#import <QuartzCore/CATransaction.h>
31#import <WebCore/GraphicsLayer.h>
32#import <WebKitSystemInterface.h>
33
34using namespace WebCore;
35
36@interface CATransaction (Details)
37+ (void)synchronize;
38@end
39
40namespace WebKit {
41
42PassRefPtr<LayerTreeHostCAMac> LayerTreeHostCAMac::create(WebPage* webPage)
43{
44    RefPtr<LayerTreeHostCAMac> host = adoptRef(new LayerTreeHostCAMac(webPage));
45    host->initialize();
46    return host.release();
47}
48
49LayerTreeHostCAMac::LayerTreeHostCAMac(WebPage* webPage)
50    : LayerTreeHostCA(webPage)
51{
52}
53
54LayerTreeHostCAMac::~LayerTreeHostCAMac()
55{
56    ASSERT(!m_flushPendingLayerChangesRunLoopObserver);
57    ASSERT(!m_remoteLayerClient);
58}
59
60void LayerTreeHostCAMac::platformInitialize(LayerTreeContext& layerTreeContext)
61{
62    mach_port_t serverPort = WebProcess::shared().compositingRenderServerPort();
63    m_remoteLayerClient = WKCARemoteLayerClientMakeWithServerPort(serverPort);
64
65    WKCARemoteLayerClientSetLayer(m_remoteLayerClient.get(), rootLayer()->platformLayer());
66
67    layerTreeContext.contextID = WKCARemoteLayerClientGetClientId(m_remoteLayerClient.get());
68}
69
70void LayerTreeHostCAMac::scheduleLayerFlush()
71{
72    CFRunLoopRef currentRunLoop = CFRunLoopGetCurrent();
73
74    // Make sure we wake up the loop or the observer could be delayed until some other source fires.
75    CFRunLoopWakeUp(currentRunLoop);
76
77    if (m_flushPendingLayerChangesRunLoopObserver)
78        return;
79
80    // Run before the Core Animation commit observer, which has order 2000000.
81    const CFIndex runLoopOrder = 2000000 - 1;
82    CFRunLoopObserverContext context = { 0, this, 0, 0, 0 };
83    m_flushPendingLayerChangesRunLoopObserver.adoptCF(CFRunLoopObserverCreate(0, kCFRunLoopBeforeWaiting | kCFRunLoopExit, true, runLoopOrder, flushPendingLayerChangesRunLoopObserverCallback, &context));
84
85    CFRunLoopAddObserver(currentRunLoop, m_flushPendingLayerChangesRunLoopObserver.get(), kCFRunLoopCommonModes);
86}
87
88void LayerTreeHostCAMac::invalidate()
89{
90    if (m_flushPendingLayerChangesRunLoopObserver) {
91        CFRunLoopObserverInvalidate(m_flushPendingLayerChangesRunLoopObserver.get());
92        m_flushPendingLayerChangesRunLoopObserver = nullptr;
93    }
94
95    WKCARemoteLayerClientInvalidate(m_remoteLayerClient.get());
96    m_remoteLayerClient = nullptr;
97
98    LayerTreeHostCA::invalidate();
99}
100
101void LayerTreeHostCAMac::sizeDidChange(const IntSize& newSize)
102{
103    LayerTreeHostCA::sizeDidChange(newSize);
104    [CATransaction flush];
105    [CATransaction synchronize];
106}
107
108void LayerTreeHostCAMac::forceRepaint()
109{
110    LayerTreeHostCA::forceRepaint();
111    [CATransaction flush];
112    [CATransaction synchronize];
113}
114
115void LayerTreeHostCAMac::pauseRendering()
116{
117    CALayer* root = rootLayer()->platformLayer();
118    [root setValue:(id)kCFBooleanTrue forKey:@"NSCAViewRenderPaused"];
119    [[NSNotificationCenter defaultCenter] postNotificationName:@"NSCAViewRenderDidPauseNotification" object:nil userInfo:[NSDictionary dictionaryWithObject:root forKey:@"layer"]];
120}
121
122void LayerTreeHostCAMac::resumeRendering()
123{
124    CALayer* root = rootLayer()->platformLayer();
125    [root setValue:(id)kCFBooleanFalse forKey:@"NSCAViewRenderPaused"];
126    [[NSNotificationCenter defaultCenter] postNotificationName:@"NSCAViewRenderDidResumeNotification" object:nil userInfo:[NSDictionary dictionaryWithObject:root forKey:@"layer"]];
127}
128
129void LayerTreeHostCAMac::flushPendingLayerChangesRunLoopObserverCallback(CFRunLoopObserverRef, CFRunLoopActivity, void* context)
130{
131    // This gets called outside of the normal event loop so wrap in an autorelease pool
132    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
133    static_cast<LayerTreeHostCAMac*>(context)->performScheduledLayerFlush();
134    [pool drain];
135}
136
137void LayerTreeHostCAMac::didPerformScheduledLayerFlush()
138{
139    // We successfully flushed the pending layer changes, remove the run loop observer.
140    ASSERT(m_flushPendingLayerChangesRunLoopObserver);
141    CFRunLoopObserverInvalidate(m_flushPendingLayerChangesRunLoopObserver.get());
142    m_flushPendingLayerChangesRunLoopObserver = 0;
143
144    LayerTreeHostCA::didPerformScheduledLayerFlush();
145}
146
147} // namespace WebKit
148