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<RTCVideoViewShader> _shaderDelegate; |
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 |
| 58 [self initWithFrame:frame pixelFormat:format shaderDelegate:[[RTCDefaultSh
ader alloc] init]]; |
| 59 } |
| 60 |
| 61 - (instancetype)initWithFrame:(NSRect)frame |
| 62 pixelFormat:(NSOpenGLPixelFormat *)format |
| 63 shaderDelegate:(id<RTCVideoViewShader>)shaderDelegate { |
| 64 if (self = [super initWithFrame:frame pixelFormat:format]) { |
| 65 _shaderDelegate = shaderDelegate; |
| 66 } |
| 67 return self; |
| 68 } |
53 | 69 |
54 - (void)dealloc { | 70 - (void)dealloc { |
55 [self teardownDisplayLink]; | 71 [self teardownDisplayLink]; |
56 } | 72 } |
57 | 73 |
58 - (void)drawRect:(NSRect)rect { | 74 - (void)drawRect:(NSRect)rect { |
59 [self drawFrame]; | 75 [self drawFrame]; |
60 } | 76 } |
61 | 77 |
62 - (void)reshape { | 78 - (void)reshape { |
(...skipping 15 matching lines...) Expand all Loading... |
78 | 94 |
79 - (void)prepareOpenGL { | 95 - (void)prepareOpenGL { |
80 [super prepareOpenGL]; | 96 [super prepareOpenGL]; |
81 [self ensureGLContext]; | 97 [self ensureGLContext]; |
82 glDisable(GL_DITHER); | 98 glDisable(GL_DITHER); |
83 [self setupDisplayLink]; | 99 [self setupDisplayLink]; |
84 } | 100 } |
85 | 101 |
86 - (void)clearGLContext { | 102 - (void)clearGLContext { |
87 [self ensureGLContext]; | 103 [self ensureGLContext]; |
88 self.i420Shader = nil; | 104 self.i420TextureCache = nil; |
89 [super clearGLContext]; | 105 [super clearGLContext]; |
90 } | 106 } |
91 | 107 |
92 #pragma mark - RTCVideoRenderer | 108 #pragma mark - RTCVideoRenderer |
93 | 109 |
94 // These methods may be called on non-main thread. | 110 // These methods may be called on non-main thread. |
95 - (void)setSize:(CGSize)size { | 111 - (void)setSize:(CGSize)size { |
96 dispatch_async(dispatch_get_main_queue(), ^{ | 112 dispatch_async(dispatch_get_main_queue(), ^{ |
97 [self.delegate videoView:self didChangeVideoSize:size]; | 113 [self.delegate videoView:self didChangeVideoSize:size]; |
98 }); | 114 }); |
(...skipping 12 matching lines...) Expand all Loading... |
111 } | 127 } |
112 // This method may be called from CVDisplayLink callback which isn't on the | 128 // 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. | 129 // main thread so we have to lock the GL context before drawing. |
114 NSOpenGLContext *context = [self openGLContext]; | 130 NSOpenGLContext *context = [self openGLContext]; |
115 CGLLockContext([context CGLContextObj]); | 131 CGLLockContext([context CGLContextObj]); |
116 | 132 |
117 [self ensureGLContext]; | 133 [self ensureGLContext]; |
118 glClear(GL_COLOR_BUFFER_BIT); | 134 glClear(GL_COLOR_BUFFER_BIT); |
119 | 135 |
120 // Rendering native CVPixelBuffer is not supported on OS X. | 136 // Rendering native CVPixelBuffer is not supported on OS X. |
| 137 // TODO(magjed): Add support for NV12 texture cache on OS X. |
121 frame = [frame newI420VideoFrame]; | 138 frame = [frame newI420VideoFrame]; |
122 if (!self.i420Shader) { | 139 if (!self.i420TextureCache) { |
123 self.i420Shader = [[RTCI420Shader alloc] initWithContext:context]; | 140 self.i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:context
]; |
124 } | 141 } |
125 if (self.i420Shader && [self.i420Shader drawFrame:frame]) { | 142 RTCI420TextureCache *i420TextureCache = self.i420TextureCache; |
| 143 if (i420TextureCache) { |
| 144 [i420TextureCache uploadFrameToTextures:frame]; |
| 145 [_shaderDelegate applyShadingForFrameWithRotation:frame.rotation |
| 146 yPlane:i420TextureCache.yTexture |
| 147 uPlane:i420TextureCache.uTexture |
| 148 vPlane:i420TextureCache.vTexture]
; |
126 [context flushBuffer]; | 149 [context flushBuffer]; |
127 _lastDrawnFrame = frame; | 150 _lastDrawnFrame = frame; |
128 } else { | |
129 RTCLog(@"Failed to draw frame."); | |
130 } | 151 } |
131 CGLUnlockContext([context CGLContextObj]); | 152 CGLUnlockContext([context CGLContextObj]); |
132 } | 153 } |
133 | 154 |
134 - (void)setupDisplayLink { | 155 - (void)setupDisplayLink { |
135 if (_displayLink) { | 156 if (_displayLink) { |
136 return; | 157 return; |
137 } | 158 } |
138 // Synchronize buffer swaps with vertical refresh rate. | 159 // Synchronize buffer swaps with vertical refresh rate. |
139 GLint swapInt = 1; | 160 GLint swapInt = 1; |
(...skipping 24 matching lines...) Expand all Loading... |
164 NSOpenGLContext* context = [self openGLContext]; | 185 NSOpenGLContext* context = [self openGLContext]; |
165 NSAssert(context, @"context shouldn't be nil"); | 186 NSAssert(context, @"context shouldn't be nil"); |
166 if ([NSOpenGLContext currentContext] != context) { | 187 if ([NSOpenGLContext currentContext] != context) { |
167 [context makeCurrentContext]; | 188 [context makeCurrentContext]; |
168 } | 189 } |
169 } | 190 } |
170 | 191 |
171 @end | 192 @end |
172 | 193 |
173 #endif // !TARGET_OS_IPHONE | 194 #endif // !TARGET_OS_IPHONE |
OLD | NEW |