gallium: fix refcount bug introduced in eb20e2984
[mesa.git] / src / mesa / drivers / dri / i915 / intel_span.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "macros.h"
30 #include "mtypes.h"
31 #include "colormac.h"
32
33 #include "intel_screen.h"
34
35 #include "intel_span.h"
36 #include "intel_ioctl.h"
37 #include "swrast/swrast.h"
38
39
40 #define DBG 0
41
42 #define LOCAL_VARS \
43 intelContextPtr intel = INTEL_CONTEXT(ctx); \
44 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
45 driRenderbuffer *drb = (driRenderbuffer *) rb; \
46 GLuint pitch = drb->pitch; \
47 GLuint height = dPriv->h; \
48 char *buf = (char *) drb->Base.Data + \
49 dPriv->x * drb->cpp + \
50 dPriv->y * pitch; \
51 GLushort p; \
52 (void) buf; (void) p
53
54 #define LOCAL_DEPTH_VARS \
55 intelContextPtr intel = INTEL_CONTEXT(ctx); \
56 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
57 driRenderbuffer *drb = (driRenderbuffer *) rb; \
58 GLuint pitch = drb->pitch; \
59 GLuint height = dPriv->h; \
60 char *buf = (char *) drb->Base.Data + \
61 dPriv->x * drb->cpp + \
62 dPriv->y * pitch
63
64 #define LOCAL_STENCIL_VARS LOCAL_DEPTH_VARS
65
66 #define INIT_MONO_PIXEL(p,color)\
67 p = INTEL_PACKCOLOR565(color[0],color[1],color[2])
68
69 #define Y_FLIP(_y) (height - _y - 1)
70
71 #define HW_LOCK()
72
73 #define HW_UNLOCK()
74
75 /* 16 bit, 565 rgb color spanline and pixel functions
76 */
77 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
78 *(GLushort *)(buf + _x*2 + _y*pitch) = ( (((int)r & 0xf8) << 8) | \
79 (((int)g & 0xfc) << 3) | \
80 (((int)b & 0xf8) >> 3))
81 #define WRITE_PIXEL( _x, _y, p ) \
82 *(GLushort *)(buf + _x*2 + _y*pitch) = p
83
84 #define READ_RGBA( rgba, _x, _y ) \
85 do { \
86 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
87 rgba[0] = (((p >> 11) & 0x1f) * 255) / 31; \
88 rgba[1] = (((p >> 5) & 0x3f) * 255) / 63; \
89 rgba[2] = (((p >> 0) & 0x1f) * 255) / 31; \
90 rgba[3] = 255; \
91 } while(0)
92
93 #define TAG(x) intel##x##_565
94 #include "spantmp.h"
95
96 /* 15 bit, 555 rgb color spanline and pixel functions
97 */
98 #define WRITE_RGBA( _x, _y, r, g, b, a ) \
99 *(GLushort *)(buf + _x*2 + _y*pitch) = (((r & 0xf8) << 7) | \
100 ((g & 0xf8) << 3) | \
101 ((b & 0xf8) >> 3))
102
103 #define WRITE_PIXEL( _x, _y, p ) \
104 *(GLushort *)(buf + _x*2 + _y*pitch) = p
105
106 #define READ_RGBA( rgba, _x, _y ) \
107 do { \
108 GLushort p = *(GLushort *)(buf + _x*2 + _y*pitch); \
109 rgba[0] = (p >> 7) & 0xf8; \
110 rgba[1] = (p >> 3) & 0xf8; \
111 rgba[2] = (p << 3) & 0xf8; \
112 rgba[3] = 255; \
113 } while(0)
114
115 #define TAG(x) intel##x##_555
116 #include "spantmp.h"
117
118 /* 16 bit depthbuffer functions.
119 */
120 #define WRITE_DEPTH( _x, _y, d ) \
121 *(GLushort *)(buf + (_x)*2 + (_y)*pitch) = d;
122
123 #define READ_DEPTH( d, _x, _y ) \
124 d = *(GLushort *)(buf + (_x)*2 + (_y)*pitch);
125
126
127 #define TAG(x) intel##x##_z16
128 #include "depthtmp.h"
129
130
131 #undef LOCAL_VARS
132 #define LOCAL_VARS \
133 intelContextPtr intel = INTEL_CONTEXT(ctx); \
134 __DRIdrawablePrivate *dPriv = intel->driDrawable; \
135 driRenderbuffer *drb = (driRenderbuffer *) rb; \
136 GLuint pitch = drb->pitch; \
137 GLuint height = dPriv->h; \
138 char *buf = (char *)drb->Base.Data + \
139 dPriv->x * drb->cpp + \
140 dPriv->y * pitch; \
141 GLuint p; \
142 (void) buf; (void) p
143
144 #undef INIT_MONO_PIXEL
145 #define INIT_MONO_PIXEL(p,color)\
146 p = INTEL_PACKCOLOR8888(color[0],color[1],color[2],color[3])
147
148 /* 32 bit, 8888 argb color spanline and pixel functions
149 */
150 #define WRITE_RGBA(_x, _y, r, g, b, a) \
151 *(GLuint *)(buf + _x*4 + _y*pitch) = ((r << 16) | \
152 (g << 8) | \
153 (b << 0) | \
154 (a << 24) )
155
156 #define WRITE_PIXEL(_x, _y, p) \
157 *(GLuint *)(buf + _x*4 + _y*pitch) = p
158
159
160 #define READ_RGBA(rgba, _x, _y) \
161 do { \
162 GLuint p = *(GLuint *)(buf + _x*4 + _y*pitch); \
163 rgba[0] = (p >> 16) & 0xff; \
164 rgba[1] = (p >> 8) & 0xff; \
165 rgba[2] = (p >> 0) & 0xff; \
166 rgba[3] = (p >> 24) & 0xff; \
167 } while (0)
168
169 #define TAG(x) intel##x##_8888
170 #include "spantmp.h"
171
172
173 /* 24/8 bit interleaved depth/stencil functions
174 */
175 #define WRITE_DEPTH( _x, _y, d ) { \
176 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
177 tmp &= 0xff000000; \
178 tmp |= (d) & 0xffffff; \
179 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
180 }
181
182 #define READ_DEPTH( d, _x, _y ) \
183 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) & 0xffffff;
184
185
186 #define TAG(x) intel##x##_z24_s8
187 #include "depthtmp.h"
188
189 #define WRITE_STENCIL( _x, _y, d ) { \
190 GLuint tmp = *(GLuint *)(buf + (_x)*4 + (_y)*pitch); \
191 tmp &= 0xffffff; \
192 tmp |= ((d)<<24); \
193 *(GLuint *)(buf + (_x)*4 + (_y)*pitch) = tmp; \
194 }
195
196 #define READ_STENCIL( d, _x, _y ) \
197 d = *(GLuint *)(buf + (_x)*4 + (_y)*pitch) >> 24;
198
199 #define TAG(x) intel##x##_z24_s8
200 #include "stenciltmp.h"
201
202
203 /* Move locking out to get reasonable span performance.
204 */
205 void intelSpanRenderStart( GLcontext *ctx )
206 {
207 intelContextPtr intel = INTEL_CONTEXT(ctx);
208
209 intelFlush(&intel->ctx);
210 LOCK_HARDWARE(intel);
211 intelWaitForIdle(intel);
212 }
213
214 void intelSpanRenderFinish( GLcontext *ctx )
215 {
216 intelContextPtr intel = INTEL_CONTEXT( ctx );
217 _swrast_flush( ctx );
218 UNLOCK_HARDWARE( intel );
219 }
220
221 void intelInitSpanFuncs( GLcontext *ctx )
222 {
223 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
224 swdd->SpanRenderStart = intelSpanRenderStart;
225 swdd->SpanRenderFinish = intelSpanRenderFinish;
226 }
227
228
229 /**
230 * Plug in the Get/Put routines for the given driRenderbuffer.
231 */
232 void
233 intelSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis)
234 {
235 if (drb->Base.InternalFormat == GL_RGBA) {
236 if (vis->redBits == 5 && vis->greenBits == 5 && vis->blueBits == 5) {
237 intelInitPointers_555(&drb->Base);
238 }
239 else if (vis->redBits == 5 && vis->greenBits == 6 && vis->blueBits == 5) {
240 intelInitPointers_565(&drb->Base);
241 }
242 else {
243 assert(vis->redBits == 8);
244 assert(vis->greenBits == 8);
245 assert(vis->blueBits == 8);
246 intelInitPointers_8888(&drb->Base);
247 }
248 }
249 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT16) {
250 intelInitDepthPointers_z16(&drb->Base);
251 }
252 else if (drb->Base.InternalFormat == GL_DEPTH_COMPONENT24) {
253 intelInitDepthPointers_z24_s8(&drb->Base);
254 }
255 else if (drb->Base.InternalFormat == GL_STENCIL_INDEX8_EXT) {
256 intelInitStencilPointers_z24_s8(&drb->Base);
257 }
258 }