# Copyright 2014 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/config/cast.gni")
import("//build/config/chrome_build.gni")
import("//build/config/ui.gni")
import("//gpu/vulkan/features.gni")
import("//skia/features.gni")
import("//ui/gl/features.gni")

if (is_android) {
  import("//build/config/android/rules.gni")
}

process_json_outputs = [
  "$target_gen_dir/gpu_driver_bug_list_arrays_and_structs_autogen.h",
  "$target_gen_dir/gpu_driver_bug_list_autogen.cc",
  "$target_gen_dir/gpu_driver_bug_list_autogen.h",
  "$target_gen_dir/gpu_driver_bug_list_exceptions_autogen.h",
  "$target_gen_dir/software_rendering_list_arrays_and_structs_autogen.h",
  "$target_gen_dir/software_rendering_list_autogen.cc",
  "$target_gen_dir/software_rendering_list_autogen.h",
  "$target_gen_dir/software_rendering_list_exceptions_autogen.h",
]

action("process_json") {
  script = "process_json.py"

  inputs = [
    "gpu_driver_bug_list.json",
    "gpu_driver_bug_workaround_type.h",
    "gpu_feature_type.h",
    "software_rendering_list.json",
  ]

  outputs = process_json_outputs

  args = [
    "--output-dir",
    rebase_path("$target_gen_dir", root_build_dir),
    "--skip-testing-data",
    "--os-filter",
  ]
  if (is_win) {
    args += [ "win" ]
  } else if (is_mac) {
    args += [ "macosx" ]
  } else if (is_android || is_ohos) {
    args += [ "android" ]
  } else if (is_chromeos) {
    args += [ "chromeos" ]
  } else if (is_fuchsia) {
    args += [ "fuchsia" ]
  } else {
    args += [ "linux" ]
  }

  public_deps = [ ":workaround_list" ]
}

action("workaround_list") {
  script = "build_workaround_header.py"

  skia_workaround_list = "//third_party/skia/src/gpu/gpu_workaround_list.txt"
  inputs = [
    "gpu_workaround_list.txt",
    skia_workaround_list,
  ]

  outputs = [ "$target_gen_dir/gpu_driver_bug_workaround_autogen.h" ]

  args = [
    "--output-file",
    rebase_path(outputs[0], root_build_dir),
  ]
  foreach(file, inputs) {
    args += [ rebase_path(file, root_build_dir) ]
  }
}

