Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/Metal/RTCMTLVideoView.m

Issue 2784243003: iOS/MacOS:Refactor metal rendering by extracting common implementation (Closed)
Patch Set: Fix build linkage Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #import "WebRTC/RTCMTLVideoView.h" 11 #import "WebRTC/RTCMTLVideoView.h"
12 12
13 #import <Metal/Metal.h> 13 #import <Metal/Metal.h>
14 #import <MetalKit/MetalKit.h> 14 #import <MetalKit/MetalKit.h>
15 15
16 #import "WebRTC/RTCLogging.h" 16 #import "WebRTC/RTCLogging.h"
17 #import "WebRTC/RTCVideoFrame.h" 17 #import "WebRTC/RTCVideoFrame.h"
18 18
19 #import "RTCMTLI420Renderer.h"
19 #import "RTCMTLNV12Renderer.h" 20 #import "RTCMTLNV12Renderer.h"
20 21
21 // To avoid unreconized symbol linker errors, we're taking advantage of the objc runtime. 22 // To avoid unreconized symbol linker errors, we're taking advantage of the objc runtime.
22 // Linking errors occur when compiling for architectures that don't support Meta l. 23 // Linking errors occur when compiling for architectures that don't support Meta l.
23 #define MTKViewClass NSClassFromString(@"MTKView") 24 #define MTKViewClass NSClassFromString(@"MTKView")
24 #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer") 25 #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer")
26 #define RTCMTLI420RendererClass NSClassFromString(@"RTCMTLI420Renderer")
25 27
26 @interface RTCMTLVideoView () <MTKViewDelegate> 28 @interface RTCMTLVideoView () <MTKViewDelegate>
27 @property(nonatomic, strong) RTCMTLNV12Renderer *renderer; 29 @property(nonatomic, strong) RTCMTLI420Renderer *rendererI420;
30 @property(nonatomic, strong) RTCMTLNV12Renderer *rendererNV12;
28 @property(nonatomic, strong) MTKView *metalView; 31 @property(nonatomic, strong) MTKView *metalView;
29 @property(atomic, strong) RTCVideoFrame *videoFrame; 32 @property(atomic, strong) RTCVideoFrame *videoFrame;
30 @end 33 @end
31 34
32 @implementation RTCMTLVideoView 35 @implementation RTCMTLVideoView
33 36
34 @synthesize renderer = _renderer; 37 @synthesize rendererI420 = _rendererI420;
38 @synthesize rendererNV12 = _rendererNV12;
35 @synthesize metalView = _metalView; 39 @synthesize metalView = _metalView;
36 @synthesize videoFrame = _videoFrame; 40 @synthesize videoFrame = _videoFrame;
37 41
38 - (instancetype)initWithFrame:(CGRect)frameRect { 42 - (instancetype)initWithFrame:(CGRect)frameRect {
39 self = [super initWithFrame:frameRect]; 43 self = [super initWithFrame:frameRect];
40 if (self) { 44 if (self) {
41 [self configure]; 45 [self configure];
42 } 46 }
43 return self; 47 return self;
44 } 48 }
(...skipping 14 matching lines...) Expand all
59 #else 63 #else
60 return NO; 64 return NO;
61 #endif 65 #endif
62 } 66 }
63 67
64 + (MTKView *)createMetalView:(CGRect)frame { 68 + (MTKView *)createMetalView:(CGRect)frame {
65 MTKView *view = [[MTKViewClass alloc] initWithFrame:frame]; 69 MTKView *view = [[MTKViewClass alloc] initWithFrame:frame];
66 return view; 70 return view;
67 } 71 }
68 72
69 + (RTCMTLNV12Renderer *)createMetalRenderer { 73 + (RTCMTLNV12Renderer *)createNV12Renderer {
70 RTCMTLNV12Renderer *renderer = [[RTCMTLNV12RendererClass alloc] init]; 74 return [[RTCMTLNV12RendererClass alloc] init];
71 return renderer; 75 }
76
77 + (RTCMTLI420Renderer *)createI420Renderer {
78 return [[RTCMTLI420RendererClass alloc] init];
72 } 79 }
73 80
74 - (void)configure { 81 - (void)configure {
75 if (![RTCMTLVideoView isMetalAvailable]) { 82 NSAssert([RTCMTLVideoView isMetalAvailable], @"Metal not availiable on this de vice");
76 RTCLog("Metal unavailable");
77 return;
78 }
79 83
80 _metalView = [RTCMTLVideoView createMetalView:self.bounds]; 84 _metalView = [RTCMTLVideoView createMetalView:self.bounds];
81 _renderer = [RTCMTLVideoView createMetalRenderer]; 85 [self configureMetalView];
82
83 if ([self configureMetalRenderer]) {
84 [self configureMetalView];
85 } else {
86 _renderer = nil;
87 RTCLogError("Metal configuration falied.");
88 }
89 }
90
91 - (BOOL)configureMetalRenderer {
92 return [_renderer addRenderingDestination:_metalView];
93 } 86 }
94 87
95 - (void)configureMetalView { 88 - (void)configureMetalView {
96 if (_metalView) { 89 if (_metalView) {
97 _metalView.delegate = self; 90 _metalView.delegate = self;
98 [self addSubview:_metalView]; 91 [self addSubview:_metalView];
99 _metalView.contentMode = UIViewContentModeScaleAspectFit; 92 _metalView.contentMode = UIViewContentModeScaleAspectFit;
100 _metalView.translatesAutoresizingMaskIntoConstraints = NO; 93 _metalView.translatesAutoresizingMaskIntoConstraints = NO;
101 UILayoutGuide *margins = self.layoutMarginsGuide; 94 UILayoutGuide *margins = self.layoutMarginsGuide;
102 [_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YE S; 95 [_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YE S;
103 [_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].activ e = YES; 96 [_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].activ e = YES;
104 [_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES; 97 [_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES;
105 [_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES; 98 [_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES;
106 } 99 }
107 } 100 }
108 101
109 #pragma mark - MTKViewDelegate methods 102 #pragma mark - MTKViewDelegate methods
110 103
111 - (void)drawInMTKView:(nonnull MTKView *)view { 104 - (void)drawInMTKView:(nonnull MTKView *)view {
112 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta nce."); 105 NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign insta nce.");
113 [self.renderer drawFrame:self.videoFrame]; 106 if (!self.videoFrame) {
107 return;
108 }
109
110 id<RTCMTLRenderer> renderer = nil;
111 if (self.videoFrame.nativeHandle) {
112 if (!self.rendererNV12) {
113 self.rendererNV12 = [RTCMTLVideoView createNV12Renderer];
114 if (![self.rendererNV12 addRenderingDestination:self.metalView]) {
115 self.rendererNV12 = nil;
116 RTCLogError(@"Failed to create NV12 renderer");
117 }
118 }
119 renderer = self.rendererNV12;
120 } else {
121 if (!self.rendererI420) {
122 self.rendererI420 = [RTCMTLVideoView createI420Renderer];
123 if (![self.rendererI420 addRenderingDestination:self.metalView]) {
124 self.rendererI420 = nil;
125 RTCLogError(@"Failed to create I420 renderer");
126 }
127 }
128 renderer = self.rendererI420;
129 }
130
131 [renderer drawFrame:self.videoFrame];
114 } 132 }
115 133
116 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { 134 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
117 } 135 }
118 136
119 #pragma mark - RTCVideoRenderer 137 #pragma mark - RTCVideoRenderer
120 138
121 - (void)setSize:(CGSize)size { 139 - (void)setSize:(CGSize)size {
122 self.metalView.drawableSize = size; 140 self.metalView.drawableSize = size;
123 [self.metalView draw]; 141 [self.metalView draw];
124 } 142 }
125 143
126 - (void)renderFrame:(nullable RTCVideoFrame *)frame { 144 - (void)renderFrame:(nullable RTCVideoFrame *)frame {
127 if (frame == nil) { 145 if (frame == nil) {
128 RTCLogInfo(@"Incoming frame is nil. Exiting render callback."); 146 RTCLogInfo(@"Incoming frame is nil. Exiting render callback.");
129 return; 147 return;
130 } 148 }
131 self.videoFrame = frame; 149 self.videoFrame = frame;
132 } 150 }
133 151
134 @end 152 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698