File: drm_wrapper.h

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 (322 lines) | stat: -rw-r--r-- 11,188 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
// 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.

#ifndef UI_OZONE_PLATFORM_DRM_COMMON_DRM_WRAPPER_H_
#define UI_OZONE_PLATFORM_DRM_COMMON_DRM_WRAPPER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <optional>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value_forward.h"
#include "ui/display/types/display_constants.h"
#include "ui/display/types/gamma_ramp_rgb_entry.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/ozone/platform/drm/common/scoped_drm_types.h"

typedef struct _drmModeModeInfo drmModeModeInfo;

struct SkImageInfo;

namespace display {
class GammaCurve;
}  // namespace display

namespace ui {

class DrmWrapper;

class DrmPropertyBlobMetadata {
 public:
  DrmPropertyBlobMetadata(DrmWrapper* drm, uint32_t id);

  DrmPropertyBlobMetadata(const DrmPropertyBlobMetadata&) = delete;
  DrmPropertyBlobMetadata& operator=(const DrmPropertyBlobMetadata&) = delete;

  ~DrmPropertyBlobMetadata();

  uint32_t id() const { return id_; }

 private:
  raw_ptr<DrmWrapper> drm_;  // Not owned;
  uint32_t id_;
};

using ScopedDrmPropertyBlob = std::unique_ptr<DrmPropertyBlobMetadata>;

// Wraps DRM calls into a tight interface. Used to provide different
// implementations of the DRM calls. For the actual implementation the DRM API
// would be called. In unit tests this interface would be stubbed.
class DrmWrapper {
 public:
  DrmWrapper(const base::FilePath& device_path,
             base::ScopedFD fd,
             bool is_primary_device);
  DrmWrapper(const DrmWrapper&) = delete;
  DrmWrapper& operator=(const DrmWrapper&) = delete;
  virtual ~DrmWrapper();

  struct Property {
    // Unique identifier for the property. 0 denotes an invalid ID.
    uint32_t id = 0;

    // Depending on the property, this may be an actual value describing the
    // property or an ID of another property.
    uint64_t value = 0;
  };

  // Open device.
  virtual bool Initialize();

  /*******
   * CRTCs
   *******/

  // Get the CRTC state. This is generally used to save state before using the
  // CRTC. When the user finishes using the CRTC, the user should restore the
  // CRTC to it's initial state. Use |SetCrtc| to restore the state.
  virtual ScopedDrmCrtcPtr GetCrtc(uint32_t crtc_id) const;

  // Used to configure CRTC with ID |crtc_id| to use the connector in
  // |connectors|. The CRTC will be configured with mode |mode| and will display
  // the framebuffer with ID |framebuffer|. Before being able to display the
  // framebuffer, it should be registered with the CRTC using |AddFramebuffer|.
  virtual bool SetCrtc(uint32_t crtc_id,
                       uint32_t framebuffer,
                       std::vector<uint32_t> connectors,
                       const drmModeModeInfo& mode);

  virtual bool DisableCrtc(uint32_t crtc_id);

  /**************
   * Capabilities
   **************/

  // Queries whether a |capability| is available and stores its value in
  // |value| if found.
  virtual bool GetCapability(uint64_t capability, uint64_t* value) const;

  // Can be used to query device/driver |capability|. Sets the value of
  // |capability| to |value|. Returns true in case of a successful query.
  virtual bool SetCapability(uint64_t capability, uint64_t value);

  /************
   * Connectors
   ************/

  // Returns the connector properties for |connector_id|.
  virtual ScopedDrmConnectorPtr GetConnector(uint32_t connector_id) const;

  /********
   * Cursor
   ********/

  // Move the cursor on CRTC |crtc_id| to (x, y);
  virtual bool MoveCursor(uint32_t crtc_id, const gfx::Point& point);

  // Set the cursor to be displayed in CRTC |crtc_id|. (width, height) is the
  // cursor size pointed by |handle|.
  virtual bool SetCursor(uint32_t crtc_id,
                         uint32_t handle,
                         const gfx::Size& size);

  /************
   * DRM Master
   ************/

  virtual bool SetMaster();
  virtual bool DropMaster();
  virtual bool has_master() const;

  /**************
   * Dumb Buffers
   **************/

  virtual bool CreateDumbBuffer(const SkImageInfo& info,
                                uint32_t* handle,
                                uint32_t* stride);
  virtual bool DestroyDumbBuffer(uint32_t handle);
  virtual bool MapDumbBuffer(uint32_t handle, size_t size, void** pixels);
  virtual bool UnmapDumbBuffer(void* pixels, size_t size);

  virtual bool CloseBufferHandle(uint32_t handle);

  /**********
   * Encoders
   **********/

  // Returns the encoder properties for |encoder_id|.
  virtual ScopedDrmEncoderPtr GetEncoder(uint32_t encoder_id) const;

  /**************
   * Framebuffers
   **************/

  // Get the DRM details associated with |framebuffer|.
  virtual ScopedDrmFramebufferPtr GetFramebuffer(uint32_t framebuffer) const;

  // Deregister the given |framebuffer|.
  virtual bool RemoveFramebuffer(uint32_t framebuffer);

  // Register any format buffer with the CRTC. On successful registration, the
  // CRTC will assign a framebuffer ID to |framebuffer|.
  virtual bool AddFramebuffer2(uint32_t width,
                               uint32_t height,
                               uint32_t format,
                               uint32_t handles[4],
                               uint32_t strides[4],
                               uint32_t offsets[4],
                               uint64_t modifiers[4],
                               uint32_t* framebuffer,
                               uint32_t flags);

