chromium/media/gpu/BUILD.gn

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

import("//build/buildflag_header.gni")
import("//build/config/chromeos/ui_mode.gni")
import("//build/config/features.gni")
import("//build/config/logging.gni")
import("//build/config/ui.gni")
import("//gpu/vulkan/features.gni")
import("//media/gpu/args.gni")
import("//media/media_options.gni")
import("//testing/test.gni")
import("//third_party/libgav1/options.gni")
import("//tools/generate_stubs/rules.gni")

buildflag_header("buildflags") {
  header = "buildflags.h"

  flags = [
    "USE_VAAPI=$use_vaapi",
    "USE_VAAPI_IMAGE_CODECS=$use_vaapi_image_codecs",
    "USE_V4L2_CODEC=$use_v4l2_codec",
    "USE_LIBV4L2=$use_v4lplugin",
  ]
}

component("gpu") {
  output_name = "media_gpu"

  # Only local test code, GPU-related IPC code in the media layer, and
  # media-related content code should access //media/gpu.
  visibility = [
    ":*",
    "//ash/components/arc/mojom:media",
    "//ash/components/arc/mojom:media_mojolpm",
    "//ash/components/arc/video_accelerator:common",
    "//chrome/gpu",
    "//chromecast/*",
    "//components/chromeos_camera/*",
    "//components/mirroring/service:mirroring_service",
    "//components/viz/service/main",
    "//content/gpu:*",
    "//content/renderer:*",
    "//media/audio:unit_tests",
    "//media/gpu/ipc/*",
    "//media/gpu/test/*",
    "//media/gpu/vaapi/*",
    "//media/mojo/*",
    "//remoting/codec:encoder",
    "//third_party/blink/renderer/modules:unit_tests",
    "//third_party/blink/renderer/modules/mediarecorder",
    "//ui/gl:gl_unittests",

    # TODO(crbug.com/40752360): Remove //remoting/host:common when the usage is removed.
    "//remoting/host:common",
  ]

  if (is_apple) {
    # On Mac, content/common reaches into here to do some pre-sandbox
    # initialization.
    visibility += [ "//content/common" ]
  }

  defines = [
    "MEDIA_GPU_IMPLEMENTATION",

    # Enable VLOG(1) so that they are included in field feedbacks.
    "ENABLED_VLOG_LEVEL=1",
  ]

  sources = [
    "gpu_video_accelerator_util.cc",
    "gpu_video_accelerator_util.h",
    "gpu_video_decode_accelerator_factory.cc",
    "gpu_video_decode_accelerator_factory.h",
    "gpu_video_encode_accelerator_factory.cc",
    "gpu_video_encode_accelerator_factory.h",
  ]

  public_deps = [
    ":buildflags",
    ":command_buffer_helper",
    ":common",
    "//base",
    "//gpu",
    "//media",
    "//third_party/angle:includes",
    "//ui/gfx/geometry",
  ]
  deps = [
    "//third_party/libyuv",
    "//ui/base",
    "//ui/display/types",
    "//ui/gl",
    "//ui/platform_window",
  ]
  libs = []
  ldflags = []

  # V4L2 and VAAPI for video acceleration should not be enabled simultaneously.
  assert(!(use_v4l2_codec && use_vaapi))

  # TODO(crbug.com/1006266): consider using |use_chromeos_video_acceleration|.
  if (use_v4l2_codec || use_vaapi) {
    public_deps += [
      ":video_frame_mapper",
      "//media/gpu/chromeos",
    ]
  }

  if (is_apple) {
    public_deps += [ "//media/gpu/mac" ]
  }

  if (is_android) {
    sources += [
      "android/android_video_encode_accelerator.cc",
      "android/android_video_encode_accelerator.h",
      "android/android_video_surface_chooser.cc",
      "android/android_video_surface_chooser.h",
      "android/android_video_surface_chooser_impl.cc",
      "android/android_video_surface_chooser_impl.h",
      "android/codec_allocator.cc",
      "android/codec_allocator.h",
      "android/codec_buffer_wait_coordinator.cc",
      "android/codec_buffer_wait_coordinator.h",
      "android/codec_image.cc",
      "android/codec_image.h",
      "android/codec_image_group.cc",
      "android/codec_image_group.h",
      "android/codec_output_buffer_renderer.cc",
      "android/codec_output_buffer_renderer.h",
      "android/codec_surface_bundle.cc",
      "android/codec_surface_bundle.h",
      "android/codec_wrapper.cc",
      "android/codec_wrapper.h",
      "android/device_info.cc",
      "android/device_info.h",
      "android/direct_shared_image_video_provider.cc",
      "android/direct_shared_image_video_provider.h",
      "android/frame_info_helper.cc",
      "android/frame_info_helper.h",
      "android/maybe_render_early_manager.cc",
      "android/maybe_render_early_manager.h",
      "android/media_codec_video_decoder.cc",
      "android/media_codec_video_decoder.h",
      "android/ndk_media_codec_wrapper.cc",
      "android/ndk_media_codec_wrapper.h",
      "android/ndk_video_encode_accelerator.cc",
      "android/ndk_video_encode_accelerator.h",
      "android/pooled_shared_image_video_provider.cc",
      "android/pooled_shared_image_video_provider.h",
      "android/promotion_hint_aggregator.h",
      "android/promotion_hint_aggregator_impl.cc",
      "android/promotion_hint_aggregator_impl.h",
      "android/shared_image_video_provider.cc",
      "android/shared_image_video_provider.h",
      "android/surface_chooser_helper.cc",
      "android/surface_chooser_helper.h",
      "android/video_accelerator_util.cc",
      "android/video_accelerator_util.h",
      "android/video_frame_factory.h",
      "android/video_frame_factory_impl.cc",
      "android/video_frame_factory_impl.h",
    ]
    if (proprietary_codecs) {
      sources += [
        "android/ndk_audio_encoder.cc",
        "android/ndk_audio_encoder.h",
      ]
    }
    libs += [
      "aaudio",
      "mediandk",
    ]
    deps += [
      # TODO(crbug.com/40552063): This can be removed once CdmManager is removed.
      "//gpu/ipc/common",
      "//media/mojo:buildflags",
      "//services/service_manager/public/cpp:cpp",
      "//ui/gl:gl_jni_headers",
    ]
    if (enable_vulkan) {
      deps += [ "//gpu/vulkan:vulkan" ]
    }
  }

  if (use_v4l2_codec) {
    public_deps += [ "//media/gpu/v4l2" ]
  }

  if (use_vaapi) {
    public_deps += [
      "//media/gpu/vaapi",
      "//media/gpu/vaapi:common",
    ]
  }

  if (is_fuchsia) {
    public_deps += [ "//media/fuchsia/video" ]
  }

  if (is_win) {
    sources += [
      "windows/av1_guids.h",
      "windows/d3d11_av1_accelerator.cc",
      "windows/d3d11_av1_accelerator.h",
      "windows/d3d11_copying_texture_wrapper.cc",
      "windows/d3d11_copying_texture_wrapper.h",
      "windows/d3d11_decoder_configurator.cc",
      "windows/d3d11_decoder_configurator.h",
      "windows/d3d11_h264_accelerator.cc",
      "windows/d3d11_h264_accelerator.h",
      "windows/d3d11_picture_buffer.cc",
      "windows/d3d11_picture_buffer.h",
      "windows/d3d11_status.h",
      "windows/d3d11_texture_selector.cc",
      "windows/d3d11_texture_selector.h",
      "windows/d3d11_texture_wrapper.cc",
      "windows/d3d11_texture_wrapper.h",
      "windows/d3d11_video_decoder.cc",
      "windows/d3d11_video_decoder.h",
      "windows/d3d11_video_decoder_client.h",
      "windows/d3d11_video_decoder_wrapper.cc",
      "windows/d3d11_video_decoder_wrapper.h",
      "windows/d3d11_video_device_format_support.cc",
      "windows/d3d11_video_device_format_support.h",
      "windows/d3d11_video_frame_mailbox_release_helper.cc",
      "windows/d3d11_video_frame_mailbox_release_helper.h",
      "windows/d3d11_video_processor_proxy.cc",
      "windows/d3d11_video_processor_proxy.h",
      "windows/d3d11_vp9_accelerator.cc",
      "windows/d3d11_vp9_accelerator.h",
      "windows/d3d11_vp9_picture.cc",
      "windows/d3d11_vp9_picture.h",
      "windows/d3d12_fence.cc",
      "windows/d3d12_fence.h",
      "windows/d3d12_helpers.cc",
      "windows/d3d12_helpers.h",
      "windows/d3d12_video_decoder_wrapper.cc",
      "windows/d3d12_video_decoder_wrapper.h",
      "windows/d3d12_video_processor_wrapper.cc",
      "windows/d3d12_video_processor_wrapper.h",
      "windows/d3d_com_defs.h",
      "windows/d3d_video_decoder_wrapper.cc",
      "windows/d3d_video_decoder_wrapper.h",
      "windows/format_utils.cc",
      "windows/format_utils.h",
      "windows/h264_video_rate_control_wrapper.cc",
      "windows/h264_video_rate_control_wrapper.h",
      "windows/init_guid.cc",
      "windows/media_foundation_video_encode_accelerator_win.cc",
      "windows/media_foundation_video_encode_accelerator_win.h",
      "windows/mf_audio_encoder.cc",
      "windows/mf_audio_encoder.h",
      "windows/mf_video_processor_accelerator.cc",
      "windows/mf_video_processor_accelerator.h",
      "windows/scoped_d3d_buffers.cc",
      "windows/scoped_d3d_buffers.h",
      "windows/supported_profile_helpers.cc",
      "windows/supported_profile_helpers.h",
      "windows/video_rate_control_wrapper.h",
      "windows/vp9_video_rate_control_wrapper.cc",
      "windows/vp9_video_rate_control_wrapper.h",
    ]
    if (enable_hevc_parser_and_hw_decoder) {
      sources += [
        "windows/d3d11_h265_accelerator.cc",
        "windows/d3d11_h265_accelerator.h",
      ]
    }
    if (enable_libaom) {
      sources += [
        "windows/av1_video_rate_control_wrapper.cc",
        "windows/av1_video_rate_control_wrapper.h",
      ]
      deps += [ "//third_party/libaom:libaomrc" ]
    }
    deps += [
      "//gpu/ipc/common:common",
      "//media/base/win:color_space_util_win",
      "//third_party/angle:includes",
      "//third_party/libvpx:libvpxrc",
      "//ui/display",
    ]
    libs += [
      "d3d9.lib",
      "d3d11.lib",
      "d3d12.lib",
      "dxgi.lib",
      "dxva2.lib",
      "strmiids.lib",
      "mf.lib",
      "mfplat.lib",
      "mfuuid.lib",
    ]
    ldflags += [
      "/DELAYLOAD:d3d9.dll",
      "/DELAYLOAD:d3d11.dll",
      "/DELAYLOAD:d3d12.dll",
      "/DELAYLOAD:dxva2.dll",
      "/DELAYLOAD:mf.dll",
      "/DELAYLOAD:mfplat.dll",
    ]
  }

  if (use_ozone) {
    deps += [ "//ui/ozone" ]
  }
}

