1/*
2 * Copyright © 2020 Advanced Micro Devices, Inc.
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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "c99_alloca.h"
25
26#include "main/glthread_marshal.h"
27#include "main/dispatch.h"
28
29uint32_t
30_mesa_unmarshal_CallList(struct gl_context *ctx, const struct marshal_cmd_CallList *cmd, const uint64_t *last)
31{
32   const GLuint list = cmd->list;
33   uint64_t *ptr = (uint64_t *) cmd;
34   ptr += cmd->cmd_base.cmd_size;
35
36   if (ptr < last) {
37      const struct marshal_cmd_base *next =
38         (const struct marshal_cmd_base *)ptr;
39
40      /* If the 'next' is also a DISPATCH_CMD_CallList, we transform 'cmd' and 'next' in a CALL_CallLists.
41       * If the following commands are also CallList they're including in the CallLists we're building.
42       */
43      if (next->cmd_id == DISPATCH_CMD_CallList) {
44         const int max_list_count = 2048;
45         struct marshal_cmd_CallList *next_callist = (struct marshal_cmd_CallList *) next;
46         uint32_t *lists = alloca(max_list_count * sizeof(uint32_t));
47
48         lists[0] = cmd->list;
49         lists[1] = next_callist->list;
50
51         int count = 2;
52
53         ptr += next->cmd_size;
54         while (ptr < last && count < max_list_count) {
55            next = (const struct marshal_cmd_base *)ptr;
56            if (next->cmd_id == DISPATCH_CMD_CallList) {
57               next_callist = (struct marshal_cmd_CallList *) next;
58               lists[count++] = next_callist->list;
59               ptr += next->cmd_size;
60            } else {
61               break;
62            }
63         }
64
65         CALL_CallLists(ctx->CurrentServerDispatch, (count, GL_UNSIGNED_INT, lists));
66
67         return (uint32_t) (ptr - (uint64_t*)cmd);
68      }
69   }
70
71   CALL_CallList(ctx->CurrentServerDispatch, (list));
72   return cmd->cmd_base.cmd_size;
73}
74
75void GLAPIENTRY
76_mesa_marshal_CallList(GLuint list)
77{
78   GET_CURRENT_CONTEXT(ctx);
79   int cmd_size = sizeof(struct marshal_cmd_CallList);
80   struct marshal_cmd_CallList *cmd;
81   cmd = _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_CallList, cmd_size);
82   cmd->list = list;
83
84   _mesa_glthread_CallList(ctx, list);
85}
86