1/*
2 * Copyright (C) 2013 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#import "PlatformCALayerRemoteCustom.h"
28
29#import "LayerHostingContext.h"
30#import "RemoteLayerTreeContext.h"
31#import "RemoteLayerTreePropertyApplier.h"
32#import "WebProcess.h"
33#import <AVFoundation/AVFoundation.h>
34#import <WebCore/GraphicsLayerCA.h>
35#import <WebCore/PlatformCALayerMac.h>
36#import <WebCore/SoftLinking.h>
37#import <WebCore/WebCoreCALayerExtras.h>
38#import <wtf/RetainPtr.h>
39
40SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
41SOFT_LINK_CLASS(AVFoundation, AVPlayerLayer)
42
43using namespace WebCore;
44
45namespace WebKit {
46
47static NSString * const platformCALayerPointer = @"WKPlatformCALayer";
48
49PassRefPtr<PlatformCALayerRemote> PlatformCALayerRemoteCustom::create(PlatformLayer *platformLayer, PlatformCALayerClient* owner, RemoteLayerTreeContext& context)
50{
51    RefPtr<PlatformCALayerRemote> layer = adoptRef(new PlatformCALayerRemoteCustom(PlatformCALayerMac::layerTypeForPlatformLayer(platformLayer), platformLayer, owner, context));
52    context.layerWasCreated(*layer, layer->layerType());
53
54    return layer.release();
55}
56
57PlatformCALayerRemoteCustom::PlatformCALayerRemoteCustom(LayerType layerType, PlatformLayer * customLayer, PlatformCALayerClient* owner, RemoteLayerTreeContext& context)
58    : PlatformCALayerRemote(layerType, owner, context)
59{
60    switch (context.layerHostingMode()) {
61    case LayerHostingMode::InProcess:
62        m_layerHostingContext = LayerHostingContext::createForPort(WebProcess::shared().compositingRenderServerPort());
63        break;
64#if HAVE(OUT_OF_PROCESS_LAYER_HOSTING)
65    case LayerHostingMode::OutOfProcess:
66        m_layerHostingContext = LayerHostingContext::createForExternalHostingProcess();
67        break;
68#endif
69    }
70
71    m_layerHostingContext->setRootLayer(customLayer);
72    [customLayer setValue:[NSValue valueWithPointer:this] forKey:platformCALayerPointer];
73
74    m_platformLayer = customLayer;
75    [customLayer web_disableAllActions];
76
77    m_providesContents = layerType == LayerTypeWebGLLayer;
78
79    properties().position = FloatPoint3D(customLayer.position.x, customLayer.position.y, customLayer.zPosition);
80    properties().anchorPoint = FloatPoint3D(customLayer.anchorPoint.x, customLayer.anchorPoint.y, customLayer.anchorPointZ);
81    properties().bounds = customLayer.bounds;
82    properties().contentsRect = customLayer.contentsRect;
83}
84
85PlatformCALayerRemoteCustom::~PlatformCALayerRemoteCustom()
86{
87    [m_platformLayer setValue:nil forKey:platformCALayerPointer];
88}
89
90uint32_t PlatformCALayerRemoteCustom::hostingContextID()
91{
92    return m_layerHostingContext->contextID();
93}
94
95PassRefPtr<WebCore::PlatformCALayer> PlatformCALayerRemoteCustom::clone(PlatformCALayerClient* owner) const
96{
97    RetainPtr<CALayer *> clonedLayer;
98    bool copyContents = true;
99
100    if (layerType() == LayerTypeAVPlayerLayer) {
101        clonedLayer = adoptNS([[getAVPlayerLayerClass() alloc] init]);
102
103        AVPlayerLayer* destinationPlayerLayer = static_cast<AVPlayerLayer *>(clonedLayer.get());
104        AVPlayerLayer* sourcePlayerLayer = static_cast<AVPlayerLayer *>(platformLayer());
105        dispatch_async(dispatch_get_main_queue(), ^{
106            [destinationPlayerLayer setPlayer:[sourcePlayerLayer player]];
107        });
108        copyContents = false;
109    } else if (layerType() == LayerTypeWebGLLayer) {
110        clonedLayer = adoptNS([[CALayer alloc] init]);
111        // FIXME: currently copying WebGL contents breaks the original layer.
112        copyContents = false;
113    }
114
115    RefPtr<PlatformCALayerRemote> clone = adoptRef(new PlatformCALayerRemoteCustom(layerType(), clonedLayer.get(), owner, *context()));
116    context()->layerWasCreated(*clone, clone->layerType());
117
118    updateClonedLayerProperties(*clone, copyContents);
119
120    clone->setClonedLayer(this);
121    return clone.release();
122}
123
124CFTypeRef PlatformCALayerRemoteCustom::contents() const
125{
126    return [m_platformLayer contents];
127}
128
129void PlatformCALayerRemoteCustom::setContents(CFTypeRef contents)
130{
131    [m_platformLayer setContents:(id)contents];
132}
133
134void PlatformCALayerRemoteCustom::setNeedsDisplay(const FloatRect* rect)
135{
136    if (m_providesContents) {
137        if (rect)
138            [m_platformLayer setNeedsDisplayInRect:*rect];
139        else
140            [m_platformLayer setNeedsDisplay];
141    } else
142        PlatformCALayerRemote::setNeedsDisplay(rect);
143}
144
145} // namespace WebKit
146