OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 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 <Foundation/Foundation.h> | 11 #import <Foundation/Foundation.h> |
12 | 12 |
13 #if !TARGET_OS_IPHONE | 13 #if !TARGET_OS_IPHONE |
14 | 14 |
15 #import "WebRTC/RTCNSGLVideoView.h" | 15 #import "WebRTC/RTCNSGLVideoView.h" |
16 | 16 |
17 #import <AppKit/NSOpenGL.h> | 17 #import <AppKit/NSOpenGL.h> |
18 #import <CoreVideo/CVDisplayLink.h> | 18 #import <CoreVideo/CVDisplayLink.h> |
19 #import <OpenGL/gl3.h> | 19 #import <OpenGL/gl3.h> |
20 | 20 |
21 #import "RTCShader+Private.h" | 21 #import "RTCDefaultShader.h" |
| 22 #import "RTCI420TextureCache.h" |
22 #import "WebRTC/RTCLogging.h" | 23 #import "WebRTC/RTCLogging.h" |
23 #import "WebRTC/RTCVideoFrame.h" | 24 #import "WebRTC/RTCVideoFrame.h" |
24 | 25 |
25 @interface RTCNSGLVideoView () | 26 @interface RTCNSGLVideoView () |
26 // |videoFrame| is set when we receive a frame from a worker thread and is read | 27 // |videoFrame| is set when we receive a frame from a worker thread and is read |
27 // from the display link callback so atomicity is required. | 28 // from the display link callback so atomicity is required. |
28 @property(atomic, strong) RTCVideoFrame *videoFrame; | 29 @property(atomic, strong) RTCVideoFrame *videoFrame; |
29 @property(atomic, strong) id<RTCShader> i420Shader; | 30 @property(atomic, strong) RTCI420TextureCache *i420TextureCache; |
30 | 31 |
31 - (void)drawFrame; | 32 - (void)drawFrame; |
32 @end | 33 @end |
33 | 34 |
34 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, | 35 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
35 const CVTimeStamp *now, | 36 const CVTimeStamp *now, |
36 const CVTimeStamp *outputTime, | 37 const CVTimeStamp *outputTime, |
37 CVOptionFlags flagsIn, | 38 CVOptionFlags flagsIn, |
38 CVOptionFlags *flagsOut, | 39 CVOptionFlags *flagsOut, |
39 void *displayLinkContext) { | 40 void *displayLinkContext) { |
40 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; | 41 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; |
41 [view drawFrame]; | 42 [view drawFrame]; |
42 return kCVReturnSuccess; | 43 return kCVReturnSuccess; |
43 } | 44 } |
44 | 45 |
45 @implementation RTCNSGLVideoView { | 46 @implementation RTCNSGLVideoView { |
46 CVDisplayLinkRef _displayLink; | 47 CVDisplayLinkRef _displayLink; |
47 RTCVideoFrame *_lastDrawnFrame; | 48 RTCVideoFrame *_lastDrawnFrame; |
| 49 id<RTCVideoViewShading> _shader; |
48 } | 50 } |
49 | 51 |
50 @synthesize delegate = _delegate; | 52 @synthesize delegate = _delegate; |
51 @synthesize videoFrame = _videoFrame; | 53 @synthesize videoFrame = _videoFrame; |
52 @synthesize i420Shader = _i420Shader; | 54 @synthesize i420TextureCache = _i420TextureCache; |
| 55 |
| 56 - (instancetype)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)f
ormat { |
| 57 return [self initWithFrame:frame pixelFormat:format shader:[[RTCDefaultShader
alloc] init]]; |
| 58 } |
| 59 |
| 60 - (instancetype)initWithFrame:(NSRect)frame |
| 61 pixelFormat:(NSOpenGLPixelFormat *)format |
| 62 shader:(id<RTCVideoViewShading>)shader { |
| 63 if (self = [super initWithFrame:frame pixelFormat:format]) { |
| 64 _shader = shader; |
| 65 } |
| 66 return self; |
| 67 } |
53 | 68 |
54 - (void)dealloc { | 69 - (void)dealloc { |
55 [self teardownDisplayLink]; | 70 [self teardownDisplayLink]; |
56 } | 71 } |
57 | 72 |
58 - (void)drawRect:(NSRect)rect { | 73 - (void)drawRect:(NSRect)rect { |
59 [self drawFrame]; | 74 [self drawFrame]; |
60 } | 75 } |
61 | 76 |
62 - (void)reshape { | 77 - (void)reshape { |
(...skipping 15 matching lines...) Expand all Loading... |
78 | 93 |
79 - (void)prepareOpenGL { | 94 - (void)prepareOpenGL { |
80 [super prepareOpenGL]; | 95 [super prepareOpenGL]; |
81 [self ensureGLContext]; | 96 [self ensureGLContext]; |
82 glDisable(GL_DITHER); | 97 glDisable(GL_DITHER); |
83 [self setupDisplayLink]; | 98 [self setupDisplayLink]; |
84 } | 99 } |
85 | 100 |
86 - (void)clearGLContext { | 101 - (void)clearGLContext { |
87 [self ensureGLContext]; | 102 [self ensureGLContext]; |
88 self.i420Shader = nil; | 103 self.i420TextureCache = nil; |
89 [super clearGLContext]; | 104 [super clearGLContext]; |
90 } | 105 } |
91 | 106 |
92 #pragma mark - RTCVideoRenderer | 107 #pragma mark - RTCVideoRenderer |
93 | 108 |
94 // These methods may be called on non-main thread. | 109 // These methods may be called on non-main thread. |
95 - (void)setSize:(CGSize)size { | 110 - (void)setSize:(CGSize)size { |
96 dispatch_async(dispatch_get_main_queue(), ^{ | 111 dispatch_async(dispatch_get_main_queue(), ^{ |
97 [self.delegate videoView:self didChangeVideoSize:size]; | 112 [self.delegate videoView:self didChangeVideoSize:size]; |
98 }); | 113 }); |
(...skipping 12 matching lines...) Expand all Loading... |
111 } | 126 } |
112 // This method may be called from CVDisplayLink callback which isn't on the | 127 // This method may be called from CVDisplayLink callback which isn't on the |
113 // main thread so we have to lock the GL context before drawing. | 128 // main thread so we have to lock the GL context before drawing. |
114 NSOpenGLContext *context = [self openGLContext]; | 129 NSOpenGLContext *context = [self openGLContext]; |
115 CGLLockContext([context CGLContextObj]); | 130 CGLLockContext([context CGLContextObj]); |
116 | 131 |
117 [self ensureGLContext]; | 132 [self ensureGLContext]; |
118 glClear(GL_COLOR_BUFFER_BIT); | 133 glClear(GL_COLOR_BUFFER_BIT); |
119 | 134 |
120 // Rendering native CVPixelBuffer is not supported on OS X. | 135 // Rendering native CVPixelBuffer is not supported on OS X. |
| 136 // TODO(magjed): Add support for NV12 texture cache on OS X. |
121 frame = [frame newI420VideoFrame]; | 137 frame = [frame newI420VideoFrame]; |
122 if (!self.i420Shader) { | 138 if (!self.i420TextureCache) { |
123 self.i420Shader = [[RTCI420Shader alloc] initWithContext:context]; | 139 self.i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:context
]; |
124 } | 140 } |
125 if (self.i420Shader && [self.i420Shader drawFrame:frame]) { | 141 RTCI420TextureCache *i420TextureCache = self.i420TextureCache; |
| 142 if (i420TextureCache) { |
| 143 [i420TextureCache uploadFrameToTextures:frame]; |
| 144 [_shader applyShadingForFrameWithRotation:frame.rotation |
| 145 yPlane:i420TextureCache.yTexture |
| 146 uPlane:i420TextureCache.uTexture |
| 147 vPlane:i420TextureCache.vTexture]; |
126 [context flushBuffer]; | 148 [context flushBuffer]; |
127 _lastDrawnFrame = frame; | 149 _lastDrawnFrame = frame; |
128 } else { | |
129 RTCLog(@"Failed to draw frame."); | |
130 } | 150 } |
131 CGLUnlockContext([context CGLContextObj]); | 151 CGLUnlockContext([context CGLContextObj]); |
132 } | 152 } |
133 | 153 |
134 - (void)setupDisplayLink { | 154 - (void)setupDisplayLink { |
135 if (_displayLink) { | 155 if (_displayLink) { |
136 return; | 156 return; |
137 } | 157 } |
138 // Synchronize buffer swaps with vertical refresh rate. | 158 // Synchronize buffer swaps with vertical refresh rate. |
139 GLint swapInt = 1; | 159 GLint swapInt = 1; |
(...skipping 24 matching lines...) Expand all Loading... |
164 NSOpenGLContext* context = [self openGLContext]; | 184 NSOpenGLContext* context = [self openGLContext]; |
165 NSAssert(context, @"context shouldn't be nil"); | 185 NSAssert(context, @"context shouldn't be nil"); |
166 if ([NSOpenGLContext currentContext] != context) { | 186 if ([NSOpenGLContext currentContext] != context) { |
167 [context makeCurrentContext]; | 187 [context makeCurrentContext]; |
168 } | 188 } |
169 } | 189 } |
170 | 190 |
171 @end | 191 @end |
172 | 192 |
173 #endif // !TARGET_OS_IPHONE | 193 #endif // !TARGET_OS_IPHONE |
OLD | NEW |