summaryrefslogtreecommitdiff
path: root/mozilla-1619882-1.patch
blob: 25ff3213a31e0ee57f8d77901e526d7c9c221e60 (plain)
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
diff --git a/gfx/layers/ipc/LayersSurfaces.ipdlh b/gfx/layers/ipc/LayersSurfaces.ipdlh
--- a/gfx/layers/ipc/LayersSurfaces.ipdlh
+++ b/gfx/layers/ipc/LayersSurfaces.ipdlh
@@ -71,6 +71,8 @@
   uint32_t[] offsets;
   YUVColorSpace yUVColorSpace;
   FileDescriptor[] fence;
+  uint32_t uid;
+  FileDescriptor[] refCount;
 };
 
 struct SurfaceTextureDescriptor {
diff --git a/widget/gtk/WaylandDMABufSurface.h b/widget/gtk/WaylandDMABufSurface.h
--- a/widget/gtk/WaylandDMABufSurface.h
+++ b/widget/gtk/WaylandDMABufSurface.h
@@ -40,6 +40,23 @@
 
 class WaylandDMABufSurfaceRGBA;
 
+class DMABufRefcount {
+ public:
+  DMABufRefcount();
+  DMABufRefcount(int aFd);
+
+  bool Created() { return mFd > 0; };
+  int GetFD() { return mFd; }
+  uint64_t GetRefcount();
+  void RefAdd();
+  void Release();
+
+  ~DMABufRefcount();
+
+ private:
+  int mFd;
+};
+
 class WaylandDMABufSurface {
  public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WaylandDMABufSurface)
@@ -82,6 +99,14 @@
   void FenceWait();
   void FenceDelete();
 
+  void SetUID(uint32_t aUID) { mUID = aUID; };
+  uint32_t GetUID() { return mUID; };
+
+  void GlobalRefCountCreate();
+  bool IsGlobalRefSet();
+  void GlobalRefAdd();
+  void GlobalRefRelease();
+
   WaylandDMABufSurface(SurfaceType aSurfaceType);
 
  protected:
@@ -89,6 +114,8 @@
   virtual void ReleaseSurface() = 0;
   bool FenceCreate(int aFd);
 
+  bool GlobalRefCountImport(int aFd);
+
   virtual ~WaylandDMABufSurface() { FenceDelete(); };
 
   SurfaceType mSurfaceType;
@@ -102,6 +129,9 @@
 
   EGLSyncKHR mSync;
   RefPtr<mozilla::gl::GLContext> mGL;
+
+  mozilla::UniquePtr<DMABufRefcount> mGlobalRefCount;
+  uint32_t mUID;
 };
 
 class WaylandDMABufSurfaceRGBA : public WaylandDMABufSurface {
diff --git a/widget/gtk/WaylandDMABufSurface.cpp b/widget/gtk/WaylandDMABufSurface.cpp
--- a/widget/gtk/WaylandDMABufSurface.cpp
+++ b/widget/gtk/WaylandDMABufSurface.cpp
@@ -17,6 +17,8 @@
 #include <unistd.h>
 #include <sys/time.h>
 #include <dlfcn.h>
+#include <sys/mman.h>
+#include <sys/eventfd.h>
 
 #include "mozilla/widget/gbm.h"
 #include "mozilla/widget/va_drmcommon.h"
@@ -57,6 +59,73 @@
 #  define VA_FOURCC_NV12 0x3231564E
 #endif
 
+void DMABufRefcount::RefAdd() {
+  uint64_t counter;
+  read(mFd, &counter, sizeof(counter));
+  counter++;
+  write(mFd, &counter, sizeof(counter));
+}
+
+void DMABufRefcount::Release() {
+  uint64_t counter;
+  read(mFd, &counter, sizeof(counter));
+  counter--;
+  write(mFd, &counter, sizeof(counter));
+}
+
+uint64_t DMABufRefcount::GetRefcount() {
+  uint64_t counter;
+  read(mFd, &counter, sizeof(counter));
+  write(mFd, &counter, sizeof(counter));
+  return counter;
+}
+
+DMABufRefcount::DMABufRefcount()
+    : mFd(eventfd(1, EFD_CLOEXEC | EFD_NONBLOCK)) {}
+
+DMABufRefcount::DMABufRefcount(int aFd) : mFd(aFd) {}
+
+DMABufRefcount::~DMABufRefcount() {
+  if (mFd > 0) {
+    close(mFd);
+  }
+}
+
+void WaylandDMABufSurface::GlobalRefCountCreate() {
+  MOZ_ASSERT(!mGlobalRefCount);
+  mGlobalRefCount = MakeUnique<DMABufRefcount>();
+  if (!mGlobalRefCount->Created()) {
+    NS_WARNING("Failed to create dmabuf global ref count!");
+    mGlobalRefCount = nullptr;
+  }
+}
+
+bool WaylandDMABufSurface::GlobalRefCountImport(int aFd) {
+  MOZ_ASSERT(!mGlobalRefCount);
+  mGlobalRefCount = MakeUnique<DMABufRefcount>(aFd);
+  if (!mGlobalRefCount->Created()) {
+    NS_WARNING("Failed to import dmabuf global ref count!");
+    mGlobalRefCount = nullptr;
+    return false;
+  }
+  return true;
+}
+
+bool WaylandDMABufSurface::IsGlobalRefSet() {
+  MOZ_ASSERT(mGlobalRefCount);
+  return mGlobalRefCount->GetRefcount() > 1;
+}
+
+void WaylandDMABufSurface::GlobalRefAdd() {
+  MOZ_ASSERT(mGlobalRefCount);
+  mGlobalRefCount->RefAdd();
+}
+
+void WaylandDMABufSurface::GlobalRefRelease() {
+  MOZ_ASSERT(mGlobalRefCount);
+  mGlobalRefCount->Release();
+}
+
 WaylandDMABufSurface::WaylandDMABufSurface(SurfaceType aSurfaceType)
     : mSurfaceType(aSurfaceType),
       mBufferModifier(DRM_FORMAT_MOD_INVALID),
@@ -64,7 +133,9 @@
       mDrmFormats(),
       mStrides(),
       mOffsets(),
-      mSync(0) {
+      mSync(0),
+      mGlobalRefCount(nullptr),
+      mUID(0) {
   for (auto& slot : mDmabufFds) {
     slot = -1;
   }
@@ -316,6 +387,7 @@
   mBufferPlaneCount = desc.fds().Length();
   mGbmBufferFlags = desc.flags();
   MOZ_RELEASE_ASSERT(mBufferPlaneCount <= DMABUF_BUFFER_PLANES);
+  mUID = desc.uid();
 
   for (int i = 0; i < mBufferPlaneCount; i++) {
     mDmabufFds[i] = desc.fds()[i].ClonePlatformHandle().release();
@@ -329,6 +401,13 @@
       close(fd);
     }
   }
+
+  if (desc.refCount().Length() > 0) {
+    int fd = desc.refCount()[0].ClonePlatformHandle().release();
+    if (!GlobalRefCountImport(fd)) {
+      close(fd);
+    }
+  }
 }
 
 bool WaylandDMABufSurfaceRGBA::Create(const SurfaceDescriptor& aDesc) {
@@ -346,6 +425,7 @@
   AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> offsets;
   AutoTArray<uintptr_t, DMABUF_BUFFER_PLANES> images;
   AutoTArray<ipc::FileDescriptor, 1> fenceFDs;
+  AutoTArray<ipc::FileDescriptor, 1> refCountFDs;
 
   width.AppendElement(mWidth);
   height.AppendElement(mHeight);
@@ -362,9 +442,14 @@
         egl->fDupNativeFenceFDANDROID(egl->Display(), mSync)));
   }
 
