File: gpu_memory_buffer_support_x11.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 (179 lines) | stat: -rw-r--r-- 6,240 bytes parent folder | download | duplicates (6)
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
// 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.

#include "ui/gfx/linux/gpu_memory_buffer_support_x11.h"

#include <fcntl.h>
#include <xcb/xcb.h>

#include <memory>

#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/posix/eintr_wrapper.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/buffer_usage_util.h"
#include "ui/gfx/linux/drm_util_linux.h"
#include "ui/gfx/linux/gbm_buffer.h"
#include "ui/gfx/linux/gbm_device.h"
#include "ui/gfx/linux/gbm_util.h"
#include "ui/gfx/linux/gbm_wrapper.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/dri3.h"
#include "ui/gfx/x/future.h"

namespace ui {

namespace {

// Obtain an authenticated DRM fd from X11 and create a GbmDevice with it.
std::unique_ptr<ui::GbmDevice> CreateX11GbmDevice() {
  if (getenv("RUNNING_UNDER_RR") != nullptr) {
    LOG(WARNING) << "Running under rr, disabling dri3";
    return nullptr;
  }

  auto* connection = x11::Connection::Get();
  // |connection| may be nullptr in headless mode.
  if (!connection) {
    LOG(WARNING) << "Could not create x11 connection.";
    return nullptr;
  }

  auto& dri3 = connection->dri3();
  if (!dri3.present()) {
    LOG(WARNING) << "dri3 extension not supported.";
    return nullptr;
  }

  // Obtain an authenticated DRM fd.
  auto reply = dri3.Open({connection->default_root(), 0}).Sync();
  if (!reply)
    return nullptr;

  base::ScopedFD fd(HANDLE_EINTR(dup(reply->device_fd.get())));
  if (!fd.is_valid())
    return nullptr;
  if (HANDLE_EINTR(fcntl(fd.get(), F_SETFD, FD_CLOEXEC)) == -1)
    return nullptr;

  return ui::CreateGbmDevice(fd.release());
}

std::vector<gfx::BufferUsageAndFormat> CreateSupportedConfigList(
    ui::GbmDevice* device) {
  if (!device)
    return {};

  std::vector<gfx::BufferUsageAndFormat> configs;
  for (gfx::BufferUsage usage : {
           gfx::BufferUsage::GPU_READ,
           gfx::BufferUsage::SCANOUT,
           gfx::BufferUsage::SCANOUT_CPU_READ_WRITE,
           gfx::BufferUsage::GPU_READ_CPU_READ_WRITE,
           gfx::BufferUsage::SCANOUT_VDA_WRITE,
       }) {
    for (gfx::BufferFormat format : {
             gfx::BufferFormat::R_8,
             gfx::BufferFormat::RG_88,
             gfx::BufferFormat::RGBA_8888,
             gfx::BufferFormat::RGBX_8888,
             gfx::BufferFormat::BGRA_8888,
             gfx::BufferFormat::BGRX_8888,
             gfx::BufferFormat::BGRA_1010102,

             // On some Intel setups calling gbm_bo_create() with this format
             // results in a crash caused by an integer-divide-by-zero.
             // TODO(thomasanderson): Enable this format.
             // gfx::BufferFormat::RGBA_1010102,
             gfx::BufferFormat::BGR_565,
             gfx::BufferFormat::YUV_420_BIPLANAR,
             gfx::BufferFormat::YVU_420,
             gfx::BufferFormat::P010,
         }) {
      // At least on mesa/amdgpu, gbm_device_is_format_supported() lies.  Test
      // format support by creating a buffer directly.  Use a 2x2 buffer so that
      // YUV420 formats get properly tested.
      if (device->CreateBuffer(GetFourCCFormatFromBufferFormat(format),
                               gfx::Size(2, 2), BufferUsageToGbmFlags(usage))) {
        configs.push_back(gfx::BufferUsageAndFormat(usage, format));
      }
    }
  }
  return configs;
}

}  // namespace

// static
GpuMemoryBufferSupportX11* GpuMemoryBufferSupportX11::GetInstance() {
  static base::NoDestructor<GpuMemoryBufferSupportX11> instance;
  return instance.get();
}

GpuMemoryBufferSupportX11::GpuMemoryBufferSupportX11()
    : device_(CreateX11GbmDevice()),
      supported_configs_(CreateSupportedConfigList(device_.get())) {}

GpuMemoryBufferSupportX11::~GpuMemoryBufferSupportX11() = default;

std::unique_ptr<GbmBuffer> GpuMemoryBufferSupportX11::CreateBuffer(
    gfx::BufferFormat format,
    const gfx::Size& size,
    gfx::BufferUsage usage) {
  if (!device_) {
    LOG(ERROR) << "Can't create buffer -- gbm  device is missing.";
    return nullptr;
  }
  if (!base::Contains(supported_configs_,
                      gfx::BufferUsageAndFormat(usage, format))) {
    LOG(ERROR) << "Can't create buffer -- unsupported config: usage="
               << gfx::BufferUsageToString(usage)
               << ", format=" << gfx::BufferFormatToString(format);
    return nullptr;
  }

  static base::debug::CrashKeyString* crash_key_string =
      base::debug::AllocateCrashKeyString("buffer_usage_and_format",
                                          base::debug::CrashKeySize::Size64);
  std::string buffer_usage_and_format = gfx::BufferFormatToString(format) +
                                        std::string(",") +
                                        gfx::BufferUsageToString(usage);
  base::debug::ScopedCrashKeyString scoped_crash_key(
      crash_key_string, buffer_usage_and_format.c_str());

  return device_->CreateBuffer(GetFourCCFormatFromBufferFormat(format), size,
                               BufferUsageToGbmFlags(usage));
}

bool GpuMemoryBufferSupportX11::CanCreateNativePixmapForFormat(
    gfx::BufferFormat format) {
  return device_ && device_->CanCreateBufferForFormat(
                        GetFourCCFormatFromBufferFormat(format));
}

std::unique_ptr<GbmBuffer> GpuMemoryBufferSupportX11::CreateBufferFromHandle(
    const gfx::Size& size,
    gfx::BufferFormat format,
    gfx::NativePixmapHandle handle) {
  if (!device_) {
    LOG(ERROR) << "Can't create buffer from handle -- gbm  device is missing.";
    return nullptr;
  }

  static base::debug::CrashKeyString* crash_key_string =
      base::debug::AllocateCrashKeyString("buffer_from_handle_format",
                                          base::debug::CrashKeySize::Size64);
  std::string buffer_from_handle_format = gfx::BufferFormatToString(format);
  base::debug::ScopedCrashKeyString scoped_crash_key(
      crash_key_string, buffer_from_handle_format.c_str());

  return device_->CreateBufferFromHandle(
      GetFourCCFormatFromBufferFormat(format), size, std::move(handle));
}

}  // namespace ui