Fixed off by one errors in clipping.
[mesa.git] / src / mesa / drivers / dri / unichrome / via_fb.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 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, sub license,
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 and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <assert.h>
26
27 #include "via_context.h"
28 #include "via_ioctl.h"
29 #include "via_fb.h"
30 #include <sys/ioctl.h>
31
32 GLboolean
33 via_alloc_back_buffer(viaContextPtr vmesa)
34 {
35 drm_via_mem_t fb;
36 unsigned char *pFB;
37 #ifdef DEBUG
38 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
39 #endif
40 fb.context = vmesa->hHWContext;
41 fb.size = vmesa->back.size;
42 fb.type = VIDEO;
43 #ifdef DEBUG
44 if (VIA_DEBUG) fprintf(stderr, "context = %d, size =%d, type = %d\n", fb.context, fb.size, fb.type);
45 #endif
46 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb))
47 return GL_FALSE;
48
49 pFB = vmesa->driScreen->pFB;
50
51 vmesa->back.offset = fb.offset;
52 vmesa->back.map = (char *)(fb.offset + (GLuint)pFB);
53 vmesa->back.index = fb.index;
54 #ifdef DEBUG
55 if (VIA_DEBUG) {
56 fprintf(stderr, "back offset = %08x\n", vmesa->back.offset);
57 fprintf(stderr, "back index = %d\n", vmesa->back.index);
58 }
59
60 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
61 #endif
62 return GL_TRUE;
63 }
64
65 GLboolean
66 via_alloc_front_buffer(viaContextPtr vmesa)
67 {
68 drm_via_mem_t fb;
69 unsigned char *pFB;
70 #ifdef DEBUG
71 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
72 #endif
73 fb.context = vmesa->hHWContext;
74 fb.size = vmesa->back.size;
75 fb.type = VIDEO;
76 #ifdef DEBUG
77 if (VIA_DEBUG) fprintf(stderr, "context = %d, size =%d, type = %d\n", fb.context, fb.size, fb.type);
78 #endif
79 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb))
80 return GL_FALSE;
81
82 pFB = vmesa->driScreen->pFB;
83
84 vmesa->front.offset = fb.offset;
85 vmesa->front.map = (char *)(fb.offset + (GLuint)pFB);
86 vmesa->front.index = fb.index;
87 #ifdef DEBUG
88 if (VIA_DEBUG) {
89 fprintf(stderr, "front offset = %08x\n", vmesa->front.offset);
90 fprintf(stderr, "front index = %d\n", vmesa->front.index);
91 }
92
93
94 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
95 #endif
96 return GL_TRUE;
97 }
98
99 void
100 via_free_back_buffer(viaContextPtr vmesa)
101 {
102 drm_via_mem_t fb;
103
104 if (!vmesa) return;
105 fb.context = vmesa->hHWContext;
106 fb.index = vmesa->back.index;
107 fb.type = VIDEO;
108 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
109 vmesa->back.map = NULL;
110 }
111
112 void
113 via_free_front_buffer(viaContextPtr vmesa)
114 {
115 drm_via_mem_t fb;
116
117 if (!vmesa) return;
118 fb.context = vmesa->hHWContext;
119 fb.index = vmesa->front.index;
120 fb.type = VIDEO;
121 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
122 vmesa->front.map = NULL;
123 }
124
125 GLboolean
126 via_alloc_depth_buffer(viaContextPtr vmesa)
127 {
128 drm_via_mem_t fb;
129 unsigned char *pFB;
130 #ifdef DEBUG
131 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
132 #endif
133 fb.context = vmesa->hHWContext;
134 fb.size = vmesa->depth.size;
135 fb.type = VIDEO;
136
137 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
138 return GL_FALSE;
139 }
140
141 pFB = vmesa->driScreen->pFB;
142
143 vmesa->depth.offset = fb.offset;
144 vmesa->depth.map = (char *)(fb.offset + (GLuint)pFB);
145 vmesa->depth.index = fb.index;
146 #ifdef DEBUG
147 if (VIA_DEBUG) {
148 fprintf(stderr, "depth offset = %08x\n", vmesa->depth.offset);
149 fprintf(stderr, "depth index = %d\n", vmesa->depth.index);
150 }
151
152 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
153 #endif
154 return GL_TRUE;
155 }
156
157 void
158 via_free_depth_buffer(viaContextPtr vmesa)
159 {
160 drm_via_mem_t fb;
161
162 if (!vmesa) return;
163 fb.context = vmesa->hHWContext;
164 fb.index = vmesa->depth.index;
165 fb.type = VIDEO;
166 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
167 vmesa->depth.map = NULL;
168 }
169
170 GLboolean
171 via_alloc_dma_buffer(viaContextPtr vmesa)
172 {
173 drm_via_mem_t fb;
174 drmVIADMABuf dma;
175 #ifdef DEBUG
176 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
177 #endif
178 if (vmesa->viaScreen->agpLinearStart) {
179 /* Allocate DMA in AGP memory*/
180 fb.context = vmesa->hHWContext;
181 fb.size = vmesa->dma[0].size;
182 fb.type = AGP;
183 if (!ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
184 vmesa->dma[0].offset = fb.offset;
185 vmesa->dma[0].index = fb.index;
186 vmesa->dma[0].map = (unsigned char *)((GLuint)vmesa->viaScreen->agpLinearStart + fb.offset);
187 if (!ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
188 vmesa->dma[1].offset = fb.offset;
189 vmesa->dma[1].index = fb.index;
190 vmesa->dma[1].map = (unsigned char *)((GLuint)vmesa->viaScreen->agpLinearStart + fb.offset);
191 vmesa->useAgp = GL_TRUE;
192
193 return GL_TRUE;
194 }
195 else {
196 /* release dma[0]*/
197 return GL_FALSE;
198 }
199 }
200 return GL_FALSE;
201 }
202 else {
203 /* Allocate DMA in System memory */
204 dma.size = vmesa->dma[0].size;
205
206 if (drmVIAAllocateDMA(vmesa->driFd,&dma) < 0) {
207 return GL_FALSE;
208 }
209
210 vmesa->dma[0].offset = 0;
211 vmesa->dma[0].map = (unsigned char *)dma.address;
212 vmesa->dma[0].index = dma.index;
213
214 drmVIAAllocateDMA(vmesa->driFd, &dma);
215
216 vmesa->dma[1].offset = 0;
217 vmesa->dma[1].map = (unsigned char *)dma.address;
218 vmesa->dma[1].index = dma.index;
219 vmesa->useAgp = GL_FALSE;
220
221 return GL_TRUE;
222 }
223 #ifdef DEBUG
224 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
225 #endif
226 }
227
228 void
229 via_free_dma_buffer(viaContextPtr vmesa)
230 {
231 drmVIADMABuf dma;
232 drm_via_mem_t fb;
233
234
235 if (!vmesa) return;
236
237 /* Release AGP command buffer */
238 if (vmesa->useAgp) {
239 fb.context = vmesa->hHWContext;
240 fb.index = vmesa->dma[0].index;
241 fb.type = AGP;
242 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
243 vmesa->dma[0].map = NULL;
244 fb.index = vmesa->dma[1].index;
245 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb);
246 vmesa->dma[1].map = NULL;
247 }
248 /* Release System command buffer */
249 else {
250 /*=* John Sheng [2003.7.18] viewperf frames/sec *=*/
251 /*dma.address = (unsigned long *)vmesa->dma[0].offset;*/
252 dma.address = (unsigned long *)vmesa->dma[0].map;
253 /*=* John Sheng [2003.6.16] fix pci path *=*/
254 dma.size = (unsigned int)vmesa->dma[0].size;
255 drmVIAReleaseDMA(vmesa->driFd, &dma);
256 /*=* John Sheng [2003.7.18] viewperf frames/sec *=*/
257 /*dma.address = (unsigned long *)vmesa->dma[1].offset;*/
258 dma.address = (unsigned long *)vmesa->dma[1].map;
259 /*=* John Sheng [2003.6.16] fix pci path *=*/
260 dma.size = (unsigned int)vmesa->dma[1].size;
261 drmVIAReleaseDMA(vmesa->driFd, &dma);
262 /*=* John Sheng [2003.7.18] viewperf frames/sec *=*/
263 /*vmesa->dma[0].offset = 0;
264 vmesa->dma[1].offset = 0;*/
265 vmesa->dma[0].map = 0;
266 vmesa->dma[1].map = 0;
267 }
268 }
269
270 GLboolean
271 via_alloc_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
272 {
273 drm_via_mem_t fb;
274 #ifdef DEBUG
275 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
276 #endif
277 fb.context = vmesa->hHWContext;
278 fb.size = t->texMem.size;
279 fb.type = VIDEO;
280 #ifdef DEBUG
281 if (VIA_DEBUG) {
282 fprintf(stderr, "texture size = %d\n", fb.size);
283 fprintf(stderr, "texture type = %d\n", fb.type);
284 }
285 #endif
286 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
287 fprintf(stderr, "via_alloc_texture fail\n");
288 return GL_FALSE;
289 }
290
291 t->texMem.offset = fb.offset;
292 t->texMem.index = fb.index;
293 #ifdef DEBUG
294 if (VIA_DEBUG) fprintf(stderr, "texture index = %d\n", (GLuint)fb.index);
295 #endif
296
297 t->bufAddr = (unsigned char *)(fb.offset + (GLuint)vmesa->driScreen->pFB);
298 #ifdef DEBUG
299 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
300 #endif
301 return GL_TRUE;
302 }
303 /*=* John Sheng [2003.5.31] agp tex *=*/
304 GLboolean
305 via_alloc_texture_agp(viaContextPtr vmesa, viaTextureObjectPtr t)
306 {
307 drm_via_mem_t fb;
308 #ifdef DEBUG
309 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
310 #endif
311 fb.context = vmesa->hHWContext;
312 fb.size = t->texMem.size;
313 fb.type = AGP;
314 #ifdef DEBUG
315 if (VIA_DEBUG) {
316 fprintf(stderr, "texture_agp size = %d\n", fb.size);
317 fprintf(stderr, "texture type = %d\n", fb.type);
318 }
319 #endif
320 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
321 fprintf(stderr, "via_alloc_texture_agp fail\n");
322 return GL_FALSE;
323 }
324
325 t->texMem.offset = fb.offset;
326 t->texMem.index = fb.index;
327 #ifdef DEBUG
328 if (VIA_DEBUG) fprintf(stderr, "texture agp index = %d\n", (GLuint)fb.index);
329 #endif
330
331 t->bufAddr = (unsigned char *)((GLuint)vmesa->viaScreen->agpLinearStart + fb.offset);
332 /*=* John Sheng [2003.5.31] agp tex *=*/
333 t->inAGP = GL_TRUE;
334 #ifdef DEBUG
335 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
336 #endif
337 return GL_TRUE;
338 }
339
340 void
341 via_free_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
342 {
343 drm_via_mem_t fb;
344 #ifdef DEBUG
345 if (VIA_DEBUG) {
346 fprintf(stderr, "via_free_texture: index = %d\n",
347 t->texMem.index);
348 fprintf(stderr, "via_free_texture: size = %d\n",
349 t->texMem.size);
350 }
351 #endif
352 if (!vmesa) {
353 fprintf(stderr, "!mesa\n");
354 return;
355 }
356
357 fb.context = vmesa->hHWContext;
358 fb.index = t->texMem.index;
359
360 /*=* John Sheng [2003.5.31] agp tex *=*/
361 if(t->inAGP)
362 fb.type = AGP;
363 else
364 fb.type = VIDEO;
365
366 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
367 if(vmesa->shareCtx) {
368 fb.context = ((viaContextPtr)((GLcontext *)(vmesa->shareCtx)->DriverCtx))->hHWContext;
369 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
370 fprintf(stderr, "via_free_texture fail\n");
371 }
372 }
373 else
374 fprintf(stderr, "via_free_texture fail\n");
375 }
376
377 t->bufAddr = NULL;
378 }