| 1 |
/************************************************************************** |
| 2 |
* |
| 3 |
* Copyright 2009, VMware, Inc. |
| 4 |
* All Rights Reserved. |
| 5 |
* Copyright 2010 George Sapountzis <[email protected]> |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 8 |
* copy of this software and associated documentation files (the |
| 9 |
* "Software"), to deal in the Software without restriction, including |
| 10 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 11 |
* distribute, sub license, and/or sell copies of the Software, and to |
| 12 |
* permit persons to whom the Software is furnished to do so, subject to |
| 13 |
* the following conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice (including the |
| 16 |
* next paragraph) shall be included in all copies or substantial portions |
| 17 |
* of the Software. |
| 18 |
* |
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. |
| 22 |
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR |
| 23 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
* |
| 27 |
**************************************************************************/ |
| 28 |
|
| 29 |
#ifdef HAVE_SYS_SHM_H |
| 30 |
#include <sys/ipc.h> |
| 31 |
#include <sys/shm.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#include "util/compiler.h" |
| 35 |
#include "util/format/u_formats.h" |
| 36 |
#include "util/detect_os.h" |
| 37 |
|
| 38 |
#if DETECT_OS_POSIX |
| 39 |
# include <sys/stat.h> |
| 40 |
# include <errno.h> |
| 41 |
# include <sys/mman.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "pipe/p_state.h" |
| 45 |
#include "util/u_inlines.h" |
| 46 |
#include "util/format/u_format.h" |
| 47 |
#include "util/u_math.h" |
| 48 |
#include "util/u_memory.h" |
| 49 |
#include "util/os_file.h" |
| 50 |
|
| 51 |
#include "frontend/sw_winsys.h" |
| 52 |
#include "dri_sw_winsys.h" |
| 53 |
|
| 54 |
|
| 55 |
struct dri_sw_displaytarget |
| 56 |
{ |
| 57 |
enum pipe_format format; |
| 58 |
unsigned width; |
| 59 |
unsigned height; |
| 60 |
unsigned stride; |
| 61 |
|
| 62 |
unsigned map_flags; |
| 63 |
int shmid; |
| 64 |
void *data; |
| 65 |
void *mapped; |
| 66 |
const void *front_private; |
| 67 |
/* dmabuf */ |
| 68 |
int fd; |
| 69 |
int offset; |
| 70 |
size_t size; |
| 71 |
bool unbacked; |
| 72 |
}; |
| 73 |
|
| 74 |
struct dri_sw_winsys |
| 75 |
{ |
| 76 |
struct sw_winsys base; |
| 77 |
|
| 78 |
const struct drisw_loader_funcs *lf; |
| 79 |
}; |
| 80 |
|
| 81 |
static inline struct dri_sw_displaytarget * |
| 82 |
dri_sw_displaytarget( struct sw_displaytarget *dt ) |
| 83 |
{ |
| 84 |
return (struct dri_sw_displaytarget *)dt; |
| 85 |
} |
| 86 |
|
| 87 |
static inline struct dri_sw_winsys * |
| 88 |
dri_sw_winsys( struct sw_winsys *ws ) |
| 89 |
{ |
| 90 |
return (struct dri_sw_winsys *)ws; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static bool |
| 95 |
dri_sw_is_displaytarget_format_supported( struct sw_winsys *ws, |
| 96 |
unsigned tex_usage, |
| 97 |
enum pipe_format format ) |
| 98 |
{ |
| 99 |
/* TODO: check visuals or other sensible thing here */ |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
#ifdef HAVE_SYS_SHM_H |
| 104 |
static char * |
| 105 |
alloc_shm(struct dri_sw_displaytarget *dri_sw_dt, unsigned size) |
| 106 |
{ |
| 107 |
char *addr; |
| 108 |
|
| 109 |
/* 0600 = user read+write */ |
| 110 |
dri_sw_dt->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600); |
| 111 |
if (dri_sw_dt->shmid < 0) { |
| 112 |
dri_sw_dt->shmid = -1; |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
addr = (char *) shmat(dri_sw_dt->shmid, NULL, 0); |
| 117 |
/* mark the segment immediately for deletion to avoid leaks */ |
| 118 |
shmctl(dri_sw_dt->shmid, IPC_RMID, NULL); |
| 119 |
|
| 120 |
if (addr == (char *) -1) { |
| 121 |
dri_sw_dt->shmid = -1; |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
return addr; |
| 126 |
} |
| 127 |
#endif |
| 128 |
|
| 129 |
static struct sw_displaytarget * |
| 130 |
dri_sw_displaytarget_create(struct sw_winsys *winsys, |
| 131 |
unsigned tex_usage, |
| 132 |
enum pipe_format format, |
| 133 |
unsigned width, unsigned height, |
| 134 |
unsigned alignment, |
| 135 |
const void *front_private, |
| 136 |
unsigned *stride) |
| 137 |
{ |
| 138 |
UNUSED struct dri_sw_winsys *ws = dri_sw_winsys(winsys); |
| 139 |
struct dri_sw_displaytarget *dri_sw_dt; |
| 140 |
unsigned nblocksy, size, format_stride; |
| 141 |
|
| 142 |
dri_sw_dt = CALLOC_STRUCT(dri_sw_displaytarget); |
| 143 |
if(!dri_sw_dt) |
| 144 |
goto no_dt; |
| 145 |
|
| 146 |
dri_sw_dt->format = format; |
| 147 |
dri_sw_dt->width = width; |
| 148 |
dri_sw_dt->height = height; |
| 149 |
dri_sw_dt->front_private = front_private; |
| 150 |
|
| 151 |
format_stride = util_format_get_stride(format, width); |
| 152 |
dri_sw_dt->stride = align(format_stride, alignment); |
| 153 |
|
| 154 |
nblocksy = util_format_get_nblocksy(format, height); |
| 155 |
size = dri_sw_dt->stride * nblocksy; |
| 156 |
dri_sw_dt->size = size; |
| 157 |
|
| 158 |
dri_sw_dt->shmid = -1; |
| 159 |
dri_sw_dt->fd = -1; |
| 160 |
|
| 161 |
#ifdef HAVE_SYS_SHM_H |
| 162 |
if (ws->lf->put_image_shm) |
| 163 |
dri_sw_dt->data = alloc_shm(dri_sw_dt, size); |
| 164 |
#endif |
| 165 |
|
| 166 |
if(!dri_sw_dt->data) |
| 167 |
dri_sw_dt->data = align_malloc(size, alignment); |
| 168 |
|
| 169 |
if(!dri_sw_dt->data) |
| 170 |
goto no_data; |
| 171 |
|
| 172 |
*stride = dri_sw_dt->stride; |
| 173 |
return (struct sw_displaytarget *)dri_sw_dt; |
| 174 |
|
| 175 |
no_data: |
| 176 |
FREE(dri_sw_dt); |
| 177 |
no_dt: |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
static struct sw_displaytarget * |
| 182 |
dri_sw_displaytarget_create_mapped(struct sw_winsys *winsys, |
| 183 |
unsigned tex_usage, |
| 184 |
enum pipe_format format, |
| 185 |
unsigned width, unsigned height, |
| 186 |
unsigned stride, |
| 187 |
void *data, |
| 188 |
struct winsys_handle *whandle) |
| 189 |
{ |
| 190 |
UNUSED struct dri_sw_winsys *ws = dri_sw_winsys(winsys); |
| 191 |
struct dri_sw_displaytarget *dri_sw_dt; |
| 192 |
unsigned nblocksy, size; |
| 193 |
|
| 194 |
dri_sw_dt = CALLOC_STRUCT(dri_sw_displaytarget); |
| 195 |
if(!dri_sw_dt) |
| 196 |
return NULL; |
| 197 |
|
| 198 |
dri_sw_dt->format = format; |
| 199 |
dri_sw_dt->width = width; |
| 200 |
dri_sw_dt->height = height; |
| 201 |
|
| 202 |
dri_sw_dt->stride = stride; |
| 203 |
|
| 204 |
nblocksy = util_format_get_nblocksy(format, height); |
| 205 |
size = dri_sw_dt->stride * nblocksy; |
| 206 |
dri_sw_dt->size = size; |
| 207 |
|
| 208 |
dri_sw_dt->shmid = -1; |
| 209 |
dri_sw_dt->fd = -1; |
| 210 |
dri_sw_dt->unbacked = true; |
| 211 |
|
| 212 |
dri_sw_dt->data = data; |
| 213 |
dri_sw_dt->mapped = data; |
| 214 |
|
| 215 |
return (struct sw_displaytarget *)dri_sw_dt; |
| 216 |
} |
| 217 |
|
| 218 |
static void |
| 219 |
dri_sw_displaytarget_destroy(struct sw_winsys *ws, |
| 220 |
struct sw_displaytarget *dt) |
| 221 |
{ |
| 222 |
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt); |
| 223 |
|
| 224 |
if (dri_sw_dt->unbacked) {} |
| 225 |
else if (dri_sw_dt->fd >= 0) { |
| 226 |
if (dri_sw_dt->mapped) |
| 227 |
ws->displaytarget_unmap(ws, dt); |
| 228 |
close(dri_sw_dt->fd); |
| 229 |
} else if (dri_sw_dt->shmid >= 0) { |
| 230 |
#ifdef HAVE_SYS_SHM_H |
| 231 |
shmdt(dri_sw_dt->data); |
| 232 |
shmctl(dri_sw_dt->shmid, IPC_RMID, NULL); |
| 233 |
#endif |
| 234 |
} else { |
| 235 |
align_free(dri_sw_dt->data); |
| 236 |
} |
| 237 |
|
| 238 |
FREE(dri_sw_dt); |
| 239 |
} |
| 240 |
|
| 241 |
static void * |
| 242 |
dri_sw_displaytarget_map(struct sw_winsys *ws, |
| 243 |
struct sw_displaytarget *dt, |
| 244 |
unsigned flags) |
| 245 |
{ |
| 246 |
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt); |
| 247 |
dri_sw_dt->map_flags = flags; |
| 248 |
if (dri_sw_dt->unbacked) |
| 249 |
return dri_sw_dt->mapped; |
| 250 |
#if DETECT_OS_POSIX |
| 251 |
if (dri_sw_dt->fd > -1) { |
| 252 |
bool success = false; |
| 253 |
if (!success) { |
| 254 |
/* if this fails, it's a dmabuf that wasn't exported by us, |
| 255 |
* so it doesn't have the header that we're looking for |
| 256 |
*/ |
| 257 |
off_t size = lseek(dri_sw_dt->fd, 0, SEEK_END); |
| 258 |
lseek(dri_sw_dt->fd, 0, SEEK_SET); |
| 259 |
if (size < 1) { |
| 260 |
fprintf(stderr, "dmabuf import failed: fd has no data\n"); |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
unsigned prot = 0; |
| 264 |
if (flags & PIPE_MAP_READ) |
| 265 |
prot |= PROT_READ; |
| 266 |
if (flags & PIPE_MAP_WRITE) |
| 267 |
prot |= PROT_WRITE; |
| 268 |
dri_sw_dt->size = size; |
| 269 |
dri_sw_dt->data = mmap(NULL, dri_sw_dt->size, prot, MAP_SHARED, dri_sw_dt->fd, 0); |
| 270 |
if (dri_sw_dt->data == MAP_FAILED) { |
| 271 |
dri_sw_dt->data = NULL; |
| 272 |
fprintf(stderr, "dmabuf import failed to mmap: %s\n", strerror(errno)); |
| 273 |
} else |
| 274 |
dri_sw_dt->mapped = ((uint8_t*)dri_sw_dt->data) + dri_sw_dt->offset; |
| 275 |
} else |
| 276 |
dri_sw_dt->mapped = ((uint8_t*)dri_sw_dt->data) + dri_sw_dt->offset; |
| 277 |
} else |
| 278 |
#endif |
| 279 |
if (dri_sw_dt->front_private && (flags & PIPE_MAP_READ)) { |
| 280 |
struct dri_sw_winsys *dri_sw_ws = dri_sw_winsys(ws); |
| 281 |
dri_sw_ws->lf->get_image((void *)dri_sw_dt->front_private, 0, 0, dri_sw_dt->width, dri_sw_dt->height, dri_sw_dt->stride, dri_sw_dt->data); |
| 282 |
dri_sw_dt->mapped = dri_sw_dt->data; |
| 283 |
} else { |
| 284 |
dri_sw_dt->mapped = dri_sw_dt->data; |
| 285 |
} |
| 286 |
return dri_sw_dt->mapped; |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
dri_sw_displaytarget_unmap(struct sw_winsys *ws, |
| 291 |
struct sw_displaytarget *dt) |
| 292 |
{ |
| 293 |
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt); |
| 294 |
|
| 295 |
if (dri_sw_dt->unbacked) { |
| 296 |
dri_sw_dt->map_flags = 0; |
| 297 |
return; |
| 298 |
} |
| 299 |
#if DETECT_OS_POSIX |
| 300 |
if (dri_sw_dt->fd > -1) { |
| 301 |
munmap(dri_sw_dt->data, dri_sw_dt->size); |
| 302 |
dri_sw_dt->data = NULL; |
| 303 |
} else |
| 304 |
#endif |
| 305 |
if (dri_sw_dt->front_private && (dri_sw_dt->map_flags & PIPE_MAP_WRITE)) { |
| 306 |
struct dri_sw_winsys *dri_sw_ws = dri_sw_winsys(ws); |
| 307 |
dri_sw_ws->lf->put_image2((void *)dri_sw_dt->front_private, dri_sw_dt->data, 0, 0, dri_sw_dt->width, dri_sw_dt->height, dri_sw_dt->stride); |
| 308 |
} |
| 309 |
dri_sw_dt->map_flags = 0; |
| 310 |
dri_sw_dt->mapped = NULL; |
| 311 |
} |
| 312 |
|
| 313 |
static struct sw_displaytarget * |
| 314 |
dri_sw_displaytarget_from_handle(struct sw_winsys *winsys, |
| 315 |
const struct pipe_resource *templ, |
| 316 |
struct winsys_handle *whandle, |
| 317 |
unsigned *stride) |
| 318 |
{ |
| 319 |
#if DETECT_OS_POSIX |
| 320 |
int fd = os_dupfd_cloexec(whandle->handle); |
| 321 |
struct sw_displaytarget *sw = dri_sw_displaytarget_create(winsys, templ->usage, templ->format, templ->width0, templ->height0, 64, NULL, stride); |
| 322 |
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(sw); |
| 323 |
dri_sw_dt->fd = fd; |
| 324 |
dri_sw_dt->offset = whandle->offset; |
| 325 |
return sw; |
| 326 |
#else |
| 327 |
assert(0); |
| 328 |
return NULL; |
| 329 |
#endif |
| 330 |
} |
| 331 |
|
| 332 |
static bool |
| 333 |
dri_sw_displaytarget_get_handle(struct sw_winsys *winsys, |
| 334 |
struct sw_displaytarget *dt, |
| 335 |
struct winsys_handle *whandle) |
| 336 |
{ |
| 337 |
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt); |
| 338 |
|
| 339 |
if (whandle->type == WINSYS_HANDLE_TYPE_SHMID) { |
| 340 |
if (dri_sw_dt->shmid < 0) |
| 341 |
return false; |
| 342 |
whandle->handle = dri_sw_dt->shmid; |
| 343 |
return true; |
| 344 |
} |
| 345 |
|
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
static void |
| 350 |
dri_sw_displaytarget_display(struct sw_winsys *ws, |
| 351 |
struct sw_displaytarget *dt, |
| 352 |
void *context_private, |
| 353 |
unsigned nboxes, |
| 354 |
struct pipe_box *box) |
| 355 |
{ |
| 356 |
struct dri_sw_winsys *dri_sw_ws = dri_sw_winsys(ws); |
| 357 |
struct dri_sw_displaytarget *dri_sw_dt = dri_sw_displaytarget(dt); |
| 358 |
struct dri_drawable *dri_drawable = (struct dri_drawable *)context_private; |
| 359 |
unsigned width, height, x = 0, y = 0; |
| 360 |
unsigned blsize = util_format_get_blocksize(dri_sw_dt->format); |
| 361 |
bool is_shm = dri_sw_dt->shmid != -1; |
| 362 |
/* Set the width to 'stride / cpp'. |
| 363 |
* |
| 364 |
* PutImage correctly clips to the width of the dst drawable. |
| 365 |
*/ |
| 366 |
if (!nboxes) { |
| 367 |
width = dri_sw_dt->stride / blsize; |
| 368 |
height = dri_sw_dt->height; |
| 369 |
if (is_shm) |
| 370 |
dri_sw_ws->lf->put_image_shm(dri_drawable, dri_sw_dt->shmid, dri_sw_dt->data, 0, 0, |
| 371 |
0, 0, width, height, dri_sw_dt->stride); |
| 372 |
else |
| 373 |
dri_sw_ws->lf->put_image(dri_drawable, dri_sw_dt->data, width, height); |
| 374 |
return; |
| 375 |
} |
| 376 |
for (unsigned i = 0; i < nboxes; i++) { |
| 377 |
unsigned offset = dri_sw_dt->stride * box[i].y; |
| 378 |
unsigned offset_x = box[i].x * blsize; |
| 379 |
char *data = dri_sw_dt->data + offset; |
| 380 |
x = box[i].x; |
| 381 |
y = box[i].y; |
| 382 |
width = box[i].width; |
| 383 |
height = box[i].height; |
| 384 |
if (is_shm) { |
| 385 |
/* don't add x offset for shm, the put_image_shm will deal with it */ |
| 386 |
dri_sw_ws->lf->put_image_shm(dri_drawable, dri_sw_dt->shmid, dri_sw_dt->data, offset, offset_x, |
| 387 |
x, y, width, height, dri_sw_dt->stride); |
| 388 |
} else { |
| 389 |
data += offset_x; |
| 390 |
dri_sw_ws->lf->put_image2(dri_drawable, data, |
| 391 |
x, y, width, height, dri_sw_dt->stride); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
static void |
| 397 |
dri_destroy_sw_winsys(struct sw_winsys *winsys) |
| 398 |
{ |
| 399 |
FREE(winsys); |
| 400 |
} |
| 401 |
|
| 402 |
struct sw_winsys * |
| 403 |
dri_create_sw_winsys(const struct drisw_loader_funcs *lf) |
| 404 |
{ |
| 405 |
struct dri_sw_winsys *ws; |
| 406 |
|
| 407 |
ws = CALLOC_STRUCT(dri_sw_winsys); |
| 408 |
if (!ws) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
ws->lf = lf; |
| 412 |
ws->base.destroy = dri_destroy_sw_winsys; |
| 413 |
|
| 414 |
ws->base.is_displaytarget_format_supported = dri_sw_is_displaytarget_format_supported; |
| 415 |
|
| 416 |
/* screen texture functions */ |
| 417 |
ws->base.displaytarget_create = dri_sw_displaytarget_create; |
| 418 |
ws->base.displaytarget_create_mapped = dri_sw_displaytarget_create_mapped; |
| 419 |
ws->base.displaytarget_destroy = dri_sw_displaytarget_destroy; |
| 420 |
ws->base.displaytarget_from_handle = dri_sw_displaytarget_from_handle; |
| 421 |
ws->base.displaytarget_get_handle = dri_sw_displaytarget_get_handle; |
| 422 |
|
| 423 |
/* texture functions */ |
| 424 |
ws->base.displaytarget_map = dri_sw_displaytarget_map; |
| 425 |
ws->base.displaytarget_unmap = dri_sw_displaytarget_unmap; |
| 426 |
|
| 427 |
ws->base.displaytarget_display = dri_sw_displaytarget_display; |
| 428 |
|
| 429 |
return &ws->base; |
| 430 |
} |
| 431 |
|
| 432 |
/* vim: set sw=3 ts=8 sts=3 expandtab: */ |