-  aOutDescriptor = SurfaceDescriptorDMABuf(
-      mSurfaceType, mBufferModifier, mGbmBufferFlags, fds, width, height,
-      format, strides, offsets, GetYUVColorSpace(), fenceFDs);
+  if (mGlobalRefCount) {
+    refCountFDs.AppendElement(ipc::FileDescriptor(mGlobalRefCount->GetFD()));
+  }
+
+  aOutDescriptor =
+      SurfaceDescriptorDMABuf(mSurfaceType, mBufferModifier, mGbmBufferFlags,
+                              fds, width, height, format, strides, offsets,
+                              GetYUVColorSpace(), fenceFDs, mUID, refCountFDs);
 
   return true;
 }
@@ -693,6 +778,7 @@
   mBufferPlaneCount = aDesc.fds().Length();
   mBufferModifier = aDesc.modifier();
   mColorSpace = aDesc.yUVColorSpace();
+  mUID = aDesc.uid();
 
   MOZ_RELEASE_ASSERT(mBufferPlaneCount <= DMABUF_BUFFER_PLANES);
   for (int i = 0; i < mBufferPlaneCount; i++) {
@@ -710,6 +796,13 @@
       close(fd);
     }
   }
+
+  if (aDesc.refCount().Length() > 0) {
+    int fd = aDesc.refCount()[0].ClonePlatformHandle().release();
+    if (!GlobalRefCountImport(fd)) {
+      close(fd);
+    }
+  }
 }
 
 bool WaylandDMABufSurfaceNV12::Serialize(
@@ -721,6 +814,7 @@
   AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> strides;
   AutoTArray<uint32_t, DMABUF_BUFFER_PLANES> offsets;
   AutoTArray<ipc::FileDescriptor, 1> fenceFDs;
+  AutoTArray<ipc::FileDescriptor, 1> refCountFDs;
 
   for (int i = 0; i < mBufferPlaneCount; i++) {
     width.AppendElement(mWidth[i]);
@@ -737,9 +831,13 @@
         egl->fDupNativeFenceFDANDROID(egl->Display(), mSync)));
   }
 
