File: zwp_linux_dmabuf.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (310 lines) | stat: -rw-r--r-- 11,466 bytes parent folder | download
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/wayland/zwp_linux_dmabuf.h"
#include "base/memory/raw_ptr.h"

#include <drm_fourcc.h>
#include <linux-dmabuf-unstable-v1-server-protocol.h>

#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "components/exo/buffer.h"
#include "components/exo/display.h"
#include "components/exo/wayland/server_util.h"
#include "components/exo/wayland/wayland_dmabuf_feedback_manager.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gfx/linux/drm_util_linux.h"

namespace exo {
namespace wayland {
namespace {

////////////////////////////////////////////////////////////////////////////////
// wl_buffer_interface:

void buffer_destroy(wl_client* client, wl_resource* resource) {
  wl_resource_destroy(resource);
}

const struct wl_buffer_interface buffer_implementation = {buffer_destroy};

void HandleBufferReleaseCallback(wl_resource* resource) {
  wl_buffer_send_release(resource);
  wl_client_flush(wl_resource_get_client(resource));
}

////////////////////////////////////////////////////////////////////////////////
// linux_buffer_params_interface:

struct LinuxBufferParams {
  struct Plane {
    base::ScopedFD fd;
    uint32_t stride;
    uint32_t offset;
    uint64_t modifier;
  };

  explicit LinuxBufferParams(WaylandDmabufFeedbackManager* feedback_manager)
      : feedback_manager(feedback_manager) {}

