summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/sw/wayland/wayland_sw_winsys.c
blob: 331900e362d12610c5e3d695cc2762922ab02ba6 (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
/*
 * Mesa 3-D graphics library
 * Version:  7.11
 *
 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>

#include "pipe/p_compiler.h"
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "util/u_format.h"
#include "util/u_math.h"
#include "util/u_memory.h"
#include "state_tracker/sw_winsys.h"

#include <wayland-client.h>
#include "wayland_sw_winsys.h"

struct wayland_sw_displaytarget
{
   int fd;
   unsigned size;

   unsigned width;
   unsigned height;
   unsigned stride;

   enum pipe_format format;

   void *map;
   unsigned map_count;
};

struct wayland_sw_winsys
{
   struct sw_winsys base;

   struct wl_display *display;
};

static INLINE struct wayland_sw_displaytarget *
wayland_sw_displaytarget(struct sw_displaytarget *dt)
{
   return (struct wayland_sw_displaytarget *) dt;
}

static INLINE struct wayland_sw_winsys *
wayland_sw_winsys(struct sw_winsys *ws)
{
   return (struct wayland_sw_winsys *) ws;
}

static void
wayland_displaytarget_display(struct sw_winsys *ws,
                              struct sw_displaytarget *dt,
                              void *context_private)
{
}

static void
wayland_displaytarget_unmap(struct sw_winsys *ws,
                            struct sw_displaytarget *dt)
{
   struct wayland_sw_displaytarget *wldt = wayland_sw_displaytarget(dt);

   wldt->map_count--;
   if (wldt->map_count > 0)
      return;

   munmap(wldt->map, wldt->size);
   wldt->map = NULL;
}

static void *
wayland_displaytarget_map(struct sw_winsys *ws,
                          struct sw_displaytarget *dt,
                          unsigned flags)
{
   struct wayland_sw_displaytarget *wldt = wayland_sw_displaytarget(dt);
   uint mmap_flags = 0;

   if (wldt->map) {
      wldt->map_count++;
      return wldt->map;
   }

   if (flags & PIPE_TRANSFER_READ)
      mmap_flags |= PROT_READ;
   if (flags & PIPE_TRANSFER_WRITE)
      mmap_flags |= PROT_WRITE;

   wldt->map = mmap(NULL, wldt->size, mmap_flags,
                    MAP_SHARED, wldt->fd, 0);

   if (wldt->map == MAP_FAILED)
      return NULL;

   wldt->map_count = 1;

   return wldt->map;
}

static void
wayland_displaytarget_destroy(struct sw_winsys *ws,
                              struct sw_displaytarget *dt)
{
   struct wayland_sw_displaytarget *wldt = wayland_sw_displaytarget(dt);

   if (wldt->map)
      wayland_displaytarget_unmap(ws, dt);

   FREE(wldt);
}

static boolean
wayland_is_displaytarget_format_supported(struct sw_winsys *ws,
                                          unsigned tex_usage,
                                          enum pipe_format format)
{
   switch (format) {
   case PIPE_FORMAT_B8G8R8X8_UNORM:
   case PIPE_FORMAT_B8G8R8A8_UNORM:
      return TRUE;
   default:
      return FALSE;
   }
}

static struct sw_displaytarget *
wayland_displaytarget_create(struct sw_winsys *ws,
                             unsigned tex_usage,
                             enum pipe_format format,
                             unsigned width, unsigned height,
                             unsigned alignment,
                             unsigned *stride)
{
   struct wayland_sw_displaytarget *wldt;
   unsigned nblocksy, format_stride;
   char filename[] = "/tmp/wayland-shm-XXXXXX";

   if (!wayland_is_displaytarget_format_supported(ws, tex_usage, format))
      return NULL;

   wldt = CALLOC_STRUCT(wayland_sw_displaytarget);
   if (!wldt)
      return NULL;

   wldt->map = NULL;

   wldt->format = format;
   wldt->width = width;
   wldt->height = height;

   format_stride = util_format_get_stride(format, width);
   wldt->stride = align(format_stride, alignment);

   nblocksy = util_format_get_nblocksy(format, height);
   wldt->size = wldt->stride * nblocksy;

   wldt->fd = mkstemp(filename);
   if (wldt->fd < 0) {
      FREE(wldt);
      return NULL;
   }

   if (ftruncate(wldt->fd, wldt->size) < 0) {
      unlink(filename);
      close(wldt->fd);
      FREE(wldt);
      return NULL;
   }

   unlink(filename);

   *stride = wldt->stride;

   return (struct sw_displaytarget *) wldt;
}

static struct sw_displaytarget *
wayland_displaytarget_from_handle(struct sw_winsys *ws,
                                  const struct pipe_resource *templet,
                                  struct winsys_handle *whandle,
                                  unsigned *stride)
{
   struct wayland_sw_displaytarget *wldt;
   unsigned nblocksy;

   if (!wayland_is_displaytarget_format_supported(ws, 0, templet->format))
      return NULL;

   wldt = CALLOC_STRUCT(wayland_sw_displaytarget);
   if (!wldt)
      return NULL;
 
   wldt->fd = whandle->fd;
   wldt->stride = whandle->stride;
   wldt->width = templet->width0;
   wldt->height = templet->height0;
   wldt->format = templet->format;

   nblocksy = util_format_get_nblocksy(wldt->format, wldt->height);

   wldt->size = wldt->stride * nblocksy;

   wldt->map = NULL;

   *stride = wldt->stride;

   return (struct sw_displaytarget *) wldt;
}


static boolean
wayland_displaytarget_get_handle(struct sw_winsys *ws,
                                 struct sw_displaytarget *dt,
                                 struct winsys_handle *whandle)
{
   struct wayland_sw_displaytarget *wldt = wayland_sw_displaytarget(dt);

   whandle->fd = wldt->fd;
   whandle->stride = wldt->stride;
   whandle->size = wldt->size;

   return TRUE;
}

static void
wayland_destroy(struct sw_winsys *ws)
{
   struct wayland_sw_winsys *wayland = wayland_sw_winsys(ws);

   FREE(wayland);
}

struct sw_winsys *
wayland_create_sw_winsys(struct wl_display *display)
{
   struct wayland_sw_winsys *wlws;

   wlws = CALLOC_STRUCT(wayland_sw_winsys);
   if (!wlws)
      return NULL;

   wlws->display = display;

   wlws->base.destroy = wayland_destroy;
   wlws->base.is_displaytarget_format_supported =
      wayland_is_displaytarget_format_supported;

   wlws->base.displaytarget_create = wayland_displaytarget_create;
   wlws->base.displaytarget_from_handle = wayland_displaytarget_from_handle;
   wlws->base.displaytarget_get_handle = wayland_displaytarget_get_handle;
   wlws->base.displaytarget_destroy = wayland_displaytarget_destroy;
   wlws->base.displaytarget_map = wayland_displaytarget_map;
   wlws->base.displaytarget_unmap = wayland_displaytarget_unmap;

   wlws->base.displaytarget_display = wayland_displaytarget_display;

   return &wlws->base;
}

/* vim: set sw=3 ts=8 sts=3 expandtab: */