File: display_color_spaces.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (229 lines) | stat: -rw-r--r-- 7,329 bytes parent folder | download | duplicates (4)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/354829279): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ui/gfx/display_color_spaces.h"

#include <array>

#include "build/build_config.h"
#include "skia/ext/skcolorspace_primaries.h"

namespace gfx {

namespace {

const ContentColorUsage kAllColorUsages[] = {
    ContentColorUsage::kSRGB,
    ContentColorUsage::kWideColorGamut,
    ContentColorUsage::kHDR,
};

gfx::BufferFormat DefaultBufferFormat() {
  // ChromeOS expects the default buffer format be BGRA_8888 in several places.
  // https://crbug.com/1057501, https://crbug.com/1073237
  // The default format on Mac is BGRA in screen_mac.cc, so we set it here
  // too so that it matches with --ensure-forced-color-profile.
  // https://crbug.com/1478708
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC)
  return gfx::BufferFormat::BGRA_8888;
#else
  return gfx::BufferFormat::RGBA_8888;
#endif
}

size_t GetIndex(ContentColorUsage color_usage, bool needs_alpha) {
  switch (color_usage) {
    case ContentColorUsage::kSRGB:
      return 0 + needs_alpha;
    case ContentColorUsage::kWideColorGamut:
      return 2 + needs_alpha;
    case ContentColorUsage::kHDR:
      return 4 + needs_alpha;
  }
}

}  // namespace

DisplayColorSpaces::DisplayColorSpaces() {
  // TODO(crbug.com/40219387): Revert back to range-based for loops if possible
  for (size_t i = 0; i < kConfigCount; i++) {
    color_spaces_[i] = gfx::ColorSpace::CreateSRGB();
    buffer_formats_[i] = DefaultBufferFormat();
  }
}

DisplayColorSpaces::DisplayColorSpaces(const gfx::DisplayColorSpaces&) =
    default;

DisplayColorSpaces& DisplayColorSpaces::operator=(
    const gfx::DisplayColorSpaces&) = default;

DisplayColorSpaces::DisplayColorSpaces(const gfx::ColorSpace& c)
    : DisplayColorSpaces() {
  if (!c.IsValid())
    return;
  primaries_ = c.GetPrimaries();
  for (size_t i = 0; i < kConfigCount; i++)  // NOLINT (modernize-loop-convert)
    color_spaces_[i] = c;
}

DisplayColorSpaces::DisplayColorSpaces(const ColorSpace& c, BufferFormat f)
    : DisplayColorSpaces(c) {
  for (size_t i = 0; i < kConfigCount; i++) {
    buffer_formats_[i] = f;
  }
}

void DisplayColorSpaces::SetOutputBufferFormats(
    gfx::BufferFormat buffer_format_no_alpha,
    gfx::BufferFormat buffer_format_needs_alpha) {
  for (const auto& color_usage : kAllColorUsages) {
    size_t i_no_alpha = GetIndex(color_usage, false);
    size_t i_needs_alpha = GetIndex(color_usage, true);
    buffer_formats_[i_no_alpha] = buffer_format_no_alpha;
    buffer_formats_[i_needs_alpha] = buffer_format_needs_alpha;
  }
}

void DisplayColorSpaces::SetOutputColorSpaceAndBufferFormat(
    ContentColorUsage color_usage,
    bool needs_alpha,
    const gfx::ColorSpace& color_space,
    gfx::BufferFormat buffer_format) {
  size_t i = GetIndex(color_usage, needs_alpha);
  color_spaces_[i] = color_space;
  buffer_formats_[i] = buffer_format;
}

ColorSpace DisplayColorSpaces::GetOutputColorSpace(
    ContentColorUsage color_usage,
    bool needs_alpha) const {
  return color_spaces_[GetIndex(color_usage, needs_alpha)];
}

BufferFormat DisplayColorSpaces::GetOutputBufferFormat(
    ContentColorUsage color_usage,
    bool needs_alpha) const {
  return buffer_formats_[GetIndex(color_usage, needs_alpha)];
}

gfx::ColorSpace DisplayColorSpaces::GetRasterColorSpace() const {
  return GetOutputColorSpace(ContentColorUsage::kHDR, false /* needs_alpha */);
}