  const raw_ptr<WaylandDmabufFeedbackManager, ExperimentalAsh> feedback_manager;
  std::map<uint32_t, Plane> planes;
};

void linux_buffer_params_destroy(wl_client* client, wl_resource* resource) {
  wl_resource_destroy(resource);
}

void linux_buffer_params_add(wl_client* client,
                             wl_resource* resource,
                             int32_t fd,
                             uint32_t plane_idx,
                             uint32_t offset,
                             uint32_t stride,
                             uint32_t modifier_hi,
                             uint32_t modifier_lo) {
  LinuxBufferParams* linux_buffer_params =
      GetUserDataAs<LinuxBufferParams>(resource);

  const uint64_t modifier = (static_cast<uint64_t>(modifier_hi) << 32) | modifier_lo;
  LinuxBufferParams::Plane plane{base::ScopedFD(fd), stride, offset, modifier};

  bool inserted = linux_buffer_params->planes
                      .insert(std::make_pair(plane_idx, std::move(plane)))
                      .second;
  if (!inserted) {  // The plane was already there.
    wl_resource_post_error(resource, ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_SET,
                           "plane already set");
  }
}

bool ValidateLinuxBufferParams(wl_resource* resource,
                               int32_t width,
                               int32_t height,
                               gfx::BufferFormat format,
                               uint32_t flags) {
  if (width <= 0 || height <= 0) {
    wl_resource_post_error(resource,
                           ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_DIMENSIONS,
                           "invalid width or height");
    return false;
  }

  if (flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_INTERLACED) {
    wl_resource_post_error(resource,
                           ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
                           "flags not supported");
    return false;
  }

  LinuxBufferParams* linux_buffer_params =
      GetUserDataAs<LinuxBufferParams>(resource);

  size_t num_planes = linux_buffer_params->planes.size();
  if (num_planes == 0) {
    wl_resource_post_error(resource,
                          ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
                          "no planes given");
    return false;
  }

  // Validate that we have planes 0..num_planes-1
  for (uint32_t i = 0; i < num_planes; ++i) {
    if (!base::Contains(linux_buffer_params->planes, i)) {
      wl_resource_post_error(resource,
                             ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
                             "missing a plane");
      return false;
    }
  }

  // All planes must have the same modifier.
  uint64_t modifier = linux_buffer_params->planes[0].modifier;
  for (uint32_t i = 1; i < num_planes; ++i) {
    if (linux_buffer_params->planes[i].modifier != modifier) {
      wl_resource_post_error(resource,
                             ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_FORMAT,
                             "all planes must have same modifier");
      return false;
    }
  }

  return true;
}

wl_resource* create_buffer(wl_client* client,
                           wl_resource* resource,
                           uint32_t buffer_id,
                           int32_t width,
                           int32_t height,
                           uint32_t format,
                           uint32_t flags) {
  LinuxBufferParams* linux_buffer_params =
      GetUserDataAs<LinuxBufferParams>(resource);

  if (!linux_buffer_params->feedback_manager->IsFormatSupported(format)) {
    wl_resource_post_error(resource,
                           ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_FORMAT,
                           "format not supported");
    return nullptr;
  }

  gfx::BufferFormat buffer_format = ui::GetBufferFormatFromFourCCFormat(format);
  if (!ValidateLinuxBufferParams(resource, width, height, buffer_format,
                                 flags)) {
    return nullptr;
  }

  gfx::NativePixmapHandle handle;

  // A lot of clients (arc++, arcvm, sommelier etc) pass 0
  // (DRM_FORMAT_MOD_LINEAR) when they don't know the format modifier.
  // They're supposed to pass DRM_FORMAT_MOD_INVALID, which triggers
  // EGL import without an explicit modifier and lets the driver pick
  // up the buffer layout from out-of-band channels like kernel ioctls.
  //
  // We can't fix all the clients in one go, but we can preserve the
  // behaviour that 0 means implicit modifier, but only setting the
  // handle modifier if we get a non-0 modifier.
  //
  // TODO(hoegsberg): Once we've fixed all relevant clients, we should
  // remove this so as to catch future misuse.
  if (linux_buffer_params->planes[0].modifier != 0)
    handle.modifier = linux_buffer_params->planes[0].modifier;

  for (uint32_t i = 0; i < linux_buffer_params->planes.size(); ++i) {
    auto& plane = linux_buffer_params->planes[i];
    handle.planes.emplace_back(plane.stride, plane.offset, 0,
                               std::move(plane.fd));
  }

  bool y_invert = (flags & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT) != 0;

  std::unique_ptr<Buffer> buffer =
      linux_buffer_params->feedback_manager->GetDisplay()
          ->CreateLinuxDMABufBuffer(gfx::Size(width, height), buffer_format,
                                    std::move(handle), y_invert);
  if (!buffer) {
    zwp_linux_buffer_params_v1_send_failed(resource);
    return nullptr;
  }

  wl_resource* buffer_resource =
      wl_resource_create(client, &wl_buffer_interface, 1, buffer_id);

  buffer->set_release_callback(base::BindRepeating(
      &HandleBufferReleaseCallback, base::Unretained(buffer_resource)));

  SetImplementation(buffer_resource, &buffer_implementation, std::move(buffer));

  return buffer_resource;
}

void linux_buffer_params_create(wl_client* client,
                                wl_resource* resource,
                                int32_t width,
                                int32_t height,
                                uint32_t format,
                                uint32_t flags) {
  wl_resource* buffer_resource =
      create_buffer(client, resource, 0, width, height, format, flags);

  if (buffer_resource)
    zwp_linux_buffer_params_v1_send_created(resource, buffer_resource);
}

void linux_buffer_params_create_immed(wl_client* client,
                                      wl_resource* resource,
                                      uint32_t buffer_id,
                                      int32_t width,
                                      int32_t height,
                                      uint32_t format,
                                      uint32_t flags) {
  create_buffer(client, resource, buffer_id, width, height, format, flags);
}

const struct zwp_linux_buffer_params_v1_interface
    linux_buffer_params_implementation = {
        linux_buffer_params_destroy, linux_buffer_params_add,
        linux_buffer_params_create, linux_buffer_params_create_immed};

////////////////////////////////////////////////////////////////////////////////
// linux_dmabuf_interface:

void linux_dmabuf_destroy(wl_client* client, wl_resource* resource) {
  wl_resource_destroy(resource);
}

void linux_dmabuf_create_params(wl_client* client,
                                wl_resource* resource,
                                uint32_t id) {
  WaylandDmabufFeedbackManager* feedback_manager =
      GetUserDataAs<WaylandDmabufFeedbackManager>(resource);

  std::unique_ptr<LinuxBufferParams> linux_buffer_params =
      std::make_unique<LinuxBufferParams>(feedback_manager);

  wl_resource* linux_buffer_params_resource =
      wl_resource_create(client, &zwp_linux_buffer_params_v1_interface,
                         wl_resource_get_version(resource), id);

  SetImplementation(linux_buffer_params_resource,
                    &linux_buffer_params_implementation,
                    std::move(linux_buffer_params));
}

void linux_dmabuf_get_default_feedback(wl_client* client,
                                       wl_resource* dma_buf_resource,
                                       uint32_t feedback_id) {
  WaylandDmabufFeedbackManager* feedback_manager =
      static_cast<WaylandDmabufFeedbackManager*>(
          wl_resource_get_user_data(dma_buf_resource));

  feedback_manager->GetDefaultFeedback(client, dma_buf_resource, feedback_id);
}

void linux_dmabuf_get_surface_feedback(wl_client* client,
                                       wl_resource* dma_buf_resource,
                                       uint32_t feedback_id,
                                       wl_resource* surface_resource) {
  WaylandDmabufFeedbackManager* feedback_manager =
      static_cast<WaylandDmabufFeedbackManager*>(
          wl_resource_get_user_data(dma_buf_resource));

  feedback_manager->GetSurfaceFeedback(client, dma_buf_resource, feedback_id,
                                       surface_resource);
}

const struct zwp_linux_dmabuf_v1_interface linux_dmabuf_implementation = {
    linux_dmabuf_destroy,
    linux_dmabuf_create_params,
    linux_dmabuf_get_default_feedback,
    linux_dmabuf_get_surface_feedback,
};

}  // namespace

void bind_linux_dmabuf(wl_client* client,
                       void* data,
                       uint32_t version,
                       uint32_t id) {
  WaylandDmabufFeedbackManager* feedback_manager =
      static_cast<WaylandDmabufFeedbackManager*>(data);

  wl_resource* resource = wl_resource_create(
      client, &zwp_linux_dmabuf_v1_interface,
      std::min(version, feedback_manager->GetVersionSupportedByPlatform()), id);

  wl_resource_set_implementation(resource, &linux_dmabuf_implementation,
                                 feedback_manager, nullptr);

  if (wl_resource_get_version(resource) <
      ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION) {
    feedback_manager->SendFormatsAndModifiers(resource);
  }
}

}  // namespace wayland
}  // namespace exo