dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 28 | #if (PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))) && ENABLE(VIDEO_TRACK) |
| 29 | #include "TextTrackRepresentationCocoa.h" |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 30 | |
| 31 | #include "FloatRect.h" |
| 32 | #include "GraphicsContextCG.h" |
darin@apple.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 33 | #include "IntRect.h" |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 34 | |
| 35 | #if PLATFORM(IOS) |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 36 | #include "WebCoreThread.h" |
| 37 | #include "WebCoreThreadRun.h" |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 38 | #endif |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 39 | |
commit-queue@webkit.org | 8b928d8 | 2017-08-10 04:13:08 +0000 | [diff] [blame] | 40 | #import <pal/spi/cocoa/QuartzCoreSPI.h> |
| 41 | |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 42 | using namespace WebCore; |
| 43 | |
ryanhaddad@apple.com | 733e6f4 | 2016-05-27 20:14:16 +0000 | [diff] [blame] | 44 | @interface WebCoreTextTrackRepresentationCocoaHelper : NSObject <CALayerDelegate> { |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 45 | TextTrackRepresentationCocoa* _parent; |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 46 | } |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 47 | - (id)initWithParent:(TextTrackRepresentationCocoa*)parent; |
| 48 | @property (assign) TextTrackRepresentationCocoa* parent; |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 49 | @end |
| 50 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 51 | @implementation WebCoreTextTrackRepresentationCocoaHelper |
| 52 | - (id)initWithParent:(TextTrackRepresentationCocoa*)parent |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 53 | { |
| 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.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 64 | self.parent = nullptr; |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 65 | [super dealloc]; |
| 66 | } |
| 67 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 68 | - (void)setParent:(TextTrackRepresentationCocoa*)parent |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 69 | { |
| 70 | if (_parent) |
| 71 | [_parent->platformLayer() removeObserver:self forKeyPath:@"bounds"]; |
| 72 | |
| 73 | _parent = parent; |
| 74 | |
| 75 | if (_parent) |
jer.noble@apple.com | 3243078 | 2014-04-08 21:41:02 +0000 | [diff] [blame] | 76 | [_parent->platformLayer() addObserver:self forKeyPath:@"bounds" options:0 context:0]; |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 77 | } |
| 78 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 79 | - (TextTrackRepresentationCocoa*)parent |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 80 | { |
| 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.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 88 | #if PLATFORM(IOS) |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 89 | WebThreadRun(^{ |
| 90 | if (_parent && [keyPath isEqual:@"bounds"] && object == _parent->platformLayer()) |
darin@apple.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 91 | _parent->client().textTrackRepresentationBoundsChanged(_parent->bounds()); |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 92 | }); |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 93 | #else |
| 94 | if (_parent && [keyPath isEqual:@"bounds"] && object == _parent->platformLayer()) |
| 95 | _parent->client().textTrackRepresentationBoundsChanged(_parent->bounds()); |
| 96 | #endif |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 97 | } |
| 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.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 109 | std::unique_ptr<TextTrackRepresentation> TextTrackRepresentation::create(TextTrackRepresentationClient& client) |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 110 | { |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 111 | return std::make_unique<TextTrackRepresentationCocoa>(client); |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 112 | } |
| 113 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 114 | TextTrackRepresentationCocoa::TextTrackRepresentationCocoa(TextTrackRepresentationClient& client) |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 115 | : m_client(client) |
| 116 | , m_layer(adoptNS([[CALayer alloc] init])) |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 117 | , m_delegate(adoptNS([[WebCoreTextTrackRepresentationCocoaHelper alloc] initWithParent:this])) |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 118 | { |
| 119 | [m_layer.get() setDelegate:m_delegate.get()]; |
| 120 | [m_layer.get() setContentsGravity:kCAGravityBottom]; |
| 121 | } |
| 122 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 123 | TextTrackRepresentationCocoa::~TextTrackRepresentationCocoa() |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 124 | { |
| 125 | [m_layer.get() setDelegate:nil]; |
darin@apple.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 126 | [m_delegate.get() setParent:nullptr]; |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 129 | void TextTrackRepresentationCocoa::update() |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 130 | { |
darin@apple.com | 3d1d5fc | 2015-04-23 05:20:23 +0000 | [diff] [blame] | 131 | if (auto representation = m_client.createTextTrackRepresentationImage()) |
commit-queue@webkit.org | 8865c24 | 2016-09-09 01:06:47 +0000 | [diff] [blame] | 132 | [m_layer.get() setContents:(id)representation->nativeImage().get()]; |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 135 | void TextTrackRepresentationCocoa::setContentScale(float scale) |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 136 | { |
| 137 | [m_layer.get() setContentsScale:scale]; |
| 138 | } |
| 139 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 140 | IntRect TextTrackRepresentationCocoa::bounds() const |
dbates@webkit.org | b8ade58 | 2014-01-09 22:59:07 +0000 | [diff] [blame] | 141 | { |
| 142 | return enclosingIntRect(FloatRect([m_layer.get() bounds])); |
| 143 | } |
| 144 | |
adachan@apple.com | 5975a9c | 2016-04-07 20:04:16 +0000 | [diff] [blame] | 145 | #endif // (PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))) && ENABLE(VIDEO_TRACK) |