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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
/*
* 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 *=*/
GLuint agpFullCount = 0;
void viaDestroyTexObj(viaContextPtr vmesa, viaTextureObjectPtr t)
{
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
#endif
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;
vmesa->dirty &= ~VIA_UPLOAD_TEX0;
}
if (vmesa->CurrentTexObj[1] == t) {
vmesa->CurrentTexObj[1] = 0;
vmesa->dirty &= ~VIA_UPLOAD_TEX1;
}
}
remove_from_list(t);
free(t);
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
#endif
}
void viaSwapOutTexObj(viaContextPtr vmesa, viaTextureObjectPtr t)
{
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
#endif
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);
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
#endif
}
/* 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;
#ifdef DEBUG
if (VIA_DEBUG) {
fprintf(stderr, "%s - in\n", __FUNCTION__);
fprintf(stderr, "width = %d, height = %d \n", image->Width, image->Height);
}
#endif
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;
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "GL_RGB MESA_FORMAT_ARGB8888\n");
#endif
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;
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "GL_RGB !MESA_FORMAT_ARGB8888\n");
#endif
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));*/
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "GL_RGBA MESA_FORMAT_ARGB4444\n");
#endif
}
else if(image->TexFormat->MesaFormat == MESA_FORMAT_ARGB8888) {
GLuint *dst = (GLuint *)(t->bufAddr + t->image[level].offset);
GLuint *src = (GLuint *)image->Data;
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "GL_RGBA !MESA_FORMAT_ARGB4444\n");
#endif
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));*/
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "GL_RGBA MESA_FORMAT_ARGB1555\n");
#endif
}
}
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:;
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "Not supported texture format %s\n",
_mesa_lookup_enum_by_nr(image->Format));
#endif
}
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
#endif
}
void viaPrintLocalLRU(viaContextPtr vmesa)
{
viaTextureObjectPtr t;
foreach (t, &vmesa->TexObjList) {
if (!t->globj) {
#ifdef DEBUG
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);
}
}
#endif
}
}
}
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++) {
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "list[%d] age %d next %d prev %d\n",
j, list[j].age, list[j].next, list[j].prev);
#endif
j = list[j].next;
if (j == VIA_NR_TEX_REGIONS) break;
}
#ifdef DEBUG
if (j != VIA_NR_TEX_REGIONS)
if (VIA_DEBUG) fprintf(stderr, "Loop detected in global LRU\n");
#endif
}
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;
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
#endif
foreach_s (t, tmp, &vmesa->TexObjList) {
viaSwapOutTexObj(vmesa, t);
}
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
#endif
}
/* 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;
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - in\n", __FUNCTION__);
#endif
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;
agpFullCount++;
if (vmesa->TexObjList.prev == vmesa->CurrentTexObj[0] ||
vmesa->TexObjList.prev == vmesa->CurrentTexObj[1]) {
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "Hit bound texture in upload\n");
#endif
viaPrintLocalLRU(vmesa);
UNLOCK_HARDWARE(vmesa);
return;
}
if (vmesa->TexObjList.prev == &(vmesa->TexObjList)) {
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "Failed to upload texture, sz %d\n", t->totalSize);
#endif
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_STATECHANGE(vmesa, VIA_UPLOAD_TEX0);
if (t == vmesa->CurrentTexObj[1])
VIA_STATECHANGE(vmesa, VIA_UPLOAD_TEX1);
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);
#ifdef DEBUG
if (VIA_DEBUG) fprintf(stderr, "%s - out\n", __FUNCTION__);
#endif
}
|