+  if (mGlobalRefCount) {
+    refCountFDs.AppendElement(ipc::FileDescriptor(mGlobalRefCount->GetFD()));
+  }
+
   aOutDescriptor = SurfaceDescriptorDMABuf(
       mSurfaceType, mBufferModifier, 0, fds, width, height, format, strides,
-      offsets, GetYUVColorSpace(), fenceFDs);
+      offsets, GetYUVColorSpace(), fenceFDs, mUID, refCountFDs);
   return true;
 }
 
diff --git a/widget/gtk/WindowSurfaceWayland.h b/widget/gtk/WindowSurfaceWayland.h
--- a/widget/gtk/WindowSurfaceWayland.h
+++ b/widget/gtk/WindowSurfaceWayland.h
@@ -36,8 +36,6 @@
                             int aImageDataSize);
 
  private:
-  int CreateTemporaryFile(int aSize);
-
   wl_shm_pool* mShmPool;
   int mShmPoolFd;
   int mAllocatedSize;
diff --git a/widget/gtk/WindowSurfaceWayland.cpp b/widget/gtk/WindowSurfaceWayland.cpp
--- a/widget/gtk/WindowSurfaceWayland.cpp
+++ b/widget/gtk/WindowSurfaceWayland.cpp
@@ -206,42 +206,25 @@
   return mWindowSurfaceWayland->GetWaylandDisplay();
 }
 
-int WaylandShmPool::CreateTemporaryFile(int aSize) {
-  const char* tmppath = getenv("XDG_RUNTIME_DIR");
-  MOZ_RELEASE_ASSERT(tmppath, "Missing XDG_RUNTIME_DIR env variable.");
-
-  nsPrintfCString tmpname("%s/mozilla-shared-XXXXXX", tmppath);
-
-  char* filename;
-  int fd = -1;
-  int ret = 0;
-
-  if (tmpname.GetMutableData(&filename)) {
-    fd = mkstemp(filename);
-    if (fd >= 0) {
-      int flags = fcntl(fd, F_GETFD);
-      if (flags >= 0) {
-        fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
-      }
-    }
-  }
-
+static int WaylandAllocateShmMemory(int aSize) {
+  static int counter = 0;
+  nsPrintfCString shmName("/wayland.mozilla.ipc.%d", counter++);
+  int fd = shm_open(shmName.get(), O_CREAT | O_RDWR | O_EXCL, 0600);
   if (fd >= 0) {
-    unlink(tmpname.get());
+    shm_unlink(shmName.get());
   } else {
-    printf_stderr("Unable to create mapping file %s\n", filename);
+    printf_stderr("Unable to SHM memory segment\n");
     MOZ_CRASH();
   }
 
+  int ret = 0;
 #ifdef HAVE_POSIX_FALLOCATE
   do {
     ret = posix_fallocate(fd, 0, aSize);
   } while (ret == EINTR);
   if (ret != 0) {
     close(fd);
-    MOZ_CRASH_UNSAFE_PRINTF(
-        "posix_fallocate() fails on %s size %d error code %d\n", filename,
-        aSize, ret);
+    MOZ_CRASH("posix_fallocate() fails to allocate shm memory");
   }
 #else
   do {
@@ -249,8 +232,7 @@
   } while (ret < 0 && errno == EINTR);
   if (ret < 0) {
     close(fd);
-    MOZ_CRASH_UNSAFE_PRINTF("ftruncate() fails on %s size %d error code %d\n",
-                            filename, aSize, ret);
+    MOZ_CRASH("ftruncate() fails to allocate shm memory");
   }
 #endif
 
@@ -259,7 +241,7 @@
 
 WaylandShmPool::WaylandShmPool(nsWaylandDisplay* aWaylandDisplay, int aSize)
     : mAllocatedSize(aSize) {
-  mShmPoolFd = CreateTemporaryFile(mAllocatedSize);
+  mShmPoolFd = WaylandAllocateShmMemory(mAllocatedSize);
   mImageData = mmap(nullptr, mAllocatedSize, PROT_READ | PROT_WRITE, MAP_SHARED,
                     mShmPoolFd, 0);
   MOZ_RELEASE_ASSERT(mImageData != MAP_FAILED,

bgstack15