source_set("common") {
  defines = [ "MEDIA_GPU_IMPLEMENTATION" ]
  sources = [
    "accelerated_video_decoder.h",
    "codec_picture.cc",
    "codec_picture.h",
    "exponential_moving_average.cc",
    "exponential_moving_average.h",
    "frame_size_estimator.cc",
    "frame_size_estimator.h",
    "gpu_video_decode_accelerator_helpers.cc",
    "gpu_video_decode_accelerator_helpers.h",
    "gpu_video_encode_accelerator_helpers.cc",
    "gpu_video_encode_accelerator_helpers.h",
    "h264_decoder.cc",
    "h264_decoder.h",
    "h264_dpb.cc",
    "h264_dpb.h",
    "h264_rate_control_util.cc",
    "h264_rate_control_util.h",
    "h264_rate_controller.cc",
    "h264_rate_controller.h",
    "h264_ratectrl_rtc.cc",
    "h264_ratectrl_rtc.h",
    "hrd_buffer.cc",
    "hrd_buffer.h",
    "macros.h",
    "vp9_decoder.cc",
    "vp9_decoder.h",
    "vp9_picture.cc",
    "vp9_picture.h",
    "vp9_reference_frame_vector.cc",
    "vp9_reference_frame_vector.h",
  ]

  if (enable_hevc_parser_and_hw_decoder) {
    sources += [
      "h265_decoder.cc",
      "h265_decoder.h",
      "h265_dpb.cc",
      "h265_dpb.h",
    ]
  }

  visibility = [
    ":gpu",
    "//media/gpu/*",
  ]

  # TODO(crbug.com/1006266): consider using |use_chromeos_video_acceleration|.
  if (use_v4l2_codec || use_vaapi) {
    sources += [
      "vp8_decoder.cc",
      "vp8_decoder.h",
      "vp8_picture.cc",
      "vp8_picture.h",
      "vp8_reference_frame_vector.cc",
      "vp8_reference_frame_vector.h",
    ]
  }

  public_deps = []
  deps = [
    ":buildflags",
    "//base",
    "//build:chromeos_buildflags",
    "//media",
    "//ui/gfx:buffer_types",
    "//ui/gfx:memory_buffer",
    "//ui/gfx/geometry",
  ]

  if (is_win || use_vaapi) {
    sources += [
      "av1_builder.cc",
      "av1_builder.h",
      "h264_builder.cc",
      "h264_builder.h",
      "vp9_svc_layers.cc",
      "vp9_svc_layers.h",
    ]
    if (enable_hevc_parser_and_hw_decoder) {
      sources += [
        "h265_builder.cc",
        "h265_builder.h",
      ]
    }
  }

  if (use_av1_hw_decoder) {
    assert(use_libgav1_parser)
    sources += [
      "av1_decoder.cc",
      "av1_decoder.h",
      "av1_picture.cc",
      "av1_picture.h",
    ]
    public_deps += [ "//third_party/libgav1:libgav1_parser" ]
  }
}

