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 | |
magjed | bf08452 | 2017-01-13 07:10:16 -0800 | [diff] [blame] | 11 | #import <Foundation/Foundation.h> |
| 12 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 13 | #if !TARGET_OS_IPHONE |
| 14 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 15 | #import "RTCNSGLVideoView.h" |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 16 | |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 17 | #import <AppKit/NSOpenGL.h> |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 18 | #import <CoreVideo/CVDisplayLink.h> |
| 19 | #import <OpenGL/gl3.h> |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 20 | |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 21 | #import "RTCDefaultShader.h" |
| 22 | #import "RTCI420TextureCache.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 23 | #import "base/RTCLogging.h" |
| 24 | #import "base/RTCVideoFrame.h" |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 25 | |
| 26 | @interface RTCNSGLVideoView () |
| 27 | // |videoFrame| is set when we receive a frame from a worker thread and is read |
| 28 | // from the display link callback so atomicity is required. |
| 29 | @property(atomic, strong) RTCVideoFrame *videoFrame; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 30 | @property(atomic, strong) RTCI420TextureCache *i420TextureCache; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 31 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 32 | - (void)drawFrame; |
| 33 | @end |
| 34 | |
| 35 | static CVReturn OnDisplayLinkFired(CVDisplayLinkRef displayLink, |
| 36 | const CVTimeStamp *now, |
| 37 | const CVTimeStamp *outputTime, |
| 38 | CVOptionFlags flagsIn, |
| 39 | CVOptionFlags *flagsOut, |
| 40 | void *displayLinkContext) { |
| 41 | RTCNSGLVideoView *view = (__bridge RTCNSGLVideoView *)displayLinkContext; |
| 42 | [view drawFrame]; |
| 43 | return kCVReturnSuccess; |
| 44 | } |
| 45 | |
| 46 | @implementation RTCNSGLVideoView { |
| 47 | CVDisplayLinkRef _displayLink; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 48 | RTCVideoFrame *_lastDrawnFrame; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 49 | id<RTCVideoViewShading> _shader; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @synthesize delegate = _delegate; |
| 53 | @synthesize videoFrame = _videoFrame; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 54 | @synthesize i420TextureCache = _i420TextureCache; |
| 55 | |
| 56 | - (instancetype)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)format { |
| 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 | } |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 68 | |
| 69 | - (void)dealloc { |
| 70 | [self teardownDisplayLink]; |
| 71 | } |
| 72 | |
| 73 | - (void)drawRect:(NSRect)rect { |
| 74 | [self drawFrame]; |
| 75 | } |
| 76 | |
| 77 | - (void)reshape { |
| 78 | [super reshape]; |
| 79 | NSRect frame = [self frame]; |
adam.fedor | fee994c | 2017-06-09 05:16:10 -0700 | [diff] [blame] | 80 | [self ensureGLContext]; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 81 | CGLLockContext([[self openGLContext] CGLContextObj]); |
| 82 | glViewport(0, 0, frame.size.width, frame.size.height); |
| 83 | CGLUnlockContext([[self openGLContext] CGLContextObj]); |
| 84 | } |
| 85 | |
| 86 | - (void)lockFocus { |
| 87 | NSOpenGLContext *context = [self openGLContext]; |
| 88 | [super lockFocus]; |
| 89 | if ([context view] != self) { |
| 90 | [context setView:self]; |
| 91 | } |
| 92 | [context makeCurrentContext]; |
| 93 | } |
| 94 | |
| 95 | - (void)prepareOpenGL { |
| 96 | [super prepareOpenGL]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 97 | [self ensureGLContext]; |
| 98 | glDisable(GL_DITHER); |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 99 | [self setupDisplayLink]; |
| 100 | } |
| 101 | |
| 102 | - (void)clearGLContext { |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 103 | [self ensureGLContext]; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 104 | self.i420TextureCache = nil; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 105 | [super clearGLContext]; |
| 106 | } |
| 107 | |
| 108 | #pragma mark - RTCVideoRenderer |
| 109 | |
| 110 | // These methods may be called on non-main thread. |
| 111 | - (void)setSize:(CGSize)size { |
| 112 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 113 | [self.delegate videoView:self didChangeVideoSize:size]; |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | - (void)renderFrame:(RTCVideoFrame *)frame { |
| 118 | self.videoFrame = frame; |
| 119 | } |
| 120 | |
| 121 | #pragma mark - Private |
| 122 | |
| 123 | - (void)drawFrame { |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 124 | RTCVideoFrame *frame = self.videoFrame; |
| 125 | if (!frame || frame == _lastDrawnFrame) { |
| 126 | return; |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 127 | } |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 128 | // This method may be called from CVDisplayLink callback which isn't on the |
| 129 | // main thread so we have to lock the GL context before drawing. |
| 130 | NSOpenGLContext *context = [self openGLContext]; |
| 131 | CGLLockContext([context CGLContextObj]); |
| 132 | |
| 133 | [self ensureGLContext]; |
| 134 | glClear(GL_COLOR_BUFFER_BIT); |
| 135 | |
| 136 | // Rendering native CVPixelBuffer is not supported on OS X. |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 137 | // TODO(magjed): Add support for NV12 texture cache on OS X. |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 138 | frame = [frame newI420VideoFrame]; |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 139 | if (!self.i420TextureCache) { |
| 140 | self.i420TextureCache = [[RTCI420TextureCache alloc] initWithContext:context]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 141 | } |
magjed | 1394191 | 2017-05-30 06:11:58 -0700 | [diff] [blame] | 142 | RTCI420TextureCache *i420TextureCache = self.i420TextureCache; |
| 143 | if (i420TextureCache) { |
| 144 | [i420TextureCache uploadFrameToTextures:frame]; |
Magnus Jedvert | 6b9653e | 2017-06-05 17:58:34 +0200 | [diff] [blame] | 145 | [_shader applyShadingForFrameWithWidth:frame.width |
| 146 | height:frame.height |
| 147 | rotation:frame.rotation |
| 148 | yPlane:i420TextureCache.yTexture |
| 149 | uPlane:i420TextureCache.uTexture |
| 150 | vPlane:i420TextureCache.vTexture]; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 151 | [context flushBuffer]; |
| 152 | _lastDrawnFrame = frame; |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 153 | } |
| 154 | CGLUnlockContext([context CGLContextObj]); |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | - (void)setupDisplayLink { |
| 158 | if (_displayLink) { |
| 159 | return; |
| 160 | } |
| 161 | // Synchronize buffer swaps with vertical refresh rate. |
| 162 | GLint swapInt = 1; |
| 163 | [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
| 164 | |
| 165 | // Create display link. |
| 166 | CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink); |
| 167 | CVDisplayLinkSetOutputCallback(_displayLink, |
| 168 | &OnDisplayLinkFired, |
| 169 | (__bridge void *)self); |
| 170 | // Set the display link for the current renderer. |
| 171 | CGLContextObj cglContext = [[self openGLContext] CGLContextObj]; |
| 172 | CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj]; |
| 173 | CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext( |
| 174 | _displayLink, cglContext, cglPixelFormat); |
| 175 | CVDisplayLinkStart(_displayLink); |
| 176 | } |
| 177 | |
| 178 | - (void)teardownDisplayLink { |
| 179 | if (!_displayLink) { |
| 180 | return; |
| 181 | } |
| 182 | CVDisplayLinkRelease(_displayLink); |
| 183 | _displayLink = NULL; |
| 184 | } |
| 185 | |
magjed | 2f7f9b8 | 2017-04-13 04:15:53 -0700 | [diff] [blame] | 186 | - (void)ensureGLContext { |
| 187 | NSOpenGLContext* context = [self openGLContext]; |
| 188 | NSAssert(context, @"context shouldn't be nil"); |
| 189 | if ([NSOpenGLContext currentContext] != context) { |
| 190 | [context makeCurrentContext]; |
| 191 | } |
| 192 | } |
| 193 | |
Jon Hjelle | e799bad | 2016-01-11 13:47:11 -0800 | [diff] [blame] | 194 | @end |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 195 | |
| 196 | #endif // !TARGET_OS_IPHONE |