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.
24 #include "main/imports.h"
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"
32 #include "st_cb_bitmap.h"
33 #include "st_cb_bufferobjects.h"
34 #include "st_cb_semaphoreobjects.h"
36 #include "state_tracker/drm_driver.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
40 static struct gl_semaphore_object
*
41 st_semaphoreobj_alloc(struct gl_context
*ctx
, GLuint name
)
43 struct st_semaphore_object
*st_obj
= ST_CALLOC_STRUCT(st_semaphore_object
);
47 _mesa_initialize_semaphore_object(ctx
, &st_obj
->Base
, name
);
52 st_semaphoreobj_free(struct gl_context
*ctx
,
53 struct gl_semaphore_object
*semObj
)
55 _mesa_delete_semaphore_object(ctx
, semObj
);
60 st_import_semaphoreobj_fd(struct gl_context
*ctx
,
61 struct gl_semaphore_object
*semObj
,
64 struct st_semaphore_object
*st_obj
= st_semaphore_object(semObj
);
65 struct st_context
*st
= st_context(ctx
);
66 struct pipe_context
*pipe
= st
->pipe
;
68 pipe
->create_fence_fd(pipe
, &st_obj
->fence
, fd
, PIPE_FD_TYPE_SYNCOBJ
);
71 /* We own fd, but we no longer need it. So get rid of it */
77 st_server_wait_semaphore(struct gl_context
*ctx
,
78 struct gl_semaphore_object
*semObj
,
79 GLuint numBufferBarriers
,
80 struct gl_buffer_object
**bufObjs
,
81 GLuint numTextureBarriers
,
82 struct gl_texture_object
**texObjs
,
83 const GLenum
*srcLayouts
)
85 struct st_semaphore_object
*st_obj
= st_semaphore_object(semObj
);
86 struct st_context
*st
= st_context(ctx
);
87 struct pipe_context
*pipe
= st
->pipe
;
88 struct st_buffer_object
*bufObj
;
89 struct st_texture_object
*texObj
;
91 /* The driver is allowed to flush during fence_server_sync, be prepared */
92 st_flush_bitmap_cache(st
);
93 pipe
->fence_server_sync(pipe
, st_obj
->fence
);
96 * According to the EXT_external_objects spec, the memory operations must
97 * follow the wait. This is to make sure the flush is executed after the
98 * other party is done modifying the memory.
100 * Relevant excerpt from section "4.2.3 Waiting for Semaphores":
102 * Following completion of the semaphore wait operation, memory will also be
103 * made visible in the specified buffer and texture objects.
106 for (unsigned i
= 0; i
< numBufferBarriers
; i
++) {
110 bufObj
= st_buffer_object(bufObjs
[i
]);
111 pipe
->flush_resource(pipe
, bufObj
->buffer
);
114 for (unsigned i
= 0; i
< numTextureBarriers
; i
++) {
118 texObj
= st_texture_object(texObjs
[i
]);
119 pipe
->flush_resource(pipe
, texObj
->pt
);
124 st_server_signal_semaphore(struct gl_context
*ctx
,
125 struct gl_semaphore_object
*semObj
,
126 GLuint numBufferBarriers
,
127 struct gl_buffer_object
**bufObjs
,
128 GLuint numTextureBarriers
,
129 struct gl_texture_object
**texObjs
,
130 const GLenum
*dstLayouts
)
132 struct st_semaphore_object
*st_obj
= st_semaphore_object(semObj
);
133 struct st_context
*st
= st_context(ctx
);
134 struct pipe_context
*pipe
= st
->pipe
;
135 struct st_buffer_object
*bufObj
;
136 struct st_texture_object
*texObj
;
138 for (unsigned i
= 0; i
< numBufferBarriers
; i
++) {
142 bufObj
= st_buffer_object(bufObjs
[i
]);
143 pipe
->flush_resource(pipe
, bufObj
->buffer
);
146 for (unsigned i
= 0; i
< numTextureBarriers
; i
++) {
150 texObj
= st_texture_object(texObjs
[i
]);
151 pipe
->flush_resource(pipe
, texObj
->pt
);
154 /* The driver is allowed to flush during fence_server_signal, be prepared */
155 st_flush_bitmap_cache(st
);
156 pipe
->fence_server_signal(pipe
, st_obj
->fence
);
160 st_init_semaphoreobject_functions(struct dd_function_table
*functions
)
162 functions
->NewSemaphoreObject
= st_semaphoreobj_alloc
;
163 functions
->DeleteSemaphoreObject
= st_semaphoreobj_free
;
164 functions
->ImportSemaphoreFd
= st_import_semaphoreobj_fd
;
165 functions
->ServerWaitSemaphoreObject
= st_server_wait_semaphore
;
166 functions
->ServerSignalSemaphoreObject
= st_server_signal_semaphore
;