summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/unichrome/via_texmem.c
blob: 3d0ff920f3541531b05361a50bfdcd34dc7e30a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */


#include <stdlib.h>
#include <stdio.h>

#include "glheader.h"
#include "macros.h"
#include "mtypes.h"
#include "simple_list.h"
#include "enums.h"
#include "texformat.h"

#include "mm.h"
#include "via_context.h"
#include "via_tex.h"
#include "via_state.h"
#include "via_ioctl.h"
#include "via_fb.h"
/*=* John Sheng [2003.5.31]  agp tex *=*/

void viaDestroyTexObj(viaContextPtr vmesa, viaTextureObjectPtr t)
{
    if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
    if (!t) 
	return;

    /* This is sad - need to sync *in case* we upload a texture
     * to this newly free memory...
     */
    if (t->bufAddr) {
	via_free_texture(vmesa, t);

        if (vmesa && t->age > vmesa->dirtyAge)
            vmesa->dirtyAge = t->age;
    }

    if (t->globj)
        t->globj->DriverData = 0;

    if (vmesa) {
        if (vmesa->CurrentTexObj[0] == t) {
            vmesa->CurrentTexObj[0] = 0;
        }

        if (vmesa->CurrentTexObj[1] == t) {
            vmesa->CurrentTexObj[1] = 0;
        }
    }

    remove_from_list(t);
    free(t);
    if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
}

void viaSwapOutTexObj(viaContextPtr vmesa, viaTextureObjectPtr t)
{
    if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
    if (t->bufAddr) {
	via_free_texture(vmesa, t);

        if (t->age > vmesa->dirtyAge)
            vmesa->dirtyAge = t->age;
    }

    t->dirtyImages = ~0;
    move_to_tail(&(vmesa->SwappedOut), t);
    if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
}

/* Upload an image from mesa's internal copy.
 */
