Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / drivers / dri / i810 / i810texmem.c
1 /*
2 * GLX Hardware Device Driver for Intel i810
3 * Copyright (C) 1999 Keith Whitwell
4 * Texmem interface changes (C) 2003 Dave Airlie
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include "main/glheader.h"
27 #include "main/macros.h"
28 #include "main/mtypes.h"
29 #include "main/simple_list.h"
30 #include "main/enums.h"
31 #include "main/colormac.h"
32 #include "main/mm.h"
33
34 #include "i810screen.h"
35 #include "i810_dri.h"
36 #include "i810context.h"
37 #include "i810tex.h"
38 #include "i810state.h"
39 #include "i810ioctl.h"
40
41
42 void i810DestroyTexObj(i810ContextPtr imesa, i810TextureObjectPtr t)
43 {
44 /* See if it was the driver's current object.
45 */
46 if ( imesa != NULL ) {
47 if (imesa->CurrentTexObj[0] == t) {
48 imesa->CurrentTexObj[0] = 0;
49 imesa->dirty &= ~I810_UPLOAD_TEX0;
50 }
51
52 if (imesa->CurrentTexObj[1] == t) {
53 imesa->CurrentTexObj[1] = 0;
54 imesa->dirty &= ~I810_UPLOAD_TEX1;
55 }
56 }
57 }
58
59
60
61 #if defined(i386) || defined(__i386__)
62 /* From linux kernel i386 header files, copes with odd sizes better
63 * than COPY_DWORDS would:
64 */
65 static INLINE void * __memcpy(void * to, const void * from, size_t n)
66 {
67 int d0, d1, d2;
68 __asm__ __volatile__(
69 "rep ; movsl\n\t"
70 "testb $2,%b4\n\t"
71 "je 1f\n\t"
72 "movsw\n"
73 "1:\ttestb $1,%b4\n\t"
74 "je 2f\n\t"
75 "movsb\n"
76 "2:"
77 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
78 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
79 : "memory");
80 return (to);
81 }
82 #else
83 /* Allow compilation on other architectures */
84 #define __memcpy memcpy
85 #endif
86
87 /* Upload an image from mesa's internal copy.
88 */
89 static void i810UploadTexLevel( i810ContextPtr imesa,
90 i810TextureObjectPtr t, int hwlevel )
91 {
92 const struct gl_texture_image *image = t->image[hwlevel].image;
93 int j;
94 GLuint texelBytes;
95
96 if (!image || !image->Data)
97 return;
98
99 texelBytes = _mesa_get_format_bytes(image->TexFormat);
100
101 if (image->Width * texelBytes == t->Pitch) {
102 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[hwlevel].offset);
103 GLubyte *src = (GLubyte *)image->Data;
104
105 memcpy( dst, src, t->Pitch * image->Height );
106 }
107 else {
108 switch (texelBytes) {
109 case 1:
110 {
111 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[hwlevel].offset);
112 GLubyte *src = (GLubyte *)image->Data;
113
114 for (j = 0 ; j < image->Height ; j++, dst += t->Pitch) {
115 __memcpy(dst, src, image->Width );
116 src += image->Width;
117 }
118 }
119 break;
120 case 2:
121 {
122 GLushort *dst = (GLushort *)(t->BufAddr + t->image[hwlevel].offset);
123 GLushort *src = (GLushort *)image->Data;
124
125 for (j = 0 ; j < image->Height ; j++, dst += (t->Pitch/2)) {
126 __memcpy(dst, src, image->Width * 2 );
127 src += image->Width;
128 }
129 }
130 break;
131 default:
132 fprintf(stderr, "%s: Not supported texel size %d\n",
133 __FUNCTION__, texelBytes);
134 }
135 }
136 }
137
138 /* This is called with the lock held. May have to eject our own and/or
139 * other client's texture objects to make room for the upload.
140 */
141 int i810UploadTexImagesLocked( i810ContextPtr imesa, i810TextureObjectPtr t )
142 {
143 int i;
144 int ofs;
145 int numLevels;
146
147 /* Do we need to eject LRU texture objects?
148 */
149 if (!t->base.memBlock) {
150 int heap;
151
152 heap = driAllocateTexture( imesa->texture_heaps, imesa->nr_heaps,
153 (driTextureObject *) t);
154
155 if ( heap == -1 ) {
156 return -1;
157 }
158
159 ofs = t->base.memBlock->ofs;
160 t->BufAddr = imesa->i810Screen->tex.map + ofs;
161 t->Setup[I810_TEXREG_MI3] = imesa->i810Screen->textureOffset + ofs;
162
163 if (t == imesa->CurrentTexObj[0])
164 I810_STATECHANGE(imesa, I810_UPLOAD_TEX0);
165
166 if (t == imesa->CurrentTexObj[1])
167 I810_STATECHANGE(imesa, I810_UPLOAD_TEX1);
168
169 /* i810UpdateTexLRU( imesa, t );*/
170 }
171 driUpdateTextureLRU( (driTextureObject *) t );
172
173 if (imesa->texture_heaps[0]->timestamp >= GET_DISPATCH_AGE(imesa))
174 i810WaitAgeLocked( imesa, imesa->texture_heaps[0]->timestamp );
175
176 numLevels = t->base.lastLevel - t->base.firstLevel + 1;
177 for (i = 0 ; i < numLevels ; i++)
178 if (t->base.dirty_images[0] & (1<<i))
179 i810UploadTexLevel( imesa, t, i );
180
181 t->base.dirty_images[0] = 0;
182
183 return 0;
184 }