Merge remote-tracking branch 'origin/master' into vulkan
[mesa.git] / src / egl / main / eglsync.c
1 /**************************************************************************
2 *
3 * Copyright 2010 LunarG, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include <string.h>
30
31 #include "eglsync.h"
32 #include "eglcurrent.h"
33 #include "egldriver.h"
34 #include "egllog.h"
35
36
37 /**
38 * Parse the list of sync attributes and return the proper error code.
39 */
40 static EGLint
41 _eglParseSyncAttribList(_EGLSync *sync, const EGLint *attrib_list)
42 {
43 EGLint i, err = EGL_SUCCESS;
44
45 if (!attrib_list)
46 return EGL_SUCCESS;
47
48 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
49 EGLint attr = attrib_list[i++];
50 EGLint val = attrib_list[i];
51
52 switch (attr) {
53 default:
54 (void) val;
55 err = EGL_BAD_ATTRIBUTE;
56 break;
57 }
58
59 if (err != EGL_SUCCESS) {
60 _eglLog(_EGL_DEBUG, "bad sync attribute 0x%04x", attr);
61 break;
62 }
63 }
64
65 return err;
66 }
67
68
69 static EGLint
70 _eglParseSyncAttribList64(_EGLSync *sync, const EGLAttrib *attrib_list)
71 {
72 EGLint i, err = EGL_SUCCESS;
73
74 if (!attrib_list)
75 return EGL_SUCCESS;
76
77 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
78 EGLint attr = attrib_list[i++];
79 EGLint val = attrib_list[i];
80
81 switch (attr) {
82 case EGL_CL_EVENT_HANDLE_KHR:
83 if (sync->Type == EGL_SYNC_CL_EVENT_KHR) {
84 sync->CLEvent = val;
85 break;
86 }
87 /* fall through */
88 default:
89 (void) val;
90 err = EGL_BAD_ATTRIBUTE;
91 break;
92 }
93
94 if (err != EGL_SUCCESS) {
95 _eglLog(_EGL_DEBUG, "bad sync attribute 0x%04x", attr);
96 break;
97 }
98 }
99
100 return err;
101 }
102
103
104 EGLBoolean
105 _eglInitSync(_EGLSync *sync, _EGLDisplay *dpy, EGLenum type,
106 const EGLint *attrib_list, const EGLAttrib *attrib_list64)
107 {
108 EGLint err;
109
110 if (!(type == EGL_SYNC_REUSABLE_KHR && dpy->Extensions.KHR_reusable_sync) &&
111 !(type == EGL_SYNC_FENCE_KHR && dpy->Extensions.KHR_fence_sync) &&
112 !(type == EGL_SYNC_CL_EVENT_KHR && dpy->Extensions.KHR_cl_event2 &&
113 attrib_list64))
114 return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
115
116 _eglInitResource(&sync->Resource, sizeof(*sync), dpy);
117 sync->Type = type;
118 sync->SyncStatus = EGL_UNSIGNALED_KHR;
119
120 switch (type) {
121 case EGL_SYNC_CL_EVENT_KHR:
122 sync->SyncCondition = EGL_SYNC_CL_EVENT_COMPLETE_KHR;
123 break;
124 default:
125 sync->SyncCondition = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
126 }
127
128 if (attrib_list64)
129 err = _eglParseSyncAttribList64(sync, attrib_list64);
130 else
131 err = _eglParseSyncAttribList(sync, attrib_list);
132
133 if (err != EGL_SUCCESS)
134 return _eglError(err, "eglCreateSyncKHR");
135
136 if (type == EGL_SYNC_CL_EVENT_KHR && !sync->CLEvent)
137 return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
138
139 return EGL_TRUE;
140 }
141
142
143 EGLBoolean
144 _eglGetSyncAttrib(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
145 EGLint attribute, EGLAttrib *value)
146 {
147 switch (attribute) {
148 case EGL_SYNC_TYPE_KHR:
149 *value = sync->Type;
150 break;
151 case EGL_SYNC_STATUS_KHR:
152 /* update the sync status */
153 if (sync->SyncStatus != EGL_SIGNALED_KHR &&
154 (sync->Type == EGL_SYNC_FENCE_KHR ||
155 sync->Type == EGL_SYNC_CL_EVENT_KHR))
156 drv->API.ClientWaitSyncKHR(drv, dpy, sync, 0, 0);
157
158 *value = sync->SyncStatus;
159 break;
160 case EGL_SYNC_CONDITION_KHR:
161 if (sync->Type != EGL_SYNC_FENCE_KHR &&
162 sync->Type != EGL_SYNC_CL_EVENT_KHR)
163 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
164 *value = sync->SyncCondition;
165 break;
166 default:
167 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
168 break;
169 }
170
171 return EGL_TRUE;
172 }