egl: Cleanup control flow in _eglParseSyncAttribList
[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 <inttypes.h>
30 #include <string.h>
31
32 #include "eglsync.h"
33 #include "eglcurrent.h"
34 #include "egldriver.h"
35 #include "egllog.h"
36
37
38 /**
39 * Parse the list of sync attributes and return the proper error code.
40 */
41 static EGLint
42 _eglParseSyncAttribList(_EGLSync *sync, const EGLint *attrib_list)
43 {
44 EGLint i;
45
46 if (!attrib_list)
47 return EGL_SUCCESS;
48
49 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
50 EGLint attr = attrib_list[i++];
51 EGLint val = attrib_list[i];
52 EGLint err = EGL_SUCCESS;
53
54 switch (attr) {
55 default:
56 (void) val;
57 err = EGL_BAD_ATTRIBUTE;
58 break;
59 }
60
61 if (err != EGL_SUCCESS) {
62 _eglLog(_EGL_DEBUG, "bad sync attribute 0x%04x", attr);
63 return err;
64 }
65 }
66
67 return EGL_SUCCESS;
68 }
69
70
71 static EGLint
72 _eglParseSyncAttribList64(_EGLSync *sync, const EGLAttrib *attrib_list)
73 {
74 EGLint i;
75
76 if (!attrib_list)
77 return EGL_SUCCESS;
78
79 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
80 EGLAttrib attr = attrib_list[i++];
81 EGLAttrib val = attrib_list[i];
82 EGLint err = EGL_SUCCESS;
83
84 switch (attr) {
85 case EGL_CL_EVENT_HANDLE_KHR:
86 if (sync->Type == EGL_SYNC_CL_EVENT_KHR) {
87 sync->CLEvent = val;
88 break;
89 }
90 /* fall through */
91 default:
92 (void) val;
93 err = EGL_BAD_ATTRIBUTE;
94 break;
95 }
96
97 if (err != EGL_SUCCESS) {
98 _eglLog(_EGL_DEBUG, "bad sync attribute 0x%" PRIxPTR, attr);
99 return err;
100 }
101 }
102
103 return EGL_SUCCESS;
104 }
105
106
107 EGLBoolean
108 _eglInitSync(_EGLSync *sync, _EGLDisplay *dpy, EGLenum type,
109 const EGLint *attrib_list, const EGLAttrib *attrib_list64)
110 {
111 EGLint err;
112
113 if (!(type == EGL_SYNC_REUSABLE_KHR && dpy->Extensions.KHR_reusable_sync) &&
114 !(type == EGL_SYNC_FENCE_KHR && dpy->Extensions.KHR_fence_sync) &&
115 !(type == EGL_SYNC_CL_EVENT_KHR && dpy->Extensions.KHR_cl_event2 &&
116 attrib_list64))
117 return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
118
119 _eglInitResource(&sync->Resource, sizeof(*sync), dpy);
120 sync->Type = type;
121 sync->SyncStatus = EGL_UNSIGNALED_KHR;
122
123 switch (type) {
124 case EGL_SYNC_CL_EVENT_KHR:
125 sync->SyncCondition = EGL_SYNC_CL_EVENT_COMPLETE_KHR;
126 break;
127 default:
128 sync->SyncCondition = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
129 }
130
131 if (attrib_list64)
132 err = _eglParseSyncAttribList64(sync, attrib_list64);
133 else
134 err = _eglParseSyncAttribList(sync, attrib_list);
135
136 if (err != EGL_SUCCESS)
137 return _eglError(err, "eglCreateSyncKHR");
138
139 if (type == EGL_SYNC_CL_EVENT_KHR && !sync->CLEvent)
140 return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
141
142 return EGL_TRUE;
143 }
144
145
146 EGLBoolean
147 _eglGetSyncAttrib(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
148 EGLint attribute, EGLAttrib *value)
149 {
150 switch (attribute) {
151 case EGL_SYNC_TYPE_KHR:
152 *value = sync->Type;
153 break;
154 case EGL_SYNC_STATUS_KHR:
155 /* update the sync status */
156 if (sync->SyncStatus != EGL_SIGNALED_KHR &&
157 (sync->Type == EGL_SYNC_FENCE_KHR ||
158 sync->Type == EGL_SYNC_CL_EVENT_KHR ||
159 sync->Type == EGL_SYNC_REUSABLE_KHR))
160 drv->API.ClientWaitSyncKHR(drv, dpy, sync, 0, 0);
161
162 *value = sync->SyncStatus;
163 break;
164 case EGL_SYNC_CONDITION_KHR:
165 if (sync->Type != EGL_SYNC_FENCE_KHR &&
166 sync->Type != EGL_SYNC_CL_EVENT_KHR)
167 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
168 *value = sync->SyncCondition;
169 break;
170 default:
171 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
172 break;
173 }
174
175 return EGL_TRUE;
176 }