Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/RTCNSGLVideoView.m

Issue 2812613003: ObjC: Remove RTCOpenGLVideoRenderer (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <CoreVideo/CVDisplayLink.h> 18 #import <CoreVideo/CVDisplayLink.h>
18 #import <OpenGL/gl3.h> 19 #import <OpenGL/gl3.h>
19 20
20 #import "RTCOpenGLVideoRenderer.h" 21 #import "RTCShader+Private.h"
21 #import "WebRTC/RTCVideoFrame.h" 22 #import "WebRTC/RTCVideoFrame.h"
22 23
23 @interface RTCNSGLVideoView () 24 @interface RTCNSGLVideoView ()
24 // |videoFrame| is set when we receive a frame from a worker thread and is read 25 // |videoFrame| is set when we receive a frame from a worker thread and is read
25 // from the display link callback so atomicity is required. 26 // from the display link callback so atomicity is required.
26 @property(atomic, strong) RTCVideoFrame *videoFrame; 27 @property(atomic, strong) RTCVideoFrame *videoFrame;
27 @property(atomic, strong) RTCOpenGLVideoRenderer *glRenderer;
28 - (void)drawFrame; 28 - (void)drawFrame;
29 @end 29 @end
30 30
31 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, 31 static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink,
32 const CVTimeStamp *now, 32 const CVTimeStamp *now,
33 const CVTimeStamp *outputTime, 33 const CVTimeStamp *outputTime,
34 CVOptionFlags flagsIn, 34 CVOptionFlags flagsIn,
35 CVOptionFlags *flagsOut, 35 CVOptionFlags *flagsOut,
36 void *displayLinkContext) { 36 void *displayLinkContext) {
37 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; 37 RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext;
38 [view drawFrame]; 38 [view drawFrame];
39 return kCVReturnSuccess; 39 return kCVReturnSuccess;
40 } 40 }
41 41
42 @implementation RTCNSGLVideoView { 42 @implementation RTCNSGLVideoView {
43 CVDisplayLinkRef _displayLink; 43 CVDisplayLinkRef _displayLink;
44 id<RTCShader> _i420Shader;
45 RTCVideoFrame *_lastDrawnFrame;
44 } 46 }
45 47
46 @synthesize delegate = _delegate; 48 @synthesize delegate = _delegate;
47 @synthesize videoFrame = _videoFrame; 49 @synthesize videoFrame = _videoFrame;
48 @synthesize glRenderer = _glRenderer;
49 50
50 - (void)dealloc { 51 - (void)dealloc {
51 [self teardownDisplayLink]; 52 [self teardownDisplayLink];
52 } 53 }
53 54
54 - (void)drawRect:(NSRect)rect { 55 - (void)drawRect:(NSRect)rect {
55 [self drawFrame]; 56 [self drawFrame];
56 } 57 }
57 58
58 - (void)reshape { 59 - (void)reshape {
59 [super reshape]; 60 [super reshape];
60 NSRect frame = [self frame]; 61 NSRect frame = [self frame];
61 CGLLockContext([[self openGLContext] CGLContextObj]); 62 CGLLockContext([[self openGLContext] CGLContextObj]);
62 glViewport(0, 0, frame.size.width, frame.size.height); 63 glViewport(0, 0, frame.size.width, frame.size.height);
63 CGLUnlockContext([[self openGLContext] CGLContextObj]); 64 CGLUnlockContext([[self openGLContext] CGLContextObj]);
64 } 65 }
65 66
66 - (void)lockFocus { 67 - (void)lockFocus {
67 NSOpenGLContext *context = [self openGLContext]; 68 NSOpenGLContext *context = [self openGLContext];
68 [super lockFocus]; 69 [super lockFocus];
69 if ([context view] != self) { 70 if ([context view] != self) {
70 [context setView:self]; 71 [context setView:self];
71 } 72 }
72 [context makeCurrentContext]; 73 [context makeCurrentContext];
73 } 74 }
74 75
75 - (void)prepareOpenGL { 76 - (void)prepareOpenGL {
76 [super prepareOpenGL]; 77 [super prepareOpenGL];
77 if (!self.glRenderer) { 78 [self ensureGLContext];
78 self.glRenderer =
79 [[RTCOpenGLVideoRenderer alloc] initWithContext:[self openGLContext]];
80 }
81 [self.glRenderer setupGL];
82 [self setupDisplayLink]; 79 [self setupDisplayLink];
83 } 80 }
84 81
85 - (void)clearGLContext { 82 - (void)clearGLContext {
86 [self.glRenderer teardownGL]; 83 [self ensureGLContext];
87 self.glRenderer = nil; 84 _i420Shader = nil;
88 [super clearGLContext]; 85 [super clearGLContext];
89 } 86 }
90 87
91 #pragma mark - RTCVideoRenderer 88 #pragma mark - RTCVideoRenderer
92 89
93 // These methods may be called on non-main thread. 90 // These methods may be called on non-main thread.
94 - (void)setSize:(CGSize)size { 91 - (void)setSize:(CGSize)size {
95 dispatch_async(dispatch_get_main_queue(), ^{ 92 dispatch_async(dispatch_get_main_queue(), ^{
96 [self.delegate videoView:self didChangeVideoSize:size]; 93 [self.delegate videoView:self didChangeVideoSize:size];
97 }); 94 });
98 } 95 }
99 96
100 - (void)renderFrame:(RTCVideoFrame *)frame { 97 - (void)renderFrame:(RTCVideoFrame *)frame {
101 self.videoFrame = frame; 98 self.videoFrame = frame;
102 } 99 }
103 100
104 #pragma mark - Private 101 #pragma mark - Private
105 102
106 - (void)drawFrame { 103 - (void)drawFrame {
107 RTCVideoFrame *videoFrame = self.videoFrame; 104 RTCVideoFrame *frame = self.videoFrame;
108 if (self.glRenderer.lastDrawnFrame != videoFrame) { 105 if (!frame || frame == _lastDrawnFrame) {
109 // This method may be called from CVDisplayLink callback which isn't on the 106 return;
110 // main thread so we have to lock the GL context before drawing.
111 CGLLockContext([[self openGLContext] CGLContextObj]);
112 [self.glRenderer drawFrame:videoFrame];
113 CGLUnlockContext([[self openGLContext] CGLContextObj]);
114 } 107 }
108 // This method may be called from CVDisplayLink callback which isn't on the
109 // main thread so we have to lock the GL context before drawing.
110 NSOpenGLContext *context = [self openGLContext];
111 CGLLockContext([context CGLContextObj]);
112
113 [self ensureGLContext];
114 glClear(GL_COLOR_BUFFER_BIT);
115
116 // Rendering native CVPixelBuffer is not supported on OS X.
117 frame = [frame newI420VideoFrame];
118 if (!_i420Shader) {
119 _i420Shader = [[RTCI420Shader alloc] initWithContext:context];
120 NSAssert(_i420Shader, @"couldn't create i420 shader");
121 }
122 [_i420Shader drawFrame:frame];
123 [context flushBuffer];
124 _lastDrawnFrame = frame;
125
126 CGLUnlockContext([context CGLContextObj]);
115 } 127 }
116 128
117 - (void)setupDisplayLink { 129 - (void)setupDisplayLink {
118 if (_displayLink) { 130 if (_displayLink) {
119 return; 131 return;
120 } 132 }
121 // Synchronize buffer swaps with vertical refresh rate. 133 // Synchronize buffer swaps with vertical refresh rate.
122 GLint swapInt = 1; 134 GLint swapInt = 1;
123 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; 135 [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
124 136
(...skipping 11 matching lines...) Expand all
136 } 148 }
137 149
138 - (void)teardownDisplayLink { 150 - (void)teardownDisplayLink {
139 if (!_displayLink) { 151 if (!_displayLink) {
140 return; 152 return;
141 } 153 }
142 CVDisplayLinkRelease(_displayLink); 154 CVDisplayLinkRelease(_displayLink);
143 _displayLink = NULL; 155 _displayLink = NULL;
144 } 156 }
145 157
158 - (void)ensureGLContext {
159 NSOpenGLContext* context = [self openGLContext];
160 NSAssert(context, @"context shouldn't be nil");
161 if ([NSOpenGLContext currentContext] != context) {
162 [context makeCurrentContext];
163 }
164 }
165
146 @end 166 @end
147 167
148 #endif // !TARGET_OS_IPHONE 168 #endif // !TARGET_OS_IPHONE
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698