Chromium Code Reviews| Index: webrtc/sdk/objc/Framework/Classes/RTCNV12TextureCache.m |
| diff --git a/webrtc/sdk/objc/Framework/Classes/RTCNV12TextureCache.m b/webrtc/sdk/objc/Framework/Classes/RTCNV12TextureCache.m |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d613c8b211c5bbcd3da037424a5d7e007b87244b |
| --- /dev/null |
| +++ b/webrtc/sdk/objc/Framework/Classes/RTCNV12TextureCache.m |
| @@ -0,0 +1,116 @@ |
| +/* |
| + * Copyright 2017 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 "RTCNV12TextureCache.h" |
| + |
| +#import "RTCShader+Private.h" |
| + |
| +@implementation RTCNV12TextureCache { |
| + CVOpenGLESTextureCacheRef _textureCache; |
| + CVOpenGLESTextureRef _yTexture; |
| + CVOpenGLESTextureRef _uvTexture; |
| +} |
| + |
| +- (GLuint)yTexture { |
| + return CVOpenGLESTextureGetName(_yTexture); |
| +} |
| + |
| +- (GLuint)uvTexture { |
| + return CVOpenGLESTextureGetName(_uvTexture); |
| +} |
| + |
| +- (instancetype)initWithContext:(EAGLContext *)context { |
| + if (self = [super init]) { |
| + CVReturn ret = CVOpenGLESTextureCacheCreate( |
| + kCFAllocatorDefault, NULL, |
| +#if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API |
| + context, |
| +#else |
| + (__bridge void *)context, |
| +#endif |
| + NULL, &_textureCache); |
| + if (ret != kCVReturnSuccess) { |
| + self = nil; |
| + } |
| + } |
| + return self; |
| +} |
| + |
| +- (BOOL)uploadFrameToTextures:(RTCVideoFrame *)frame { |
| + CVPixelBufferRef pixelBuffer = frame.nativeHandle; |
| + NSParameterAssert(pixelBuffer); |
| + |
| + // Y-plane. |
| + const int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); |
|
sakal
2017/04/26 09:11:58
nit: Can you make 0 and 1 here constants.
magjed_webrtc
2017/04/26 13:14:41
Done (it's a named argument to a function now).
|
| + const int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); |
| + |
| + _yTexture = nil; |
| + CVReturn ret = CVOpenGLESTextureCacheCreateTextureFromImage( |
| + kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, |
| + RTC_PIXEL_FORMAT, lumaWidth, lumaHeight, RTC_PIXEL_FORMAT, |
| + GL_UNSIGNED_BYTE, 0, &_yTexture); |
| + if (ret != kCVReturnSuccess) { |
| + CFRelease(_yTexture); |
| + return NO; |
| + } |
| + NSAssert(CVOpenGLESTextureGetTarget(_yTexture) == GL_TEXTURE_2D, |
| + @"Unexpected GLES texture target"); |
| + glBindTexture(GL_TEXTURE_2D, self.yTexture); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| + |
| + // UV-plane. |
| + const int chromaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); |
| + const int chromeHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); |
| + |
| + _uvTexture = nil; |
| + ret = CVOpenGLESTextureCacheCreateTextureFromImage( |
| + kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, |
| + GL_LUMINANCE_ALPHA, chromaWidth, chromeHeight, GL_LUMINANCE_ALPHA, |
| + GL_UNSIGNED_BYTE, 1, &_uvTexture); |
| + if (ret != kCVReturnSuccess) { |
| + CFRelease(_uvTexture); |
| + CFRelease(_yTexture); |
| + return NO; |
| + } |
| + NSAssert(CVOpenGLESTextureGetTarget(_uvTexture) == GL_TEXTURE_2D, |
| + @"Unexpected GLES texture target"); |
| + glBindTexture(GL_TEXTURE_2D, self.uvTexture); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| + |
| + return YES; |
| +} |
| + |
| +- (void)releaseTextures { |
| + if (_uvTexture) { |
| + CFRelease(_uvTexture); |
| + _uvTexture = nil; |
| + } |
| + if (_yTexture) { |
| + CFRelease(_yTexture); |
| + _yTexture = nil; |
| + } |
| +} |
| + |
| +- (void)dealloc { |
| + [self releaseTextures]; |
| + if (_textureCache) { |
| + CFRelease(_textureCache); |
| + _textureCache = nil; |
| + } |
| +} |
| + |
| +@end |
| + |