tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "RTCShader.h" |
| 12 | |
| 13 | // Native CVPixelBufferRef rendering is only supported on iPhone because it |
| 14 | // depends on CVOpenGLESTextureCacheCreate. |
| 15 | #if TARGET_OS_IPHONE |
| 16 | |
| 17 | #import <CoreVideo/CVOpenGLESTextureCache.h> |
| 18 | |
| 19 | #import "RTCShader+Private.h" |
| 20 | #import "WebRTC/RTCVideoFrame.h" |
| 21 | |
| 22 | #include "webrtc/base/checks.h" |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 23 | #include "webrtc/base/optional.h" |
| 24 | #include "webrtc/common_video/rotation.h" |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 25 | |
| 26 | static const char kNV12FragmentShaderSource[] = |
| 27 | SHADER_VERSION |
| 28 | "precision mediump float;" |
| 29 | FRAGMENT_SHADER_IN " vec2 v_texcoord;\n" |
| 30 | "uniform lowp sampler2D s_textureY;\n" |
| 31 | "uniform lowp sampler2D s_textureUV;\n" |
| 32 | FRAGMENT_SHADER_OUT |
| 33 | "void main() {\n" |
| 34 | " mediump float y;\n" |
| 35 | " mediump vec2 uv;\n" |
| 36 | " y = " FRAGMENT_SHADER_TEXTURE "(s_textureY, v_texcoord).r;\n" |
| 37 | " uv = " FRAGMENT_SHADER_TEXTURE "(s_textureUV, v_texcoord).ra -\n" |
| 38 | " vec2(0.5, 0.5);\n" |
| 39 | " " FRAGMENT_SHADER_COLOR " = vec4(y + 1.403 * uv.y,\n" |
| 40 | " y - 0.344 * uv.x - 0.714 * uv.y,\n" |
| 41 | " y + 1.770 * uv.x,\n" |
| 42 | " 1.0);\n" |
| 43 | " }\n"; |
| 44 | |
| 45 | @implementation RTCNativeNV12Shader { |
| 46 | GLuint _vertexBuffer; |
| 47 | GLuint _nv12Program; |
| 48 | GLint _ySampler; |
| 49 | GLint _uvSampler; |
| 50 | CVOpenGLESTextureCacheRef _textureCache; |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 51 | // Store current rotation and only upload new vertex data when rotation |
| 52 | // changes. |
| 53 | rtc::Optional<webrtc::VideoRotation> _currentRotation; |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | - (instancetype)initWithContext:(GlContextType *)context { |
| 57 | if (self = [super init]) { |
| 58 | if (![self setupNV12Program] || ![self setupTextureCacheWithContext:context] || |
| 59 | !RTCSetupVerticesForProgram(_nv12Program, &_vertexBuffer, nullptr)) { |
| 60 | self = nil; |
| 61 | } |
| 62 | } |
| 63 | return self; |
| 64 | } |
| 65 | |
| 66 | - (void)dealloc { |
| 67 | glDeleteProgram(_nv12Program); |
| 68 | glDeleteBuffers(1, &_vertexBuffer); |
| 69 | if (_textureCache) { |
| 70 | CFRelease(_textureCache); |
| 71 | _textureCache = nullptr; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | - (BOOL)setupNV12Program { |
| 76 | _nv12Program = RTCCreateProgramFromFragmentSource(kNV12FragmentShaderSource); |
| 77 | if (!_nv12Program) { |
| 78 | return NO; |
| 79 | } |
| 80 | _ySampler = glGetUniformLocation(_nv12Program, "s_textureY"); |
| 81 | _uvSampler = glGetUniformLocation(_nv12Program, "s_textureUV"); |
| 82 | |
| 83 | return (_ySampler >= 0 && _uvSampler >= 0); |
| 84 | } |
| 85 | |
| 86 | - (BOOL)setupTextureCacheWithContext:(GlContextType *)context { |
| 87 | CVReturn ret = CVOpenGLESTextureCacheCreate( |
| 88 | kCFAllocatorDefault, NULL, |
| 89 | #if COREVIDEO_USE_EAGLCONTEXT_CLASS_IN_API |
| 90 | context, |
| 91 | #else |
| 92 | (__bridge void *)context, |
| 93 | #endif |
| 94 | NULL, &_textureCache); |
| 95 | return ret == kCVReturnSuccess; |
| 96 | } |
| 97 | |
| 98 | - (BOOL)drawFrame:(RTCVideoFrame *)frame { |
| 99 | CVPixelBufferRef pixelBuffer = frame.nativeHandle; |
| 100 | RTC_CHECK(pixelBuffer); |
| 101 | glUseProgram(_nv12Program); |
| 102 | const OSType pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer); |
| 103 | RTC_CHECK(pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange || |
| 104 | pixelFormat == kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) |
| 105 | << "Unsupported native pixel format: " << pixelFormat; |
| 106 | |
| 107 | // Y-plane. |
| 108 | const int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); |
| 109 | const int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); |
| 110 | |
| 111 | CVOpenGLESTextureRef lumaTexture = nullptr; |
| 112 | glActiveTexture(GL_TEXTURE0); |
| 113 | glUniform1i(_ySampler, 0); |
| 114 | CVReturn ret = CVOpenGLESTextureCacheCreateTextureFromImage( |
| 115 | kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, |
| 116 | RTC_PIXEL_FORMAT, lumaWidth, lumaHeight, RTC_PIXEL_FORMAT, |
| 117 | GL_UNSIGNED_BYTE, 0, &lumaTexture); |
| 118 | if (ret != kCVReturnSuccess) { |
| 119 | CFRelease(lumaTexture); |
| 120 | return NO; |
| 121 | } |
| 122 | |
| 123 | RTC_CHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), |
| 124 | CVOpenGLESTextureGetTarget(lumaTexture)); |
| 125 | glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(lumaTexture)); |
| 126 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 127 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 128 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 130 | |
| 131 | // UV-plane. |
| 132 | const int chromaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); |
| 133 | const int chromeHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); |
| 134 | |
| 135 | CVOpenGLESTextureRef chromaTexture = nullptr; |
| 136 | glActiveTexture(GL_TEXTURE1); |
| 137 | glUniform1i(_uvSampler, 1); |
| 138 | ret = CVOpenGLESTextureCacheCreateTextureFromImage( |
| 139 | kCFAllocatorDefault, _textureCache, pixelBuffer, NULL, GL_TEXTURE_2D, |
| 140 | GL_LUMINANCE_ALPHA, chromaWidth, chromeHeight, GL_LUMINANCE_ALPHA, |
| 141 | GL_UNSIGNED_BYTE, 1, &chromaTexture); |
| 142 | if (ret != kCVReturnSuccess) { |
| 143 | CFRelease(chromaTexture); |
| 144 | CFRelease(lumaTexture); |
| 145 | return NO; |
| 146 | } |
| 147 | |
| 148 | RTC_CHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), |
| 149 | CVOpenGLESTextureGetTarget(chromaTexture)); |
| 150 | glBindTexture(GL_TEXTURE_2D, CVOpenGLESTextureGetName(chromaTexture)); |
| 151 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 152 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 153 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 154 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 155 | |
| 156 | glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 157 | if (!_currentRotation || frame.rotation != *_currentRotation) { |
| 158 | _currentRotation = rtc::Optional<webrtc::VideoRotation>( |
| 159 | static_cast<webrtc::VideoRotation>(frame.rotation)); |
| 160 | RTCSetVertexData(*_currentRotation); |
| 161 | } |
tkchin | 04dbb34 | 2016-08-08 03:10:07 -0700 | [diff] [blame] | 162 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 163 | |
| 164 | CFRelease(chromaTexture); |
| 165 | CFRelease(lumaTexture); |
| 166 | |
| 167 | return YES; |
| 168 | } |
| 169 | |
| 170 | @end |
| 171 | #endif // TARGET_OS_IPHONE |