# The buffer validation functionality is in its own component so that it can be
# depended on without pulling the entire //media/gpu target.
component("buffer_validation") {
  defines = [ "IS_MEDIA_GPU_BUFFER_VALIDATION_IMPL" ]
  sources = [
    "buffer_validation.cc",
    "buffer_validation.h",
  ]

  public_deps = [
    "//base",
    "//media",
  ]

  if (is_linux || is_chromeos) {
    deps = [ "//build/config/linux/libdrm" ]
  }

  if (is_chromeos) {
    deps += [ "//ui/gfx:gfx_switches" ]
  }
}

source_set("command_buffer_helper") {
  defines = [ "MEDIA_GPU_IMPLEMENTATION" ]
  sources = [
    "command_buffer_helper.cc",
    "command_buffer_helper.h",
  ]

  public_deps = [
    "//base",
    "//gpu/command_buffer/common",
    "//gpu/command_buffer/common:gles2_utils",
    "//gpu/command_buffer/service",
    "//gpu/command_buffer/service:gles2",
    "//gpu/ipc/service",
    "//ui/gl",
  ]
}

# TODO(crbug.com/1006266): consider using |use_chromeos_video_acceleration|.
if (use_v4l2_codec || use_vaapi) {
  source_set("video_frame_mapper") {
    defines = [ "MEDIA_GPU_IMPLEMENTATION" ]
    sources = [ "video_frame_mapper_factory.cc" ]
    public_deps = [ ":video_frame_mapper_common" ]
    deps = []

    # generic_dmabuf_video_frame_mapper
    if (use_v4l2_codec || use_vaapi) {
      deps += [ "//media/gpu/chromeos:video_frame_mapper" ]
    }

    # vaapi_dmabuf_video_frame_mapper
    if (use_vaapi) {
      deps += [
        "//media/gpu/vaapi",
        "//media/gpu/vaapi:common",
      ]
    }
  }

  source_set("video_frame_mapper_common") {
    defines = [ "MEDIA_GPU_IMPLEMENTATION" ]
    sources = [
      "video_frame_mapper.cc",
      "video_frame_mapper.h",
      "video_frame_mapper_factory.h",
    ]

    public_deps = [
      ":buildflags",
      "//base",
      "//media",
    ]

    deps = [ "//media/gpu/chromeos:video_frame_resource" ]
  }
}

