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 #include "main/imports.h"
26 #include "main/mtypes.h"
27
28 #include "main/externalobjects.h"
29
30 #include "st_context.h"
31 #include "st_cb_memoryobjects.h"
32
33 #include "state_tracker/drm_driver.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_screen.h"
36
37 static struct gl_memory_object *
st_memoryobj_alloc(struct gl_context * ctx,GLuint name)38 st_memoryobj_alloc(struct gl_context *ctx, GLuint name)
39 {
40 struct st_memory_object *st_obj = ST_CALLOC_STRUCT(st_memory_object);
41 if (!st_obj)
42 return NULL;
43
44 _mesa_initialize_memory_object(ctx, &st_obj->Base, name);
45 return &st_obj->Base;
46 }
47
48 static void
st_memoryobj_free(struct gl_context * ctx,struct gl_memory_object * obj)49 st_memoryobj_free(struct gl_context *ctx,
50 struct gl_memory_object *obj)
51 {
52 _mesa_delete_memory_object(ctx, obj);
53 }
54
55
56 static void
st_import_memoryobj_fd(struct gl_context * ctx,struct gl_memory_object * obj,GLuint64 size,int fd)57 st_import_memoryobj_fd(struct gl_context *ctx,
58 struct gl_memory_object *obj,
59 GLuint64 size,
60 int fd)
61 {
62 struct st_memory_object *st_obj = st_memory_object(obj);
63 struct st_context *st = st_context(ctx);
64 struct pipe_context *pipe = st->pipe;
65 struct pipe_screen *screen = pipe->screen;
66 struct winsys_handle whandle;
67
68 whandle.type = DRM_API_HANDLE_TYPE_FD;
69 whandle.handle = fd;
70 whandle.offset = 0;
71 whandle.layer = 0;
72 whandle.stride = 0;
73
74 st_obj->memory = screen->memobj_create_from_handle(screen,
75 &whandle,
76 obj->Dedicated);
77
78 #if !defined(_WIN32)
79 /* We own fd, but we no longer need it. So get rid of it */
80 close(fd);
81 #endif
82 }
83
84 void
st_init_memoryobject_functions(struct dd_function_table * functions)85 st_init_memoryobject_functions(struct dd_function_table *functions)
86 {
87 functions->NewMemoryObject = st_memoryobj_alloc;
88 functions->DeleteMemoryObject = st_memoryobj_free;
89 functions->ImportMemoryObjectFd = st_import_memoryobj_fd;
90 }
91