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 #include <inttypes.h>
29 #include <string.h>
30
31 #include "eglcurrent.h"
32 #include "egldriver.h"
33 #include "egllog.h"
34 #include "eglsync.h"
35
36 /**
37 * Parse the list of sync attributes and return the proper error code.
38 */
39 static EGLint
_eglParseSyncAttribList(_EGLSync * sync,const EGLAttrib * attrib_list)40 _eglParseSyncAttribList(_EGLSync *sync, const EGLAttrib *attrib_list)
41 {
42 EGLint i;
43
44 if (!attrib_list)
45 return EGL_SUCCESS;
46
47 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
48 EGLAttrib attr = attrib_list[i++];
49 EGLAttrib val = attrib_list[i];
50 EGLint err = EGL_SUCCESS;
51
52 switch (attr) {
53 case EGL_CL_EVENT_HANDLE_KHR:
54 if (sync->Type == EGL_SYNC_CL_EVENT_KHR) {
55 sync->CLEvent = val;
56 } else {
57 err = EGL_BAD_ATTRIBUTE;
58 }
59 break;
60 case EGL_SYNC_NATIVE_FENCE_FD_ANDROID:
61 if (sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID) {
62 /* we take ownership of the native fd, so no dup(): */
63 sync->SyncFd = val;
64 } else {
65 err = EGL_BAD_ATTRIBUTE;
66 }
67 break;
68 default:
69 err = EGL_BAD_ATTRIBUTE;
70 break;
71 }
72
73 if (err != EGL_SUCCESS) {
74 _eglLog(_EGL_DEBUG, "bad sync attribute 0x%" PRIxPTR, attr);
75 return err;
76 }
77 }
78
79 return EGL_SUCCESS;
80 }
81
82 EGLBoolean
_eglInitSync(_EGLSync * sync,_EGLDisplay * disp,EGLenum type,const EGLAttrib * attrib_list)83 _eglInitSync(_EGLSync *sync, _EGLDisplay *disp, EGLenum type,
84 const EGLAttrib *attrib_list)
85 {
86 EGLint err;
87
88 _eglInitResource(&sync->Resource, sizeof(*sync), disp);
89 sync->Type = type;
90 sync->SyncStatus = EGL_UNSIGNALED_KHR;
91 sync->SyncFd = EGL_NO_NATIVE_FENCE_FD_ANDROID;
92
93 err = _eglParseSyncAttribList(sync, attrib_list);
94
95 switch (type) {
96 case EGL_SYNC_CL_EVENT_KHR:
97 sync->SyncCondition = EGL_SYNC_CL_EVENT_COMPLETE_KHR;
98 break;
99 case EGL_SYNC_NATIVE_FENCE_ANDROID:
100 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID)
101 sync->SyncCondition = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
102 else
103 sync->SyncCondition = EGL_SYNC_NATIVE_FENCE_SIGNALED_ANDROID;
104 break;
105 default:
106 sync->SyncCondition = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
107 }
108
109 if (err != EGL_SUCCESS)
110 return _eglError(err, "eglCreateSyncKHR");
111
112 if (type == EGL_SYNC_CL_EVENT_KHR && !sync->CLEvent)
113 return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
114
115 return EGL_TRUE;
116 }
117
118 EGLBoolean
_eglGetSyncAttrib(_EGLDisplay * disp,_EGLSync * sync,EGLint attribute,EGLAttrib * value)119 _eglGetSyncAttrib(_EGLDisplay *disp, _EGLSync *sync, EGLint attribute,
120 EGLAttrib *value)
121 {
122 switch (attribute) {
123 case EGL_SYNC_TYPE_KHR:
124 *value = sync->Type;
125 break;
126 case EGL_SYNC_STATUS_KHR:
127 /* update the sync status */
128 if (sync->SyncStatus != EGL_SIGNALED_KHR &&
129 (sync->Type == EGL_SYNC_FENCE_KHR ||
130 sync->Type == EGL_SYNC_CL_EVENT_KHR ||
131 sync->Type == EGL_SYNC_REUSABLE_KHR ||
132 sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID))
133 disp->Driver->ClientWaitSyncKHR(disp, sync, 0, 0);
134
135 *value = sync->SyncStatus;
136 break;
137 case EGL_SYNC_CONDITION_KHR:
138 if (sync->Type != EGL_SYNC_FENCE_KHR &&
139 sync->Type != EGL_SYNC_CL_EVENT_KHR &&
140 sync->Type != EGL_SYNC_NATIVE_FENCE_ANDROID)
141 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
142 *value = sync->SyncCondition;
143 break;
144
145 default:
146 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
147 break;
148 }
149
150 return EGL_TRUE;
151 }
152