Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2017 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 | |
| 11 #import "RTCNV12TextureCache.h" | |
| 12 | |
| 13 #import "RTCShader+Private.h" | |
| 14 | |
| 15 @implementation RTCNV12TextureCache { | |
| 16 CVOpenGLESTextureCacheRef _textureCache; | |
| 17 CVOpenGLESTextureRef _yTexture; | |
| 18 CVOpenGLESTextureRef _uvTexture; | |
| 19 } | |
| 20 | |
| 21 - (GLuint)yTexture { | |
| 22 return CVOpenGLESTextureGetName(_yTexture); | |
| 23 } | |
| 24 | |
| 25 - (GLuint)uvTexture { | |
| 26 return CVOpenGLESTextureGetName(_uvTexture); | |
| 27 } | |
| 28 | |
| 29 - (instancetype)initWithContext:(EAGLContext *)context { | |
| 30 if (self = [super init]) { | |
| 31 CVReturn ret = CVOpenGLESTextureCacheCreate( | |
| 32 kCFAllocatorDefault, NULL, | |
| 33 #if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API | |
| 34 context, | |
| 35 #else | |
| 36 (__bridge void *)context, | |
| 37 #endif | |
| 38 NULL, &_textureCache); | |
| 39 if (ret != kCVReturnSuccess) { | |
| 40 self = nil; | |
| 41 } | |
| 42 } | |
| 43 return self; | |
| 44 } | |
| 45 | |
| 46 - (BOOL)uploadFrameToTextures:(RTCVideoFrame *)frame { | |
| 47 CVPixelBufferRef pixelBuffer = frame.nativeHandle; | |
| 48 NSParameterAssert(pixelBuffer); | |
| 49 | |
| 50 // Y-plane. | |
| 51 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).
| |
| 52 const int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); | |
| 53 | |
| 54 _yTexture = nil; | |
| 55 CVReturn ret = CVOpenGLESTextureCacheCreateTextureFromImage( | |
| 56 kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, | |
| 57 RTC_PIXEL_FORMAT, lumaWidth, lumaHeight, RTC_PIXEL_FORMAT, | |
| 58 GL_UNSIGNED_BYTE, 0, &_yTexture); | |
| 59 if (ret != kCVReturnSuccess) { | |
| 60 CFRelease(_yTexture); | |
| 61 return NO; | |
| 62 } | |
| 63 NSAssert(CVOpenGLESTextureGetTarget(_yTexture) == GL_TEXTURE_2D, | |
| 64 @"Unexpected GLES texture target"); | |
| 65 glBindTexture(GL_TEXTURE_2D, self.yTexture); | |
| 66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 70 | |
| 71 // UV-plane. | |
| 72 const int chromaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); | |
| 73 const int chromeHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); | |
| 74 | |
| 75 _uvTexture = nil; | |
| 76 ret = CVOpenGLESTextureCacheCreateTextureFromImage( | |
| 77 kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, | |
| 78 GL_LUMINANCE_ALPHA, chromaWidth, chromeHeight, GL_LUMINANCE_ALPHA, | |
| 79 GL_UNSIGNED_BYTE, 1, &_uvTexture); | |
| 80 if (ret != kCVReturnSuccess) { | |
| 81 CFRelease(_uvTexture); | |
| 82 CFRelease(_yTexture); | |
| 83 return NO; | |
| 84 } | |
| 85 NSAssert(CVOpenGLESTextureGetTarget(_uvTexture) == GL_TEXTURE_2D, | |
| 86 @"Unexpected GLES texture target"); | |
| 87 glBindTexture(GL_TEXTURE_2D, self.uvTexture); | |
| 88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 92 | |
| 93 return YES; | |
| 94 } | |
| 95 | |
| 96 - (void)releaseTextures { | |
| 97 if (_uvTexture) { | |
| 98 CFRelease(_uvTexture); | |
| 99 _uvTexture = nil; | |
| 100 } | |
| 101 if (_yTexture) { | |
| 102 CFRelease(_yTexture); | |
| 103 _yTexture = nil; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 - (void)dealloc { | |
| 108 [self releaseTextures]; | |
| 109 if (_textureCache) { | |
| 110 CFRelease(_textureCache); | |
| 111 _textureCache = nil; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 @end | |
| 116 | |
| OLD | NEW |