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

#ifndef GPU_COMMAND_BUFFER_SERVICE_FEATURE_INFO_H_
#define GPU_COMMAND_BUFFER_SERVICE_FEATURE_INFO_H_

#include <memory>
#include <string>
#include <string_view>

#include "base/memory/ref_counted.h"
#include "gpu/command_buffer/common/context_creation_attribs.h"
#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
#include "gpu/command_buffer/service/gles2_cmd_validation.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "gpu/config/gpu_feature_info.h"
#include "gpu/gpu_gles2_export.h"
#include "ui/gfx/extension_set.h"

namespace base {
class CommandLine;
}

namespace gl {
struct GLVersionInfo;
}

namespace gpu {
namespace gles2 {

// FeatureInfo records the features that are available for a ContextGroup.
class GPU_GLES2_EXPORT FeatureInfo : public base::RefCounted<FeatureInfo> {
 public:
  struct FeatureFlags {
    FeatureFlags();

    gfx::GpuMemoryBufferFormatSet gpu_memory_buffer_formats = {
        gfx::BufferFormat::BGR_565,   gfx::BufferFormat::RGBA_4444,
        gfx::BufferFormat::RGBA_8888, gfx::BufferFormat::RGBX_8888,
        gfx::BufferFormat::YVU_420,   gfx::BufferFormat::YUV_420_BIPLANAR,
    };
    // Use glBlitFramebuffer() and glRenderbufferStorageMultisample() with
    // GL_EXT_framebuffer_multisample-style semantics (as opposed to
    // GL_EXT_multisampled_render_to_texture semantics).
    bool chromium_framebuffer_multisample = false;
    bool chromium_sync_query = false;
    bool multisampled_render_to_texture = false;
    // Use the IMG GLenum values rather than EXT.
    bool use_img_for_multisampled_render_to_texture = false;
    bool chromium_screen_space_antialiasing = false;
    bool use_chromium_screen_space_antialiasing_via_shaders = false;
    bool oes_standard_derivatives = false;
    bool oes_egl_image_external = false;
    bool oes_egl_image_external_essl3 = false;
    bool nv_egl_stream_consumer_external = false;
    bool oes_depth24 = false;
    bool oes_compressed_etc1_rgb8_texture = false;
    bool packed_depth24_stencil8 = false;
    bool npot_ok = false;
    bool enable_texture_filter_anisotropic = false;
    bool enable_texture_float_linear = false;
    bool enable_texture_half_float_linear = false;
    bool enable_color_buffer_float = false;
    bool enable_color_buffer_half_float = false;
    bool angle_translated_shader_source = false;
    bool angle_pack_reverse_row_order = false;
    bool arb_texture_rectangle = false;
    bool angle_instanced_arrays = false;
    bool occlusion_query_boolean = false;
    bool native_vertex_array_object = false;
    bool ext_texture_format_astc = false;
    bool ext_texture_format_astc_hdr = false;
    bool ext_texture_format_atc = false;
    bool ext_texture_format_bgra8888 = false;
    bool ext_texture_format_dxt1 = false;
    bool ext_texture_format_dxt5 = false;
    bool enable_shader_name_hashing = false;
    bool enable_samplers = false;
    bool ext_draw_buffers = false;
    bool nv_draw_buffers = false;
    bool ext_frag_depth = false;
    bool ext_shader_texture_lod = false;
    bool use_async_readpixels = false;
    bool map_buffer_range = false;
    bool ext_discard_framebuffer = false;
    bool angle_depth_texture = false;
    bool is_software_webgl = false;
    bool angle_texture_usage = false;
    bool ext_texture_storage = false;
    bool blend_equation_advanced = false;
    bool blend_equation_advanced_coherent = false;
    bool ext_texture_rg = false;
    bool ext_texture_norm16 = false;
    bool chromium_image_ycbcr_420v = false;
    bool chromium_image_ar30 = false;
    bool chromium_image_ab30 = false;
    bool chromium_image_ycbcr_p010 = false;
    bool emulate_primitive_restart_fixed_index = false;
    bool ext_render_buffer_format_bgra8888 = false;
    bool ext_multisample_compatibility = false;
    bool ext_blend_func_extended = false;
    bool ext_read_format_bgra = false;
    bool chromium_color_buffer_float_rgb = false;
    bool chromium_color_buffer_float_rgba = false;
    bool angle_robust_client_memory = false;
    bool khr_debug = false;
    bool chromium_bind_generates_resource = false;
    bool angle_webgl_compatibility = false;
    bool ext_srgb_write_control = false;
    bool ext_srgb = false;
    bool chromium_copy_texture = false;
    bool chromium_copy_compressed_texture = false;
    bool ext_disjoint_timer_query = false;
    bool angle_client_arrays = false;
    bool angle_request_extension = false;
    bool ext_debug_marker = false;
    bool ext_pixel_buffer_object = false;
    bool ext_unpack_subimage = false;
    bool oes_rgb8_rgba8 = false;
    bool angle_robust_resource_initialization = false;
    bool nv_fence = false;
    bool ext_window_rectangles = false;
    bool chromium_gpu_fence = false;
    bool separate_stencil_ref_mask_writemask = false;
    bool mesa_framebuffer_flip_y = false;
    bool ovr_multiview2 = false;
    bool khr_parallel_shader_compile = false;
    bool khr_robust_buffer_access_behavior = false;
    bool webgl_multi_draw = false;
    bool nv_internalformat_sample_query = false;
    bool amd_framebuffer_multisample_advanced = false;
    bool ext_float_blend = false;
    bool chromium_completion_query = false;
    bool oes_fbo_render_mipmap = false;
    bool webgl_draw_instanced_base_vertex_base_instance = false;
    bool webgl_multi_draw_instanced_base_vertex_base_instance = false;
    bool ext_texture_compression_bptc = false;
    bool ext_texture_compression_rgtc = false;
    bool oes_draw_buffers_indexed = false;
    bool ext_yuv_target = false;
    bool angle_shader_pixel_local_storage = false;
    bool angle_rgbx_internal_format = false;
    bool angle_provoking_vertex = false;
    bool angle_clip_cull_distance = false;
    bool angle_polygon_mode = false;
    bool ext_clip_control = false;
    bool ext_polygon_offset_clamp = false;
    bool angle_blob_cache = false;
  };

