File: managed_display_info.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 (698 lines) | stat: -rw-r--r-- 25,526 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// Copyright 2013 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/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ui/display/manager/managed_display_info.h"

#include <stdio.h>

#include <limits>
#include <string>
#include <string_view>
#include <vector>

#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "ui/display/display.h"
#include "ui/display/display_features.h"
#include "ui/display/display_switches.h"
#include "ui/display/manager/util/display_manager_test_util.h"
#include "ui/display/manager/util/display_manager_util.h"
#include "ui/display/types/display_constants.h"
#include "ui/display/util/display_util.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/display_color_spaces.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/geometry/insets_conversions.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/size_f.h"

namespace display {

namespace {

const float kDpi96 = 96.0;

// The recommended default external display DPI, only used when an external
// display is connected for the first time. e.g. when a 4K native mode is used
// when firstly connected, the content is almost certainly too small. The value
// comes from the metrics of currently most used external effective display DPI
// - Ash.Display.ExternalDisplay.ActiveEffectiveDPI.
const float kRecommendedDefaultExternalDisplayDpi = kDpi96;

// Check the content of |spec| and fill |bounds| and |device_scale_factor|.
// Returns true when |bounds| is found.
void GetDisplayBounds(const std::string& spec,
                      gfx::Rect* bounds,
                      float* device_scale_factor) {
  int width = 0;
  int height = 0;
  int x = 0;
  int y = 0;
  if (sscanf(spec.c_str(), "%dx%d*%f", &width, &height, device_scale_factor) >=
          2 ||
      sscanf(spec.c_str(), "%d+%d-%dx%d*%f", &x, &y, &width, &height,
             device_scale_factor) >= 4) {
    bounds->SetRect(x, y, width, height);

    auto equals_within_epsilon = [device_scale_factor](float dsf) {
      return std::abs(*device_scale_factor - dsf) < 0.01f;
    };
    if (equals_within_epsilon(1.77f)) {
      *device_scale_factor = kDsf_1_777;
    } else if (equals_within_epsilon(1.8f)) {
      *device_scale_factor = kDsf_1_8;
    } else if (equals_within_epsilon(2.25f)) {
      *device_scale_factor = kDsf_2_252;
    } else if (equals_within_epsilon(2.66f)) {
      *device_scale_factor = kDsf_2_666;
    }
    return;
  }
  LOG(FATAL) << "Invalid format:" << spec;
}

// Display mode list is sorted by:
//  * the area in pixels in ascending order
//  * refresh rate in descending order
struct ManagedDisplayModeSorter {
  bool operator()(const ManagedDisplayMode& a, const ManagedDisplayMode& b) {
    gfx::Size size_a_dip = a.GetSizeInDIP();
    gfx::Size size_b_dip = b.GetSizeInDIP();
    if (size_a_dip.GetArea() == size_b_dip.GetArea())
      return (a.refresh_rate() > b.refresh_rate());
    return (size_a_dip.GetArea() < size_b_dip.GetArea());
  }
};

bool IsWithinEpsilon(float a, float b) {
  constexpr float kEpsilon = 0.0001f;
  return std::abs(a - b) < kEpsilon;
}

std::string PanelOrientationToString(PanelOrientation orientation) {
  switch (orientation) {
    case kNormal:
      return "Normal";
    case kBottomUp:
      return "BottomUp";
    case kLeftUp:
      return "LeftUp";
    case kRightUp:
      return "RightUp";
  }
  NOTREACHED();
}

}  // namespace

ManagedDisplayMode::ManagedDisplayMode() = default;

ManagedDisplayMode::ManagedDisplayMode(const gfx::Size& size) : size_(size) {}

ManagedDisplayMode::ManagedDisplayMode(const gfx::Size& size,
                                       float refresh_rate,
                                       bool is_interlaced,
                                       bool native)
    : size_(size),
      refresh_rate_(refresh_rate),
      is_interlaced_(is_interlaced),
      native_(native) {}

ManagedDisplayMode::ManagedDisplayMode(const gfx::Size& size,
                                       float refresh_rate,
                                       bool is_interlaced,
                                       bool native,
                                       float device_scale_factor)
    : size_(size),
      refresh_rate_(refresh_rate),
      is_interlaced_(is_interlaced),
      native_(native),
      device_scale_factor_(device_scale_factor) {}

ManagedDisplayMode::~ManagedDisplayMode() = default;

ManagedDisplayMode::ManagedDisplayMode(const ManagedDisplayMode& other) =
    default;

ManagedDisplayMode& ManagedDisplayMode::operator=(
    const ManagedDisplayMode& other) = default;

bool ManagedDisplayMode::operator==(const ManagedDisplayMode& other) const {
  return size_ == other.size_ && is_interlaced_ == other.is_interlaced_ &&
         native_ == other.native_ &&
         IsWithinEpsilon(refresh_rate_, other.refresh_rate_) &&
         IsWithinEpsilon(device_scale_factor_, other.device_scale_factor_);
}

gfx::Size ManagedDisplayMode::GetSizeInDIP() const {
  gfx::SizeF size_dip(size_);
  size_dip.InvScale(device_scale_factor_);
  return gfx::ToFlooredSize(size_dip);
}

bool ManagedDisplayMode::IsEquivalent(const ManagedDisplayMode& other) const {
  if (display::features::IsListAllDisplayModesEnabled())
    return *this == other;

  return size_ == other.size_ &&
         IsWithinEpsilon(device_scale_factor_, other.device_scale_factor_);
}

std::string ManagedDisplayMode::ToString() const {
  return base::StringPrintf(
      "DisplayMode{size: %s, refresh_rate: %f, interlaced:"
      " %d, native: %d, device_scale_factor: %f}",
      size_.ToString().c_str(), refresh_rate_, is_interlaced_, native_,
      device_scale_factor_);
}

// static
ManagedDisplayInfo ManagedDisplayInfo::CreateFromSpec(const std::string& spec) {
  return CreateFromSpecWithID(spec, kInvalidDisplayId);
}

// static
ManagedDisplayInfo ManagedDisplayInfo::CreateFromSpecWithID(
    const std::string& spec,
    int64_t id) {
  // Default bounds for a display.
  const int kDefaultHostWindowX = 200;
  const int kDefaultHostWindowY = 200;
  const int kDefaultHostWindowWidth = 1366;
  const int kDefaultHostWindowHeight = 768;
  gfx::Rect bounds_in_native(kDefaultHostWindowX, kDefaultHostWindowY,
                             kDefaultHostWindowWidth, kDefaultHostWindowHeight);
  std::string_view main_spec = spec;

  gfx::RoundedCornersF panel_corners_radii;
  std::vector<std::string_view> parts = base::SplitStringPiece(
      main_spec, "~", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  if (parts.size() == 2) {
    std::vector<std::string_view> radii_part = base::SplitStringPiece(
        parts[1], "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);

    DCHECK(radii_part.size() == 1 || radii_part.size() == 4);

    float radii[4];
    int radius_in_int = 0;
    for (size_t idx = 0; idx < radii_part.size(); ++idx) {
      std::string_view radius = radii_part[idx];
      bool conversion_success = base::StringToInt(radius, &radius_in_int);
      DCHECK(conversion_success);
      radii[idx] = static_cast<float>(radius_in_int);
    }

    panel_corners_radii =
        (radii_part.size() == 1)
            ? gfx::RoundedCornersF{radii[0]}
            : gfx::RoundedCornersF{radii[0], radii[1], radii[2], radii[3]};

    main_spec = parts[0];
  }

  float zoom_factor = 1.0f;
  parts = base::SplitStringPiece(main_spec, "@", base::KEEP_WHITESPACE,
                                 base::SPLIT_WANT_NONEMPTY);
  if (parts.size() == 2) {
    double scale_in_double = 0;
    if (base::StringToDouble(parts[1], &scale_in_double))
      zoom_factor = scale_in_double;
    main_spec = parts[0];
  }

  parts = base::SplitStringPiece(main_spec, "/", base::KEEP_WHITESPACE,
                                 base::SPLIT_WANT_NONEMPTY);
  Display::Rotation rotation(Display::ROTATE_0);
  bool has_overscan = false;
  bool has_hdr = false;
  if (!parts.empty()) {
    main_spec = parts[0];
    if (parts.size() >= 2) {
      std::string_view options = parts[1];
      for (char c : options) {
        switch (c) {
          case 'o':
            has_overscan = true;
            break;
          case 'h':
            has_hdr = true;
            break;
          case 'r':  // rotate 90 degrees to 'right'.
            rotation = Display::ROTATE_90;
            break;
          case 'u':  // 180 degrees, 'u'pside-down.
            rotation = Display::ROTATE_180;
            break;
          case 'l':  // rotate 90 degrees to 'left'.
            rotation = Display::ROTATE_270;
            break;
        }
      }
    }
  }

  float device_scale_factor = 1.0f;
  ManagedDisplayModeList display_modes;

  if (!main_spec.empty()) {
    GetDisplayBounds(std::string(main_spec), &bounds_in_native,
                     &device_scale_factor);

    parts = base::SplitStringPiece(main_spec, "#", base::KEEP_WHITESPACE,
                                   base::SPLIT_WANT_NONEMPTY);
    if (parts.size() == 2) {
      size_t native_mode = 0;
      int largest_area = -1;
      float highest_refresh_rate = -1.0f;
      main_spec = parts[0];
      std::string_view resolution_list = parts[1];
      parts =
          base::SplitStringPiece(resolution_list, "|", base::KEEP_WHITESPACE,
                                 base::SPLIT_WANT_NONEMPTY);
      for (size_t i = 0; i < parts.size(); ++i) {
        gfx::Size size;
        float refresh_rate = 60.0f;
        bool is_interlaced = false;

        gfx::Rect mode_bounds;
        std::vector<std::string_view> resolution = base::SplitStringPiece(
            parts[i], "%", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
        float device_scale_factor_for_mode = device_scale_factor;
        GetDisplayBounds(std::string(resolution[0]), &mode_bounds,
                         &device_scale_factor_for_mode);
        size = mode_bounds.size();
        if (resolution.size() > 1) {
          double refresh_rate_in_double = 0.0;
          if (base::StringToDouble(resolution[1], &refresh_rate_in_double))
            refresh_rate = refresh_rate_in_double;
        }
        if (size.GetArea() >= largest_area &&
            refresh_rate > highest_refresh_rate) {
          // Use mode with largest area and highest refresh rate as native.
          largest_area = size.GetArea();
          highest_refresh_rate = refresh_rate;
          native_mode = i;
        }
        display_modes.emplace_back(size, refresh_rate, is_interlaced, false,
                                   device_scale_factor_for_mode);
      }
      ManagedDisplayMode dm = display_modes[native_mode];
      display_modes[native_mode] =
          ManagedDisplayMode(dm.size(), dm.refresh_rate(), dm.is_interlaced(),
                             true, dm.device_scale_factor());
    }
  }

  ManagedDisplayInfo display_info =
      id == kInvalidDisplayId ? CreateDisplayInfo(GetASynthesizedDisplayId())
                              : CreateDisplayInfo(id);
  display_info.set_device_scale_factor(device_scale_factor);
  display_info.SetRotation(rotation, Display::RotationSource::ACTIVE);
  display_info.SetRotation(rotation, Display::RotationSource::USER);
  display_info.set_zoom_factor(zoom_factor);
  display_info.SetBounds(bounds_in_native);
  display_info.set_has_overscan(has_overscan);
  display_info.set_panel_corners_radii(panel_corners_radii);

  if (!display_modes.size()) {
    display_modes.emplace_back(display_info.size_in_pixel(), 60.0f,
                               /*interlace=*/false, /*native=*/true,
                               device_scale_factor);
  }

  display_info.SetManagedDisplayModes(display_modes);

  // To test the overscan, it creates the default 5% overscan.
  if (has_overscan) {
    int width = bounds_in_native.width() / device_scale_factor / 40;
    int height = bounds_in_native.height() / device_scale_factor / 40;
    display_info.SetOverscanInsets(gfx::Insets::VH(height, width));
    display_info.UpdateDisplaySize();
  }

  if (has_hdr) {
    gfx::DisplayColorSpaces display_color_spaces{
        gfx::ColorSpace::CreateHDR10(), gfx::BufferFormat::BGRA_1010102};
    display_info.set_display_color_spaces(display_color_spaces);
  }

  DVLOG(1) << "DisplayInfoFromSpec info=" << display_info.ToString()
           << ", spec=" << spec;
  return display_info;
}

ManagedDisplayInfo::ManagedDisplayInfo()
    : id_(kInvalidDisplayId),
      year_of_manufacture_(kInvalidYearOfManufacture),
      has_overscan_(false),
      active_rotation_source_(Display::RotationSource::UNKNOWN),
      touch_support_(Display::TouchSupport::UNKNOWN),
      device_scale_factor_(1.0f),
      device_dpi_(kDpi96),
      panel_orientation_(display::PanelOrientation::kNormal),
      zoom_factor_(1.f),
      refresh_rate_(60.f),
      is_interlaced_(false),
      from_native_platform_(false),
      native_(false),
      is_aspect_preserving_scaling_(false),
      clear_overscan_insets_(false),
      bits_per_channel_(0),
      variable_refresh_rate_state_(VariableRefreshRateState::kVrrNotCapable),
      vsync_rate_min_(std::nullopt) {}

ManagedDisplayInfo::ManagedDisplayInfo(int64_t id,
                                       const std::string& name,
                                       bool has_overscan)
    : id_(id),
      name_(name),
      year_of_manufacture_(kInvalidYearOfManufacture),
      has_overscan_(has_overscan),
      active_rotation_source_(Display::RotationSource::UNKNOWN),
      touch_support_(Display::TouchSupport::UNKNOWN),
      device_scale_factor_(1.0f),
      device_dpi_(kDpi96),
      panel_orientation_(display::PanelOrientation::kNormal),
      zoom_factor_(1.f),
      refresh_rate_(60.f),
      is_interlaced_(false),
      from_native_platform_(false),
      native_(false),
      is_aspect_preserving_scaling_(false),
      clear_overscan_insets_(false),
      bits_per_channel_(0),
      variable_refresh_rate_state_(VariableRefreshRateState::kVrrNotCapable),
      vsync_rate_min_(std::nullopt) {
  has_overscan_ = true;
}

ManagedDisplayInfo::ManagedDisplayInfo(const ManagedDisplayInfo& other) =
    default;

ManagedDisplayInfo::~ManagedDisplayInfo() = default;

void ManagedDisplayInfo::SetRotation(Display::Rotation rotation,
                                     Display::RotationSource source) {
  rotations_[source] = rotation;
  rotations_[Display::RotationSource::ACTIVE] = rotation;
  active_rotation_source_ = source;
}

Display::Rotation ManagedDisplayInfo::GetActiveRotation() const {
  return GetRotation(Display::RotationSource::ACTIVE);
}

Display::Rotation ManagedDisplayInfo::GetLogicalActiveRotation() const {
  return GetRotationWithPanelOrientation(
      GetRotation(Display::RotationSource::ACTIVE));
}

Display::Rotation ManagedDisplayInfo::GetRotation(
    Display::RotationSource source) const {
  if (rotations_.find(source) == rotations_.end())
    return Display::ROTATE_0;
  return rotations_.at(source);
}

void ManagedDisplayInfo::AddZoomFactorForSize(const std::string& size,
                                              float zoom_factor) {
  zoom_factor_map_[size] = zoom_factor;
}

void ManagedDisplayInfo::Copy(const ManagedDisplayInfo& native_info) {
  DCHECK(id_ == native_info.id_);
  port_display_id_ = native_info.port_display_id_;
  edid_display_id_ = native_info.edid_display_id_;
  connector_index_ = native_info.connector_index_;
  manufacturer_id_ = native_info.manufacturer_id_;
  product_id_ = native_info.product_id_;
  year_of_manufacture_ = native_info.year_of_manufacture_;
  name_ = native_info.name_;
  has_overscan_ = native_info.has_overscan_;

  active_rotation_source_ = native_info.active_rotation_source_;
  touch_support_ = native_info.touch_support_;
  connection_type_ = native_info.connection_type_;
  physical_size_ = native_info.physical_size_;
  device_scale_factor_ = native_info.device_scale_factor_;
  DCHECK(!native_info.bounds_in_native_.IsEmpty());
  bounds_in_native_ = native_info.bounds_in_native_;
  device_dpi_ = native_info.device_dpi_;
  panel_orientation_ = native_info.panel_orientation_,
  size_in_pixel_ = native_info.size_in_pixel_;
  is_aspect_preserving_scaling_ = native_info.is_aspect_preserving_scaling_;
  display_modes_ = native_info.display_modes_;
  maximum_cursor_size_ = native_info.maximum_cursor_size_;
  display_color_spaces_ = native_info.display_color_spaces_;
  snapshot_color_space_ = native_info.snapshot_color_space_;

  bits_per_channel_ = native_info.bits_per_channel_;
  refresh_rate_ = native_info.refresh_rate_;
  is_interlaced_ = native_info.is_interlaced_;
  native_ = native_info.native_;
  panel_corners_radii_ = native_info.panel_corners_radii_;

  drm_formats_and_modifiers_ = native_info.drm_formats_and_modifiers_;
  variable_refresh_rate_state_ = native_info.variable_refresh_rate_state_;
  vsync_rate_min_ = native_info.vsync_rate_min_;
  detected_ = native_info.detected_;

  // Rotation, color_profile and overscan are given by preference,
  // or unit tests. Don't copy if this native_info came from
  // DisplayChangeObserver.
  if (native_info.from_native_platform())
    return;
  // Update the overscan_insets_in_dip_ either if the inset should be
  // cleared, or has non empty insets.
  if (native_info.clear_overscan_insets())
    overscan_insets_in_dip_ = gfx::Insets();
  else if (!native_info.overscan_insets_in_dip_.IsEmpty())
    overscan_insets_in_dip_ = native_info.overscan_insets_in_dip_;

  rotations_ = native_info.rotations_;
  zoom_factor_ = native_info.zoom_factor_;
}

void ManagedDisplayInfo::SetBounds(const gfx::Rect& new_bounds_in_native) {
  DCHECK_NE(new_bounds_in_native.width(), new_bounds_in_native.height());

  bounds_in_native_ = new_bounds_in_native;
  size_in_pixel_ = new_bounds_in_native.size();
  UpdateDisplaySize();
}

float ManagedDisplayInfo::GetEffectiveDeviceScaleFactor() const {
  if (zoom_factor_ == 1.0f) {
    return device_scale_factor_;
  }
  // When the display zoom is applied, try to adjust the final scale so that it
  // will produce the integer pixel size (wider side) when the scale is applied
  // to the logical size. Note that this a best effort and not guaranteed.
  const float scale_factor = device_scale_factor_ * zoom_factor_;
  const int pixel_size =
      std::max(bounds_in_native_.width(), bounds_in_native_.height());
  const float logical_size_f = pixel_size / scale_factor;
  // Floor the value by default but allow very close value to be roudnd up.
  const int32_t logical_size = base::ClampFloor(logical_size_f + 0.0005);
  return pixel_size / static_cast<float>(logical_size);
}

void ManagedDisplayInfo::UpdateZoomFactorToMatchTargetDPI() {
  // Only update zoom factor if device dpi is valid.
  if (!device_dpi_) {
    return;
  }

  const float target_zoom_factor =
      device_dpi_ / kRecommendedDefaultExternalDisplayDpi;

  // Refine zoom factor based on available zoom factors in settings.
  const int display_larger_side =
      std::max(bounds_in_native_.width(), bounds_in_native_.height());
  const std::vector<float> avaialble_zoom_factors =
      GetDisplayZoomFactorsByDisplayWidth(display_larger_side);
  DCHECK_GE(avaialble_zoom_factors.size(), 1u);

  const float min_zoom_factor = avaialble_zoom_factors.front();
  const float max_zoom_factor = avaialble_zoom_factors.back();
  // Check min boundary.
  if (target_zoom_factor <= min_zoom_factor) {
    zoom_factor_ = min_zoom_factor;
  } else if (target_zoom_factor >= max_zoom_factor) {
    // Check max boundary.
    zoom_factor_ = max_zoom_factor;
  } else {
    // Round to the neareast available zoom factor.
    DCHECK(std::is_sorted(avaialble_zoom_factors.begin(),
                          avaialble_zoom_factors.end()));
    for (size_t i = 0; i < avaialble_zoom_factors.size() - 1; i++) {
      const float left_bound = avaialble_zoom_factors[i];
      const float right_bound = avaialble_zoom_factors[i + 1];
      if (target_zoom_factor >= right_bound) {
        continue;
      }

      zoom_factor_ =
          (target_zoom_factor - left_bound < right_bound - target_zoom_factor)
              ? left_bound
              : right_bound;
      break;
    }
  }

  // Also update the zoom factor in the zoom_factor_map_.
  AddZoomFactorForSize(size_in_pixel_.ToString(), zoom_factor_);
}

gfx::Size ManagedDisplayInfo::GetSizeInPixelWithPanelOrientation() const {
  gfx::Size size = bounds_in_native_.size();
  if (panel_orientation_ == display::PanelOrientation::kLeftUp ||
      panel_orientation_ == display::PanelOrientation::kRightUp) {
    return gfx::Size(size.height(), size.width());
  }
  return size;
}

void ManagedDisplayInfo::UpdateDisplaySize() {
  size_in_pixel_ = GetSizeInPixelWithPanelOrientation();

  if (!overscan_insets_in_dip_.IsEmpty()) {
    gfx::Insets insets_in_pixel = GetOverscanInsetsInPixel();
    size_in_pixel_.Enlarge(-insets_in_pixel.width(), -insets_in_pixel.height());
  } else {
    overscan_insets_in_dip_ = gfx::Insets();
  }

  if (GetActiveRotation() == Display::ROTATE_90 ||
      GetActiveRotation() == Display::ROTATE_270) {
    size_in_pixel_.SetSize(size_in_pixel_.height(), size_in_pixel_.width());
  }
}

void ManagedDisplayInfo::SetOverscanInsets(const gfx::Insets& insets_in_dip) {
  overscan_insets_in_dip_ = insets_in_dip;
}

gfx::Insets ManagedDisplayInfo::GetOverscanInsetsInPixel() const {
  return gfx::ToFlooredInsets(gfx::ConvertInsetsToPixels(
      overscan_insets_in_dip_, device_scale_factor_));
}

void ManagedDisplayInfo::SetSnapshotColorSpace(
    const gfx::ColorSpace& snapshot_color) {
  snapshot_color_space_ = snapshot_color;
}

gfx::ColorSpace ManagedDisplayInfo::GetSnapshotColorSpace() const {
  return snapshot_color_space_;
}

void ManagedDisplayInfo::SetManagedDisplayModes(
    const ManagedDisplayModeList& display_modes) {
  display_modes_ = display_modes;
  std::sort(display_modes_.begin(), display_modes_.end(),
            ManagedDisplayModeSorter());
}

gfx::Size ManagedDisplayInfo::GetNativeModeSize() const {
  for (const ManagedDisplayMode& display_mode : display_modes_) {
    if (display_mode.native())
      return display_mode.size();
  }
  return gfx::Size();
}

std::string ManagedDisplayInfo::ToString() const {
  int rotation_degree = static_cast<int>(GetActiveRotation()) * 90;

  std::string result = base::StringPrintf(
      "ManagedDisplayInfo[%lld] port_display_id=%lld, edid_display_id=%lld, "
      "native bounds=%s, size=%s, device-scale=%g, "
      "display-zoom=%g, overscan=%s, rotation=%d, touchscreen=%s, "
      "panel_corners_radii=%s, panel_orientation=%s, detected=%s, "
      "color_space=%s",
      static_cast<long long int>(id_),
      static_cast<long long int>(port_display_id_),
      static_cast<long long int>(edid_display_id_),
      bounds_in_native_.ToString().c_str(), size_in_pixel_.ToString().c_str(),
      device_scale_factor_, zoom_factor_,
      overscan_insets_in_dip_.ToString().c_str(), rotation_degree,
      touch_support_ == Display::TouchSupport::AVAILABLE     ? "yes"
      : touch_support_ == Display::TouchSupport::UNAVAILABLE ? "no"
                                                             : "unknown",
      panel_corners_radii_.ToString().c_str(),
      PanelOrientationToString(panel_orientation_).c_str(),
      detected_ ? "true" : "false",
      display_color_spaces_.GetRasterColorSpace().ToString().c_str());

  return result;
}

std::string ManagedDisplayInfo::ToFullString() const {
  std::string display_modes_str;
  for (const ManagedDisplayMode& m : display_modes_) {
    if (!display_modes_str.empty())
      display_modes_str += ",";
    base::StringAppendF(&display_modes_str, "(%dx%d@%g%c%s %g)",
                        m.size().width(), m.size().height(), m.refresh_rate(),
                        m.is_interlaced() ? 'I' : 'P', m.native() ? "(N)" : "",
                        m.device_scale_factor());
  }
  return ToString() + ", display_modes==" + display_modes_str;
}

Display::Rotation ManagedDisplayInfo::GetRotationWithPanelOrientation(
    Display::Rotation rotation) const {
  int offset = 0;
  switch (panel_orientation_) {
    case PanelOrientation::kNormal:
      break;
    case PanelOrientation::kBottomUp:
      offset = 2;
      break;
    case PanelOrientation::kRightUp:
      offset = 1;
      break;
    case PanelOrientation::kLeftUp:
      offset = 3;
      break;
  }
  return static_cast<Display::Rotation>((static_cast<int>(rotation) + offset) %
                                        4);
}

ManagedDisplayInfo CreateDisplayInfo(int64_t id, const gfx::Rect& bounds) {
  display::ManagedDisplayInfo info(
      id, base::StringPrintf("Display-%d", static_cast<int>(id)), false);

  const int64_t alternate_id = ProduceAlternativeSchemeIdForId(id);
  if (features::IsEdidBasedDisplayIdsEnabled()) {
    info.set_edid_display_id(id);
    info.set_connector_index(GetNextSynthesizedEdidDisplayConnectorIndex());

    info.set_port_display_id(alternate_id);
  } else {
    info.set_port_display_id(id);
    // Output index is stored in the first 8 bits.
    info.set_connector_index(id & 0xFF);

    info.set_edid_display_id(alternate_id);
  }
  if (!bounds.IsEmpty()) {
    info.SetBounds(bounds);
  }
  return info;
}

}  // namespace display