component("config") {
  sources = [
    "device_perf_info.cc",
    "device_perf_info.h",
    "gpu_blocklist.cc",
    "gpu_blocklist.h",
    "gpu_config_export.h",
    "gpu_control_list.cc",
    "gpu_control_list.h",
    "gpu_crash_keys.cc",
    "gpu_crash_keys.h",
    "gpu_domain_guilt.h",
    "gpu_driver_bug_list.cc",
    "gpu_driver_bug_list.h",
    "gpu_driver_bug_workaround_type.h",
    "gpu_driver_bug_workarounds.cc",
    "gpu_driver_bug_workarounds.h",
    "gpu_feature_info.cc",
    "gpu_feature_info.h",
    "gpu_feature_type.h",
    "gpu_finch_features.cc",
    "gpu_finch_features.h",
    "//arkweb/chromium_ext/gpu/config/gpu_finch_features_ext.cc",
    "//arkweb/chromium_ext/gpu/config/gpu_finch_features_ext.h",
    "gpu_info.cc",
    "gpu_info.h",
    "gpu_info_collector.cc",
    "gpu_info_collector.h",
    "gpu_mode.h",
    "gpu_preferences.cc",
    "gpu_preferences.h",
    "gpu_switches.cc",
    "gpu_switches.h",
    "gpu_switching.cc",
    "gpu_switching.h",
    "gpu_test_config.cc",
    "gpu_test_config.h",
    "gpu_test_expectations_parser.cc",
    "gpu_test_expectations_parser.h",
    "gpu_util.cc",
    "gpu_util.h",
    "skia_limits.cc",
    "skia_limits.h",
    "webgpu_blocklist_impl.cc",
    "webgpu_blocklist_impl.h",
  ]

  if (is_fuchsia) {
    sources += [ "gpu_info_collector_fuchsia.cc" ]
  }

  sources += process_json_outputs

  defines = [ "GPU_CONFIG_IMPLEMENTATION" ]

  configs += [ "//build/config/compiler:wexit_time_destructors" ]

  public_deps = [
    "//components/crash/core/common:crash_key",
    "//third_party/dawn/include/dawn:cpp_headers",
  ]

  if (enable_vulkan) {
    public_deps += [ "//gpu/vulkan" ]
  }

  deps = [
    ":process_json",
    "//base",
    "//build:branding_buildflags",
    "//build:chromecast_buildflags",
    "//components/viz/common/resources:shared_image_format",
    "//gpu/command_buffer/common",
    "//gpu/ipc/common:gpu_preferences_interface",
    "//gpu/vulkan:buildflags",
    "//services/webnn/public/mojom:features",
    "//skia:buildflags",
    "//third_party/re2",
    "//third_party/vulkan-headers/src:vulkan_headers",
    "//ui/gfx",
    "//ui/gl",
    "//ui/gl:buildflags",
    "//ui/gl:gl_headers",
    "//ui/gl/init",
  ]

  if (enable_vulkan) {
    deps += [ "//gpu/ipc/common:vulkan_interface" ]
  }

  if (use_dawn || skia_use_dawn) {
    public_deps += [ "//third_party/dawn/include/dawn:headers" ]

    deps += [
      "//third_party/dawn/include/dawn:cpp_headers",
      "//third_party/dawn/src/dawn:proc",
      "//third_party/dawn/src/dawn/native",
    ]
  }

  # GpuPreferences is using its own mojo bindings which creates a
  # cycle between this target and gpu_preferences_interface.
  allow_circular_includes_from =
      [ "//gpu/ipc/common:gpu_preferences_interface" ]

  if (is_android) {
    sources += [ "gpu_info_collector_android.cc" ]
  }
  if (is_win) {
    sources += [ "gpu_info_collector_win.cc" ]
    deps += [ "//third_party/microsoft_dxheaders:dxguids" ]
    libs = [ "dxgi.lib" ]
  }
  if (is_mac) {
    sources += [ "gpu_info_collector_mac.mm" ]
    frameworks = [ "OpenGL.framework" ]
  }
  if (is_ios) {
    sources += [ "gpu_info_collector_ios.mm" ]
  }
  if (is_linux || is_chromeos || is_ohos) {
    sources += [ "gpu_info_collector_linux.cc" ]
  }
  if (is_linux || is_chromeos || is_apple || is_fuchsia || is_android || is_ohos) {
    deps += [ "//third_party/angle:angle_gpu_info_util" ]
  }
  if (use_ozone) {
    deps += [ "//ui/ozone" ]
  }
  if (is_ohos) {
    deps += [ "//ohos_glue:ohos_adapter_glue_source" ]
  }
}

if (is_android) {
  java_cpp_strings("java_switches_srcjar") {
    # External code should depend on ":config_java" instead.
    visibility = [ ":*" ]
    sources = [ "gpu_switches.cc" ]
    class_name = "org.chromium.gpu.config.GpuSwitches"
  }

  java_cpp_features("java_features_srcjar") {
    # External code should depend on ":config_java" instead.
    visibility = [ ":*" ]
    sources = [ "gpu_finch_features.cc" ]
    class_name = "org.chromium.gpu.config.GpuFeatures"
  }

  android_library("config_java") {
    # Right now, this only includes the Java switches/features. But if we need
    # more Java files, they should be added here as necessary.
    srcjar_deps = [
      ":java_features_srcjar",
      ":java_switches_srcjar",
    ]
  }
}