1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Advanced Micro Devices, Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "c99_alloca.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "main/glthread_marshal.h"
27bf215546Sopenharmony_ci#include "main/dispatch.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ciuint32_t
30bf215546Sopenharmony_ci_mesa_unmarshal_CallList(struct gl_context *ctx, const struct marshal_cmd_CallList *cmd, const uint64_t *last)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci   const GLuint list = cmd->list;
33bf215546Sopenharmony_ci   uint64_t *ptr = (uint64_t *) cmd;
34bf215546Sopenharmony_ci   ptr += cmd->cmd_base.cmd_size;
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci   if (ptr < last) {
37bf215546Sopenharmony_ci      const struct marshal_cmd_base *next =
38bf215546Sopenharmony_ci         (const struct marshal_cmd_base *)ptr;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci      /* If the 'next' is also a DISPATCH_CMD_CallList, we transform 'cmd' and 'next' in a CALL_CallLists.
41bf215546Sopenharmony_ci       * If the following commands are also CallList they're including in the CallLists we're building.
42bf215546Sopenharmony_ci       */
43bf215546Sopenharmony_ci      if (next->cmd_id == DISPATCH_CMD_CallList) {
44bf215546Sopenharmony_ci         const int max_list_count = 2048;
45bf215546Sopenharmony_ci         struct marshal_cmd_CallList *next_callist = (struct marshal_cmd_CallList *) next;
46bf215546Sopenharmony_ci         uint32_t *lists = alloca(max_list_count * sizeof(uint32_t));
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci         lists[0] = cmd->list;
49bf215546Sopenharmony_ci         lists[1] = next_callist->list;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci         int count = 2;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci         ptr += next->cmd_size;
54bf215546Sopenharmony_ci         while (ptr < last && count < max_list_count) {
55bf215546Sopenharmony_ci            next = (const struct marshal_cmd_base *)ptr;
56bf215546Sopenharmony_ci            if (next->cmd_id == DISPATCH_CMD_CallList) {
57bf215546Sopenharmony_ci               next_callist = (struct marshal_cmd_CallList *) next;
58bf215546Sopenharmony_ci               lists[count++] = next_callist->list;
59bf215546Sopenharmony_ci               ptr += next->cmd_size;
60bf215546Sopenharmony_ci            } else {
61bf215546Sopenharmony_ci               break;
62bf215546Sopenharmony_ci            }
63bf215546Sopenharmony_ci         }
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci         CALL_CallLists(ctx->CurrentServerDispatch, (count, GL_UNSIGNED_INT, lists));
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci         return (uint32_t) (ptr - (uint64_t*)cmd);
68bf215546Sopenharmony_ci      }
69bf215546Sopenharmony_ci   }
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   CALL_CallList(ctx->CurrentServerDispatch, (list));
72bf215546Sopenharmony_ci   return cmd->cmd_base.cmd_size;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_civoid GLAPIENTRY
76bf215546Sopenharmony_ci_mesa_marshal_CallList(GLuint list)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci   GET_CURRENT_CONTEXT(ctx);
79bf215546Sopenharmony_ci   int cmd_size = sizeof(struct marshal_cmd_CallList);
80bf215546Sopenharmony_ci   struct marshal_cmd_CallList *cmd;
81bf215546Sopenharmony_ci   cmd = _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_CallList, cmd_size);
82bf215546Sopenharmony_ci   cmd->list = list;
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci   _mesa_glthread_CallList(ctx, list);
85bf215546Sopenharmony_ci}
86