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