mesa/st: Actually free the driver part of memory objects on destruction.
[mesa.git] / src / mesa / state_tracker / st_cb_memoryobjects.c
1 /*
2 * Copyright © 2017 Red Hat.
3 * Copyright © 2017 Valve Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "main/mtypes.h"
27
28 #include "main/externalobjects.h"
29
30 #include "st_context.h"
31 #include "st_cb_memoryobjects.h"
32 #include "st_util.h"
33
34 #include "frontend/drm_driver.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37
38 #ifdef HAVE_LIBDRM
39 #include "drm-uapi/drm_fourcc.h"
40 #endif
41
42 static struct gl_memory_object *
43 st_memoryobj_alloc(struct gl_context *ctx, GLuint name)
44 {
45 struct st_memory_object *st_obj = ST_CALLOC_STRUCT(st_memory_object);
46 if (!st_obj)
47 return NULL;
48
49 _mesa_initialize_memory_object(ctx, &st_obj->Base, name);
50 return &st_obj->Base;
51 }
52
53 static void
54 st_memoryobj_free(struct gl_context *ctx,
55 struct gl_memory_object *obj)
56 {
57 struct st_memory_object *st_obj = st_memory_object(obj);
58 struct st_context *st = st_context(ctx);
59 struct pipe_context *pipe = st->pipe;
60 struct pipe_screen *screen = pipe->screen;
61
62 screen->memobj_destroy(screen, st_obj->memory);
63 _mesa_delete_memory_object(ctx, obj);
64 }
65
66
67 static void
68 st_import_memoryobj_fd(struct gl_context *ctx,
69 struct gl_memory_object *obj,
70 GLuint64 size,
71 int fd)
72 {
73 struct st_memory_object *st_obj = st_memory_object(obj);
74 struct st_context *st = st_context(ctx);
75 struct pipe_context *pipe = st->pipe;
76 struct pipe_screen *screen = pipe->screen;
77 struct winsys_handle whandle = {
78 .type = WINSYS_HANDLE_TYPE_FD,
79 .handle = fd,
80 #ifdef HAVE_LIBDRM
81 .modifier = DRM_FORMAT_MOD_INVALID,
82 #endif
83 };
84
85 st_obj->memory = screen->memobj_create_from_handle(screen,
86 &whandle,
87 obj->Dedicated);
88
89 #if !defined(_WIN32)
90 /* We own fd, but we no longer need it. So get rid of it */
91 close(fd);
92 #endif
93 }
94
95 void
96 st_init_memoryobject_functions(struct dd_function_table *functions)
97 {
98 functions->NewMemoryObject = st_memoryobj_alloc;
99 functions->DeleteMemoryObject = st_memoryobj_free;
100 functions->ImportMemoryObjectFd = st_import_memoryobj_fd;
101 }