File: drm_device_manager.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 (93 lines) | stat: -rw-r--r-- 2,893 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
// Copyright 2015 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/ozone/platform/drm/gpu/drm_device_manager.h"

#include <algorithm>
#include <memory>
#include <utility>

#include "base/containers/contains.h"
#include "base/logging.h"
#include "ui/ozone/platform/drm/gpu/drm_device.h"
#include "ui/ozone/platform/drm/gpu/drm_device_generator.h"

namespace ui {

DrmDeviceManager::DrmDeviceManager(
    std::unique_ptr<DrmDeviceGenerator> drm_device_generator)
    : drm_device_generator_(std::move(drm_device_generator)) {}

DrmDeviceManager::~DrmDeviceManager() {
  DCHECK(drm_device_map_.empty());
}

bool DrmDeviceManager::AddDrmDevice(const base::FilePath& path,
                                    base::ScopedFD fd) {
  if (base::Contains(devices_, path, &DrmDevice::device_path)) {
    VLOG(2) << "Got request to add existing device: " << path.value();
    return false;
  }

  scoped_refptr<DrmDevice> device = drm_device_generator_->CreateDevice(
      path, std::move(fd), !primary_device_);
  if (!device) {
    // This is expected for non-modesetting devices like VGEM.
    VLOG(1) << "Could not initialize DRM device for " << path.value();
    return false;
  }

  if (!primary_device_) {
    VLOG(1) << "Primary DRM device added: " << path;
    primary_device_ = device;
  }

  devices_.push_back(device);
  return true;
}

void DrmDeviceManager::RemoveDrmDevice(const base::FilePath& path) {
  auto it = std::ranges::find(devices_, path, &DrmDevice::device_path);
  if (it == devices_.end()) {
    VLOG(2) << "Got request to remove non-existent device: " << path.value();
    return;
  }

  DCHECK_NE(primary_device_, *it);
  devices_.erase(it);
}

void DrmDeviceManager::UpdateDrmDevice(gfx::AcceleratedWidget widget,
                                       const scoped_refptr<DrmDevice>& device) {
  drm_device_map_[widget] = device;
}

void DrmDeviceManager::RemoveDrmDevice(gfx::AcceleratedWidget widget) {
  auto it = drm_device_map_.find(widget);
  if (it != drm_device_map_.end())
    drm_device_map_.erase(it);
}

scoped_refptr<DrmDevice> DrmDeviceManager::GetDrmDevice(
    gfx::AcceleratedWidget widget) {
  if (widget == gfx::kNullAcceleratedWidget)
    return primary_device_;

  auto it = drm_device_map_.find(widget);
  DLOG_IF(WARNING, it == drm_device_map_.end())
      << "Attempting to get device for unknown widget " << widget;
  // If the widget isn't associated with a display (headless mode) we can
  // allocate buffers from any controller since they will never be scanned out.
  // Use the primary DRM device as a fallback when allocating these buffers.
  if (it == drm_device_map_.end() || !it->second)
    return primary_device_;

  return it->second;
}

const DrmDeviceVector& DrmDeviceManager::GetDrmDevices() const {
  return devices_;
}

}  // namespace ui