File: drm_gpu_util.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 (399 lines) | stat: -rw-r--r-- 13,885 bytes parent folder | download | duplicates (5)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2018 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/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ui/ozone/platform/drm/gpu/drm_gpu_util.h"

#include <fcntl.h>
#include <xf86drm.h>
#include <xf86drmMode.h>

#include <utility>

#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
#include "ui/display/types/display_color_management.h"
#include "ui/display/types/gamma_ramp_rgb_entry.h"
#include "ui/ozone/platform/drm/common/drm_util.h"
#include "ui/ozone/platform/drm/common/hardware_display_controller_info.h"
#include "ui/ozone/platform/drm/gpu/drm_device.h"
#include "ui/ozone/platform/drm/gpu/hardware_display_plane_manager.h"

namespace ui {

namespace {

struct PossibleCrtcsForConnector {
  uint32_t connector_id;
  std::vector<uint32_t> possible_crtcs;
};

// Recursively build out all possible permutations of CRTC-connector pairings
// given a set of connectors and their possible CRTCs. Each CRTC/connector can
// only be used once per permutation (CrtcConnectorPairs).
// |connectors_it| is an iterator of |connectors| that tracks which connector
// has been used (connector left of |connectors_it|). Passing around
// |connectors_it| is safe due to the constness of |connectors|.
// |crtcs_used_in_current_permutation| tracks if a CRTC has already been used as
// part of the current permutation.
// For example:
//   Connector A can have CRTCs 1, 2, 3
//   Connector B can have CRTCs 2, 3
//   Connector C can have CRTCs 1, 3
// Returned pairings would be:
//   {{A, 1}, {B, 2}, {C, 3}},
//   {{A, 2}, {B, 1}, {C, 3}},
//   {{A, 3}, {B, 2}, {C, 1}}
// But not {{A, 1}, {B, 3}, {C, nothing}} as connector C must also be assigned
// to a valid CRTC and permutations like this are discarded.
std::vector<CrtcConnectorPairs> BuildCrtcConnectorPermutations(
    const std::vector<PossibleCrtcsForConnector>& connectors,
    std::vector<PossibleCrtcsForConnector>::iterator connectors_it,
    base::flat_set<uint32_t /*crtc_id*/>& crtcs_used_in_current_permutation) {
  if (connectors_it == connectors.end()) {
    return {};
  }

  std::vector<CrtcConnectorPairs> permutations;
  const PossibleCrtcsForConnector& connector = *connectors_it;
  // Terminate the recursion once |connectors_it| reaches the end of
  // |connectors|. Also ensures that all |permutations| will have all the
  // connectors paired up with a CRTC.
  if (connectors_it == connectors.end() - 1) {
    // Possible permutations at this point are all unused CRTCs + the current
    // connector.
    for (const uint32_t crtc_id : connector.possible_crtcs) {
      if (!crtcs_used_in_current_permutation.contains(crtc_id)) {
        permutations.push_back({CrtcConnectorPair{
            .crtc_id = crtc_id, .connector_id = connector.connector_id}});
      }
    }
    return permutations;
  }

  for (const uint32_t crtc_id : connector.possible_crtcs) {
    // Skip |crtc_id| if it is already being used in this permutation.
    if (crtcs_used_in_current_permutation.contains(crtc_id)) {
      continue;
    }

    // Mark |crtc| as being in use for the current permutation so that it isn't
    // used multiple times per CrtcConnectorPairs.
    crtcs_used_in_current_permutation.insert(crtc_id);
    std::vector<CrtcConnectorPairs> next_connector_permutations =
        BuildCrtcConnectorPermutations(connectors, connectors_it + 1,
                                       crtcs_used_in_current_permutation);
    crtcs_used_in_current_permutation.erase(crtc_id);

    // Add the current |crtc|-|connector| pair to |next_connector_permutations|
    // as part of recursively building up CrtcConnectorPairs.
    for (auto& permutation : next_connector_permutations) {
      permutation.push_back(CrtcConnectorPair{
          .crtc_id = crtc_id, .connector_id = connector.connector_id});
    }
    permutations.insert(permutations.end(), next_connector_permutations.begin(),
                        next_connector_permutations.end());
  }

  return permutations;
}

// Constants for parsing CTM values.
constexpr uint64_t kCtmSignMask = (1ull << 63);
constexpr uint64_t kCtmValueMask = ~(1ull << 63);
constexpr float kCtmValueScale = static_cast<float>(1ull << 32);

}  // namespace

ControllerConfigParams::ControllerConfigParams(
    int64_t display_id,
    scoped_refptr<DrmDevice> drm,
    uint32_t crtc,
    uint32_t connector,
    gfx::Point origin,
    std::unique_ptr<drmModeModeInfo> pmode,
    bool enable_vrr,
    uint64_t base_connector)
    : display_id(display_id),
      drm(drm),
      crtc(crtc),
      connector(connector),
      base_connector_id(base_connector ? base_connector
                                       : static_cast<uint64_t>(connector)),
      origin(origin),
      mode(std::move(pmode)),
      enable_vrr(enable_vrr) {}

ControllerConfigParams::ControllerConfigParams(
    const ControllerConfigParams& other)
    : display_id(other.display_id),
      drm(other.drm),
      crtc(other.crtc),
      connector(other.connector),
      base_connector_id(other.base_connector_id),
      origin(other.origin),
      enable_vrr(other.enable_vrr) {
  if (other.mode) {
    drmModeModeInfo mode_obj = *other.mode.get();
    mode = std::make_unique<drmModeModeInfo>(mode_obj);
  }
}

ControllerConfigParams::ControllerConfigParams(ControllerConfigParams&& other)
    : display_id(other.display_id),
      drm(other.drm),
      crtc(other.crtc),
      connector(other.connector),
      base_connector_id(other.base_connector_id),
      origin(other.origin),
      enable_vrr(other.enable_vrr) {
  if (other.mode) {
    drmModeModeInfo mode_obj = *other.mode.get();
    mode = std::make_unique<drmModeModeInfo>(mode_obj);
  }
}

ControllerConfigParams::~ControllerConfigParams() = default;

bool GetDrmPropertyForName(DrmWrapper* drm,
                           drmModeObjectProperties* properties,
                           const std::string& name,
                           DrmWrapper::Property* property) {
  for (uint32_t i = 0; i < properties->count_props; ++i) {
    ScopedDrmPropertyPtr drm_property(drm->GetProperty(properties->props[i]));
    if (name != drm_property->name)
      continue;

    property->id = drm_property->prop_id;
    property->value = properties->prop_values[i];
    if (property->id)
      return true;
  }

  return false;
}

bool AddPropertyIfValid(drmModeAtomicReq* property_set,
                        uint32_t object_id,
                        const DrmWrapper::Property& property) {
  if (!property.id)
    return true;

  int ret = drmModeAtomicAddProperty(property_set, object_id, property.id,
                                     property.value);
  if (ret < 0) {
    LOG(ERROR) << "Failed to set property object_id=" << object_id
               << " property_id=" << property.id
               << " property_value=" << property.value << " error=" << -ret;
    return false;
  }

  return true;
}

ScopedDrmColorLutPtr CreateLutBlob(const display::GammaCurve& source,
                                   size_t size) {
  TRACE_EVENT0("drm", "CreateLutBlob");
  if (source.IsDefaultIdentity()) {
    return nullptr;
  }

  ScopedDrmColorLutPtr lut(
      static_cast<drm_color_lut*>(malloc(sizeof(drm_color_lut) * size)));
  drm_color_lut* p = lut.get();
  for (size_t i = 0; i < size; ++i) {
    // Be robust to `size` being 1, since some tests do this.
    source.Evaluate(i / std::max(size - 1.f, 1.f), p[i].red, p[i].green,
                    p[i].blue);
  }
  return lut;
}

bool ParseLutBlob(const void* data, size_t size, display::GammaCurve& result) {
  // LUT blobs are an array of drm_color_lut entries, and so the size of the
  // blob must be a multiple of the size of drm_color_lut.
  if (size % sizeof(drm_color_lut) != 0) {
    LOG(ERROR) << "Invalid size for LUT blob.";
    return false;
  }
  size_t entry_count = size / sizeof(drm_color_lut);
  const drm_color_lut* entries = reinterpret_cast<const drm_color_lut*>(data);
  std::vector<display::GammaRampRGBEntry> lut(entry_count);
  for (size_t i = 0; i < entry_count; ++i) {
    lut[i].r = entries[i].red;
    lut[i].g = entries[i].green;
    lut[i].b = entries[i].blue;
  }
  result = display::GammaCurve(std::move(lut));
  return true;
}

ScopedDrmColorCtmPtr CreateCTMBlob(const skcms_Matrix3x3& color_matrix,
                                   bool negative_values_broken) {
  ScopedDrmColorCtmPtr ctm(
      static_cast<drm_color_ctm*>(drmMalloc(sizeof(drm_color_ctm))));
  for (size_t i = 0; i < 9; ++i) {
    float value = color_matrix.vals[i / 3][i % 3];
    if (value < 0) {
      if (negative_values_broken) {
        ctm->matrix[i] = 0;
      } else {
        ctm->matrix[i] =
            static_cast<uint64_t>(-value * kCtmValueScale) & kCtmValueMask;
        ctm->matrix[i] |= kCtmSignMask;
      }
    } else {
      ctm->matrix[i] =
          static_cast<uint64_t>(value * kCtmValueScale) & kCtmValueMask;
    }
  }

  return ctm;
}

bool ParseCTMBlob(const void* data, size_t size, skcms_Matrix3x3& result) {
  // CTM blobs must contain exactly 9 (3x3) numbers which are encoded in
  // uint64_ts.
  if (size != 9 * sizeof(uint64_t)) {
    LOG(ERROR) << "Invalid size for CTM blob.";
    return false;
  }
  const uint64_t* data_u64 = reinterpret_cast<const uint64_t*>(data);
  for (size_t i = 0; i < 9; ++i) {
    float sign = (data_u64[i] & kCtmSignMask) ? -1.f : 1.f;
    float value = (data_u64[i] & kCtmValueMask) / kCtmValueScale;
    result.vals[i / 3][i % 3] = sign * value;
  }
  return true;
}

ScopedDrmModeRectPtr CreateDCBlob(const gfx::Rect& rect) {
  // Damage rect can be empty, but sending empty or negative rects can result in
  // artifacting and black screens. Filter them out here.
  if (rect.width() <= 0 || rect.height() <= 0 || rect.x() < 0 || rect.y() < 0) {
    return nullptr;
  }

  ScopedDrmModeRectPtr dmg_rect(
      static_cast<drm_mode_rect*>(malloc(sizeof(drm_mode_rect))));
  dmg_rect->x1 = rect.x();
  dmg_rect->y1 = rect.y();
  dmg_rect->x2 = rect.right();
  dmg_rect->y2 = rect.bottom();
  return dmg_rect;
}

std::vector<std::unique_ptr<HardwareDisplayControllerInfo>>
GetDisplayInfosAndUpdateCrtcs(DrmWrapper& drm) {
  auto [displays, invalid_crtcs] = GetDisplayInfosAndInvalidCrtcs(drm);
  // Disable invalid CRTCs to allow the preferred CRTCs to be enabled later
  // instead.
  for (uint32_t crtc : invalid_crtcs) {
    drm.DisableCrtc(crtc);
    VLOG(1) << "Disabled undesired CRTC " << crtc;
  }
  return std::move(displays);
}

void DrmWriteIntoTraceHelper(const drmModeModeInfo& mode_info,
                             perfetto::TracedValue context) {
  auto dict = std::move(context).WriteDictionary();

  dict.Add("name", mode_info.name);
  dict.Add("type", mode_info.type);
  dict.Add("flags", mode_info.flags);
  dict.Add("clock", mode_info.clock);
  dict.Add("hdisplay", mode_info.hdisplay);
  dict.Add("vdisplay", mode_info.vdisplay);
}

std::vector<CrtcConnectorPairs> GetAllCrtcConnectorPermutations(
    const DrmDevice& drm,
    const std::vector<ControllerConfigParams>& controllers_params) {
  if (controllers_params.empty()) {
    LOG(DFATAL) << "No connectors specified in controllers_params to generate "
                   "CRTC-connector pairings";
    return {};
  }

  std::vector<PossibleCrtcsForConnector> possible_crtcs_for_connectors;
  for (auto params : controllers_params) {
    const uint32_t possible_crtcs_bitmask =
        drm.plane_manager()->GetPossibleCrtcsBitmaskForConnector(
            params.connector);
    std::vector<uint32_t> possible_crtc_ids =
        GetPossibleCrtcIdsFromBitmask(drm, possible_crtcs_bitmask);
    possible_crtcs_for_connectors.push_back(
        {.connector_id = params.connector,
         .possible_crtcs = std::move(possible_crtc_ids)});
  }

  base::flat_set<uint32_t /*crtc_id*/> crtcs_used_in_current_permutation;
  std::vector<CrtcConnectorPairs> permutations = BuildCrtcConnectorPermutations(
      possible_crtcs_for_connectors, possible_crtcs_for_connectors.begin(),
      crtcs_used_in_current_permutation);

  return permutations;
}

void ApplyCrtcColorSpaceConversion(DrmWrapper* drm,
                                   uint32_t crtc_id,
                                   float rgb[3]) {
  // Look up all properties on this CRTC and create a helper lambda to look up
  // their blobs.
  ScopedDrmObjectPropertyPtr props(
      drm->GetObjectProperties(crtc_id, DRM_MODE_OBJECT_CRTC));
  if (!props) {
    return;
  }
  auto get_blob_by_name = [&](const char* name) {
    DrmDevice::Property property;
    if (!GetDrmPropertyForName(drm, props.get(), name, &property)) {
      return ScopedDrmPropertyBlobPtr(nullptr);
    }
    return drm->GetPropertyBlob(property.value);
  };

  // Apply DEGAMMA.
  ScopedDrmPropertyBlobPtr degamma_blob = get_blob_by_name("DEGAMMA_LUT");
  if (degamma_blob) {
    display::GammaCurve curve;
    if (ParseLutBlob(degamma_blob->data, degamma_blob->length, curve)) {
      curve.Evaluate(rgb);
    }
  }

  // Apply CTM.
  ScopedDrmPropertyBlobPtr ctm_blob = get_blob_by_name("CTM");
  if (ctm_blob) {
    skcms_Matrix3x3 ctm;
    if (ParseCTMBlob(ctm_blob->data, ctm_blob->length, ctm)) {
      float temp[3] = {0, 0, 0};
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          temp[i] += ctm.vals[i][j] * rgb[j];
        }
      }
      for (int i = 0; i < 3; ++i) {
        rgb[i] = temp[i];
      }
    }
  }

  // Apply GAMMA.
  ScopedDrmPropertyBlobPtr gamma_blob = get_blob_by_name("GAMMA_LUT");
  if (gamma_blob) {
    display::GammaCurve curve;
    if (ParseLutBlob(gamma_blob->data, gamma_blob->length, curve)) {
      curve.Evaluate(rgb);
    }
  }
}

}  // namespace ui