Merge branch 'xa_branch'
[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 "egllog.h"
34
35
36 #ifdef EGL_KHR_reusable_sync
37
38
39 /**
40 * Parse the list of sync attributes and return the proper error code.
41 */
42 static EGLint
43 _eglParseSyncAttribList(_EGLSync *sync, const EGLint *attrib_list)
44 {
45 EGLint i, err = EGL_SUCCESS;
46
47 if (!attrib_list)
48 return EGL_SUCCESS;
49
50 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
51 EGLint attr = attrib_list[i++];
52 EGLint val = attrib_list[i];
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 break;
64 }
65 }
66
67 return err;
68 }
69
70
71 EGLBoolean
72 _eglInitSync(_EGLSync *sync, _EGLDisplay *dpy, EGLenum type,
73 const EGLint *attrib_list)
74 {
75 EGLint err;
76
77 if (!(type == EGL_SYNC_REUSABLE_KHR && dpy->Extensions.KHR_reusable_sync) &&
78 !(type == EGL_SYNC_FENCE_KHR && dpy->Extensions.KHR_fence_sync))
79 return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
80
81 _eglInitResource(&sync->Resource, sizeof(*sync), dpy);
82 sync->Type = type;
83 sync->SyncStatus = EGL_UNSIGNALED_KHR;
84 sync->SyncCondition = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
85
86 err = _eglParseSyncAttribList(sync, attrib_list);
87 if (err != EGL_SUCCESS)
88 return _eglError(err, "eglCreateSyncKHR");
89
90 return EGL_TRUE;
91 }
92
93
94 EGLBoolean
95 _eglGetSyncAttribKHR(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
96 EGLint attribute, EGLint *value)
97 {
98 if (!value)
99 return _eglError(EGL_BAD_PARAMETER, "eglGetConfigs");
100
101 switch (attribute) {
102 case EGL_SYNC_TYPE_KHR:
103 *value = sync->Type;
104 break;
105 case EGL_SYNC_STATUS_KHR:
106 *value = sync->SyncStatus;
107 break;
108 case EGL_SYNC_CONDITION_KHR:
109 if (sync->Type != EGL_SYNC_FENCE_KHR)
110 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
111 *value = sync->SyncCondition;
112 break;
113 default:
114 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
115 break;
116 }
117
118 return EGL_TRUE;
119 }
120
121
122 #endif /* EGL_KHR_reusable_sync */