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