Fix for buffer overrun caused by ALLOC_STATE not having args surrounded by parenthesi...
[mesa.git] / src / mesa / drivers / dri / r300 / radeon_state.c
1 /**************************************************************************
2
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 #include "glheader.h"
37 #include "imports.h"
38 #include "api_arrayelt.h"
39 #include "enums.h"
40 #include "colormac.h"
41 #include "light.h"
42
43 #include "swrast/swrast.h"
44 #include "array_cache/acache.h"
45 #include "tnl/tnl.h"
46 #include "tnl/t_pipeline.h"
47 #include "swrast_setup/swrast_setup.h"
48
49 #include "r200_context.h"
50 #include "radeon_ioctl.h"
51 #include "radeon_state.h"
52 #include "r200_state.h"
53 #include "r300_ioctl.h"
54
55
56 /* =============================================================
57 * Scissoring
58 */
59
60 static GLboolean intersect_rect(drm_clip_rect_t * out,
61 drm_clip_rect_t * a, drm_clip_rect_t * b)
62 {
63 *out = *a;
64 if (b->x1 > out->x1)
65 out->x1 = b->x1;
66 if (b->y1 > out->y1)
67 out->y1 = b->y1;
68 if (b->x2 < out->x2)
69 out->x2 = b->x2;
70 if (b->y2 < out->y2)
71 out->y2 = b->y2;
72 if (out->x1 >= out->x2)
73 return GL_FALSE;
74 if (out->y1 >= out->y2)
75 return GL_FALSE;
76 return GL_TRUE;
77 }
78
79 void radeonRecalcScissorRects(radeonContextPtr radeon)
80 {
81 drm_clip_rect_t *out;
82 int i;
83
84 /* Grow cliprect store?
85 */
86 if (radeon->state.scissor.numAllocedClipRects < radeon->numClipRects) {
87 while (radeon->state.scissor.numAllocedClipRects <
88 radeon->numClipRects) {
89 radeon->state.scissor.numAllocedClipRects += 1; /* zero case */
90 radeon->state.scissor.numAllocedClipRects *= 2;
91 }
92
93 if (radeon->state.scissor.pClipRects)
94 FREE(radeon->state.scissor.pClipRects);
95
96 radeon->state.scissor.pClipRects =
97 MALLOC(radeon->state.scissor.numAllocedClipRects *
98 sizeof(drm_clip_rect_t));
99
100 if (radeon->state.scissor.pClipRects == NULL) {
101 radeon->state.scissor.numAllocedClipRects = 0;
102 return;
103 }
104 }
105
106 out = radeon->state.scissor.pClipRects;
107 radeon->state.scissor.numClipRects = 0;
108
109 for (i = 0; i < radeon->numClipRects; i++) {
110 if (intersect_rect(out,
111 &radeon->pClipRects[i],
112 &radeon->state.scissor.rect)) {
113 radeon->state.scissor.numClipRects++;
114 out++;
115 }
116 }
117 }
118
119 void radeonUpdateScissor(GLcontext* ctx)
120 {
121 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
122
123 assert(radeon->state.scissor.enabled == ctx->Scissor.Enabled);
124
125 if (radeon->dri.drawable) {
126 __DRIdrawablePrivate *dPriv = radeon->dri.drawable;
127 int x1 = dPriv->x + ctx->Scissor.X;
128 int y1 = dPriv->y + dPriv->h - (ctx->Scissor.Y + ctx->Scissor.Height);
129
130 radeon->state.scissor.rect.x1 = x1;
131 radeon->state.scissor.rect.y1 = y1;
132 radeon->state.scissor.rect.x2 = x1 + ctx->Scissor.Width - 1;
133 radeon->state.scissor.rect.y2 = y1 + ctx->Scissor.Height - 1;
134
135 radeonRecalcScissorRects(radeon);
136 }
137 }
138
139 static void radeonScissor(GLcontext* ctx, GLint x, GLint y, GLsizei w, GLsizei h)
140 {
141 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
142
143 if (ctx->Scissor.Enabled) {
144 /* We don't pipeline cliprect changes */
145 if (IS_FAMILY_R200(radeon))
146 R200_FIREVERTICES((r200ContextPtr)radeon);
147 else
148 r300Flush(ctx);
149
150 radeonUpdateScissor(ctx);
151 }
152 }
153
154
155 /**
156 * Update cliprects and scissors.
157 */
158 void radeonSetCliprects(radeonContextPtr radeon, GLenum mode)
159 {
160 __DRIdrawablePrivate *dPriv = radeon->dri.drawable;
161
162 switch (mode) {
163 case GL_FRONT_LEFT:
164 radeon->numClipRects = dPriv->numClipRects;
165 radeon->pClipRects = dPriv->pClipRects;
166 break;
167 case GL_BACK_LEFT:
168 /* Can't ignore 2d windows if we are page flipping.
169 */
170 if (dPriv->numBackClipRects == 0 || radeon->doPageFlip) {
171 radeon->numClipRects = dPriv->numClipRects;
172 radeon->pClipRects = dPriv->pClipRects;
173 } else {
174 radeon->numClipRects = dPriv->numBackClipRects;
175 radeon->pClipRects = dPriv->pBackClipRects;
176 }
177 break;
178 default:
179 fprintf(stderr, "bad mode in radeonSetCliprects\n");
180 radeon->numClipRects = 0;
181 radeon->pClipRects = 0;
182 return;
183 }
184
185 if (radeon->state.scissor.enabled)
186 radeonRecalcScissorRects(radeon);
187 }
188
189
190 /**
191 * Handle common enable bits.
192 * Called as a fallback by r200Enable/r300Enable.
193 */
194 void radeonEnable(GLcontext* ctx, GLenum cap, GLboolean state)
195 {
196 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
197
198 switch(cap) {
199 case GL_SCISSOR_TEST:
200 /* We don't pipeline cliprect & scissor changes */
201 if (IS_FAMILY_R200(radeon))
202 R200_FIREVERTICES((r200ContextPtr)radeon);
203 else
204 r300Flush(ctx);
205
206 radeon->state.scissor.enabled = state;
207 radeonUpdateScissor(ctx);
208 break;
209
210 default:
211 return;
212 }
213 }
214
215
216 /**
217 * Initialize default state.
218 * This function is called once at context init time from
219 * r200InitState/r300InitState
220 */
221 void radeonInitState(radeonContextPtr radeon)
222 {
223 radeon->Fallback = 0;
224
225 if (radeon->glCtx->Visual.doubleBufferMode && radeon->sarea->pfCurrentPage == 0) {
226 radeon->state.color.drawOffset = radeon->radeonScreen->backOffset;
227 radeon->state.color.drawPitch = radeon->radeonScreen->backPitch;
228 } else {
229 radeon->state.color.drawOffset = radeon->radeonScreen->frontOffset;
230 radeon->state.color.drawPitch = radeon->radeonScreen->frontPitch;
231 }
232
233 radeon->state.pixel.readOffset = radeon->state.color.drawOffset;
234 radeon->state.pixel.readPitch = radeon->state.color.drawPitch;
235 }
236
237
238 /**
239 * Initialize common state functions.
240 * Called by r200InitStateFuncs/r300InitStateFuncs
241 */
242 void radeonInitStateFuncs(struct dd_function_table *functions)
243 {
244 functions->Scissor = radeonScissor;
245 }