Fix a refcounting mistake with first_swap_fence.
[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 "vbo/vbo.h"
45 #include "tnl/tnl.h"
46 #include "tnl/t_pipeline.h"
47 #include "swrast_setup/swrast_setup.h"
48
49 #include "radeon_ioctl.h"
50 #include "radeon_state.h"
51 #include "r300_ioctl.h"
52 #include "framebuffer.h"
53
54 /* =============================================================
55 * Scissoring
56 */
57
58 static GLboolean intersect_rect(drm_clip_rect_t * out,
59 drm_clip_rect_t * a, drm_clip_rect_t * b)
60 {
61 *out = *a;
62 if (b->x1 > out->x1)
63 out->x1 = b->x1;
64 if (b->y1 > out->y1)
65 out->y1 = b->y1;
66 if (b->x2 < out->x2)
67 out->x2 = b->x2;
68 if (b->y2 < out->y2)
69 out->y2 = b->y2;
70 if (out->x1 >= out->x2)
71 return GL_FALSE;
72 if (out->y1 >= out->y2)
73 return GL_FALSE;
74 return GL_TRUE;
75 }
76
77 void radeonRecalcScissorRects(radeonContextPtr radeon)
78 {
79 drm_clip_rect_t *out;
80 int i;
81
82 /* Grow cliprect store?
83 */
84 if (radeon->state.scissor.numAllocedClipRects < radeon->numClipRects) {
85 while (radeon->state.scissor.numAllocedClipRects <
86 radeon->numClipRects) {
87 radeon->state.scissor.numAllocedClipRects += 1; /* zero case */
88 radeon->state.scissor.numAllocedClipRects *= 2;
89 }
90
91 if (radeon->state.scissor.pClipRects)
92 FREE(radeon->state.scissor.pClipRects);
93
94 radeon->state.scissor.pClipRects =
95 MALLOC(radeon->state.scissor.numAllocedClipRects *
96 sizeof(drm_clip_rect_t));
97
98 if (radeon->state.scissor.pClipRects == NULL) {
99 radeon->state.scissor.numAllocedClipRects = 0;
100 return;
101 }
102 }
103
104 out = radeon->state.scissor.pClipRects;
105 radeon->state.scissor.numClipRects = 0;
106
107 for (i = 0; i < radeon->numClipRects; i++) {
108 if (intersect_rect(out,
109 &radeon->pClipRects[i],
110 &radeon->state.scissor.rect)) {
111 radeon->state.scissor.numClipRects++;
112 out++;
113 }
114 }
115 }
116
117 void radeonUpdateScissor(GLcontext* ctx)
118 {
119 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
120
121 if (radeon->dri.drawable) {
122 __DRIdrawablePrivate *dPriv = radeon->dri.drawable;
123 int x1 = dPriv->x + ctx->Scissor.X;
124 int y1 = dPriv->y + dPriv->h - (ctx->Scissor.Y + ctx->Scissor.Height);
125
126 radeon->state.scissor.rect.x1 = x1;
127 radeon->state.scissor.rect.y1 = y1;
128 radeon->state.scissor.rect.x2 = x1 + ctx->Scissor.Width - 1;
129 radeon->state.scissor.rect.y2 = y1 + ctx->Scissor.Height - 1;
130
131 radeonRecalcScissorRects(radeon);
132 }
133 }
134
135 static void radeonScissor(GLcontext* ctx, GLint x, GLint y, GLsizei w, GLsizei h)
136 {
137 if (ctx->Scissor.Enabled) {
138 /* We don't pipeline cliprect changes */
139 r300Flush(ctx);
140 radeonUpdateScissor(ctx);
141 }
142 }
143
144
145 /**
146 * Update cliprects and scissors.
147 */
148 void radeonSetCliprects(radeonContextPtr radeon)
149 {
150 __DRIdrawablePrivate *const drawable = radeon->dri.drawable;
151 __DRIdrawablePrivate *const readable = radeon->dri.readable;
152 GLframebuffer *const draw_fb = (GLframebuffer*)drawable->driverPrivate;
153 GLframebuffer *const read_fb = (GLframebuffer*)readable->driverPrivate;
154
155 if (draw_fb->_ColorDrawBufferMask[0] == BUFFER_BIT_BACK_LEFT) {
156 /* Can't ignore 2d windows if we are page flipping. */
157 if (drawable->numBackClipRects == 0 || radeon->doPageFlip) {
158 radeon->numClipRects = drawable->numClipRects;
159 radeon->pClipRects = drawable->pClipRects;
160 } else {
161 radeon->numClipRects = drawable->numBackClipRects;
162 radeon->pClipRects = drawable->pBackClipRects;
163 }
164 } else {
165 /* front buffer (or none, or multiple buffers */
166 radeon->numClipRects = drawable->numClipRects;
167 radeon->pClipRects = drawable->pClipRects;
168 }
169
170 if ((draw_fb->Width != drawable->w) ||
171 (draw_fb->Height != drawable->h)) {
172 _mesa_resize_framebuffer(radeon->glCtx, draw_fb,
173 drawable->w, drawable->h);
174 draw_fb->Initialized = GL_TRUE;
175 }
176
177 if (drawable != readable) {
178 if ((read_fb->Width != readable->w) ||
179 (read_fb->Height != readable->h)) {
180 _mesa_resize_framebuffer(radeon->glCtx, read_fb,
181 readable->w, readable->h);
182 read_fb->Initialized = GL_TRUE;
183 }
184 }
185
186 if (radeon->state.scissor.enabled)
187 radeonRecalcScissorRects(radeon);
188
189 radeon->lastStamp = drawable->lastStamp;
190 }
191
192
193 /**
194 * Handle common enable bits.
195 * Called as a fallback by r200Enable/r300Enable.
196 */
197 void radeonEnable(GLcontext* ctx, GLenum cap, GLboolean state)
198 {
199 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
200
201 switch(cap) {
202 case GL_SCISSOR_TEST:
203 /* We don't pipeline cliprect & scissor changes */
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
234
235 /**
236 * Initialize common state functions.
237 * Called by r200InitStateFuncs/r300InitStateFuncs
238 */
239 void radeonInitStateFuncs(struct dd_function_table *functions)
240 {
241 functions->Scissor = radeonScissor;
242 }