aboutsummaryrefslogtreecommitdiffstats
path: root/src/egl/main/eglsync.c
blob: 8b8ab16b0d268ec3d7d0c0f3d3dae2009b2be663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**************************************************************************
 *
 * Copyright 2010 LunarG, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/


#include <string.h>

#include "eglsync.h"
#include "eglcurrent.h"
#include "egldriver.h"
#include "egllog.h"


/**
 * Parse the list of sync attributes and return the proper error code.
 */
static EGLint
_eglParseSyncAttribList(_EGLSync *sync, const EGLint *attrib_list)
{
   EGLint i, err = EGL_SUCCESS;

   if (!attrib_list)
      return EGL_SUCCESS;

   for (i = 0; attrib_list[i] != EGL_NONE; i++) {
      EGLint attr = attrib_list[i++];
      EGLint val = attrib_list[i];

      switch (attr) {
      default:
         (void) val;
         err = EGL_BAD_ATTRIBUTE;
         break;
      }

      if (err != EGL_SUCCESS) {
         _eglLog(_EGL_DEBUG, "bad sync attribute 0x%04x", attr);
         break;
      }
   }

   return err;
}


static EGLint
_eglParseSyncAttribList64(_EGLSync *sync, const EGLAttribKHR *attrib_list)
{
   EGLint i, err = EGL_SUCCESS;

   if (!attrib_list)
      return EGL_SUCCESS;

   for (i = 0; attrib_list[i] != EGL_NONE; i++) {
      EGLint attr = attrib_list[i++];
      EGLint val = attrib_list[i];

      switch (attr) {
      case EGL_CL_EVENT_HANDLE_KHR:
         if (sync->Type == EGL_SYNC_CL_EVENT_KHR) {
            sync->CLEvent = val;
            break;
         }
         /* fall through */
      default:
         (void) val;
         err = EGL_BAD_ATTRIBUTE;
         break;
      }

      if (err != EGL_SUCCESS) {
         _eglLog(_EGL_DEBUG, "bad sync attribute 0x%04x", attr);
         break;
      }
   }

   return err;
}


EGLBoolean
_eglInitSync(_EGLSync *sync, _EGLDisplay *dpy, EGLenum type,
             const EGLint *attrib_list, const EGLAttribKHR *attrib_list64)
{
   EGLint err;

   if (!(type == EGL_SYNC_REUSABLE_KHR && dpy->Extensions.KHR_reusable_sync) &&
       !(type == EGL_SYNC_FENCE_KHR && dpy->Extensions.KHR_fence_sync) &&
       !(type == EGL_SYNC_CL_EVENT_KHR && dpy->Extensions.KHR_cl_event2 &&
         attrib_list64))
      return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");

   _eglInitResource(&sync->Resource, sizeof(*sync), dpy);
   sync->Type = type;
   sync->SyncStatus = EGL_UNSIGNALED_KHR;

   switch (type) {
   case EGL_SYNC_CL_EVENT_KHR:
      sync->SyncCondition = EGL_SYNC_CL_EVENT_COMPLETE_KHR;
      break;
   default:
      sync->SyncCondition = EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR;
   }

   if (attrib_list64)
      err = _eglParseSyncAttribList64(sync, attrib_list64);
   else
      err = _eglParseSyncAttribList(sync, attrib_list);

   if (err != EGL_SUCCESS)
      return _eglError(err, "eglCreateSyncKHR");

   if (type == EGL_SYNC_CL_EVENT_KHR && !sync->CLEvent)
      return _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");

   return EGL_TRUE;
}


EGLBoolean
_eglGetSyncAttribKHR(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
                     EGLint attribute, EGLint *value)
{
   if (!value)
      return _eglError(EGL_BAD_PARAMETER, "eglGetSyncAttribKHR");

   switch (attribute) {
   case EGL_SYNC_TYPE_KHR:
      *value = sync->Type;
      break;
   case EGL_SYNC_STATUS_KHR:
      /* update the sync status */
      if (sync->SyncStatus != EGL_SIGNALED_KHR &&
          (sync->Type == EGL_SYNC_FENCE_KHR ||
           sync->Type == EGL_SYNC_CL_EVENT_KHR))
         drv->API.ClientWaitSyncKHR(drv, dpy, sync, 0, 0);

      *value = sync->SyncStatus;
      break;
   case EGL_SYNC_CONDITION_KHR:
      if (sync->Type != EGL_SYNC_FENCE_KHR &&
          sync->Type != EGL_SYNC_CL_EVENT_KHR)
         return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
      *value = sync->SyncCondition;
      break;
   default:
      return _eglError(EGL_BAD_ATTRIBUTE, "eglGetSyncAttribKHR");
      break;
   }

   return EGL_TRUE;
}