Files
platform-external-webrtc/webrtc/sdk/objc/Framework/Classes/RTCOpenGLVideoRenderer.mm
tkchin 04dbb34d51 iOS: Add support for rendering native CVPixelBuffers directly
This CL adds support in RTCEAGLVideoView for rendering CVPixelBuffers as
OpenGL ES textures directly, compared to the current code that first
converts the CVPixelBuffers to I420, and then reuploads them as
textures. This is only supported on iOS with the use of a
CVOpenGLESTextureCache.

The I420 rendering and native rendering are separated in two different
implementations of a simple shader interface:
@protocol Shader
- (BOOL)drawFrame:(RTCVideoFrame*)frame;
@end
GL resources are allocated when the shader is instantiated and released
when the shader is destroyed. RTCEAGLVideoView will lazily instantiate
the necessary shader when it receives the first frame of that kind. This
is primarily done to avoid allocating GL resources for both I420 and
native rendering.

Some other changes are:
 - Print GL shader compilation errors.
 - Remove updateTextureSizesForFrame() function. The textures will
   resize automatically anyway when the texture data is uploaded with
   glTexImage2D().

patch from issue 2154243002 at patchset 140001 (http://crrev.com/2154243002#ps140001)
Continuing magjed@'s work since he is OOO this week.

BUG=

Review-Url: https://codereview.webrtc.org/2202823004
Cr-Commit-Position: refs/heads/master@{#13668}
2016-08-08 10:10:12 +00:00

107 lines
2.3 KiB
Plaintext

/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCOpenGLVideoRenderer.h"
#import "RTCShader+Private.h"
#import "WebRTC/RTCVideoFrame.h"
@implementation RTCOpenGLVideoRenderer {
GlContextType *_context;
BOOL _isInitialized;
id<RTCShader> _i420Shader;
id<RTCShader> _nv12Shader;
}
@synthesize lastDrawnFrame = _lastDrawnFrame;
+ (void)initialize {
// Disable dithering for performance.
glDisable(GL_DITHER);
}
- (instancetype)initWithContext:(GlContextType *)context {
NSAssert(context != nil, @"context cannot be nil");
if (self = [super init]) {
_context = context;
}
return self;
}
- (BOOL)drawFrame:(RTCVideoFrame *)frame {
if (!_isInitialized || !frame || frame == _lastDrawnFrame) {
return NO;
}
[self ensureGLContext];
glClear(GL_COLOR_BUFFER_BIT);
id<RTCShader> shader = nil;
#if TARGET_OS_IPHONE
if (frame.nativeHandle) {
if (!_nv12Shader) {
_nv12Shader = [[RTCNativeNV12Shader alloc] initWithContext:_context];
}
shader = _nv12Shader;
#else
// Rendering native CVPixelBuffer is not supported on OS X.
if (false) {
#endif
} else {
if (!_i420Shader) {
_i420Shader = [[RTCI420Shader alloc] initWithContext:_context];
}
shader = _i420Shader;
}
if (!shader || ![shader drawFrame:frame]) {
return NO;
}
#if !TARGET_OS_IPHONE
[_context flushBuffer];
#endif
_lastDrawnFrame = frame;
return YES;
}
- (void)setupGL {
if (_isInitialized) {
return;
}
[self ensureGLContext];
_isInitialized = YES;
}
- (void)teardownGL {
if (!_isInitialized) {
return;
}
[self ensureGLContext];
_i420Shader = nil;
_nv12Shader = nil;
_isInitialized = NO;
}
#pragma mark - Private
- (void)ensureGLContext {
NSAssert(_context, @"context shouldn't be nil");
#if TARGET_OS_IPHONE
if ([EAGLContext currentContext] != _context) {
[EAGLContext setCurrentContext:_context];
}
#else
if ([NSOpenGLContext currentContext] != _context) {
[_context makeCurrentContext];
}
#endif
}
@end