Simplfy clear() and swapbuffers() code.
[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 "xf86drm.h"
31 #include <sys/ioctl.h>
32
33 GLboolean
34 via_alloc_draw_buffer(viaContextPtr vmesa, viaBuffer *buf)
35 {
36 drm_via_mem_t mem;
37 mem.context = vmesa->hHWContext;
38 mem.size = buf->size;
39 mem.type = VIDEO;
40
41 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &mem))
42 return GL_FALSE;
43
44
45 buf->offset = mem.offset;
46 buf->map = (char *)vmesa->driScreen->pFB + mem.offset;
47 buf->index = mem.index;
48 return GL_TRUE;
49 }
50
51 void
52 via_free_draw_buffer(viaContextPtr vmesa, viaBuffer *buf)
53 {
54 drm_via_mem_t mem;
55
56 if (!vmesa) return;
57
58 mem.context = vmesa->hHWContext;
59 mem.index = buf->index;
60 mem.type = VIDEO;
61 ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &mem);
62 buf->map = NULL;
63 }
64
65
66 GLboolean
67 via_alloc_dma_buffer(viaContextPtr vmesa)
68 {
69 drmVIADMAInit init;
70
71 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
72 vmesa->dma = (GLubyte *) malloc(VIA_DMA_BUFSIZ);
73
74 /*
75 * Check whether AGP DMA has been initialized.
76 */
77 init.func = VIA_DMA_INITIALIZED;
78 vmesa->useAgp =
79 ( 0 == drmCommandWrite(vmesa->driFd, DRM_VIA_DMA_INIT,
80 &init, sizeof(init)));
81 if (vmesa->useAgp)
82 printf("unichrome_dri.so: Using AGP.\n");
83 else
84 printf("unichrome_dri.so: Using PCI.\n");
85
86 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
87 return ((vmesa->dma) ? GL_TRUE : GL_FALSE);
88 }
89
90 void
91 via_free_dma_buffer(viaContextPtr vmesa)
92 {
93 if (!vmesa) return;
94 free(vmesa->dma);
95 vmesa->dma = 0;
96 }
97
98 GLboolean
99 via_alloc_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
100 {
101 drm_via_mem_t fb;
102 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
103 fb.context = vmesa->hHWContext;
104 fb.size = t->texMem.size;
105 fb.type = VIDEO;
106 if (VIA_DEBUG) {
107 fprintf(stderr, "texture size = %d\n", fb.size);
108 fprintf(stderr, "texture type = %d\n", fb.type);
109 }
110 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
111 fprintf(stderr, "via_alloc_texture fail\n");
112 return GL_FALSE;
113 }
114
115 t->texMem.offset = fb.offset;
116 t->texMem.index = fb.index;
117 if (VIA_DEBUG) fprintf(stderr, "texture index = %d\n", (GLuint)fb.index);
118
119 t->bufAddr = (unsigned char *)(fb.offset + (GLuint)vmesa->driScreen->pFB);
120 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
121 return GL_TRUE;
122 }
123 /*=* John Sheng [2003.5.31] agp tex *=*/
124 GLboolean
125 via_alloc_texture_agp(viaContextPtr vmesa, viaTextureObjectPtr t)
126 {
127 drm_via_mem_t fb;
128 if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
129 fb.context = vmesa->hHWContext;
130 fb.size = t->texMem.size;
131 fb.type = AGP;
132 if (VIA_DEBUG) {
133 fprintf(stderr, "texture_agp size = %d\n", fb.size);
134 fprintf(stderr, "texture type = %d\n", fb.type);
135 }
136 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_ALLOCMEM, &fb)) {
137 fprintf(stderr, "via_alloc_texture_agp fail\n");
138 return GL_FALSE;
139 }
140
141 t->texMem.offset = fb.offset;
142 t->texMem.index = fb.index;
143 if (VIA_DEBUG) fprintf(stderr, "texture agp index = %d\n", (GLuint)fb.index);
144
145 t->bufAddr = (unsigned char *)((GLuint)vmesa->viaScreen->agpLinearStart + fb.offset);
146 /*=* John Sheng [2003.5.31] agp tex *=*/
147 t->inAGP = GL_TRUE;
148 if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
149 return GL_TRUE;
150 }
151
152 void
153 via_free_texture(viaContextPtr vmesa, viaTextureObjectPtr t)
154 {
155 drm_via_mem_t fb;
156 if (VIA_DEBUG) {
157 fprintf(stderr, "via_free_texture: index = %d\n",
158 t->texMem.index);
159 fprintf(stderr, "via_free_texture: size = %d\n",
160 t->texMem.size);
161 }
162 if (!vmesa) {
163 fprintf(stderr, "!mesa\n");
164 return;
165 }
166
167 fb.context = vmesa->hHWContext;
168 fb.index = t->texMem.index;
169
170 /*=* John Sheng [2003.5.31] agp tex *=*/
171 if(t->inAGP)
172 fb.type = AGP;
173 else
174 fb.type = VIDEO;
175
176 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
177 if(vmesa->shareCtx) {
178 fb.context = ((viaContextPtr)((GLcontext *)(vmesa->shareCtx)->DriverCtx))->hHWContext;
179 if (ioctl(vmesa->driFd, DRM_IOCTL_VIA_FREEMEM, &fb)) {
180 fprintf(stderr, "via_free_texture fail\n");
181 }
182 }
183 else
184 fprintf(stderr, "via_free_texture fail\n");
185 }
186
187 t->bufAddr = NULL;
188 }