File: display_manager_test_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 (81 lines) | stat: -rw-r--r-- 2,271 bytes parent folder | download | duplicates (8)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/manager/util/display_manager_test_util.h"

#include <stdint.h>

#include "base/check_op.h"
#include "ui/display/display_features.h"
#include "ui/display/util/display_util.h"

namespace display {

namespace {
// Use larger than max int to catch overflow early.
constexpr int64_t kSynthesizedDisplayIdStart = 2200000000LL;

int64_t next_synthesized_display_id = kSynthesizedDisplayIdStart;
uint8_t edid_device_index = 0;
uint8_t edid_display_index = 0;

}  // namespace

void ResetDisplayIdForTest() {
  next_synthesized_display_id = kSynthesizedDisplayIdStart;
  edid_device_index = 0;
  edid_display_index = 0;
}

int64_t GetASynthesizedDisplayId() {
  const int64_t id = next_synthesized_display_id;
  next_synthesized_display_id =
      features::IsEdidBasedDisplayIdsEnabled()
          ? next_synthesized_display_id += 0x100
          : SynthesizeDisplayIdFromSeed(next_synthesized_display_id);
  return id;
}

int64_t SynthesizeDisplayIdFromSeed(int64_t id) {
  // Connector index is stored in the first 8 bits for port-base display IDs.
  int next_output_index = id & 0xFF;
  next_output_index++;
  DCHECK_GT(0x100, next_output_index);
  const int64_t base = static_cast<int64_t>(~static_cast<uint64_t>(0xFF) & id);

  if (id == kSynthesizedDisplayIdStart) {
    return id + 0x100 + next_output_index;
  }
  return base + next_output_index;
}

int64_t GetNextSynthesizedEdidDisplayConnectorIndex() {
  if (edid_display_index == 255) {
    edid_display_index = 0;
    edid_device_index++;
  } else {
    edid_display_index++;
  }
  // Synthesized IDs are limited to 256^2 unique IDs.
  DCHECK_LT(edid_device_index, 255)
      << "Connector index exceeded 65536. Cannot "
         "synthesize any more unique display IDs.";

  return ConnectorIndex16(edid_device_index, edid_display_index);
}

int64_t ProduceAlternativeSchemeIdForId(int64_t id) {
  DCHECK_GT(id, 0);
  int magnitude = 1;
  int64_t id_copy = id;
  do {
    magnitude *= 10;
    id_copy /= 10;
  } while (id_copy / 10);

  const int64_t new_id = id - magnitude;
  return new_id > 0 ? new_id : id + magnitude;
}

}  // namespace display