1 /*
2 * Copyright © 2017 Valve Corporation.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24
25 #include "main/mtypes.h"
26 #include "main/context.h"
27
28 #include "main/externalobjects.h"
29
30 #include "st_context.h"
31 #include "st_texture.h"
32 #include "st_util.h"
33 #include "st_cb_bitmap.h"
34 #include "st_cb_bufferobjects.h"
35 #include "st_cb_semaphoreobjects.h"
36
37 #include "frontend/drm_driver.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_screen.h"
40
41 static struct gl_semaphore_object *
st_semaphoreobj_alloc(struct gl_context * ctx,GLuint name)42 st_semaphoreobj_alloc(struct gl_context *ctx, GLuint name)
43 {
44 struct st_semaphore_object *st_obj = ST_CALLOC_STRUCT(st_semaphore_object);
45 if (!st_obj)
46 return NULL;
47
48 _mesa_initialize_semaphore_object(ctx, &st_obj->Base, name);
49 return &st_obj->Base;
50 }
51
52 static void
st_semaphoreobj_free(struct gl_context * ctx,struct gl_semaphore_object * semObj)53 st_semaphoreobj_free(struct gl_context *ctx,
54 struct gl_semaphore_object *semObj)
55 {
56 _mesa_delete_semaphore_object(ctx, semObj);
57 }
58
59
60 static void
st_import_semaphoreobj_fd(struct gl_context * ctx,struct gl_semaphore_object * semObj,int fd)61 st_import_semaphoreobj_fd(struct gl_context *ctx,
62 struct gl_semaphore_object *semObj,
63 int fd)
64 {
65 struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
66 struct st_context *st = st_context(ctx);
67 struct pipe_context *pipe = st->pipe;
68
69 pipe->create_fence_fd(pipe, &st_obj->fence, fd, PIPE_FD_TYPE_SYNCOBJ);
70
71 #if !defined(_WIN32)
72 /* We own fd, but we no longer need it. So get rid of it */
73 close(fd);
74 #endif
75 }
76
77 static void
st_server_wait_semaphore(struct gl_context * ctx,struct gl_semaphore_object * semObj,GLuint numBufferBarriers,struct gl_buffer_object ** bufObjs,GLuint numTextureBarriers,struct gl_texture_object ** texObjs,const GLenum * srcLayouts)78 st_server_wait_semaphore(struct gl_context *ctx,
79 struct gl_semaphore_object *semObj,
80 GLuint numBufferBarriers,
81 struct gl_buffer_object **bufObjs,
82 GLuint numTextureBarriers,
83 struct gl_texture_object **texObjs,
84 const GLenum *srcLayouts)
85 {
86 struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
87 struct st_context *st = st_context(ctx);
88 struct pipe_context *pipe = st->pipe;
89 struct st_buffer_object *bufObj;
90 struct st_texture_object *texObj;
91
92 /* The driver is allowed to flush during fence_server_sync, be prepared */
93 st_flush_bitmap_cache(st);
94 pipe->fence_server_sync(pipe, st_obj->fence);
95
96 /**
97 * According to the EXT_external_objects spec, the memory operations must
98 * follow the wait. This is to make sure the flush is executed after the
99 * other party is done modifying the memory.
100 *
101 * Relevant excerpt from section "4.2.3 Waiting for Semaphores":
102 *
103 * Following completion of the semaphore wait operation, memory will also be
104 * made visible in the specified buffer and texture objects.
105 *
106 */
107 for (unsigned i = 0; i < numBufferBarriers; i++) {
108 if (!bufObjs[i])
109 continue;
110
111 bufObj = st_buffer_object(bufObjs[i]);
112 if (bufObj->buffer)
113 pipe->flush_resource(pipe, bufObj->buffer);
114 }
115
116 for (unsigned i = 0; i < numTextureBarriers; i++) {
117 if (!texObjs[i])
118 continue;
119
120 texObj = st_texture_object(texObjs[i]);
121 if (texObj->pt)
122 pipe->flush_resource(pipe, texObj->pt);
123 }
124 }
125
126 static void
st_server_signal_semaphore(struct gl_context * ctx,struct gl_semaphore_object * semObj,GLuint numBufferBarriers,struct gl_buffer_object ** bufObjs,GLuint numTextureBarriers,struct gl_texture_object ** texObjs,const GLenum * dstLayouts)127 st_server_signal_semaphore(struct gl_context *ctx,
128 struct gl_semaphore_object *semObj,
129 GLuint numBufferBarriers,
130 struct gl_buffer_object **bufObjs,
131 GLuint numTextureBarriers,
132 struct gl_texture_object **texObjs,
133 const GLenum *dstLayouts)
134 {
135 struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
136 struct st_context *st = st_context(ctx);
137 struct pipe_context *pipe = st->pipe;
138 struct st_buffer_object *bufObj;
139 struct st_texture_object *texObj;
140
141 for (unsigned i = 0; i < numBufferBarriers; i++) {
142 if (!bufObjs[i])
143 continue;
144
145 bufObj = st_buffer_object(bufObjs[i]);
146 if (bufObj->buffer)
147 pipe->flush_resource(pipe, bufObj->buffer);
148 }
149
150 for (unsigned i = 0; i < numTextureBarriers; i++) {
151 if (!texObjs[i])
152 continue;
153
154 texObj = st_texture_object(texObjs[i]);
155 if (texObj->pt)
156 pipe->flush_resource(pipe, texObj->pt);
157 }
158
159 /* The driver is allowed to flush during fence_server_signal, be prepared */
160 st_flush_bitmap_cache(st);
161 pipe->fence_server_signal(pipe, st_obj->fence);
162 }
163
164 void
st_init_semaphoreobject_functions(struct dd_function_table * functions)165 st_init_semaphoreobject_functions(struct dd_function_table *functions)
166 {
167 functions->NewSemaphoreObject = st_semaphoreobj_alloc;
168 functions->DeleteSemaphoreObject = st_semaphoreobj_free;
169 functions->ImportSemaphoreFd = st_import_semaphoreobj_fd;
170 functions->ServerWaitSemaphoreObject = st_server_wait_semaphore;
171 functions->ServerSignalSemaphoreObject = st_server_signal_semaphore;
172 }
173