blob: 679e5acae431cf6e7620471ca58c72fe7b3da575 [file] [log] [blame]
dbates@webkit.orgb8ade582014-01-09 22:59:07 +00001/*
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
adachan@apple.com5975a9c2016-04-07 20:04:16 +000028#if (PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))) && ENABLE(VIDEO_TRACK)
29#include "TextTrackRepresentationCocoa.h"
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000030
31#include "FloatRect.h"
32#include "GraphicsContextCG.h"
darin@apple.com3d1d5fc2015-04-23 05:20:23 +000033#include "IntRect.h"
adachan@apple.com5975a9c2016-04-07 20:04:16 +000034
35#if PLATFORM(IOS)
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000036#include "WebCoreThread.h"
37#include "WebCoreThreadRun.h"
adachan@apple.com5975a9c2016-04-07 20:04:16 +000038#endif
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000039
commit-queue@webkit.org8b928d82017-08-10 04:13:08 +000040#import <pal/spi/cocoa/QuartzCoreSPI.h>
41
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000042using namespace WebCore;
43
ryanhaddad@apple.com733e6f42016-05-27 20:14:16 +000044@interface WebCoreTextTrackRepresentationCocoaHelper : NSObject <CALayerDelegate> {
adachan@apple.com5975a9c2016-04-07 20:04:16 +000045 TextTrackRepresentationCocoa* _parent;
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000046}
adachan@apple.com5975a9c2016-04-07 20:04:16 +000047- (id)initWithParent:(TextTrackRepresentationCocoa*)parent;
48@property (assign) TextTrackRepresentationCocoa* parent;
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000049@end
50
adachan@apple.com5975a9c2016-04-07 20:04:16 +000051@implementation WebCoreTextTrackRepresentationCocoaHelper
52- (id)initWithParent:(TextTrackRepresentationCocoa*)parent
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000053{
54 if (!(self = [super init]))
55 return nil;
56
57 self.parent = parent;
58
59 return self;
60}
61
62- (void)dealloc
63{
darin@apple.com3d1d5fc2015-04-23 05:20:23 +000064 self.parent = nullptr;
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000065 [super dealloc];
66}
67
adachan@apple.com5975a9c2016-04-07 20:04:16 +000068- (void)setParent:(TextTrackRepresentationCocoa*)parent
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000069{
70 if (_parent)
71 [_parent->platformLayer() removeObserver:self forKeyPath:@"bounds"];
72
73 _parent = parent;
74
75 if (_parent)
jer.noble@apple.com32430782014-04-08 21:41:02 +000076 [_parent->platformLayer() addObserver:self forKeyPath:@"bounds" options:0 context:0];
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000077}
78
adachan@apple.com5975a9c2016-04-07 20:04:16 +000079- (TextTrackRepresentationCocoa*)parent
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000080{
81 return _parent;
82}
83
84- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
85{
86 UNUSED_PARAM(change);
87 UNUSED_PARAM(context);
adachan@apple.com5975a9c2016-04-07 20:04:16 +000088#if PLATFORM(IOS)
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000089 WebThreadRun(^{
90 if (_parent && [keyPath isEqual:@"bounds"] && object == _parent->platformLayer())
darin@apple.com3d1d5fc2015-04-23 05:20:23 +000091 _parent->client().textTrackRepresentationBoundsChanged(_parent->bounds());
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000092 });
adachan@apple.com5975a9c2016-04-07 20:04:16 +000093#else
94 if (_parent && [keyPath isEqual:@"bounds"] && object == _parent->platformLayer())
95 _parent->client().textTrackRepresentationBoundsChanged(_parent->bounds());
96#endif
dbates@webkit.orgb8ade582014-01-09 22:59:07 +000097}
98
99- (id)actionForLayer:(CALayer *)layer forKey:(NSString *)event
100{
101 UNUSED_PARAM(layer);
102 UNUSED_PARAM(event);
103 // Returning a NSNull from this delegate method disables all implicit CALayer actions.
104 return [NSNull null];
105}
106
107@end
108
darin@apple.com3d1d5fc2015-04-23 05:20:23 +0000109std::unique_ptr<TextTrackRepresentation> TextTrackRepresentation::create(TextTrackRepresentationClient& client)
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000110{
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000111 return std::make_unique<TextTrackRepresentationCocoa>(client);
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000112}
113
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000114TextTrackRepresentationCocoa::TextTrackRepresentationCocoa(TextTrackRepresentationClient& client)
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000115 : m_client(client)
116 , m_layer(adoptNS([[CALayer alloc] init]))
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000117 , m_delegate(adoptNS([[WebCoreTextTrackRepresentationCocoaHelper alloc] initWithParent:this]))
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000118{
119 [m_layer.get() setDelegate:m_delegate.get()];
120 [m_layer.get() setContentsGravity:kCAGravityBottom];
121}
122
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000123TextTrackRepresentationCocoa::~TextTrackRepresentationCocoa()
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000124{
125 [m_layer.get() setDelegate:nil];
darin@apple.com3d1d5fc2015-04-23 05:20:23 +0000126 [m_delegate.get() setParent:nullptr];
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000127}
128
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000129void TextTrackRepresentationCocoa::update()
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000130{
darin@apple.com3d1d5fc2015-04-23 05:20:23 +0000131 if (auto representation = m_client.createTextTrackRepresentationImage())
commit-queue@webkit.org8865c242016-09-09 01:06:47 +0000132 [m_layer.get() setContents:(id)representation->nativeImage().get()];
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000133}
134
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000135void TextTrackRepresentationCocoa::setContentScale(float scale)
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000136{
137 [m_layer.get() setContentsScale:scale];
138}
139
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000140IntRect TextTrackRepresentationCocoa::bounds() const
dbates@webkit.orgb8ade582014-01-09 22:59:07 +0000141{
142 return enclosingIntRect(FloatRect([m_layer.get() bounds]));
143}
144
adachan@apple.com5975a9c2016-04-07 20:04:16 +0000145#endif // (PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))) && ENABLE(VIDEO_TRACK)