# TODO(watk): Run this on bots. http://crbug.com/461437
if (is_android) {
  test("video_decode_accelerator_unittest") {
    testonly = true

    data = [ "//media/test/data/" ]

    deps = [
      ":android_video_decode_accelerator_unittests",
      ":gpu",
      "//base",
      "//base/test:test_support",
      "//gpu/command_buffer/service:android_texture_owner_unittests",
      "//media:test_support",
      "//media/base/android:media_java",
      "//media/test:run_all_unittests",
      "//mojo/core/embedder",
      "//testing/gtest",
      "//third_party/angle:includes",
      "//ui/android:ui_java",
      "//ui/base",
      "//ui/gfx",
      "//ui/gfx:test_support",
      "//ui/gfx/geometry",
      "//ui/gl",
      "//ui/gl:test_support",
      "//ui/gl/init",
    ]
  }
}

source_set("android_video_decode_accelerator_unittests") {
  if (is_android) {
    testonly = true
    sources = [
      "android/android_video_surface_chooser_impl_unittest.cc",
      "android/codec_allocator_unittest.cc",
      "android/codec_image_group_unittest.cc",
      "android/codec_image_unittest.cc",
      "android/codec_wrapper_unittest.cc",
      "android/fake_codec_allocator.cc",
      "android/fake_codec_allocator.h",
      "android/frame_info_helper_unittest.cc",
      "android/maybe_render_early_manager_unittest.cc",
      "android/media_codec_video_decoder_unittest.cc",
      "android/mock_android_video_surface_chooser.cc",
      "android/mock_android_video_surface_chooser.h",
      "android/mock_codec_buffer_wait_coordinator.cc",
      "android/mock_codec_buffer_wait_coordinator.h",
      "android/mock_codec_image.cc",
      "android/mock_codec_image.h",
      "android/mock_device_info.cc",
      "android/mock_device_info.h",
      "android/mock_promotion_hint_aggregator.cc",
      "android/mock_promotion_hint_aggregator.h",
      "android/mock_shared_image_video_provider.cc",
      "android/mock_shared_image_video_provider.h",
      "android/ndk_media_codec_wrapper_unittest.cc",
      "android/ndk_video_encode_accelerator_tests.cc",
      "android/pooled_shared_image_video_provider_unittest.cc",
      "android/promotion_hint_aggregator_impl_unittest.cc",
      "android/surface_chooser_helper_unittest.cc",
      "android/video_frame_factory_impl_unittest.cc",
    ]
    deps = [
      ":gpu",
      "//base/test:test_support",
      "//gpu:test_support",
      "//gpu/command_buffer/service:android_texture_owner_test_support",
      "//media",
      "//media:test_support",
      "//testing/gmock",
      "//testing/gtest",
      "//third_party/libyuv:libyuv",
      "//ui/gl",
      "//ui/gl/init",
    ]
  }
}