gfx::ColorSpace DisplayColorSpaces::GetCompositingColorSpace(
    bool needs_alpha,
    ContentColorUsage color_usage) const {
  gfx::ColorSpace result = GetOutputColorSpace(color_usage, needs_alpha);
  if (!result.IsSuitableForBlending())
    result = gfx::ColorSpace::CreateExtendedSRGB();
  return result;
}

bool DisplayColorSpaces::SupportsHDR() const {
  return GetOutputColorSpace(ContentColorUsage::kHDR, false).IsHDR() ||
         GetOutputColorSpace(ContentColorUsage::kHDR, true).IsHDR() ||
         hdr_max_luminance_relative_ > 1.f;
}

ColorSpace DisplayColorSpaces::GetScreenInfoColorSpace() const {
  return GetOutputColorSpace(ContentColorUsage::kHDR, false /* needs_alpha */);
}

void DisplayColorSpaces::ToStrings(
    std::vector<std::string>* out_names,
    std::vector<gfx::ColorSpace>* out_color_spaces,
    std::vector<gfx::BufferFormat>* out_buffer_formats) const {
  // The names of the configurations.
  std::array<const char*, kConfigCount> config_names = {
      "sRGB/no-alpha", "sRGB/alpha",   "WCG/no-alpha",
      "WCG/alpha",     "HDR/no-alpha", "HDR/alpha",
  };
  // Names for special configuration subsets (e.g, all sRGB, all WCG, etc).
  constexpr size_t kSpecialConfigCount = 5;
  std::array<const char*, kSpecialConfigCount> special_config_names = {
      "sRGB", "WCG", "SDR", "HDR", "all",
  };
  const std::array<std::array<const size_t, 2>, kSpecialConfigCount>
      special_config_indices = {{
          {0, 2},
          {2, 4},
          {0, 4},
          {4, 6},
          {0, 6},
      }};

  // We don't want to take up 6 lines (one for each config) if we don't need to.
  // To avoid this, build up half-open intervals [i, j) which have the same
  // color space and buffer formats, and group them together. The above "special
  // configs" give groups that have a common name.
  size_t i = 0;
  size_t j = 0;
  while (i != kConfigCount) {
    // Keep growing the interval [i, j) until entry j is different, or past the
    // end.
    if (color_spaces_[i] == color_spaces_[j] &&
        buffer_formats_[i] == buffer_formats_[j] && j != kConfigCount) {
      j += 1;
      continue;
    }

    // Populate the name for the group from the "special config" names.
    std::string name;
    for (size_t k = 0; k < kSpecialConfigCount; ++k) {
      if (i == special_config_indices[k][0] &&
          j == special_config_indices[k][1]) {
        name = special_config_names[k];
        break;
      }
    }
    // If that didn't work, just list the configs.
    if (name.empty()) {
      for (size_t k = i; k < j; ++k) {
        name += std::string(config_names[k]);
        if (k != j - 1)
          name += ",";
      }
    }

    // Add an entry, and continue with the interval [j, j).
    out_names->push_back(name);
    out_buffer_formats->push_back(buffer_formats_[i]);
    out_color_spaces->push_back(color_spaces_[i]);
    i = j;
  };
}

bool DisplayColorSpaces::operator==(const DisplayColorSpaces& other) const {
  for (size_t i = 0; i < kConfigCount; ++i) {
    if (color_spaces_[i] != other.color_spaces_[i])
      return false;
    if (buffer_formats_[i] != other.buffer_formats_[i])
      return false;
  }
  if (primaries_ != other.primaries_)
    return false;
  if (sdr_max_luminance_nits_ != other.sdr_max_luminance_nits_)
    return false;
  if (hdr_max_luminance_relative_ != other.hdr_max_luminance_relative_)
    return false;

  return true;
}

// static
bool DisplayColorSpaces::EqualExceptForHdrHeadroom(
    const DisplayColorSpaces& a,
    const DisplayColorSpaces& b) {
  DisplayColorSpaces b_with_a_params = b;
  b_with_a_params.hdr_max_luminance_relative_ = a.hdr_max_luminance_relative_;
  return a == b_with_a_params;
}

}  // namespace gfx