OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 232 |
233 id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer]; | 233 id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer]; |
234 commandBuffer.label = commandBufferLabel; | 234 commandBuffer.label = commandBufferLabel; |
235 | 235 |
236 __block dispatch_semaphore_t block_semaphore = _inflight_semaphore; | 236 __block dispatch_semaphore_t block_semaphore = _inflight_semaphore; |
237 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) { | 237 [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) { |
238 // GPU work completed. | 238 // GPU work completed. |
239 dispatch_semaphore_signal(block_semaphore); | 239 dispatch_semaphore_signal(block_semaphore); |
240 }]; | 240 }]; |
241 | 241 |
242 MTLRenderPassDescriptor *_renderPassDescriptor = _view.currentRenderPassDescri
ptor; | 242 MTLRenderPassDescriptor *renderPassDescriptor = _view.currentRenderPassDescrip
tor; |
243 if (_renderPassDescriptor) { // Valid drawable. | 243 if (renderPassDescriptor) { // Valid drawable. |
244 id<MTLRenderCommandEncoder> renderEncoder = | 244 id<MTLRenderCommandEncoder> renderEncoder = |
245 [commandBuffer renderCommandEncoderWithDescriptor:_renderPassDescriptor]
; | 245 [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; |
246 renderEncoder.label = renderEncoderLabel; | 246 renderEncoder.label = renderEncoderLabel; |
247 | 247 |
248 // Set context state. | 248 // Set context state. |
249 [renderEncoder pushDebugGroup:renderEncoderDebugGroup]; | 249 [renderEncoder pushDebugGroup:renderEncoderDebugGroup]; |
250 [renderEncoder setRenderPipelineState:_pipelineState]; | 250 [renderEncoder setRenderPipelineState:_pipelineState]; |
251 [renderEncoder setVertexBuffer:_vertexBuffer offset:_offset * sizeof(float)
atIndex:0]; | 251 [renderEncoder setVertexBuffer:_vertexBuffer offset:_offset * sizeof(float)
atIndex:0]; |
252 [renderEncoder setFragmentTexture:_yTexture atIndex:0]; | 252 [renderEncoder setFragmentTexture:_yTexture atIndex:0]; |
253 [renderEncoder setFragmentTexture:_CrCbTexture atIndex:1]; | 253 [renderEncoder setFragmentTexture:_CrCbTexture atIndex:1]; |
254 | 254 |
255 [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip | 255 [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip |
256 vertexStart:0 | 256 vertexStart:0 |
257 vertexCount:4 | 257 vertexCount:4 |
258 instanceCount:1]; | 258 instanceCount:1]; |
259 [renderEncoder popDebugGroup]; | 259 [renderEncoder popDebugGroup]; |
260 [renderEncoder endEncoding]; | 260 [renderEncoder endEncoding]; |
261 | 261 |
262 [commandBuffer presentDrawable:_view.currentDrawable]; | 262 [commandBuffer presentDrawable:_view.currentDrawable]; |
263 } | 263 } |
264 | 264 |
265 // CPU work is completed, GPU work can be started. | 265 // CPU work is completed, GPU work can be started. |
266 [commandBuffer commit]; | 266 [commandBuffer commit]; |
267 } | 267 } |
268 | 268 |
269 #pragma mark - RTCMTLRenderer | 269 #pragma mark - RTCMTLRenderer |
270 | 270 |
271 - (void)drawFrame:(RTCVideoFrame *)frame { | 271 - (void)drawFrame:(RTCVideoFrame *)frame { |
272 [self setupTexturesForFrame:frame]; | |
273 @autoreleasepool { | 272 @autoreleasepool { |
274 [self render]; | 273 if ([self setupTexturesForFrame:frame]) |
| 274 [self render]; |
275 } | 275 } |
276 } | 276 } |
277 | 277 |
278 - (void)setupTexturesForFrame:(nonnull RTCVideoFrame *)frame { | 278 - (BOOL)setupTexturesForFrame:(nonnull RTCVideoFrame *)frame { |
279 CVPixelBufferRef pixelBuffer = frame.nativeHandle; | 279 CVPixelBufferRef pixelBuffer = frame.nativeHandle; |
280 | 280 |
281 id<MTLTexture> lumaTexture = nil; | 281 id<MTLTexture> lumaTexture = nil; |
282 id<MTLTexture> chromaTexture = nil; | 282 id<MTLTexture> chromaTexture = nil; |
283 CVMetalTextureRef outTexture = nullptr; | 283 CVMetalTextureRef outTexture = nullptr; |
284 | 284 |
285 // Luma (y) texture. | 285 // Luma (y) texture. |
286 int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); | 286 int lumaWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); |
287 int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); | 287 int lumaHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); |
288 | 288 |
(...skipping 17 matching lines...) Expand all Loading... |
306 lumaHeight / 2, indexPlane, &outTexture); | 306 lumaHeight / 2, indexPlane, &outTexture); |
307 if (result == kCVReturnSuccess) { | 307 if (result == kCVReturnSuccess) { |
308 chromaTexture = CVMetalTextureGetTexture(outTexture); | 308 chromaTexture = CVMetalTextureGetTexture(outTexture); |
309 } | 309 } |
310 CVBufferRelease(outTexture); | 310 CVBufferRelease(outTexture); |
311 | 311 |
312 if (lumaTexture != nil && chromaTexture != nil) { | 312 if (lumaTexture != nil && chromaTexture != nil) { |
313 _yTexture = lumaTexture; | 313 _yTexture = lumaTexture; |
314 _CrCbTexture = chromaTexture; | 314 _CrCbTexture = chromaTexture; |
315 _offset = offsetForRotation((webrtc::VideoRotation)frame.rotation); | 315 _offset = offsetForRotation((webrtc::VideoRotation)frame.rotation); |
| 316 return YES; |
316 } | 317 } |
| 318 return NO; |
317 } | 319 } |
318 | 320 |
319 @end | 321 @end |
OLD | NEW |