display_color_spaces.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef UI_GFX_DISPLAY_COLOR_SPACES_H_
  5. #define UI_GFX_DISPLAY_COLOR_SPACES_H_
  6. #include <string>
  7. #include <vector>
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #include "ui/gfx/buffer_types.h"
  10. #include "ui/gfx/color_space.h"
  11. #include "ui/gfx/color_space_export.h"
  12. #include "ui/gfx/hdr_static_metadata.h"
  13. namespace mojo {
  14. template <class T, class U>
  15. struct StructTraits;
  16. } // namespace mojo
  17. namespace gfx {
  18. namespace mojom {
  19. class DisplayColorSpacesDataView;
  20. } // namespace mojom
  21. // The values are set so std::max() can be used to find the widest.
  22. enum class ContentColorUsage : uint8_t {
  23. // These values are histogrammed over time; do not change their ordinal
  24. // values. When deleting a color usage replace it with a dummy value; when
  25. // adding a color usage, do so at the bottom (and update kMaxValue).
  26. kSRGB = 0,
  27. kWideColorGamut = 1,
  28. kHDR = 2,
  29. kMaxValue = kHDR,
  30. };
  31. // This structure is used by a display::Display to specify the color space that
  32. // should be used to display content of various types. This lives in here, as
  33. // opposed to in ui/display because it is used directly by components/viz.
  34. class COLOR_SPACE_EXPORT DisplayColorSpaces {
  35. public:
  36. static constexpr size_t kConfigCount = 6;
  37. // Initialize as sRGB-only.
  38. DisplayColorSpaces();
  39. DisplayColorSpaces(const DisplayColorSpaces& display_color_space);
  40. DisplayColorSpaces& operator=(const DisplayColorSpaces& display_color_space);
  41. // Initialize as |color_space| for all settings. If |color_space| is the
  42. // default (invalid) color space, then initialize to sRGB. The BufferFormat
  43. // will be set to a default value (BGRA_8888 or RGBA_8888) depending on
  44. // build configuration.
  45. explicit DisplayColorSpaces(const ColorSpace& color_space);
  46. // Initialize as |color_space| and |buffer_format| for all settings. If
  47. // |color_space| is the default (invalid) color space, then initialize to
  48. // sRGB.
  49. DisplayColorSpaces(const ColorSpace& color_space, BufferFormat buffer_format);
  50. // Set the color space and buffer format for the final output surface when the
  51. // specified content is being displayed.
  52. void SetOutputColorSpaceAndBufferFormat(ContentColorUsage color_usage,
  53. bool needs_alpha,
  54. const gfx::ColorSpace& color_space,
  55. gfx::BufferFormat buffer_format);
  56. // Set the buffer format for all color usages to |buffer_format_no_alpha| when
  57. // alpha is not needed and |buffer_format_with_alpha| when alpha is needed.
  58. void SetOutputBufferFormats(gfx::BufferFormat buffer_format_no_alpha,
  59. gfx::BufferFormat buffer_format_with_alpha);
  60. // Retrieve parameters for a specific usage and alpha.
  61. ColorSpace GetOutputColorSpace(ContentColorUsage color_usage,
  62. bool needs_alpha) const;
  63. BufferFormat GetOutputBufferFormat(ContentColorUsage color_usage,
  64. bool needs_alpha) const;
  65. // Set the maximum SDR luminance, in nits. This is a non-default value only
  66. // on Windows.
  67. void SetSDRMaxLuminanceNits(float sdr_max_luminance_nits) {
  68. sdr_max_luminance_nits_ = sdr_max_luminance_nits;
  69. }
  70. float GetSDRMaxLuminanceNits() const { return sdr_max_luminance_nits_; }
  71. // Set the maximum luminance that HDR content can display. This is represented
  72. // as a multiple of the SDR white luminance (so a display that is incapable of
  73. // HDR would have a value of 1.0).
  74. void SetHDRMaxLuminanceRelative(float hdr_max_luminance_relative) {
  75. hdr_max_luminance_relative_ = hdr_max_luminance_relative;
  76. }
  77. float GetHDRMaxLuminanceRelative() const {
  78. return hdr_max_luminance_relative_;
  79. }
  80. // TODO(https://crbug.com/1116870): These helper functions exist temporarily
  81. // to handle the transition of display::ScreenInfo off of ColorSpace. All
  82. // calls to these functions are to be eliminated.
  83. ColorSpace GetScreenInfoColorSpace() const;
  84. // Return the color space that should be used for rasterization.
  85. // TODO: This will eventually need to take a ContentColorUsage.
  86. gfx::ColorSpace GetRasterColorSpace() const;
  87. // Return the color space in which compositing (and, in particular, blending,
  88. // should be performed). This space may not (on Windows) be suitable for
  89. // output.
  90. gfx::ColorSpace GetCompositingColorSpace(bool needs_alpha,
  91. ContentColorUsage color_usage) const;
  92. // Return true if the HDR color spaces are, indeed, HDR.
  93. bool SupportsHDR() const;
  94. // Return the primaries that define the color gamut of the display.
  95. SkColorSpacePrimaries GetPrimaries() const;
  96. // Output as a vector of strings. This is a helper function for printing in
  97. // about:gpu. All output vectors will be the same length. Each entry will be
  98. // the configuration name, its buffer format, and its color space.
  99. void ToStrings(std::vector<std::string>* out_names,
  100. std::vector<gfx::ColorSpace>* out_color_spaces,
  101. std::vector<gfx::BufferFormat>* out_buffer_formats) const;
  102. bool operator==(const DisplayColorSpaces& other) const;
  103. bool operator!=(const DisplayColorSpaces& other) const;
  104. private:
  105. // Serialization of DisplayColorSpaces directly accesses members.
  106. friend struct IPC::ParamTraits<gfx::DisplayColorSpaces>;
  107. friend struct mojo::StructTraits<gfx::mojom::DisplayColorSpacesDataView,
  108. gfx::DisplayColorSpaces>;
  109. gfx::ColorSpace color_spaces_[kConfigCount];
  110. gfx::BufferFormat buffer_formats_[kConfigCount];
  111. float sdr_max_luminance_nits_ = ColorSpace::kDefaultSDRWhiteLevel;
  112. float hdr_max_luminance_relative_ = 1.f;
  113. };
  114. } // namespace gfx
  115. #endif // UI_GFX_DISPLAY_COLOR_SPACES_H_