blob: ff23bc6e6e1d93c68f544871e7859c44d2a0a480 [file] [log] [blame]
denicija124a6fc2017-03-31 02:47:29 -07001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "WebRTC/RTCMTLNSVideoView.h"
12
13#import <Metal/Metal.h>
14#import <MetalKit/MetalKit.h>
15
16#import "WebRTC/RTCVideoFrame.h"
17
18#import "RTCMTLI420Renderer.h"
19
denicijad2088152017-04-28 02:14:54 -070020@interface RTCMTLNSVideoView ()<MTKViewDelegate>
denicija124a6fc2017-03-31 02:47:29 -070021@property(nonatomic) id<RTCMTLRenderer> renderer;
22@property(nonatomic, strong) MTKView *metalView;
23@property(atomic, strong) RTCVideoFrame *videoFrame;
24@end
25
26@implementation RTCMTLNSVideoView {
27 id<RTCMTLRenderer> _renderer;
28}
29
30@synthesize renderer = _renderer;
31@synthesize metalView = _metalView;
32@synthesize videoFrame = _videoFrame;
33
34- (instancetype)initWithFrame:(CGRect)frameRect {
35 self = [super initWithFrame:frameRect];
36 if (self) {
37 [self configure];
38 }
39 return self;
40}
41
42- (instancetype)initWithCoder:(NSCoder *)aCoder {
43 self = [super initWithCoder:aCoder];
44 if (self) {
45 [self configure];
46 }
47 return self;
48}
49
50#pragma mark - Private
51
52+ (BOOL)isMetalAvailable {
adam.fedor42742a52017-06-12 07:32:02 -070053 return [MTLCopyAllDevices() count] > 0;
denicija124a6fc2017-03-31 02:47:29 -070054}
55
56- (void)configure {
57 if ([[self class] isMetalAvailable]) {
58 _metalView = [[MTKView alloc] initWithFrame:self.bounds];
59 [self addSubview:_metalView];
60 _metalView.layerContentsPlacement = NSViewLayerContentsPlacementScaleProportionallyToFit;
61 _metalView.translatesAutoresizingMaskIntoConstraints = NO;
62 _metalView.framebufferOnly = YES;
63 _metalView.delegate = self;
64
65 _renderer = [[RTCMTLI420Renderer alloc] init];
66 if (![(RTCMTLI420Renderer *)_renderer addRenderingDestination:_metalView]) {
67 _renderer = nil;
68 };
69 }
70}
71
72- (void)updateConstraints {
73 NSDictionary *views = NSDictionaryOfVariableBindings(_metalView);
74
75 NSArray *constraintsHorizontal =
76 [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_metalView]-0-|"
77 options:0
78 metrics:nil
79 views:views];
80 [self addConstraints:constraintsHorizontal];
81
82 NSArray *constraintsVertical =
83 [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_metalView]-0-|"
84 options:0
85 metrics:nil
86 views:views];
87 [self addConstraints:constraintsVertical];
88 [super updateConstraints];
89}
90
91#pragma mark - MTKViewDelegate methods
92- (void)drawInMTKView:(nonnull MTKView *)view {
denicijad2088152017-04-28 02:14:54 -070093 if (self.videoFrame == nil) {
94 return;
95 }
denicija124a6fc2017-03-31 02:47:29 -070096 if (view == self.metalView) {
97 [_renderer drawFrame:self.videoFrame];
98 }
99}
100
101- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
102}
103
104#pragma mark - RTCVideoRenderer
105
106- (void)setSize:(CGSize)size {
107 _metalView.drawableSize = size;
108 [_metalView draw];
109}
110
111- (void)renderFrame:(nullable RTCVideoFrame *)frame {
112 if (frame == nil) {
113 return;
114 }
115 self.videoFrame = [frame newI420VideoFrame];
116}
117
118@end