2 * Copyright © 2017 Valve Corporation.
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:
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
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.
25 #include "main/mtypes.h"
26 #include "main/context.h"
28 #include "main/externalobjects.h"
30 #include "st_context.h"
31 #include "st_texture.h"
33 #include "st_cb_bitmap.h"
34 #include "st_cb_bufferobjects.h"
35 #include "st_cb_semaphoreobjects.h"
37 #include "frontend/drm_driver.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_screen.h"
41 static struct gl_semaphore_object
*
42 st_semaphoreobj_alloc(struct gl_context
*ctx
, GLuint name
)
44 struct st_semaphore_object
*st_obj
= ST_CALLOC_STRUCT(st_semaphore_object
);
48 _mesa_initialize_semaphore_object(ctx
, &st_obj
->Base
, name
);
53 st_semaphoreobj_free(struct gl_context
*ctx
,
54 struct gl_semaphore_object
*semObj
)
56 _mesa_delete_semaphore_object(ctx
, semObj
);
61 st_import_semaphoreobj_fd(struct gl_context
*ctx
,
62 struct gl_semaphore_object
*semObj
,
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
;
69 pipe
->create_fence_fd(pipe
, &st_obj
->fence
, fd
, PIPE_FD_TYPE_SYNCOBJ
);
72 /* We own fd, but we no longer need it. So get rid of it */
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
)
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
;
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
);
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.
101 * Relevant excerpt from section "4.2.3 Waiting for Semaphores":
103 * Following completion of the semaphore wait operation, memory will also be
104 * made visible in the specified buffer and texture objects.
107 for (unsigned i
= 0; i
< numBufferBarriers
; i
++) {
111 bufObj
= st_buffer_object(bufObjs
[i
]);
112 pipe
->flush_resource(pipe
, bufObj
->buffer
);
115 for (unsigned i
= 0; i
< numTextureBarriers
; i
++) {
119 texObj
= st_texture_object(texObjs
[i
]);
120 pipe
->flush_resource(pipe
, texObj
->pt
);
125 st_server_signal_semaphore(struct gl_context
*ctx
,
126 struct gl_semaphore_object
*semObj
,
127 GLuint numBufferBarriers
,
128 struct gl_buffer_object
**bufObjs
,
129 GLuint numTextureBarriers
,
130 struct gl_texture_object
**texObjs
,
131 const GLenum
*dstLayouts
)
133 struct st_semaphore_object
*st_obj
= st_semaphore_object(semObj
);
134 struct st_context
*st
= st_context(ctx
);
135 struct pipe_context
*pipe
= st
->pipe
;
136 struct st_buffer_object
*bufObj
;
137 struct st_texture_object
*texObj
;
139 for (unsigned i
= 0; i
< numBufferBarriers
; i
++) {
143 bufObj
= st_buffer_object(bufObjs
[i
]);
144 pipe
->flush_resource(pipe
, bufObj
->buffer
);
147 for (unsigned i
= 0; i
< numTextureBarriers
; i
++) {
151 texObj
= st_texture_object(texObjs
[i
]);
152 pipe
->flush_resource(pipe
, texObj
->pt
);
155 /* The driver is allowed to flush during fence_server_signal, be prepared */
156 st_flush_bitmap_cache(st
);
157 pipe
->fence_server_signal(pipe
, st_obj
->fence
);
161 st_init_semaphoreobject_functions(struct dd_function_table
*functions
)
163 functions
->NewSemaphoreObject
= st_semaphoreobj_alloc
;
164 functions
->DeleteSemaphoreObject
= st_semaphoreobj_free
;
165 functions
->ImportSemaphoreFd
= st_import_semaphoreobj_fd
;
166 functions
->ServerWaitSemaphoreObject
= st_server_wait_semaphore
;
167 functions
->ServerSignalSemaphoreObject
= st_server_signal_semaphore
;