Skip to content

Commit

Permalink
ios: metal port could cause a deadlock if first video frame arrives b…
Browse files Browse the repository at this point in the history
…efore video view is ready
  • Loading branch information
z-dule committed Jun 13, 2024
1 parent 5858c30 commit 4cb671c
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions iosx/src/flowmgr/AVSVideoView_Metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,22 @@ - (void)render:(CADisplayLink*)displayLink
}
[_lock unlock];

id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
commandBuffer.label = @"AVSCommandBuffer";

__block dispatch_semaphore_t blockSem = _gpuSemaphore;

if (!_renderPassDescriptor || _viewFrame.size.width == 0 || _viewFrame.size.height == 0) {
dispatch_semaphore_signal(blockSem);
return;
}

id<CAMetalDrawable> currentDrawable = [_metalLayer nextDrawable];
if (!currentDrawable) {
dispatch_semaphore_signal(blockSem);
return;
}

__block dispatch_semaphore_t blockSem = _gpuSemaphore;
id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];

commandBuffer.label = @"AVSCommandBuffer";
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> _Nonnull) {
// GPU work has completed.
[_lock lock];
Expand All @@ -335,33 +342,30 @@ - (void)render:(CADisplayLink*)displayLink
dispatch_semaphore_signal(blockSem);
}];

if (_renderPassDescriptor) {
_renderPassDescriptor.colorAttachments[0].texture = currentDrawable.texture;
_renderPassDescriptor.colorAttachments[0].texture = currentDrawable.texture;

id<MTLRenderCommandEncoder> renderEncoder =
[commandBuffer renderCommandEncoderWithDescriptor:_renderPassDescriptor];
renderEncoder.label = @"AVSEncoder";

// Set context state.
//[renderEncoder pushDebugGroup:renderEncoderDebugGroup];
[renderEncoder setRenderPipelineState:_pipelineState];
[renderEncoder setVertexBuffer:_vertexBuffer offset:0 atIndex:0];

[renderEncoder setFragmentTexture:_yTexture atIndex:0];
[renderEncoder setFragmentTexture:_uTexture atIndex:1];
[renderEncoder setFragmentTexture:_vTexture atIndex:2];

[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip
vertexStart:0
vertexCount:4
instanceCount:1];
id<MTLRenderCommandEncoder> renderEncoder =
[commandBuffer renderCommandEncoderWithDescriptor:_renderPassDescriptor];
renderEncoder.label = @"AVSEncoder";

// Set context state.
//[renderEncoder pushDebugGroup:renderEncoderDebugGroup];
[renderEncoder setRenderPipelineState:_pipelineState];
[renderEncoder setVertexBuffer:_vertexBuffer offset:0 atIndex:0];

[renderEncoder setFragmentTexture:_yTexture atIndex:0];
[renderEncoder setFragmentTexture:_uTexture atIndex:1];
[renderEncoder setFragmentTexture:_vTexture atIndex:2];

[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip
vertexStart:0
vertexCount:4
instanceCount:1];

//[renderEncoder popDebugGroup];
[renderEncoder endEncoding];

[commandBuffer presentDrawable:currentDrawable];
}
//[renderEncoder popDebugGroup];
[renderEncoder endEncoding];

[commandBuffer presentDrawable:currentDrawable];
[commandBuffer commit];
}

Expand Down Expand Up @@ -402,7 +406,7 @@ - (void)startRunning
- (void)stopRunning
{
_running = NO;
[_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_displayLink invalidate];
_displayLink = nil;
}

Expand Down

0 comments on commit 4cb671c

Please sign in to comment.