static void viaUploadTexLevel(viaTextureObjectPtr t, int level)
{
    const struct gl_texture_image *image = t->image[level].image;
    int i, j;
    if (VIA_DEBUG) {
	fprintf(stderr, "%s - in\n", __FUNCTION__);    
	fprintf(stderr, "width = %d, height = %d \n", image->Width, image->Height);    
    }	
    switch (t->image[level].internalFormat) {
    case GL_RGB:
    {
	if (image->TexFormat->MesaFormat == MESA_FORMAT_ARGB8888) {
	    GLuint *dst = (GLuint *)(t->bufAddr + t->image[level].offset);
	    GLuint *src = (GLuint *)image->Data;
	    if (VIA_DEBUG) fprintf(stderr, "GL_RGB MESA_FORMAT_ARGB8888\n");    
	    if (image->Width < 8) {
		 for (i = 0; i < image->Height ; i++) {
		    for (j = 0; j < image->Width ; j++) {
			dst[j] = *src;
			src++;
		    }
		    dst += 8;
		}
	    }
	    else {
		for (j = 0; j < image->Height * image->Width; j++) {
        	    *dst = *src;
		    dst++;
        	    src++;
    		}
	    }
	    /*memcpy(dst, src, image->Height * image->Width * sizeof(GLuint));*/
	}
	else {
	    GLushort *dst = (GLushort *)(t->bufAddr + t->image[level].offset);
	    GLushort *src = (GLushort *)image->Data;
	    if (VIA_DEBUG) fprintf(stderr, "GL_RGB !MESA_FORMAT_ARGB8888\n");    
	    if (image->Width < 16) {
		 for (i = 0; i < image->Height ; i++) {
		    for (j = 0; j < image->Width ; j++) {
			dst[j] = *src;
			src++;
		    }
		    dst += 16;
		}
	    }
	    else {
		for (j = 0; j < image->Height * image->Width; j++) {
        	    *dst = *src;
		    dst++;
        	    src++;
    		}
	    }
	    /*memcpy(dst, src, image->Height * image->Width * sizeof(GLushort));*/
	}
    }
    break;

    case GL_RGBA:
    {
        if (image->TexFormat->MesaFormat == MESA_FORMAT_ARGB4444) {    

	    GLushort *dst = (GLushort *)(t->bufAddr + t->image[level].offset);
            GLushort *src = (GLushort *)image->Data;
	    if (image->Width < 16) {
		 for (i = 0; i < image->Height ; i++) {
		    for (j = 0; j < image->Width ; j++) {
			dst[j] = *src;
			src++;
		    }
		    dst += 16;
		}
	    }
	    else {
    		for (j = 0; j < image->Height * image->Width; j++) {
            	        *dst = *src; 
            		src++;
			dst++;
        	}
	    }
	    /*memcpy(dst, src, image->Height * image->Width * sizeof(GLushort));*/
	    if (VIA_DEBUG) fprintf(stderr, "GL_RGBA MESA_FORMAT_ARGB4444\n");    
        }
	else if(image->TexFormat->MesaFormat == MESA_FORMAT_ARGB8888) {
            GLuint *dst = (GLuint *)(t->bufAddr + t->image[level].offset);
            GLuint *src = (GLuint *)image->Data;
	    if (VIA_DEBUG) fprintf(stderr, "GL_RGBA !MESA_FORMAT_ARGB4444\n");    
	    if (image->Width < 8) {
		 for (i = 0; i < image->Height ; i++) {
		    for (j = 0; j < image->Width ; j++) {
			dst[j] = *src;
			src++;
		    }
		    dst += 8;
		}
	    }
	    else {
        	for (j = 0; j < image->Height * image->Width; j++) {
            	    *dst = *src;
            	    dst++;
            	    src++;
        	}
	    }
	    /*memcpy(dst, src, image->Height * image->Width * sizeof(GLuint));*/
	}
	else if(image->TexFormat->MesaFormat == MESA_FORMAT_ARGB1555) {
	    GLushort *dst = (GLushort *)(t->bufAddr + t->image[level].offset);
            GLushort *src = (GLushort *)image->Data;
	    if (image->Width < 16) {
		 for (i = 0; i < image->Height ; i++) {
		    for (j = 0; j < image->Width ; j++) {
			dst[j] = *src;
			src++;
		    }
		    dst += 16;
		}
	    }
	    else {
    		for (j = 0; j < image->Height * image->Width; j++) {
            	        *dst = *src; 
            		src++;
			dst++;
        	}
	    }
	    /*memcpy(dst, src, image->Height * image->Width * sizeof(GLushort));*/
	    if (VIA_DEBUG) fprintf(stderr, "GL_RGBA MESA_FORMAT_ARGB1555\n");    
        }
    }
    break;

    case GL_LUMINANCE:
    {
        GLubyte *dst = (GLubyte *)(t->bufAddr + t->image[level].offset);
        GLubyte *src = (GLubyte *)image->Data;

        for (j = 0; j < image->Height * image->Width; j++) {
            *dst = *src;
	    dst++;
            src++;
        }
    }
    break;

    case GL_INTENSITY:
    {
        GLubyte *dst = (GLubyte *)(t->bufAddr + t->image[level].offset);
        GLubyte *src = (GLubyte *)image->Data;

        for (j = 0; j < image->Height * image->Width; j++) {
            *dst = *src;
	    dst++;
            src++;
        }
    }
    break;

    case GL_LUMINANCE_ALPHA:
    {
        GLushort *dst = (GLushort *)(t->bufAddr + t->image[level].offset);
        GLushort *src = (GLushort *)image->Data;

        for (j = 0; j < image->Height * image->Width; j++) {
            *dst = *src;
            dst++;
            src++;
        }
    }
    break;

    case GL_ALPHA:
    {
        GLubyte *dst = (GLubyte *)(t->bufAddr + t->image[level].offset);
        GLubyte *src = (GLubyte *)image->Data;

        for (j = 0; j < image->Height * image->Width; j++) {
            *dst = *src;
            dst++;    
            src++;
        }
    }
    break;

    /* TODO: Translate color indices *now*:
     */
    case GL_COLOR_INDEX:
    {
        GLubyte *dst = (GLubyte *)(t->bufAddr + t->image[level].offset);
        GLubyte *src = (GLubyte *)image->Data;

        for (j = 0; j < image->Height * image->Width; j++) {
            *dst = *src;
            dst++;
            src++;
        }
    }
    break;

    default:;
        if (VIA_DEBUG) fprintf(stderr, "Not supported texture format %s\n",
                _mesa_lookup_enum_by_nr(image->Format));
    }
    if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
}

void viaPrintLocalLRU(viaContextPtr vmesa)
{
    viaTextureObjectPtr t;

    foreach (t, &vmesa->TexObjList) {
        if (!t->globj) {
            if (VIA_DEBUG) {
		fprintf(stderr, "offset = %x, index = %x, size = %x\n",
                    t->texMem.offset,
                    t->texMem.index,
                    t->texMem.size);
	    }
	    else {
        	if (VIA_DEBUG) {
		    fprintf(stderr, "offset = %x, siez = %x\n",
                	t->texMem.offset,
                    t->texMem.size);
		}
	    }
	}	    	    
    }
}