static_library("test_support") {
  visibility = [ "//media/gpu/*" ]
  testonly = true
  sources = [
    "test/fake_command_buffer_helper.cc",
    "test/fake_command_buffer_helper.h",
  ]
  configs += [ "//media:media_config" ]
  public_deps = [
    ":gpu",
    "//base",
    "//media",
  ]
}

source_set("unit_tests") {
  testonly = true
  deps = [
    "//base",
    "//base/test:test_support",
    "//media:test_support",
    "//media/gpu",
    "//media/gpu:test_support",
    "//testing/gmock",
    "//testing/gtest",
    "//ui/gl:test_support",
  ]
  sources = [
    "exponential_moving_average_unittest.cc",
    "frame_size_estimator_unittest.cc",
    "h264_decoder_unittest.cc",
    "h264_rate_control_util_unittest.cc",
    "h264_rate_controller_unittest.cc",
    "h264_ratectrl_rtc_unittest.cc",
    "hrd_buffer_unittest.cc",
  ]
  if (enable_hevc_parser_and_hw_decoder) {
    sources += [ "h265_decoder_unittest.cc" ]
  }
  if (is_linux || is_chromeos) {
    sources += [ "buffer_validation_unittest.cc" ]

    deps += [ ":buffer_validation" ]
  }

  # TODO(b/219079009): consider running these tests without VA-API or V4L2.
  if (is_chromeos && (use_v4l2_codec || use_vaapi)) {
    deps += [ "//media/gpu/chromeos:unit_tests" ]
  }
  if (use_vaapi) {
    deps += [ "//media/gpu/vaapi:unit_test" ]
  }
  if (use_v4l2_codec) {
    deps += [ "//media/gpu/v4l2:unit_test" ]
  }

  # TODO(crbug.com/1006266): consider using |use_chromeos_video_acceleration|.
  if (use_v4l2_codec || use_vaapi) {
    sources += [ "vp8_decoder_unittest.cc" ]
  }

  if (use_av1_hw_decoder && media_use_ffmpeg) {
    sources += [ "av1_decoder_unittest.cc" ]
    deps += [
      "//build:chromeos_buildflags",
      "//third_party/ffmpeg",
    ]
  }

  if (is_win) {
    sources += [
      "windows/d3d11_copying_texture_wrapper_unittest.cc",
      "windows/d3d11_decoder_configurator_unittest.cc",
      "windows/d3d11_picture_buffer_unittest.cc",
      "windows/d3d11_texture_selector_unittest.cc",
      "windows/d3d11_texture_wrapper_unittest.cc",
      "windows/d3d11_video_decoder_unittest.cc",
      "windows/d3d11_video_device_format_support_unittest.cc",
      "windows/d3d11_video_processor_proxy_unittest.cc",
      "windows/d3d12_helpers_unittest.cc",
      "windows/mf_audio_encoder_unittest.cc",
      "windows/mf_video_processor_accelerator_unittest.cc",
      "windows/scoped_d3d_buffers_unittest.cc",
      "windows/supported_profile_helpers_unittest.cc",
    ]
    libs = [ "dxguid.lib" ]
  }

  if (is_apple) {
    deps += [ "//media/gpu/mac:unit_tests" ]
  }

  if (is_win || use_vaapi) {
    sources += [
      "av1_builder_unittest.cc",
      "h264_builder_unittest.cc",
      "vp9_svc_layers_unittest.cc",
    ]
  }
}

