1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139

media / mojo / services / mojo_video_encode_accelerator_provider.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/mojo/services/mojo_video_encode_accelerator_provider.h"

#include <memory>
#include <utility>

#include "base/feature_list.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "gpu/ipc/service/gpu_channel.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
#include "media/gpu/command_buffer_helper.h"
#include "media/gpu/gpu_video_encode_accelerator_factory.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/platform_handle.h"

namespace {
void BindVEAProvider(
    mojo::PendingReceiver<media::mojom::VideoEncodeAcceleratorProvider>
        receiver,
    media::MojoVideoEncodeAcceleratorProvider::
        CreateAndInitializeVideoEncodeAcceleratorCallback create_vea_callback,
    gpu::GpuPreferences gpu_preferences,
    gpu::GpuDriverBugWorkarounds gpu_workarounds,
    const gpu::GPUInfo::GPUDevice& gpu_device,
    base::WeakPtr<media::MediaGpuChannelManager> media_gpu_channel_manager,
    scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner) {
  auto vea_provider =
      std::make_unique<media::MojoVideoEncodeAcceleratorProvider>(
          std::move(create_vea_callback), gpu_preferences, gpu_workarounds,
          gpu_device, media_gpu_channel_manager, gpu_task_runner);
  mojo::MakeSelfOwnedReceiver(std::move(vea_provider), std::move(receiver));
}

scoped_refptr<media::CommandBufferHelper> GetCommandBufferHelper(
    scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner,
    base::WeakPtr<media::MediaGpuChannelManager> media_gpu_channel_manager,
    base::UnguessableToken channel_token,
    int32_t route_id) {
  DCHECK(gpu_task_runner->BelongsToCurrentThread());
  if (!media_gpu_channel_manager) {
    return nullptr;
  }

  gpu::GpuChannel* channel =
      media_gpu_channel_manager->LookupChannel(channel_token);
  if (!channel) {
    return nullptr;
  }

  gpu::CommandBufferStub* stub = channel->LookupCommandBuffer(route_id);
  if (!stub) {
    return nullptr;
  }

  return media::CommandBufferHelper::Create(stub);
}

}  // namespace

namespace media {

// static
void MojoVideoEncodeAcceleratorProvider::Create(
    mojo::PendingReceiver<mojom::VideoEncodeAcceleratorProvider> receiver,
    CreateAndInitializeVideoEncodeAcceleratorCallback create_vea_callback,
    const gpu::GpuPreferences& gpu_preferences,
    const gpu::GpuDriverBugWorkarounds& gpu_workarounds,
    const gpu::GPUInfo::GPUDevice& gpu_device,
    scoped_refptr<base::SequencedTaskRunner> runner,
    base::WeakPtr<media::MediaGpuChannelManager> media_gpu_channel_manager,
    scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner) {
  DCHECK(runner);
  runner->PostTask(
      FROM_HERE, base::BindOnce(BindVEAProvider, std::move(receiver),
                                std::move(create_vea_callback), gpu_preferences,
                                gpu_workarounds, gpu_device,
                                media_gpu_channel_manager, gpu_task_runner));
}

MojoVideoEncodeAcceleratorProvider::MojoVideoEncodeAcceleratorProvider(
    CreateAndInitializeVideoEncodeAcceleratorCallback create_vea_callback,
    const gpu::GpuPreferences& gpu_preferences,
    const gpu::GpuDriverBugWorkarounds& gpu_workarounds,
    const gpu::GPUInfo::GPUDevice& gpu_device,
    base::WeakPtr<media::MediaGpuChannelManager> media_gpu_channel_manager,
    scoped_refptr<base::SingleThreadTaskRunner> gpu_task_runner)
    : create_vea_callback_(std::move(create_vea_callback)),
      gpu_preferences_(gpu_preferences),
      gpu_workarounds_(gpu_workarounds),
      gpu_device_(gpu_device),
      media_gpu_channel_manager_(media_gpu_channel_manager),
      gpu_task_runner_(gpu_task_runner) {}

MojoVideoEncodeAcceleratorProvider::~MojoVideoEncodeAcceleratorProvider() =
    default;

void MojoVideoEncodeAcceleratorProvider::CreateVideoEncodeAccelerator(
    mojom::EncodeCommandBufferIdPtr command_buffer_id,
    mojo::PendingReceiver<mojom::VideoEncodeAccelerator> receiver) {
  GetCommandBufferHelperCB get_helper_cb = GetCommandBufferHelperCB();
  if (command_buffer_id) {
    get_helper_cb = base::BindRepeating(
        &GetCommandBufferHelper, gpu_task_runner_, media_gpu_channel_manager_,
        command_buffer_id->channel_token, command_buffer_id->route_id);
  }
  auto create_service_cb = base::BindOnce(
      &MojoVideoEncodeAcceleratorService::Create, std::move(receiver),
      create_vea_callback_, gpu_preferences_, gpu_workarounds_, gpu_device_,
      get_helper_cb, gpu_task_runner_);

  if (base::FeatureList::IsEnabled(kUseTaskRunnerForMojoVEAService)) {
#if BUILDFLAG(IS_WIN)
    base::ThreadPool::CreateCOMSTATaskRunner({base::MayBlock()},
        base::SingleThreadTaskRunnerThreadMode::DEDICATED)
#else
    base::ThreadPool::CreateSequencedTaskRunner({base::MayBlock()})
#endif
        ->PostTask(FROM_HERE, std::move(create_service_cb));
  } else {
    std::move(create_service_cb).Run();
  }
}

void MojoVideoEncodeAcceleratorProvider::
    GetVideoEncodeAcceleratorSupportedProfiles(
        GetVideoEncodeAcceleratorSupportedProfilesCallback callback) {
  std::move(callback).Run(
      GpuVideoEncodeAcceleratorFactory::GetSupportedProfiles(
          gpu_preferences_, gpu_workarounds_, gpu_device_));
}

}  // namespace media