mesa/st: start adding memory object support
[mesa.git] / src / mesa / state_tracker / st_cb_memoryobjects.c
1 #include "main/imports.h"
2 #include "main/mtypes.h"
3
4 #include "main/externalobjects.h"
5
6 #include "st_context.h"
7 #include "st_cb_memoryobjects.h"
8
9 #include "state_tracker/drm_driver.h"
10 #include "pipe/p_context.h"
11 #include "pipe/p_screen.h"
12
13 static struct gl_memory_object *
14 st_memoryobj_alloc(struct gl_context *ctx, GLuint name)
15 {
16 struct st_memory_object *st_obj = ST_CALLOC_STRUCT(st_memory_object);
17 if (!st_obj)
18 return NULL;
19
20 _mesa_initialize_memory_object(ctx, &st_obj->Base, name);
21 return &st_obj->Base;
22 }
23
24 static void
25 st_memoryobj_free(struct gl_context *ctx,
26 struct gl_memory_object *obj)
27 {
28 _mesa_delete_memory_object(ctx, obj);
29 }
30
31
32 static void
33 st_import_memoryobj_fd(struct gl_context *ctx,
34 struct gl_memory_object *obj,
35 GLuint64 size,
36 int fd)
37 {
38 struct st_memory_object *st_obj = st_memory_object(obj);
39 struct st_context *st = st_context(ctx);
40 struct pipe_context *pipe = st->pipe;
41 struct pipe_screen *screen = pipe->screen;
42 struct winsys_handle whandle;
43
44 whandle.type = DRM_API_HANDLE_TYPE_FD;
45 whandle.handle = fd;
46 whandle.offset = 0;
47 whandle.layer = 0;
48 whandle.stride = 0;
49
50 st_obj->memory = screen->memobj_create_from_handle(screen,
51 &whandle,
52 obj->Dedicated);
53
54 /* We own fd, but we no longer need it. So get rid of it */
55 close(fd);
56 }
57
58 void
59 st_init_memoryobject_functions(struct dd_function_table *functions)
60 {
61 functions->NewMemoryObject = st_memoryobj_alloc;
62 functions->DeleteMemoryObject = st_memoryobj_free;
63 functions->ImportMemoryObjectFd = st_import_memoryobj_fd;
64 }