if (enable_hevc_parser_and_hw_decoder) {
  fuzzer_test("media_h265_decoder_fuzzer") {
    sources = [ "h265_decoder_fuzzer.cc" ]
    deps = [
      ":gpu",
      "//base",
      "//media:test_support",
    ]
  }
}

fuzzer_test("media_h264_decoder_fuzzer") {
  sources = [ "h264_decoder_fuzzer.cc" ]
  deps = [
    ":gpu",
    "//base",
    "//media:test_support",
  ]
  seed_corpuses = [ "//media/test/data" ]
}

# TODO(crbug.com/1006266): consider using |use_chromeos_video_acceleration|.
if (use_v4l2_codec || use_vaapi) {
  fuzzer_test("media_vp8_decoder_fuzzer") {
    sources = [ "vp8_decoder_fuzzer.cc" ]
    deps = [
      ":common",
      ":gpu",
      "//base",
      "//media:test_support",
    ]
    seed_corpuses = [ "//media/test/data" ]
  }
}

if (use_av1_hw_decoder) {
  fuzzer_test("media_av1_decoder_fuzzer") {
    sources = [ "av1_decoder_fuzzertest.cc" ]
    deps = [
      ":common",
      "//base",
      "//media:test_support",
    ]
  }
}

fuzzer_test("media_vp9_decoder_fuzzer") {
  sources = [ "vp9_decoder_fuzzer.cc" ]
  deps = [
    ":gpu",
    "//base",
    "//media:test_support",
  ]
  seed_corpuses = [ "//media/test/data" ]
}