1 /**************************************************************************
3 * Copyright 2011 Marek Olšák <maraeo@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Marek Olšák <maraeo@gmail.com>
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_memory.h"
38 #include "st_context.h"
39 #include "st_cb_syncobj.h"
41 struct st_sync_object
{
42 struct gl_sync_object b
;
44 struct pipe_fence_handle
*fence
;
45 simple_mtx_t mutex
; /**< protects "fence" */
49 static struct gl_sync_object
*st_new_sync_object(struct gl_context
*ctx
)
51 struct st_sync_object
*so
= CALLOC_STRUCT(st_sync_object
);
53 simple_mtx_init(&so
->mutex
, mtx_plain
);
57 static void st_delete_sync_object(struct gl_context
*ctx
,
58 struct gl_sync_object
*obj
)
60 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
61 struct st_sync_object
*so
= (struct st_sync_object
*)obj
;
63 screen
->fence_reference(screen
, &so
->fence
, NULL
);
64 simple_mtx_destroy(&so
->mutex
);
69 static void st_fence_sync(struct gl_context
*ctx
, struct gl_sync_object
*obj
,
70 GLenum condition
, GLbitfield flags
)
72 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
73 struct st_sync_object
*so
= (struct st_sync_object
*)obj
;
75 assert(condition
== GL_SYNC_GPU_COMMANDS_COMPLETE
&& flags
== 0);
76 assert(so
->fence
== NULL
);
78 /* Deferred flush are only allowed when there's a single context. See issue 1430 */
79 pipe
->flush(pipe
, &so
->fence
, ctx
->Shared
->RefCount
== 1 ? PIPE_FLUSH_DEFERRED
: 0);
82 static void st_client_wait_sync(struct gl_context
*ctx
,
83 struct gl_sync_object
*obj
,
84 GLbitfield flags
, GLuint64 timeout
)
86 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
87 struct pipe_screen
*screen
= pipe
->screen
;
88 struct st_sync_object
*so
= (struct st_sync_object
*)obj
;
89 struct pipe_fence_handle
*fence
= NULL
;
91 /* If the fence doesn't exist, assume it's signalled. */
92 simple_mtx_lock(&so
->mutex
);
94 simple_mtx_unlock(&so
->mutex
);
95 so
->b
.StatusFlag
= GL_TRUE
;
99 /* We need a local copy of the fence pointer, so that we can call
100 * fence_finish unlocked.
102 screen
->fence_reference(screen
, &fence
, so
->fence
);
103 simple_mtx_unlock(&so
->mutex
);
105 /* Section 4.1.2 of OpenGL 4.5 (Compatibility Profile) says:
106 * [...] if ClientWaitSync is called and all of the following are true:
107 * - the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
108 * - sync is unsignaled when ClientWaitSync is called,
109 * - and the calls to ClientWaitSync and FenceSync were issued from
111 * then the GL will behave as if the equivalent of Flush were inserted
112 * immediately after the creation of sync.
114 * Assume GL_SYNC_FLUSH_COMMANDS_BIT is always set, because applications
117 if (screen
->fence_finish(screen
, pipe
, fence
, timeout
)) {
118 simple_mtx_lock(&so
->mutex
);
119 screen
->fence_reference(screen
, &so
->fence
, NULL
);
120 simple_mtx_unlock(&so
->mutex
);
121 so
->b
.StatusFlag
= GL_TRUE
;
123 screen
->fence_reference(screen
, &fence
, NULL
);
126 static void st_check_sync(struct gl_context
*ctx
, struct gl_sync_object
*obj
)
128 st_client_wait_sync(ctx
, obj
, 0, 0);
131 static void st_server_wait_sync(struct gl_context
*ctx
,
132 struct gl_sync_object
*obj
,
133 GLbitfield flags
, GLuint64 timeout
)
135 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
136 struct pipe_screen
*screen
= pipe
->screen
;
137 struct st_sync_object
*so
= (struct st_sync_object
*)obj
;
138 struct pipe_fence_handle
*fence
= NULL
;
140 /* Nothing needs to be done here if the driver does not support async
142 if (!pipe
->fence_server_sync
)
145 /* If the fence doesn't exist, assume it's signalled. */
146 simple_mtx_lock(&so
->mutex
);
148 simple_mtx_unlock(&so
->mutex
);
149 so
->b
.StatusFlag
= GL_TRUE
;
153 /* We need a local copy of the fence pointer. */
154 screen
->fence_reference(screen
, &fence
, so
->fence
);
155 simple_mtx_unlock(&so
->mutex
);
157 pipe
->fence_server_sync(pipe
, fence
);
158 screen
->fence_reference(screen
, &fence
, NULL
);
161 void st_init_syncobj_functions(struct dd_function_table
*functions
)
163 functions
->NewSyncObject
= st_new_sync_object
;
164 functions
->FenceSync
= st_fence_sync
;
165 functions
->DeleteSyncObject
= st_delete_sync_object
;
166 functions
->CheckSync
= st_check_sync
;
167 functions
->ClientWaitSync
= st_client_wait_sync
;
168 functions
->ServerWaitSync
= st_server_wait_sync
;