radeon/r200/r300: attempt to move lock to common code
[mesa.git] / src / mesa / drivers / dri / radeon / common_misc.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 "main/glheader.h"
37 #include "main/imports.h"
38 #include "main/api_arrayelt.h"
39 #include "main/enums.h"
40 #include "main/colormac.h"
41 #include "main/light.h"
42 #include "main/framebuffer.h"
43
44 #include "swrast/swrast.h"
45 #include "vbo/vbo.h"
46 #include "tnl/tnl.h"
47 #include "tnl/t_pipeline.h"
48 #include "swrast_setup/swrast_setup.h"
49
50 #include "dri_util.h"
51 #include "radeon_drm.h"
52 #include "radeon_screen.h"
53 #include "common_context.h"
54 #include "common_misc.h"
55 /* =============================================================
56 * Scissoring
57 */
58
59 static GLboolean intersect_rect(drm_clip_rect_t * out,
60 drm_clip_rect_t * a, drm_clip_rect_t * b)
61 {
62 *out = *a;
63 if (b->x1 > out->x1)
64 out->x1 = b->x1;
65 if (b->y1 > out->y1)
66 out->y1 = b->y1;
67 if (b->x2 < out->x2)
68 out->x2 = b->x2;
69 if (b->y2 < out->y2)
70 out->y2 = b->y2;
71 if (out->x1 >= out->x2)
72 return GL_FALSE;
73 if (out->y1 >= out->y2)
74 return GL_FALSE;
75 return GL_TRUE;
76 }
77
78 void radeonRecalcScissorRects(radeonContextPtr radeon)
79 {
80 drm_clip_rect_t *out;
81 int i;
82
83 /* Grow cliprect store?
84 */
85 if (radeon->state.scissor.numAllocedClipRects < radeon->numClipRects) {
86 while (radeon->state.scissor.numAllocedClipRects <
87 radeon->numClipRects) {
88 radeon->state.scissor.numAllocedClipRects += 1; /* zero case */
89 radeon->state.scissor.numAllocedClipRects *= 2;
90 }
91
92 if (radeon->state.scissor.pClipRects)
93 FREE(radeon->state.scissor.pClipRects);
94
95 radeon->state.scissor.pClipRects =
96 MALLOC(radeon->state.scissor.numAllocedClipRects *
97 sizeof(drm_clip_rect_t));
98
99 if (radeon->state.scissor.pClipRects == NULL) {
100 radeon->state.scissor.numAllocedClipRects = 0;
101 return;
102 }
103 }
104
105 out = radeon->state.scissor.pClipRects;
106 radeon->state.scissor.numClipRects = 0;
107
108 for (i = 0; i < radeon->numClipRects; i++) {
109 if (intersect_rect(out,
110 &radeon->pClipRects[i],
111 &radeon->state.scissor.rect)) {
112 radeon->state.scissor.numClipRects++;
113 out++;
114 }
115 }
116 }
117
118 /**
119 * Update cliprects and scissors.
120 */
121 void radeonSetCliprects(radeonContextPtr radeon)
122 {
123 __DRIdrawablePrivate *const drawable = radeon->dri.drawable;
124 __DRIdrawablePrivate *const readable = radeon->dri.readable;
125 GLframebuffer *const draw_fb = (GLframebuffer*)drawable->driverPrivate;
126 GLframebuffer *const read_fb = (GLframebuffer*)readable->driverPrivate;
127
128 if (!radeon->radeonScreen->driScreen->dri2.enabled) {
129 if (draw_fb->_ColorDrawBufferIndexes[0] == BUFFER_BACK_LEFT) {
130 /* Can't ignore 2d windows if we are page flipping. */
131 if (drawable->numBackClipRects == 0 || radeon->doPageFlip ||
132 radeon->sarea->pfCurrentPage == 1) {
133 radeon->numClipRects = drawable->numClipRects;
134 radeon->pClipRects = drawable->pClipRects;
135 } else {
136 radeon->numClipRects = drawable->numBackClipRects;
137 radeon->pClipRects = drawable->pBackClipRects;
138 }
139 } else {
140 /* front buffer (or none, or multiple buffers */
141 radeon->numClipRects = drawable->numClipRects;
142 radeon->pClipRects = drawable->pClipRects;
143 }
144 }
145
146 if ((draw_fb->Width != drawable->w) ||
147 (draw_fb->Height != drawable->h)) {
148 _mesa_resize_framebuffer(radeon->glCtx, draw_fb,
149 drawable->w, drawable->h);
150 draw_fb->Initialized = GL_TRUE;
151 }
152
153 if (drawable != readable) {
154 if ((read_fb->Width != readable->w) ||
155 (read_fb->Height != readable->h)) {
156 _mesa_resize_framebuffer(radeon->glCtx, read_fb,
157 readable->w, readable->h);
158 read_fb->Initialized = GL_TRUE;
159 }
160 }
161
162 if (radeon->state.scissor.enabled)
163 radeonRecalcScissorRects(radeon);
164
165 radeon->lastStamp = drawable->lastStamp;
166 }
167
168 void radeonUpdateScissor( GLcontext *ctx )
169 {
170 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
171
172 if ( rmesa->dri.drawable ) {
173 __DRIdrawablePrivate *dPriv = rmesa->dri.drawable;
174
175 int x = ctx->Scissor.X;
176 int y = dPriv->h - ctx->Scissor.Y - ctx->Scissor.Height;
177 int w = ctx->Scissor.X + ctx->Scissor.Width - 1;
178 int h = dPriv->h - ctx->Scissor.Y - 1;
179
180 rmesa->state.scissor.rect.x1 = x + dPriv->x;
181 rmesa->state.scissor.rect.y1 = y + dPriv->y;
182 rmesa->state.scissor.rect.x2 = w + dPriv->x + 1;
183 rmesa->state.scissor.rect.y2 = h + dPriv->y + 1;
184
185 radeonRecalcScissorRects( rmesa );
186 }
187 }