void viaPrintGlobalLRU(viaContextPtr vmesa)
{
    int i, j;
    drm_via_tex_region_t *list = vmesa->sarea->texList;

    for (i = 0, j = VIA_NR_TEX_REGIONS; i < VIA_NR_TEX_REGIONS; i++) {
        if (VIA_DEBUG) fprintf(stderr, "list[%d] age %d next %d prev %d\n",
                j, list[j].age, list[j].next, list[j].prev);
        j = list[j].next;
        if (j == VIA_NR_TEX_REGIONS) break;
    }
    if (j != VIA_NR_TEX_REGIONS)
       if (VIA_DEBUG) fprintf(stderr, "Loop detected in global LRU\n");
}

void viaResetGlobalLRU(viaContextPtr vmesa)
{
    drm_via_tex_region_t *list = vmesa->sarea->texList;
    int sz = 1 << vmesa->viaScreen->logTextureGranularity;
    int i;

    /* (Re)initialize the global circular LRU list.  The last element
     * in the array (VIA_NR_TEX_REGIONS) is the sentinal.  Keeping it
     * at the end of the array allows it to be addressed rationally
     * when looking up objects at a particular location in texture
     * memory.
     */
    for (i = 0; (i + 1) * sz <= vmesa->viaScreen->textureSize; i++) {
        list[i].prev = i - 1;
        list[i].next = i + 1;
        list[i].age = 0;
    }

    i--;
    list[0].prev = VIA_NR_TEX_REGIONS;
    list[i].prev = i - 1;
    list[i].next = VIA_NR_TEX_REGIONS;
    list[VIA_NR_TEX_REGIONS].prev = i;
    list[VIA_NR_TEX_REGIONS].next = 0;
    vmesa->sarea->texAge = 0;
}

void viaUpdateTexLRU(viaContextPtr vmesa, viaTextureObjectPtr t)
{
    vmesa->texAge = ++vmesa->sarea->texAge;
    move_to_head(&(vmesa->TexObjList), t);
}

/* Called for every shared texture region which has increased in age
 * since we last held the lock.
 *
 * Figures out which of our textures have been ejected by other clients,
 * and pushes a placeholder texture onto the LRU list to represent
 * the other client's textures.
 */
void viaTexturesGone(viaContextPtr vmesa,
                     GLuint offset,
                     GLuint size,
                     GLuint inUse)
{
    viaTextureObjectPtr t, tmp;
    if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);    
    foreach_s (t, tmp, &vmesa->TexObjList) {
        viaSwapOutTexObj(vmesa, t);
    }
    if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
}

/* This is called with the lock held.  May have to eject our own and/or
 * other client's texture objects to make room for the upload.
 */
void viaUploadTexImages(viaContextPtr vmesa, viaTextureObjectPtr t)
{
    int i, j;
    int numLevels;
    if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
    LOCK_HARDWARE(vmesa);

     j = 0;
    if (!t->bufAddr) {
        while (1) {

    	    /*=* John Sheng [2003.5.31]  agp tex *=*/
	    if (via_alloc_texture_agp(vmesa, t))
		break;
	    if (via_alloc_texture(vmesa, t))
		break;

	    vmesa->agpFullCount++; 
            if (vmesa->TexObjList.prev == vmesa->CurrentTexObj[0] ||
                vmesa->TexObjList.prev == vmesa->CurrentTexObj[1]) {
                if (VIA_DEBUG) fprintf(stderr, "Hit bound texture in upload\n");
                viaPrintLocalLRU(vmesa);
                UNLOCK_HARDWARE(vmesa);
                return;
            }

            if (vmesa->TexObjList.prev == &(vmesa->TexObjList)) {
                if (VIA_DEBUG) fprintf(stderr, "Failed to upload texture, sz %d\n", t->totalSize);
                mmDumpMemInfo(vmesa->texHeap);
                UNLOCK_HARDWARE(vmesa);
                return;
            }

            viaSwapOutTexObj(vmesa, vmesa->TexObjList.prev);
        }
	/*=* John Sheng [2003.5.31]  agp tex *=*/
        /*t->bufAddr = (char *)((GLuint)vmesa->driScreen->pFB + t->texMem.offset);*/

        if (t == vmesa->CurrentTexObj[0])
            VIA_FLUSH_DMA(vmesa);

        if (t == vmesa->CurrentTexObj[1])
            VIA_FLUSH_DMA(vmesa);

        viaUpdateTexLRU(vmesa, t);
	
	j++;
    }

    numLevels = t->lastLevel - t->firstLevel + 1;

    for (i = 0; i < numLevels; i++)
        if (t->dirtyImages & (1 << i))
            viaUploadTexLevel(t, i);

    t->dirtyImages = 0;

    UNLOCK_HARDWARE(vmesa);
    if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);    
}