  /*******
   * Gamma
   *******/

  virtual bool SetGammaRamp(uint32_t crtc_id, const display::GammaCurve& lut);

  /********
   * Planes
   ********/

  // Returns the list of all planes available on this DRM device.
  virtual ScopedDrmPlaneResPtr GetPlaneResources() const;

  // Returns the properties associated with plane with id |plane_id|.
  virtual ScopedDrmPlanePtr GetPlane(uint32_t plane_id) const;

  /************
   * Properties
   ************/

  // Returns the properties associated with object with id |object_id| and type
  // |object_type|. |object_type| is one of DRM_MODE_OBJECT_*.
  virtual ScopedDrmObjectPropertyPtr GetObjectProperties(
      uint32_t object_id,
      uint32_t object_type) const;

  // Sets a property (defined by {|property_id|, |property_value|} on an object
  // with ID |object_id| and type |object_type|.
  // |object_id| and |property_id| are unique identifiers.
  // |object_type| is one of DRM_MODE_OBJECT_*.
  virtual bool SetObjectProperty(uint32_t object_id,
                                 uint32_t object_type,
                                 uint32_t property_id,
                                 uint32_t property_value);

  virtual ScopedDrmPropertyPtr GetProperty(uint32_t id) const;

  // Returns the property with name |name| associated with |connector|. Returns
  // NULL if property not found. If the returned value is valid, it must be
  // released using FreeProperty().
  virtual ScopedDrmPropertyPtr GetProperty(drmModeConnector* connector,
                                           const char* name) const;

  // Sets the value of property with ID |property_id| to |value|. The property
  // is applied to the connector with ID |connector_id|.
  virtual bool SetProperty(uint32_t connector_id,
                           uint32_t property_id,
                           uint64_t value);

  /****************
   * Property Blobs
   ****************/

  // Creates a property blob with data |blob| of size |size|.
  virtual ScopedDrmPropertyBlob CreatePropertyBlob(const void* blob,
                                                   size_t size);
  // Creates a property blob with |size| for data |blob| which user space
  // can't read back.
  virtual ScopedDrmPropertyBlob CreatePropertyBlobWithFlags(const void* blob,
                                                            size_t size,
                                                            uint32_t flags);
  virtual void DestroyPropertyBlob(uint32_t id);

  // Returns a binary blob associated with |property_id|. May be nullptr if the
  // property couldn't be found.
  virtual ScopedDrmPropertyBlobPtr GetPropertyBlob(uint32_t property_id) const;

  // Returns a binary blob associated with |connector|. The binary blob is
  // associated with the property with name |name|. Return NULL if the property
  // could not be found or if the property does not have a binary blob. If valid
  // the returned object must be freed using FreePropertyBlob().
  virtual ScopedDrmPropertyBlobPtr GetPropertyBlob(drmModeConnector* connector,
                                                   const char* name) const;

  /***********
   * Resources
   ***********/

  // Returns all the DRM resources for this device. This includes CRTC,
  // connectors, and encoders state.
  virtual ScopedDrmResourcesPtr GetResources() const;

  /*********
   * Utility
   *********/

  // Adds trace records to |context|.
  virtual void WriteIntoTrace(perfetto::TracedDictionary dict) const;

  virtual std::optional<std::string> GetDriverName() const;

  // TODO(gildekel): remove once DrmWrapper and DrmDevice are completely
  // decoupled.
  // Returns a list of supported drm formats and modifiers for |crtc_id|. Note:
  // implementation in wrapper is a stub. Full implementation is in
  // DrmDevice::GetFormatsAndModifiersForCrtc().
  virtual display::DrmFormatsAndModifiers GetFormatsAndModifiersForCrtc(
      uint32_t crtc_id) const;

  // Extracts the FD from the given |drm|. The |drm| object will be invalidated.
  static base::ScopedFD ToScopedFD(std::unique_ptr<DrmWrapper> drm);

  base::FilePath device_path() const { return device_path_; }
  bool is_atomic() const { return is_atomic_; }
  bool is_primary_device() const { return is_primary_device_; }

 protected:
  // TODO(gildekel): move CommitProperties() and PageFlip() to the public API
  // once DrmWrapper and DrmDevice are completely decoupled. Consider changing
  // the signature to `void* user_data` instead of |page_flip_id|, which is too
  // specific.
  bool CommitProperties(drmModeAtomicReq* properties,
                        uint32_t flags,
                        uint64_t page_flip_id);

  bool PageFlip(uint32_t crtc_id, uint32_t framebuffer, uint64_t page_flip_id);

  const int& GetFd() const { return drm_fd_.get(); }

 private:
  // Path to the DRM device (in sysfs).
  const base::FilePath device_path_;

  // DRM device FD.
  base::ScopedFD drm_fd_;

  // Whether or not DRM was successfully set to atomic during the initialization
  // of this DRM device.
  bool is_atomic_ = false;

  const bool is_primary_device_;

  // DRM master for a device is initially acquired implicitly in Chrome by
  // opening the device node when no one else is holding the master, not through
  // set master ioctl.
  bool has_master_ = true;
};

}  // namespace ui

#endif  // UI_OZONE_PLATFORM_DRM_COMMON_DRM_WRAPPER_H_