13dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt/*
23dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * Copyright © 2008 Intel Corporation
33dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *
43dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
53dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * copy of this software and associated documentation files (the "Software"),
63dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * to deal in the Software without restriction, including without limitation
73dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the
93dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * Software is furnished to do so, subject to the following conditions:
103dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *
113dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * The above copyright notice and this permission notice (including the next
123dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * paragraph) shall be included in all copies or substantial portions of the
133dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * Software.
143dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *
153dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
183dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
193dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
203dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
213dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * IN THE SOFTWARE.
223dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *
233dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * Authors:
243dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *    Eric Anholt <eric@anholt.net>
253dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *
263dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt */
273dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
28c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace/**
29c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace * \file
30c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace * \brief Support for GL_ARB_sync and EGL_KHR_fence_sync.
313dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt *
32c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace * GL_ARB_sync is implemented by flushing the current batchbuffer and keeping a
333dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * reference on it.  We can then check for completion or wait for completion
343dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * using the normal buffer object mechanisms.  This does mean that if an
353dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * application is using many sync objects, it will emit small batchbuffers
363dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * which may end up being a significant overhead.  In other tests of removing
373dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * gratuitous batchbuffer syncs in Mesa, it hasn't appeared to be a significant
383dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt * performance bottleneck, though.
393dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt */
403dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
413dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt#include "main/imports.h"
423dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
43a69274454b6bde265a910ca5bd3199217431f5b5Kenneth Graunke#include "brw_context.h"
443dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt#include "intel_batchbuffer.h"
453dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
46c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestruct brw_fence {
474f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   struct brw_context *brw;
48c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   /** The fence waits for completion of this batch. */
49c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   drm_intel_bo *batch_bo;
50c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
514f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   mtx_t mutex;
52c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   bool signalled;
53c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace};
54c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
559ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacestruct brw_gl_sync {
569ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct gl_sync_object gl;
57c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   struct brw_fence fence;
582516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versace};
592516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versace
60c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void
61ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versacebrw_fence_init(struct brw_context *brw, struct brw_fence *fence)
62ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace{
63ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace   fence->brw = brw;
64ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace   fence->batch_bo = NULL;
65ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace   mtx_init(&fence->mutex, mtx_plain);
66ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace}
67ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace
68ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versacestatic void
69c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacebrw_fence_finish(struct brw_fence *fence)
70c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
71c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (fence->batch_bo)
72c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      drm_intel_bo_unreference(fence->batch_bo);
73ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace
74ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace   mtx_destroy(&fence->mutex);
75c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
76c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
77c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void
78c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacebrw_fence_insert(struct brw_context *brw, struct brw_fence *fence)
79c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
80c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   assert(!fence->batch_bo);
81c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   assert(!fence->signalled);
82c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
834b35ab9bdb4e663f41ff5c9ae5bbcc650b6093f9Chris Wilson   brw_emit_mi_flush(brw);
84c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   fence->batch_bo = brw->batch.bo;
85c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   drm_intel_bo_reference(fence->batch_bo);
86c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   intel_batchbuffer_flush(brw);
87c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
88c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
89c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic bool
904f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikovbrw_fence_has_completed_locked(struct brw_fence *fence)
91c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
92c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (fence->signalled)
93c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      return true;
94c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
95c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (fence->batch_bo && !drm_intel_bo_busy(fence->batch_bo)) {
96c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      drm_intel_bo_unreference(fence->batch_bo);
97c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      fence->batch_bo = NULL;
98c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      fence->signalled = true;
99c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      return true;
100c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   }
101c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
102c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   return false;
103c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
104c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
105c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic bool
1064f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikovbrw_fence_has_completed(struct brw_fence *fence)
1074f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov{
1084f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   bool ret;
1094f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
1104f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   mtx_lock(&fence->mutex);
1114f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   ret = brw_fence_has_completed_locked(fence);
1124f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   mtx_unlock(&fence->mutex);
1134f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
1144f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   return ret;
1154f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov}
1164f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
1174f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikovstatic bool
1184f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikovbrw_fence_client_wait_locked(struct brw_context *brw, struct brw_fence *fence,
1194f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov                             uint64_t timeout)
120c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
121c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (fence->signalled)
122c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      return true;
123c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
124c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   assert(fence->batch_bo);
125c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
126c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and returns
127c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    * immediately for timeouts <= 0.  The best we can do is to clamp the
128c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    * timeout to INT64_MAX.  This limits the maximum timeout from 584 years to
129c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    * 292 years - likely not a big deal.
130c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    */
131c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (timeout > INT64_MAX)
132c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      timeout = INT64_MAX;
133c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
134c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (drm_intel_gem_bo_wait(fence->batch_bo, timeout) != 0)
135c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      return false;
136c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
137c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   fence->signalled = true;
138c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   drm_intel_bo_unreference(fence->batch_bo);
139c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   fence->batch_bo = NULL;
140c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
141c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   return true;
142c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
143c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
1444f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov/**
1454f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov * Return true if the function successfully signals or has already signalled.
1464f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov * (This matches the behavior expected from __DRI2fence::client_wait_sync).
1474f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov */
1484f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikovstatic bool
1494f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikovbrw_fence_client_wait(struct brw_context *brw, struct brw_fence *fence,
1504f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov                      uint64_t timeout)
1514f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov{
1524f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   bool ret;
1534f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
1544f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   mtx_lock(&fence->mutex);
1554f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   ret = brw_fence_client_wait_locked(brw, fence, timeout);
1564f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   mtx_unlock(&fence->mutex);
1574f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
1584f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   return ret;
1594f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov}
1604f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
161c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void
162c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacebrw_fence_server_wait(struct brw_context *brw, struct brw_fence *fence)
163c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
164c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   /* We have nothing to do for WaitSync.  Our GL command stream is sequential,
165c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    * so given that the sync object has already flushed the batchbuffer, any
166c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    * batchbuffers coming after this waitsync will naturally not occur until
167c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    * the previous one is done.
168c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace    */
169c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
170c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
1713dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholtstatic struct gl_sync_object *
1729ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_gl_new_sync(struct gl_context *ctx, GLuint id)
1733dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
1749ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct brw_gl_sync *sync;
1753dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
1762516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versace   sync = calloc(1, sizeof(*sync));
17700f3c7baeb3e8f00ce7bc8a1384ef545e648f1d9Chad Versace   if (!sync)
17800f3c7baeb3e8f00ce7bc8a1384ef545e648f1d9Chad Versace      return NULL;
1793dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
1809ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   return &sync->gl;
1813dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
1823dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
1833dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholtstatic void
1849ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_gl_delete_sync(struct gl_context *ctx, struct gl_sync_object *_sync)
1853dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
1869ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
1873dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
188c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   brw_fence_finish(&sync->fence);
1893dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt   free(sync);
1903dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
1913dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
1923dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholtstatic void
1939ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_gl_fence_sync(struct gl_context *ctx, struct gl_sync_object *_sync,
1949ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace                  GLenum condition, GLbitfield flags)
1953dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
196ca437579b3974b91a5298707c459908a628c1098Kenneth Graunke   struct brw_context *brw = brw_context(ctx);
1979ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
1983dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
199ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace   brw_fence_init(brw, &sync->fence);
200c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   brw_fence_insert(brw, &sync->fence);
2013dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
2023dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
2032516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versacestatic void
2049ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_gl_client_wait_sync(struct gl_context *ctx, struct gl_sync_object *_sync,
2059ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace                        GLbitfield flags, GLuint64 timeout)
2063dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
207c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   struct brw_context *brw = brw_context(ctx);
2089ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
2093dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
210c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (brw_fence_client_wait(brw, &sync->fence, timeout))
2119ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace      sync->gl.StatusFlag = 1;
2123dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
2133dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
2142516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versacestatic void
2159ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_gl_server_wait_sync(struct gl_context *ctx, struct gl_sync_object *_sync,
2162516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versace                          GLbitfield flags, GLuint64 timeout)
2173dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
218c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   struct brw_context *brw = brw_context(ctx);
2199ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
220c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
221c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   brw_fence_server_wait(brw, &sync->fence);
2223dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
2233dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
224c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void
2259ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_gl_check_sync(struct gl_context *ctx, struct gl_sync_object *_sync)
2263dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
2279ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
2283dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
229c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (brw_fence_has_completed(&sync->fence))
2309ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace      sync->gl.StatusFlag = 1;
2313dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
2323dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt
2332516d835b17563b097efa3a980c3b9b5e77d7f00Chad Versacevoid
2349ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_init_syncobj_functions(struct dd_function_table *functions)
2353dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt{
2369ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   functions->NewSyncObject = brw_gl_new_sync;
2379ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   functions->DeleteSyncObject = brw_gl_delete_sync;
2389ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   functions->FenceSync = brw_gl_fence_sync;
2399ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   functions->CheckSync = brw_gl_check_sync;
2409ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   functions->ClientWaitSync = brw_gl_client_wait_sync;
2419ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   functions->ServerWaitSync = brw_gl_server_wait_sync;
2423dbba95b72262344b82fba018b7c2c1208754cd2Eric Anholt}
243c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
244c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void *
2459ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versacebrw_dri_create_fence(__DRIcontext *ctx)
246c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
247c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   struct brw_context *brw = ctx->driverPrivate;
248c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   struct brw_fence *fence;
249c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
250c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   fence = calloc(1, sizeof(*fence));
251c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   if (!fence)
252c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace      return NULL;
253c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
254ce1d67c2e5916e97bb65c0bd3b782efca7d3dea5Chad Versace   brw_fence_init(brw, fence);
255c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   brw_fence_insert(brw, fence);
256c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
257c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   return fence;
258c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
259c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
260c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void
26174b02a744913ffaaf409feb0df30aaa92188e250Chad Versacebrw_dri_destroy_fence(__DRIscreen *dri_screen, void *_fence)
262c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
26374b02a744913ffaaf409feb0df30aaa92188e250Chad Versace   struct brw_fence *fence = _fence;
264c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
265c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   brw_fence_finish(fence);
266c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   free(fence);
267c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
268c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
269c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic GLboolean
27074b02a744913ffaaf409feb0df30aaa92188e250Chad Versacebrw_dri_client_wait_sync(__DRIcontext *ctx, void *_fence, unsigned flags,
2719ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace                         uint64_t timeout)
272c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
27374b02a744913ffaaf409feb0df30aaa92188e250Chad Versace   struct brw_fence *fence = _fence;
274c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
2754f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   return brw_fence_client_wait(fence->brw, fence, timeout);
276c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
277c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
278c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versacestatic void
27974b02a744913ffaaf409feb0df30aaa92188e250Chad Versacebrw_dri_server_wait_sync(__DRIcontext *ctx, void *_fence, unsigned flags)
280c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace{
28174b02a744913ffaaf409feb0df30aaa92188e250Chad Versace   struct brw_fence *fence = _fence;
282c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
2834f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   /* We might be called here with a NULL fence as a result of WaitSyncKHR
2844f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov    * on a EGL_KHR_reusable_sync fence. Nothing to do here in such case.
2854f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov    */
2864f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   if (!fence)
2874f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov      return;
2884f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov
2894f48674d51f03d8c954a89dfc49539a1dc750c4dEmil Velikov   brw_fence_server_wait(fence->brw, fence);
290c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace}
291c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
292c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versaceconst __DRI2fenceExtension intelFenceExtension = {
293c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   .base = { __DRI2_FENCE, 1 },
294c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace
2959ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   .create_fence = brw_dri_create_fence,
2969ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   .destroy_fence = brw_dri_destroy_fence,
2979ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   .client_wait_sync = brw_dri_client_wait_sync,
2989ea48fc877f1b0d78edb37cafb4067bab776a74aChad Versace   .server_wait_sync = brw_dri_server_wait_sync,
299c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace   .get_fence_from_cl_event = NULL,
300c636284ee8ee95bb3f3ad31aaf26a9512ec5006cChad Versace};
301