File: display_color_spaces.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (270 lines) | stat: -rw-r--r-- 8,916 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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 <cmath>

#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)];
}

ColorSpace DisplayColorSpaces::GetRasterAndCompositeColorSpace(
    ContentColorUsage color_usage) const {
  gfx::ColorSpace result;
  if (color_usage == ContentColorUsage::kSRGB) {
    result =
        GetOutputColorSpace(ContentColorUsage::kSRGB, /*needs_alpha=*/true);
    if (!result.IsSuitableForBlending()) {
      result = ColorSpace::CreateSRGB();
    }
  } else {
    result = GetOutputColorSpace(ContentColorUsage::kWideColorGamut,
                                 /*needs_alpha=*/true);

    // The below logic is to work around the issue that Windows' output buffer
    // choices are limited. It is not a generic operation. Windows only offers
    // three usable options for backbuffer formats:
    // * 8-bit sRGB
    // * 10-bit Rec2020 PQ
    // * F16 sRGB linear HDR (identical to PQ, with 1.0 matching 80 nits)
    // Absent in this list is an 8-bit wide color gamut option (like P3). This
    // gives us the options of:
    // * Raster wide color gamut content into F16 extended-sRGB buffers.
    //   This is expensive, but raster/composite results are always consistent.
    // * Raster wide color gamut into 8-bit P3 buffers
    //   This isn't expensive, but raster/composite results differ based on
    //   whether or not WCG content is visible.
    // We go with the second option, and do all non-sRGB raster and composite
    // in P3.
    if (!result.IsSuitableForBlending()) {
      result =
          ColorSpace(ColorSpace::PrimaryID::P3, ColorSpace::TransferID::SRGB);
    }

    // Report the HDR version of the space only if HDR is both requested and
    // supported.
    if (SupportsHDR() && color_usage == ContentColorUsage::kHDR) {
      result = result.GetAsHDR();
    }
  }

  return result;
}

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

float DisplayColorSpaces::GetHdrHeadroom() const {
  return std::log2(hdr_max_luminance_relative_);
}

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;
}

DisplayColorSpacesRef::DisplayColorSpacesRef() = default;

DisplayColorSpacesRef::DisplayColorSpacesRef(
    const gfx::DisplayColorSpaces& color_spaces)
    : color_spaces_(color_spaces) {}

}  // namespace gfx