Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "RTCOpenGLVideoRenderer.h" |
| 12 | |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 13 | #import "RTCShader+Private.h" |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 14 | #import "WebRTC/RTCVideoFrame.h" |
| 15 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 16 | @implementation RTCOpenGLVideoRenderer { |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 17 | GlContextType *_context; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 18 | BOOL _isInitialized; |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 19 | id<RTCShader> _i420Shader; |
| 20 | id<RTCShader> _nv12Shader; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | @synthesize lastDrawnFrame = _lastDrawnFrame; |
| 24 | |
| 25 | + (void)initialize { |
| 26 | // Disable dithering for performance. |
| 27 | glDisable(GL_DITHER); |
| 28 | } |
| 29 | |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 30 | - (instancetype)initWithContext:(GlContextType *)context { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 31 | NSAssert(context != nil, @"context cannot be nil"); |
| 32 | if (self = [super init]) { |
| 33 | _context = context; |
| 34 | } |
| 35 | return self; |
| 36 | } |
| 37 | |
| 38 | - (BOOL)drawFrame:(RTCVideoFrame *)frame { |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 39 | if (!_isInitialized || !frame || frame == _lastDrawnFrame) { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 40 | return NO; |
| 41 | } |
| 42 | [self ensureGLContext]; |
| 43 | glClear(GL_COLOR_BUFFER_BIT); |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 44 | id<RTCShader> shader = nil; |
| 45 | #if TARGET_OS_IPHONE |
| 46 | if (frame.nativeHandle) { |
| 47 | if (!_nv12Shader) { |
| 48 | _nv12Shader = [[RTCNativeNV12Shader alloc] initWithContext:_context]; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 49 | } |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 50 | shader = _nv12Shader; |
| 51 | #else |
| 52 | // Rendering native CVPixelBuffer is not supported on OS X. |
| 53 | if (false) { |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 54 | #endif |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 55 | } else { |
| 56 | if (!_i420Shader) { |
| 57 | _i420Shader = [[RTCI420Shader alloc] initWithContext:_context]; |
| 58 | } |
| 59 | shader = _i420Shader; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 60 | } |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 61 | if (!shader || ![shader drawFrame:frame]) { |
| 62 | return NO; |
| 63 | } |
| 64 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 65 | #if !TARGET_OS_IPHONE |
| 66 | [_context flushBuffer]; |
| 67 | #endif |
| 68 | _lastDrawnFrame = frame; |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 69 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 70 | return YES; |
| 71 | } |
| 72 | |
| 73 | - (void)setupGL { |
| 74 | if (_isInitialized) { |
| 75 | return; |
| 76 | } |
| 77 | [self ensureGLContext]; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 78 | _isInitialized = YES; |
| 79 | } |
| 80 | |
| 81 | - (void)teardownGL { |
| 82 | if (!_isInitialized) { |
| 83 | return; |
| 84 | } |
| 85 | [self ensureGLContext]; |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 86 | _i420Shader = nil; |
| 87 | _nv12Shader = nil; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 88 | _isInitialized = NO; |
| 89 | } |
| 90 | |
| 91 | #pragma mark - Private |
| 92 | |
| 93 | - (void)ensureGLContext { |
| 94 | NSAssert(_context, @"context shouldn't be nil"); |
| 95 | #if TARGET_OS_IPHONE |
| 96 | if ([EAGLContext currentContext] != _context) { |
| 97 | [EAGLContext setCurrentContext:_context]; |
| 98 | } |
| 99 | #else |
| 100 | if ([NSOpenGLContext currentContext] != _context) { |
| 101 | [_context makeCurrentContext]; |
| 102 | } |
| 103 | #endif |
| 104 | } |
| 105 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 106 | @end |