1 /*
2  * Copyright © 2012 Intel 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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "glthread_marshal.h"
25 #include "dispatch.h"
26 
27 struct marshal_cmd_ShaderSource
28 {
29    struct marshal_cmd_base cmd_base;
30    GLuint shader;
31    GLsizei count;
32    /* Followed by GLint length[count], then the contents of all strings,
33     * concatenated.
34     */
35 };
36 
37 
38 void
_mesa_unmarshal_ShaderSource(struct gl_context * ctx,const struct marshal_cmd_ShaderSource * cmd)39 _mesa_unmarshal_ShaderSource(struct gl_context *ctx,
40                              const struct marshal_cmd_ShaderSource *cmd)
41 {
42    const GLint *cmd_length = (const GLint *) (cmd + 1);
43    const GLchar *cmd_strings = (const GLchar *) (cmd_length + cmd->count);
44    /* TODO: how to deal with malloc failure? */
45    const GLchar * *string = malloc(cmd->count * sizeof(const GLchar *));
46    int i;
47 
48    for (i = 0; i < cmd->count; ++i) {
49       string[i] = cmd_strings;
50       cmd_strings += cmd_length[i];
51    }
52    CALL_ShaderSource(ctx->CurrentServerDispatch,
53                      (cmd->shader, cmd->count, string, cmd_length));
54    free((void *)string);
55 }
56 
57 
58 static size_t
measure_ShaderSource_strings(GLsizei count,const GLchar * const * string,const GLint * length_in,GLint * length_out)59 measure_ShaderSource_strings(GLsizei count, const GLchar * const *string,
60                              const GLint *length_in, GLint *length_out)
61 {
62    int i;
63    size_t total_string_length = 0;
64 
65    for (i = 0; i < count; ++i) {
66       if (length_in == NULL || length_in[i] < 0) {
67          if (string[i])
68             length_out[i] = strlen(string[i]);
69       } else {
70          length_out[i] = length_in[i];
71       }
72       total_string_length += length_out[i];
73    }
74    return total_string_length;
75 }
76 
77 
78 void GLAPIENTRY
_mesa_marshal_ShaderSource(GLuint shader,GLsizei count,const GLchar * const * string,const GLint * length)79 _mesa_marshal_ShaderSource(GLuint shader, GLsizei count,
80                            const GLchar * const *string, const GLint *length)
81 {
82    /* TODO: how to report an error if count < 0? */
83 
84    GET_CURRENT_CONTEXT(ctx);
85    /* TODO: how to deal with malloc failure? */
86    const size_t fixed_cmd_size = sizeof(struct marshal_cmd_ShaderSource);
87    STATIC_ASSERT(sizeof(struct marshal_cmd_ShaderSource) % sizeof(GLint) == 0);
88    size_t length_size = count * sizeof(GLint);
89    GLint *length_tmp = malloc(length_size);
90    size_t total_string_length =
91       measure_ShaderSource_strings(count, string, length, length_tmp);
92    size_t total_cmd_size = fixed_cmd_size + length_size + total_string_length;
93 
94    if (total_cmd_size <= MARSHAL_MAX_CMD_SIZE && count > 0) {
95       struct marshal_cmd_ShaderSource *cmd =
96          _mesa_glthread_allocate_command(ctx, DISPATCH_CMD_ShaderSource,
97                                          total_cmd_size);
98       GLint *cmd_length = (GLint *) (cmd + 1);
99       GLchar *cmd_strings = (GLchar *) (cmd_length + count);
100       int i;
101 
102       cmd->shader = shader;
103       cmd->count = count;
104       memcpy(cmd_length, length_tmp, length_size);
105       for (i = 0; i < count; ++i) {
106          memcpy(cmd_strings, string[i], cmd_length[i]);
107          cmd_strings += cmd_length[i];
108       }
109    } else {
110       _mesa_glthread_finish(ctx);
111       CALL_ShaderSource(ctx->CurrentServerDispatch,
112                         (shader, count, string, length_tmp));
113    }
114    free(length_tmp);
115 }
116