  FeatureInfo();

  // Constructor with workarounds taken from the current process's CommandLine
  FeatureInfo(const GpuDriverBugWorkarounds& gpu_driver_bug_workarounds,
              const GpuFeatureInfo& gpu_feature_info);

  FeatureInfo(const FeatureInfo&) = delete;
  FeatureInfo& operator=(const FeatureInfo&) = delete;

  // Initializes the feature information. Needs a current GL context.
  void Initialize(ContextType context_type,
                  bool is_passthrough_cmd_decoder,
                  const DisallowedFeatures& disallowed_features);

  // Same as initialize but with a provided `complete_fbo_for_workarounds` to
  // use with the ensure_previous_framebuffer_not_deleted driver bug workaround.
  void InitializeWithCompleteFramebufferForWorkarounds(
      ContextType context_type,
      bool is_passthrough_cmd_decoder,
      const DisallowedFeatures& disallowed_features,
      uint32_t complete_fbo_for_workarounds);

  // Same as above, but allows reinitialization.
  void ForceReinitialize();

  // Helper that defaults to no disallowed features and a GLES2 context.
  void InitializeForTesting();
  // Helper that defaults to no disallowed Features.
  void InitializeForTesting(ContextType context_type);
  // Helper that defaults to a GLES2 context.
  void InitializeForTesting(const DisallowedFeatures& disallowed_features);

  const Validators* validators() const {
    return &validators_;
  }

  ContextType context_type() const { return context_type_; }

  const gfx::ExtensionSet& extensions() const { return extensions_; }

  const FeatureFlags& feature_flags() const {
    return feature_flags_;
  }

  const GpuDriverBugWorkarounds& workarounds() const { return workarounds_; }

  const DisallowedFeatures& disallowed_features() const {
    return disallowed_features_;
  }

  const gl::GLVersionInfo& gl_version_info() const {
    DCHECK(gl_version_info_.get());
    return *(gl_version_info_.get());
  }

  bool IsES3Capable() const;
  void EnableES3Validators();

  bool disable_shader_translator() const { return disable_shader_translator_; }

  bool IsWebGLContext() const;
  bool IsWebGL1OrES2Context() const;
  bool IsWebGL2OrES3Context() const;
  bool IsWebGL2OrES3OrHigherContext() const;
  bool IsES31ForTestingContext() const;

  void EnableCHROMIUMColorBufferFloatRGBA();
  void EnableCHROMIUMColorBufferFloatRGB();
  void EnableEXTFloatBlend();
  void EnableEXTColorBufferFloat();
  void EnableEXTColorBufferHalfFloat();
  void EnableEXTTextureFilterAnisotropic();
  void EnableOESDrawBuffersIndexed();
  void EnableOESFboRenderMipmap();
  void EnableOESTextureFloatLinear();
  void EnableOESTextureHalfFloatLinear();

  bool ext_color_buffer_float_available() const {
    return ext_color_buffer_float_available_;
  }

  bool ext_color_buffer_half_float_available() const {
    return ext_color_buffer_half_float_available_;
  }

  bool oes_texture_float_available() const {
    return oes_texture_float_available_;
  }

  bool oes_texture_float_linear_available() const {
    return oes_texture_float_linear_available_;
  }

  bool oes_texture_half_float_linear_available() const {
    return oes_texture_half_float_linear_available_;
  }

  bool is_passthrough_cmd_decoder() const {
    return is_passthrough_cmd_decoder_;
  }

 private:
  friend class base::RefCounted<FeatureInfo>;
  friend class BufferManagerClientSideArraysTest;

  ~FeatureInfo();

  void AddExtensionString(std::string_view s);
  void InitializeBasicState(const base::CommandLine* command_line);
  void InitializeFeatures(uint32_t complete_fbo_for_workarounds);
  void InitializeFloatAndHalfFloatFeatures(const gfx::ExtensionSet& extensions);

  void EnableANGLEInstancedArrayIfPossible(const gfx::ExtensionSet& extensions);
  void EnableWEBGLMultiDrawIfPossible(const gfx::ExtensionSet& extensions);

  bool initialized_ = false;

  Validators validators_;

  DisallowedFeatures disallowed_features_;

  ContextType context_type_ = CONTEXT_TYPE_OPENGLES2;
  bool is_passthrough_cmd_decoder_ = false;

  // The set of extensions returned by glGetString(GL_EXTENSIONS);
  gfx::ExtensionSet extensions_;

  // Flags for some features
  FeatureFlags feature_flags_;

  // Flags for Workarounds.
  GpuDriverBugWorkarounds workarounds_;

  bool ext_color_buffer_float_available_ = false;
  bool ext_color_buffer_half_float_available_ = false;
  bool ext_texture_filter_anisotropic_available_ = false;
  bool oes_texture_float_available_ = false;
  bool oes_texture_float_linear_available_ = false;
  bool oes_texture_half_float_linear_available_ = false;

  bool disable_shader_translator_;
  std::unique_ptr<gl::GLVersionInfo> gl_version_info_;
};

}  // namespace gles2
}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_FEATURE_INFO_H_