denicija 070d5e30a8 Reland of Add metal view, shaders and renderer. (patchset #1 id:1 of https://codereview.webrtc.org/2711003004/ )
Reason for revert:
Re-land

Original issue's description:
> Revert of Add metal view, shaders and renderer. (patchset #18 id:340001 of https://codereview.webrtc.org/2651743007/ )
>
> Reason for revert:
> Reverting due to breakage in the Google3 import
>
> Original issue's description:
> > Add metal view, shaders and renderer.
> >
> > This CL submits standalone Metal view, renderer and shader.
> >
> > BUG=webrtc:7079
> >
> > Review-Url: https://codereview.webrtc.org/2651743007
> > Cr-Commit-Position: refs/heads/master@{#16787}
> > Committed: fc8c97f950
>
> TBR=magjed@webrtc.org,kthelgason@webrtc.org,tkchin@webrtc.org,haysc@webrtc.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=webrtc:7079
>
> Review-Url: https://codereview.webrtc.org/2711003004
> Cr-Commit-Position: refs/heads/master@{#16788}
> Committed: b681aabdfc

TBR=magjed@webrtc.org,kthelgason@webrtc.org,tkchin@webrtc.org,haysc@webrtc.org
# Not skipping CQ checks because original CL landed more than 1 days ago.
BUG=webrtc:7079

Review-Url: https://codereview.webrtc.org/2716703004
Cr-Commit-Position: refs/heads/master@{#16846}
2017-02-26 19:44:13 +00:00

108 lines
2.9 KiB
Objective-C

/*
* Copyright 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "WebRTC/RTCMTLVideoView.h"
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
#import "WebRTC/RTCLogging.h"
#import "WebRTC/RTCVideoFrame.h"
#import "RTCMTLNV12Renderer.h"
@interface RTCMTLVideoView () <MTKViewDelegate>
@property(nonatomic, strong) id<RTCMTLRenderer> renderer;
@property(nonatomic, strong) MTKView *metalView;
@property(atomic, strong) RTCVideoFrame *videoFrame;
@end
@implementation RTCMTLVideoView {
id<RTCMTLRenderer> _renderer;
}
@synthesize renderer = _renderer;
@synthesize metalView = _metalView;
@synthesize videoFrame = _videoFrame;
- (instancetype)initWithFrame:(CGRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
[self configure];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aCoder {
self = [super initWithCoder:aCoder];
if (self) {
[self configure];
}
return self;
}
#pragma mark - Private
+ (BOOL)isMetalAvailable {
#if defined(__OBJC__) && COREVIDEO_SUPPORTS_METAL
return YES;
#elif
return NO;
#endif
}
- (void)configure {
if ([RTCMTLVideoView isMetalAvailable]) {
_metalView = [[MTKView alloc] initWithFrame:self.bounds];
[self addSubview:_metalView];
_metalView.delegate = self;
_metalView.contentMode = UIViewContentModeScaleAspectFit;
_metalView.translatesAutoresizingMaskIntoConstraints = NO;
UILayoutGuide *margins = self.layoutMarginsGuide;
[_metalView.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YES;
[_metalView.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].active = YES;
[_metalView.leftAnchor constraintEqualToAnchor:margins.leftAnchor].active = YES;
[_metalView.rightAnchor constraintEqualToAnchor:margins.rightAnchor].active = YES;
_renderer = [[RTCMTLNV12Renderer alloc] init];
if (![(RTCMTLNV12Renderer *)_renderer addRenderingDestination:_metalView]) {
_renderer = nil;
};
} else {
RTCLogError("Metal configuration falied.");
}
}
#pragma mark - MTKViewDelegate methods
- (void)drawInMTKView:(nonnull MTKView *)view {
NSAssert(view == self.metalView, @"Receiving draw callbacks from foreign instance.");
[_renderer drawFrame:self.videoFrame];
}
- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {
}
#pragma mark - RTCVideoRenderer
- (void)setSize:(CGSize)size {
_metalView.drawableSize = size;
[_metalView draw];
}
- (void)renderFrame:(nullable RTCVideoFrame *)frame {
if (frame == nil) {
RTCLogInfo(@"Incoming frame is nil. Exiting render callback.");
return;
}
self.videoFrame = frame;
}
@end