diff --git a/media/gpu/windows/d3d12_copy_command_list_wrapper.cc b/media/gpu/windows/d3d12_copy_command_list_wrapper.cc --- a/media/gpu/windows/d3d12_copy_command_list_wrapper.cc +++ b/media/gpu/windows/d3d12_copy_command_list_wrapper.cc @@ -60,7 +60,16 @@ D3D12CopyCommandQueueWrapper::D3D12CopyCommandQueueWrapper( command_list_(std::move(command_list)), fence_(base::MakeRefCounted(fence)) {} -D3D12CopyCommandQueueWrapper::~D3D12CopyCommandQueueWrapper() = default; +D3D12CopyCommandQueueWrapper::~D3D12CopyCommandQueueWrapper() { + // Drain the COPY queue before releasing the command queue / allocator / + // command list. Callers (D3D12VideoEncodeAccelerator) hold raw + // ID3D12Resource pointers in in-flight CopyTextureRegion commands; D3D12 + // command lists do NOT AddRef recorded resources, so releasing those + // resources (or this allocator) while the GPU is still executing the copy + // is undefined behaviour. SignalAndWaitCPU enqueues a fence signal after + // the last submitted work and CPU-blocks until the GPU reaches it. + fence_->SignalAndWaitCPU(*command_queue_.Get()); +} bool D3D12CopyCommandQueueWrapper::CopyTextureRegion( const D3D12_TEXTURE_COPY_LOCATION& dst_location, diff --git a/media/gpu/windows/d3d12_video_encode_accelerator.cc b/media/gpu/windows/d3d12_video_encode_accelerator.cc --- a/media/gpu/windows/d3d12_video_encode_accelerator.cc +++ b/media/gpu/windows/d3d12_video_encode_accelerator.cc @@ -984,6 +984,12 @@ bool D3D12VideoEncodeAccelerator::DoEncodeTask( void D3D12VideoEncodeAccelerator::DestroyTask() { DVLOGF(2); DCHECK_CALLED_ON_VALID_SEQUENCE(encoder_sequence_checker_); + // input_texture_ / upload_buffer_ may still be referenced by in-flight + // COPY-queue work submitted from CreateResourceForSharedMemoryVideoFrame() + // when the delegate early-returned before the encode-queue CPU sync. They + // are declared after copy_command_queue_ and would otherwise be released + // first; explicitly drain the COPY queue before member destruction. + copy_command_queue_.reset(); delete this; }