1/*
2 * Copyright © 2017 Valve Corporation.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "main/imports.h"
25#include "main/mtypes.h"
26#include "main/context.h"
27
28#include "main/externalobjects.h"
29
30#include "st_context.h"
31#include "st_texture.h"
32#include "st_util.h"
33#include "st_cb_bitmap.h"
34#include "st_cb_bufferobjects.h"
35#include "st_cb_semaphoreobjects.h"
36
37#include "state_tracker/drm_driver.h"
38#include "pipe/p_context.h"
39#include "pipe/p_screen.h"
40
41static struct gl_semaphore_object *
42st_semaphoreobj_alloc(struct gl_context *ctx, GLuint name)
43{
44   struct st_semaphore_object *st_obj = ST_CALLOC_STRUCT(st_semaphore_object);
45   if (!st_obj)
46      return NULL;
47
48   _mesa_initialize_semaphore_object(ctx, &st_obj->Base, name);
49   return &st_obj->Base;
50}
51
52static void
53st_semaphoreobj_free(struct gl_context *ctx,
54                     struct gl_semaphore_object *semObj)
55{
56   _mesa_delete_semaphore_object(ctx, semObj);
57}
58
59
60static void
61st_import_semaphoreobj_fd(struct gl_context *ctx,
62                       struct gl_semaphore_object *semObj,
63                       int fd)
64{
65   struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
66   struct st_context *st = st_context(ctx);
67   struct pipe_context *pipe = st->pipe;
68
69   pipe->create_fence_fd(pipe, &st_obj->fence, fd, PIPE_FD_TYPE_SYNCOBJ);
70
71#if !defined(_WIN32)
72   /* We own fd, but we no longer need it. So get rid of it */
73   close(fd);
74#endif
75}
76
77static void
78st_server_wait_semaphore(struct gl_context *ctx,
79                         struct gl_semaphore_object *semObj,
80                         GLuint numBufferBarriers,
81                         struct gl_buffer_object **bufObjs,
82                         GLuint numTextureBarriers,
83                         struct gl_texture_object **texObjs,
84                         const GLenum *srcLayouts)
85{
86   struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
87   struct st_context *st = st_context(ctx);
88   struct pipe_context *pipe = st->pipe;
89   struct st_buffer_object *bufObj;
90   struct st_texture_object *texObj;
91
92   /* The driver is allowed to flush during fence_server_sync, be prepared */
93   st_flush_bitmap_cache(st);
94   pipe->fence_server_sync(pipe, st_obj->fence);
95
96   /**
97    * According to the EXT_external_objects spec, the memory operations must
98    * follow the wait. This is to make sure the flush is executed after the
99    * other party is done modifying the memory.
100    *
101    * Relevant excerpt from section "4.2.3 Waiting for Semaphores":
102    *
103    * Following completion of the semaphore wait operation, memory will also be
104    * made visible in the specified buffer and texture objects.
105    *
106    */
107   for (unsigned i = 0; i < numBufferBarriers; i++) {
108      if (!bufObjs[i])
109         continue;
110
111      bufObj = st_buffer_object(bufObjs[i]);
112      pipe->flush_resource(pipe, bufObj->buffer);
113   }
114
115   for (unsigned i = 0; i < numTextureBarriers; i++) {
116      if (!texObjs[i])
117         continue;
118
119      texObj = st_texture_object(texObjs[i]);
120      pipe->flush_resource(pipe, texObj->pt);
121   }
122}
123
124static void
125st_server_signal_semaphore(struct gl_context *ctx,
126                           struct gl_semaphore_object *semObj,
127                           GLuint numBufferBarriers,
128                           struct gl_buffer_object **bufObjs,
129                           GLuint numTextureBarriers,
130                           struct gl_texture_object **texObjs,
131                           const GLenum *dstLayouts)
132{
133   struct st_semaphore_object *st_obj = st_semaphore_object(semObj);
134   struct st_context *st = st_context(ctx);
135   struct pipe_context *pipe = st->pipe;
136   struct st_buffer_object *bufObj;
137   struct st_texture_object *texObj;
138
139   for (unsigned i = 0; i < numBufferBarriers; i++) {
140      if (!bufObjs[i])
141         continue;
142
143      bufObj = st_buffer_object(bufObjs[i]);
144      pipe->flush_resource(pipe, bufObj->buffer);
145   }
146
147   for (unsigned i = 0; i < numTextureBarriers; i++) {
148      if (!texObjs[i])
149         continue;
150
151      texObj = st_texture_object(texObjs[i]);
152      pipe->flush_resource(pipe, texObj->pt);
153   }
154
155   /* The driver is allowed to flush during fence_server_signal, be prepared */
156   st_flush_bitmap_cache(st);
157   pipe->fence_server_signal(pipe, st_obj->fence);
158}
159
160void
161st_init_semaphoreobject_functions(struct dd_function_table *functions)
162{
163   functions->NewSemaphoreObject = st_semaphoreobj_alloc;
164   functions->DeleteSemaphoreObject = st_semaphoreobj_free;
165   functions->ImportSemaphoreFd = st_import_semaphoreobj_fd;
166   functions->ServerWaitSemaphoreObject = st_server_wait_semaphore;
167   functions->ServerSignalSemaphoreObject = st_server_signal_semaphore;
168}
169