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 "RTCNV12Renderer.h" | |
| 12 | |
| 13 #import <Metal/Metal.h> | |
| 14 #import <MetalKit/MetalKit.h> | |
| 15 | |
| 16 #import "WebRTC/RTCLogging.h" | |
| 17 #import "WebRTC/RTCVideoFrame.h" | |
| 18 | |
| 19 #include "webrtc/api/video/video_rotation.h" | |
| 20 | |
| 21 static const float cubeVertexData[64] = { | |
| 22 -1.0, -1.0, 0.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1. 0, 0.0, | |
| 23 | |
| 24 // rotation = 90, offset = 16. | |
| 25 -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0. 0, 0.0, | |
| 26 | |
| 27 // rotation = 180, offset = 32. | |
| 28 -1.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0. 0, 1.0, | |
| 29 | |
| 30 // rotation = 270, offset = 48. | |
| 31 -1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 0.0, 1.0, -1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1. 0, 1.0, | |
| 32 }; | |
| 33 | |
| 34 static inline int offsetForRotation(webrtc::VideoRotation rotation) { | |
| 35 switch (rotation) { | |
| 36 case webrtc::kVideoRotation_0: | |
| 37 return 0; | |
| 38 case webrtc::kVideoRotation_90: | |
| 39 return 16; | |
| 40 case webrtc::kVideoRotation_180: | |
| 41 return 32; | |
| 42 case webrtc::kVideoRotation_270: | |
| 43 return 48; | |
| 44 } | |
| 45 return 0; | |
| 46 } | |
| 47 | |
| 48 // The max number of command buffers in flight. | |
| 49 // For now setting it up to 1. | |
| 50 // In future we might use tripple buffering method if it improves performance. | |
|
Chuck
2017/02/15 14:54:05
s/tripple/triple/
| |
| 51 | |
| 52 static const NSInteger kMaxInflightBuffers = 1; | |
| 53 | |
| 54 @interface RTCNV12Renderer () <MTKViewDelegate> | |
| 55 @end | |
| 56 | |
| 57 @implementation RTCNV12Renderer { | |
| 58 __kindof MTKView *_view; | |
| 59 | |
| 60 // Controller. | |
| 61 dispatch_semaphore_t _inflight_semaphore; | |
| 62 | |
| 63 // Renderer. | |
| 64 id<MTLDevice> _device; | |
| 65 id<MTLCommandQueue> _commandQueue; | |
| 66 id<MTLLibrary> _defaultLibrary; | |
| 67 id<MTLRenderPipelineState> _pipelineState; | |
| 68 | |
| 69 // Textures. | |
| 70 CVMetalTextureCacheRef textureCache; | |
| 71 id<MTLTexture> yTexture; | |
| 72 id<MTLTexture> CrCbTexture; | |
| 73 | |
| 74 // Buffers. | |
| 75 id<MTLBuffer> _vertexBuffer; | |
| 76 | |
| 77 // RTC Frame parameters. | |
| 78 int offset; | |
| 79 } | |
| 80 | |
| 81 - (instancetype)initWithView:(__kindof MTKView *)view { | |
| 82 if (self = [super init]) { | |
| 83 _view = view; | |
| 84 | |
| 85 // Offset of 0 is equal to rotation of 0. | |
| 86 offset = 0; | |
| 87 _inflight_semaphore = dispatch_semaphore_create(kMaxInflightBuffers); | |
| 88 } | |
| 89 | |
| 90 return self; | |
| 91 } | |
| 92 | |
| 93 - (BOOL)setup { | |
| 94 BOOL success = NO; | |
| 95 if ([self setupMetal] && _device) { | |
|
Chuck
2017/02/15 14:54:05
Why not check _device on 110 and return NO?
| |
| 96 [self setupView]; | |
| 97 [self loadAssets]; | |
| 98 [self setupBuffers]; | |
| 99 [self initializeTextureCache]; | |
| 100 success = YES; | |
| 101 } | |
| 102 return success; | |
| 103 } | |
| 104 | |
| 105 #pragma mark - GPU methods | |
| 106 | |
| 107 - (BOOL)setupMetal { | |
| 108 // Set the view to use the default device. | |
| 109 _device = MTLCreateSystemDefaultDevice(); | |
| 110 | |
| 111 // Create a new command queue. | |
| 112 _commandQueue = [_device newCommandQueue]; | |
| 113 | |
| 114 // Load all the shader files with a metal file extension in the project. | |
| 115 NSError *libraryError = nil; | |
| 116 NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"org.webrtc.WebRTC "]; | |
| 117 NSString *libraryFile = [frameworkBundle pathForResource:@"rtc_shaders" ofType :@"metallib"]; | |
| 118 if (!libraryFile) { | |
| 119 RTCLog( | |
| 120 @"Metal Error: library not found in framework bundle with identifier org .webrtc.WebRTC."); | |
| 121 return NO; | |
| 122 } | |
| 123 | |
| 124 _defaultLibrary = [_device newLibraryWithFile:libraryFile error:&libraryError] ; | |
| 125 if (!_defaultLibrary) { | |
| 126 RTCLog(@"Metal error: Failed to load library. %@", libraryError); | |
| 127 return NO; | |
| 128 } | |
| 129 | |
| 130 return YES; | |
| 131 } | |
| 132 | |
| 133 - (void)setupView { | |
| 134 _view.device = _device; | |
| 135 _view.delegate = self; | |
| 136 // This is very important, should not be removed. | |
|
Chuck
2017/02/15 14:54:05
Nit: why is it important? Add to comment
daniela-webrtc
2017/02/16 15:38:00
It's lingering comment from before. Also I realize
| |
| 137 _view.contentMode = UIViewContentModeScaleAspectFit; | |
| 138 | |
| 139 _view.preferredFramesPerSecond = 30; | |
| 140 _view.autoResizeDrawable = NO; | |
| 141 } | |
| 142 | |
| 143 - (void)loadAssets { | |
| 144 // As defined in Shaders.metal. | |
| 145 id<MTLFunction> vertexFunction = [_defaultLibrary newFunctionWithName:@"vertex Passthrough"]; | |
| 146 id<MTLFunction> fragmentFunction = | |
| 147 [_defaultLibrary newFunctionWithName:@"fragmentColorConversion"]; | |
| 148 | |
| 149 MTLRenderPipelineDescriptor *pipelineDescriptor = [[MTLRenderPipelineDescripto r alloc] init]; | |
| 150 pipelineDescriptor.label = @"Pipeline"; | |
| 151 pipelineDescriptor.vertexFunction = vertexFunction; | |
| 152 pipelineDescriptor.fragmentFunction = fragmentFunction; | |
| 153 pipelineDescriptor.colorAttachments[0].pixelFormat = _view.colorPixelFormat; | |
| 154 pipelineDescriptor.depthAttachmentPixelFormat = MTLPixelFormatInvalid; | |
| 155 NSError *error = nil; | |
| 156 _pipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineDescrip tor error:&error]; | |
| 157 | |
| 158 if (!_pipelineState) { | |
| 159 RTCLog(@"Metal error: Failed to create pipeline state. %@", error); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 - (void)setupBuffers { | |
| 164 _vertexBuffer = [_device newBufferWithBytes:cubeVertexData | |
| 165 length:sizeof(cubeVertexData) | |
| 166 options:MTLResourceOptionCPUCacheModeDefau lt]; | |
| 167 } | |
| 168 | |
| 169 - (void)initializeTextureCache { | |
| 170 CVReturn status = | |
| 171 CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, _device, nil, &texture Cache); | |
| 172 if (status != kCVReturnSuccess) { | |
| 173 RTCLog(@"Metal error: Failed to initialize metal texture cache. Return statu s is %d", status); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 - (void)render { | |
| 178 dispatch_semaphore_wait(_inflight_semaphore, DISPATCH_TIME_FOREVER); | |
| 179 | |
| 180 id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer]; | |
| 181 commandBuffer.label = @"RTCCommandBuffer"; | |
| 182 | |
| 183 __block dispatch_semaphore_t block_semaphore = _inflight_semaphore; | |
| 184 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) { | |
| 185 dispatch_semaphore_signal(block_semaphore); | |
| 186 }]; | |
| 187 | |
| 188 MTLRenderPassDescriptor *_renderPassDescriptor = _view.currentRenderPassDescri ptor; | |
| 189 if (_renderPassDescriptor) { // Valid drawable. | |
| 190 | |
|
Chuck
2017/02/15 14:54:05
nit: extra blank line here can be removed
| |
| 191 id<MTLRenderCommandEncoder> renderEncoder = | |
| 192 [commandBuffer renderCommandEncoderWithDescriptor:_renderPassDescriptor] ; | |
| 193 renderEncoder.label = @"RTCEncoder"; | |
| 194 | |
| 195 // Set context state. | |
| 196 [renderEncoder pushDebugGroup:@"DrawFrame"]; | |
| 197 [renderEncoder setRenderPipelineState:_pipelineState]; | |
| 198 [renderEncoder setVertexBuffer:_vertexBuffer offset:offset * sizeof(float) a tIndex:0]; | |
| 199 [renderEncoder setFragmentTexture:yTexture atIndex:0]; | |
| 200 [renderEncoder setFragmentTexture:CrCbTexture atIndex:1]; | |
| 201 | |
| 202 [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip | |
| 203 vertexStart:0 | |
| 204 vertexCount:4 | |
| 205 instanceCount:1]; | |
| 206 [renderEncoder popDebugGroup]; | |
| 207 [renderEncoder endEncoding]; | |
| 208 | |
| 209 [commandBuffer presentDrawable:_view.currentDrawable]; | |
| 210 } | |
| 211 | |
| 212 [commandBuffer commit]; | |
| 213 } | |
| 214 | |
| 215 #pragma mark - MTKViewDelegate | |
| 216 | |
| 217 - (void)drawInMTKView:(MTKView *)view { | |
| 218 if (!yTexture && !CrCbTexture) { | |
| 219 NSLog(@"No current frame. Aborting drawinFrame:"); | |
| 220 return; | |
| 221 } | |
| 222 @autoreleasepool { | |
| 223 [self render]; | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { | |
| 228 } | |
| 229 | |
| 230 #pragma mark - RTCVideoRenderer | |
| 231 | |
| 232 - (void)setSize:(CGSize)size { | |
| 233 _view.drawableSize = size; | |
| 234 [_view draw]; | |
| 235 } | |
| 236 | |
| 237 - (void)renderFrame:(nullable RTCVideoFrame *)frame { | |
| 238 if (frame == NULL) { | |
| 239 return; | |
| 240 } | |
| 241 [self setupSyncVariablesForFrame:frame]; | |
| 242 } | |
| 243 | |
| 244 - (void)setupSyncVariablesForFrame:(nonnull RTCVideoFrame *)frame { | |
| 245 CVPixelBufferRef pixelBuffer = frame.nativeHandle; | |
| 246 | |
| 247 id<MTLTexture> lumaTexture = nil; | |
| 248 id<MTLTexture> chromaTexture = nil; | |
| 249 CVMetalTextureRef outTexture; | |
| 250 | |
| 251 // Luma (y) texture. | |
| 252 int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); | |
| 253 int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); | |
| 254 | |
| 255 int indexPlane = 0; | |
| 256 CVReturn result = CVMetalTextureCacheCreateTextureFromImage( | |
| 257 kCFAllocatorDefault, textureCache, pixelBuffer, nil, MTLPixelFormatR8Unorm , lumaWidth, | |
| 258 lumaHeight, indexPlane, &outTexture); | |
| 259 | |
| 260 if (result == kCVReturnSuccess) { | |
| 261 lumaTexture = CVMetalTextureGetTexture(outTexture); | |
| 262 } | |
| 263 | |
| 264 CVBufferRelease(outTexture); | |
| 265 outTexture = nil; | |
| 266 | |
| 267 // Chroma (CrCb) texture. | |
| 268 indexPlane = 1; | |
| 269 result = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textur eCache, pixelBuffer, | |
| 270 nil, MTLPixelFormatRG8Unorm , lumaWidth / 2, | |
| 271 lumaHeight / 2, indexPlane, &outTexture); | |
| 272 if (result == kCVReturnSuccess) { | |
| 273 chromaTexture = CVMetalTextureGetTexture(outTexture); | |
| 274 } | |
| 275 CVBufferRelease(outTexture); | |
| 276 | |
| 277 if (lumaTexture != nil && chromaTexture != nil) { | |
| 278 dispatch_async(dispatch_get_main_queue(), ^{ | |
| 279 yTexture = lumaTexture; | |
| 280 CrCbTexture = chromaTexture; | |
| 281 offset = offsetForRotation((webrtc::VideoRotation)frame.rotation); | |
| 282 }); | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 @end | |
| OLD | NEW |