glx: Use initstate_r / random_r instead of corrupting global random number state
[mesa.git] / src / glx / x11 / compsize.c
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30
31 #include <GL/gl.h>
32 #include "indirect_size.h"
33 #include "glxclient.h"
34
35 /*
36 ** Return the number of elements per group of a specified format
37 */
38 GLint
39 __glElementsPerGroup(GLenum format, GLenum type)
40 {
41 /*
42 ** To make row length computation valid for image extraction,
43 ** packed pixel types assume elements per group equals one.
44 */
45 switch (type) {
46 case GL_UNSIGNED_BYTE_3_3_2:
47 case GL_UNSIGNED_BYTE_2_3_3_REV:
48 case GL_UNSIGNED_SHORT_5_6_5:
49 case GL_UNSIGNED_SHORT_5_6_5_REV:
50 case GL_UNSIGNED_SHORT_4_4_4_4:
51 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
52 case GL_UNSIGNED_SHORT_5_5_5_1:
53 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
54 case GL_UNSIGNED_SHORT_8_8_APPLE:
55 case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
56 case GL_UNSIGNED_SHORT_15_1_MESA:
57 case GL_UNSIGNED_SHORT_1_15_REV_MESA:
58 case GL_UNSIGNED_INT_8_8_8_8:
59 case GL_UNSIGNED_INT_8_8_8_8_REV:
60 case GL_UNSIGNED_INT_10_10_10_2:
61 case GL_UNSIGNED_INT_2_10_10_10_REV:
62 case GL_UNSIGNED_INT_24_8_NV:
63 case GL_UNSIGNED_INT_24_8_MESA:
64 case GL_UNSIGNED_INT_8_24_REV_MESA:
65 return 1;
66 default:
67 break;
68 }
69
70 switch (format) {
71 case GL_RGB:
72 case GL_BGR:
73 return 3;
74 case GL_422_EXT:
75 case GL_422_REV_EXT:
76 case GL_422_AVERAGE_EXT:
77 case GL_422_REV_AVERAGE_EXT:
78 case GL_YCBCR_422_APPLE:
79 case GL_LUMINANCE_ALPHA:
80 return 2;
81 case GL_RGBA:
82 case GL_BGRA:
83 case GL_ABGR_EXT:
84 return 4;
85 case GL_COLOR_INDEX:
86 case GL_STENCIL_INDEX:
87 case GL_DEPTH_COMPONENT:
88 case GL_RED:
89 case GL_GREEN:
90 case GL_BLUE:
91 case GL_ALPHA:
92 case GL_LUMINANCE:
93 case GL_INTENSITY:
94 return 1;
95 default:
96 return 0;
97 }
98 }
99
100 /*
101 ** Return the number of bytes per element, based on the element type (other
102 ** than GL_BITMAP).
103 */
104 GLint
105 __glBytesPerElement(GLenum type)
106 {
107 switch (type) {
108 case GL_UNSIGNED_SHORT:
109 case GL_SHORT:
110 case GL_UNSIGNED_SHORT_5_6_5:
111 case GL_UNSIGNED_SHORT_5_6_5_REV:
112 case GL_UNSIGNED_SHORT_4_4_4_4:
113 case GL_UNSIGNED_SHORT_4_4_4_4_REV:
114 case GL_UNSIGNED_SHORT_5_5_5_1:
115 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
116 case GL_UNSIGNED_SHORT_8_8_APPLE:
117 case GL_UNSIGNED_SHORT_8_8_REV_APPLE:
118 case GL_UNSIGNED_SHORT_15_1_MESA:
119 case GL_UNSIGNED_SHORT_1_15_REV_MESA:
120 return 2;
121 case GL_UNSIGNED_BYTE:
122 case GL_BYTE:
123 case GL_UNSIGNED_BYTE_3_3_2:
124 case GL_UNSIGNED_BYTE_2_3_3_REV:
125 return 1;
126 case GL_INT:
127 case GL_UNSIGNED_INT:
128 case GL_FLOAT:
129 case GL_UNSIGNED_INT_8_8_8_8:
130 case GL_UNSIGNED_INT_8_8_8_8_REV:
131 case GL_UNSIGNED_INT_10_10_10_2:
132 case GL_UNSIGNED_INT_2_10_10_10_REV:
133 case GL_UNSIGNED_INT_24_8_NV:
134 case GL_UNSIGNED_INT_24_8_MESA:
135 case GL_UNSIGNED_INT_8_24_REV_MESA:
136 return 4;
137 default:
138 return 0;
139 }
140 }
141
142 /*
143 ** Compute memory required for internal packed array of data of given type
144 ** and format.
145 */
146 GLint
147 __glImageSize(GLsizei width, GLsizei height, GLsizei depth,
148 GLenum format, GLenum type, GLenum target)
149 {
150 int bytes_per_row;
151 int components;
152
153 switch (target) {
154 case GL_PROXY_TEXTURE_1D:
155 case GL_PROXY_TEXTURE_2D:
156 case GL_PROXY_TEXTURE_3D:
157 case GL_PROXY_TEXTURE_4D_SGIS:
158 case GL_PROXY_TEXTURE_CUBE_MAP:
159 case GL_PROXY_TEXTURE_RECTANGLE_ARB:
160 case GL_PROXY_HISTOGRAM:
161 case GL_PROXY_COLOR_TABLE:
162 case GL_PROXY_TEXTURE_COLOR_TABLE_SGI:
163 case GL_PROXY_POST_CONVOLUTION_COLOR_TABLE:
164 case GL_PROXY_POST_COLOR_MATRIX_COLOR_TABLE:
165 case GL_PROXY_POST_IMAGE_TRANSFORM_COLOR_TABLE_HP:
166 return 0;
167 }
168
169 if (width < 0 || height < 0 || depth < 0) {
170 return 0;
171 }
172
173 /*
174 ** Zero is returned if either format or type are invalid.
175 */
176 components = __glElementsPerGroup(format, type);
177 if (type == GL_BITMAP) {
178 if (format == GL_COLOR_INDEX || format == GL_STENCIL_INDEX) {
179 bytes_per_row = (width + 7) >> 3;
180 }
181 else {
182 return 0;
183 }
184 }
185 else {
186 bytes_per_row = __glBytesPerElement(type) * width;
187 }
188
189 return bytes_per_row * height * depth * components;
190 }