summaryrefslogtreecommitdiff
path: root/indra/newview/llviewertexture.cpp
blob: a2f6b70006a4975819c3f2ffad79df112817b8b9 (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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
/** 
 * @file llviewertexture.cpp
 * @brief Object which handles a received image (and associated texture(s))
 *
 * $LicenseInfo:firstyear=2000&license=viewergpl$
 * 
 * Copyright (c) 2000-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llviewertexture.h"

// Library includes
#include "imageids.h"
#include "llmath.h"
#include "llerror.h"
#include "llgl.h"
#include "llglheaders.h"
#include "llhost.h"
#include "llimage.h"
#include "llimagebmp.h"
#include "llimagej2c.h"
#include "llimagetga.h"
#include "llmemtype.h"
#include "llstl.h"
#include "llvfile.h"
#include "llvfs.h"
#include "message.h"
#include "lltimer.h"

// viewer includes
#include "llimagegl.h"
#include "lldrawpool.h"
#include "lltexturefetch.h"
#include "llviewertexturelist.h"
#include "llviewercontrol.h"
#include "pipeline.h"
#include "llappviewer.h"
#include "lltextureatlas.h"
#include "lltextureatlasmanager.h"
#include "lltextureentry.h"
#include "llmediaentry.h"
#include "llvovolume.h"
#include "llviewermedia.h"
///////////////////////////////////////////////////////////////////////////////

// statics
LLPointer<LLViewerTexture>        LLViewerTexture::sNullImagep = NULL;
LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sMissingAssetImagep = NULL;
LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sWhiteImagep = NULL;
LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sDefaultImagep = NULL;
LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sSmokeImagep = NULL;
LLViewerMediaTexture::media_map_t LLViewerMediaTexture::sMediaMap ;
LLTexturePipelineTester* LLViewerTextureManager::sTesterp = NULL ;

S32 LLViewerTexture::sImageCount = 0;
S32 LLViewerTexture::sRawCount = 0;
S32 LLViewerTexture::sAuxCount = 0;
LLTimer LLViewerTexture::sEvaluationTimer;
F32 LLViewerTexture::sDesiredDiscardBias = 0.f;
F32 LLViewerTexture::sDesiredDiscardScale = 1.1f;
S32 LLViewerTexture::sBoundTextureMemoryInBytes = 0;
S32 LLViewerTexture::sTotalTextureMemoryInBytes = 0;
S32 LLViewerTexture::sMaxBoundTextureMemInMegaBytes = 0;
S32 LLViewerTexture::sMaxTotalTextureMemInMegaBytes = 0;
S32 LLViewerTexture::sMaxDesiredTextureMemInBytes = 0 ;
BOOL LLViewerTexture::sDontLoadVolumeTextures = FALSE;
BOOL LLViewerTexture::sUseTextureAtlas        = FALSE ;

const F32 desired_discard_bias_min = -2.0f; // -max number of levels to improve image quality by
const F32 desired_discard_bias_max = 1.5f; // max number of levels to reduce image quality by

//----------------------------------------------------------------------------------------------
//namespace: LLViewerTextureAccess
//----------------------------------------------------------------------------------------------
LLViewerMediaTexture* LLViewerTextureManager::createMediaTexture(const LLUUID &media_id, BOOL usemipmaps, LLImageGL* gl_image)
{
	return new LLViewerMediaTexture(media_id, usemipmaps, gl_image) ;		
}
 
LLViewerTexture*  LLViewerTextureManager::findTexture(const LLUUID& id) 
{
	LLViewerTexture* tex ;
	//search fetched texture list
	tex = gTextureList.findImage(id) ;
	
	//search media texture list
	if(!tex)
	{
		tex = LLViewerTextureManager::findMediaTexture(id) ;
	}
	return tex ;
}
		
LLViewerMediaTexture* LLViewerTextureManager::findMediaTexture(const LLUUID &media_id)
{
	return LLViewerMediaTexture::findMediaTexture(media_id) ;	
}

LLViewerMediaTexture*  LLViewerTextureManager::getMediaTexture(const LLUUID& id, BOOL usemipmaps, LLImageGL* gl_image) 
{
	LLViewerMediaTexture* tex = LLViewerMediaTexture::findMediaTexture(id) ;	
	if(!tex)
	{
		tex = LLViewerTextureManager::createMediaTexture(id, usemipmaps, gl_image) ;
	}

	tex->initVirtualSize() ;

	return tex ;
}

LLViewerFetchedTexture* LLViewerTextureManager::staticCastToFetchedTexture(LLViewerTexture* tex, BOOL report_error)
{
	if(!tex)
	{
		return NULL ;
	}

	S8 type = tex->getType() ;
	if(type == LLViewerTexture::FETCHED_TEXTURE || type == LLViewerTexture::LOD_TEXTURE)
	{
		return static_cast<LLViewerFetchedTexture*>(tex) ;
	}

	if(report_error)
	{
		llerrs << "not a fetched texture type: " << type << llendl ;
	}

	return NULL ;
}

LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(BOOL usemipmaps, BOOL generate_gl_tex)
{
	LLPointer<LLViewerTexture> tex = new LLViewerTexture(usemipmaps) ;
	if(generate_gl_tex)
	{
		tex->generateGLTexture() ;
	}
	return tex ;
}
LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const LLUUID& id, BOOL usemipmaps, BOOL generate_gl_tex) 
{
	LLPointer<LLViewerTexture> tex = new LLViewerTexture(id, usemipmaps) ;
	if(generate_gl_tex)
	{
		tex->generateGLTexture() ;
	}
	return tex ;
}
LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const LLImageRaw* raw, BOOL usemipmaps) 
{
	LLPointer<LLViewerTexture> tex = new LLViewerTexture(raw, usemipmaps) ;
	return tex ;
}
LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const U32 width, const U32 height, const U8 components, BOOL usemipmaps, BOOL generate_gl_tex) 
{
	LLPointer<LLViewerTexture> tex = new LLViewerTexture(width, height, components, usemipmaps) ;
	if(generate_gl_tex)
	{
		tex->generateGLTexture() ;
	}
	return tex ;
}

LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTexture(
	                                               const LLUUID &image_id,											       
												   BOOL usemipmaps,
												   BOOL level_immediate,
												   S8 texture_type,
												   LLGLint internal_format,
												   LLGLenum primary_format,
												   LLHost request_from_host)
{
	return gTextureList.getImage(image_id, usemipmaps, level_immediate, texture_type, internal_format, primary_format, request_from_host) ;
}
	
LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTextureFromFile(
	                                               const std::string& filename,												   
												   BOOL usemipmaps,
												   BOOL level_immediate,
												   S8 texture_type,
												   LLGLint internal_format,
												   LLGLenum primary_format, 
												   const LLUUID& force_id)
{
	return gTextureList.getImageFromFile(filename, usemipmaps, level_immediate, texture_type, internal_format, primary_format, force_id) ;
}

LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTextureFromHost(const LLUUID& image_id, LLHost host) 
{
	return gTextureList.getImageFromHost(image_id, host) ;
}

void LLViewerTextureManager::init()
{
	LLPointer<LLImageRaw> raw = new LLImageRaw(1,1,3);
	raw->clear(0x77, 0x77, 0x77, 0xFF);
	LLViewerTexture::sNullImagep = LLViewerTextureManager::getLocalTexture(raw.get(), TRUE) ;

#if 1
	LLPointer<LLViewerFetchedTexture> imagep = LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT);
	LLViewerFetchedTexture::sDefaultImagep = imagep;

	const S32 dim = 128;
	LLPointer<LLImageRaw> image_raw = new LLImageRaw(dim,dim,3);
	U8* data = image_raw->getData();
	for (S32 i = 0; i<dim; i++)
	{
		for (S32 j = 0; j<dim; j++)
		{
#if 0
			const S32 border = 2;
			if (i<border || j<border || i>=(dim-border) || j>=(dim-border))
			{
				*data++ = 0xff;
				*data++ = 0xff;
				*data++ = 0xff;
			}
			else
#endif
			{
				*data++ = 0x7f;
				*data++ = 0x7f;
				*data++ = 0x7f;
			}
		}
	}
	imagep->createGLTexture(0, image_raw);
	image_raw = NULL;
	LLViewerFetchedTexture::sDefaultImagep->dontDiscard();
#else
 	LLViewerFetchedTexture::sDefaultImagep = LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT, TRUE, TRUE);
#endif
	
 	LLViewerFetchedTexture::sSmokeImagep = LLViewerTextureManager::getFetchedTexture(IMG_SMOKE, TRUE, TRUE);
	LLViewerFetchedTexture::sSmokeImagep->setNoDelete() ;

	LLViewerTexture::initClass() ;

	if(LLFastTimer::sMetricLog)
	{
		LLViewerTextureManager::sTesterp = new LLTexturePipelineTester() ;
	}
}

void LLViewerTextureManager::cleanup()
{
	stop_glerror();

	LLImageGL::sDefaultGLTexture = NULL ;
	LLViewerTexture::sNullImagep = NULL;
	LLViewerFetchedTexture::sDefaultImagep = NULL;	
	LLViewerFetchedTexture::sSmokeImagep = NULL;
	LLViewerFetchedTexture::sMissingAssetImagep = NULL;
	LLViewerFetchedTexture::sWhiteImagep = NULL;

	LLViewerMediaTexture::cleanup() ;	
}

//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//start of LLViewerTexture
//----------------------------------------------------------------------------------------------
// static
void LLViewerTexture::initClass()
{
	LLImageGL::sDefaultGLTexture = LLViewerFetchedTexture::sDefaultImagep->getGLTexture() ;
}

// static
void LLViewerTexture::cleanupClass()
{
}

// tuning params
const F32 discard_bias_delta = .05f;
const F32 discard_delta_time = 0.5f;
const S32 min_non_tex_system_mem = (128<<20); // 128 MB
// non-const (used externally
F32 texmem_lower_bound_scale = 0.85f;
F32 texmem_middle_bound_scale = 0.925f;

//static
void LLViewerTexture::updateClass(const F32 velocity, const F32 angular_velocity)
{
	if(LLViewerTextureManager::sTesterp)
	{
		LLViewerTextureManager::sTesterp->update() ;
	}
	LLViewerMediaTexture::updateClass() ;

	sBoundTextureMemoryInBytes = LLImageGL::sBoundTextureMemoryInBytes;//in bytes
	sTotalTextureMemoryInBytes = LLImageGL::sGlobalTextureMemoryInBytes;//in bytes
	sMaxBoundTextureMemInMegaBytes = gTextureList.getMaxResidentTexMem();//in MB	
	sMaxTotalTextureMemInMegaBytes = gTextureList.getMaxTotalTextureMem() ;//in MB
	sMaxDesiredTextureMemInBytes = MEGA_BYTES_TO_BYTES(sMaxTotalTextureMemInMegaBytes) ; //in Bytes, by default and when total used texture memory is small.

	if (BYTES_TO_MEGA_BYTES(sBoundTextureMemoryInBytes) >= sMaxBoundTextureMemInMegaBytes ||
		BYTES_TO_MEGA_BYTES(sTotalTextureMemoryInBytes) >= sMaxTotalTextureMemInMegaBytes)
	{
		//when texture memory overflows, lower down the threashold to release the textures more aggressively.
		sMaxDesiredTextureMemInBytes = llmin((S32)(sMaxDesiredTextureMemInBytes * 0.75f) , MEGA_BYTES_TO_BYTES(MAX_VIDEO_RAM_IN_MEGA_BYTES)) ;//512 MB
	
		// If we are using more texture memory than we should,
		// scale up the desired discard level
		if (sEvaluationTimer.getElapsedTimeF32() > discard_delta_time)
		{
			sDesiredDiscardBias += discard_bias_delta;
			sEvaluationTimer.reset();
		}
	}
	else if (sDesiredDiscardBias > 0.0f &&
			 BYTES_TO_MEGA_BYTES(sBoundTextureMemoryInBytes) < sMaxBoundTextureMemInMegaBytes * texmem_lower_bound_scale &&
			 BYTES_TO_MEGA_BYTES(sTotalTextureMemoryInBytes) < sMaxTotalTextureMemInMegaBytes * texmem_lower_bound_scale)
	{			 
		// If we are using less texture memory than we should,
		// scale down the desired discard level
		if (sEvaluationTimer.getElapsedTimeF32() > discard_delta_time)
		{
			sDesiredDiscardBias -= discard_bias_delta;
			sEvaluationTimer.reset();
		}
	}
	sDesiredDiscardBias = llclamp(sDesiredDiscardBias, desired_discard_bias_min, desired_discard_bias_max);
	LLViewerTexture::sUseTextureAtlas = gSavedSettings.getBOOL("EnableTextureAtlas") ;
}

//end of static functions
//-------------------------------------------------------------------------------------------
const U32 LLViewerTexture::sCurrentFileVersion = 1;

LLViewerTexture::LLViewerTexture(BOOL usemipmaps)
{
	init(true);
	mUseMipMaps = usemipmaps ;

	mID.generate();
	sImageCount++;
}

LLViewerTexture::LLViewerTexture(const LLUUID& id, BOOL usemipmaps)
	: mID(id)
{
	init(true);
	mUseMipMaps = usemipmaps ;
	
	sImageCount++;
}

LLViewerTexture::LLViewerTexture(const U32 width, const U32 height, const U8 components, BOOL usemipmaps) 
{
	init(true);

	mFullWidth = width ;
	mFullHeight = height ;
	mUseMipMaps = usemipmaps ;
	mComponents = components ;

	mID.generate();
	sImageCount++;
}

LLViewerTexture::LLViewerTexture(const LLImageRaw* raw, BOOL usemipmaps)	
{
	init(true);
	mUseMipMaps = usemipmaps ;
	mGLTexturep = new LLImageGL(raw, usemipmaps) ;
	
	// Create an empty image of the specified size and width
	mID.generate();
	sImageCount++;
}

LLViewerTexture::~LLViewerTexture()
{
	sImageCount--;
}

void LLViewerTexture::init(bool firstinit)
{
	mBoostLevel = LLViewerTexture::BOOST_NONE;

	mFullWidth = 0;
	mFullHeight = 0;
	mUseMipMaps = FALSE ;
	mComponents = 0 ;

	mTextureState = NO_DELETE ;
	mDontDiscard = FALSE;
	mMaxVirtualSize = 0.f;
	mNeedsResetMaxVirtualSize = FALSE ;
}

//virtual 
S8 LLViewerTexture::getType() const
{
	return LLViewerTexture::LOCAL_TEXTURE ;
}

void LLViewerTexture::cleanup()
{
	mFaceList.clear() ;
	
	if(mGLTexturep)
	{
		mGLTexturep->cleanup();
	}
}

// virtual
void LLViewerTexture::dump()
{
	if(mGLTexturep)
	{
		mGLTexturep->dump();
	}

	llinfos << "LLViewerTexture"
			<< " mID " << mID
			<< llendl;
}

void LLViewerTexture::setBoostLevel(S32 level)
{
	if(mBoostLevel != level)
	{
		mBoostLevel = level ;
		if(mBoostLevel != LLViewerTexture::BOOST_NONE)
		{
			setNoDelete() ;		
		}
	}
}


bool LLViewerTexture::bindDefaultImage(S32 stage) const
{
	if (stage < 0) return false;

	bool res = true;
	if (LLViewerFetchedTexture::sDefaultImagep.notNull() && (this != LLViewerFetchedTexture::sDefaultImagep.get()))
	{
		// use default if we've got it
		res = gGL.getTexUnit(stage)->bind(LLViewerFetchedTexture::sDefaultImagep);
	}
	if (!res && LLViewerTexture::sNullImagep.notNull() && (this != LLViewerTexture::sNullImagep))
	{
		res = gGL.getTexUnit(stage)->bind(LLViewerTexture::sNullImagep) ;
	}
	if (!res)
	{
		llwarns << "LLViewerTexture::bindDefaultImage failed." << llendl;
	}
	stop_glerror();
	if(LLViewerTextureManager::sTesterp)
	{
		LLViewerTextureManager::sTesterp->updateGrayTextureBinding() ;
	}
	return res;
}

//virtual 
BOOL LLViewerTexture::isMissingAsset()const		
{ 
	return FALSE; 
}

//virtual 
void LLViewerTexture::forceImmediateUpdate() 
{
}

void LLViewerTexture::addTextureStats(F32 virtual_size) const 
{
	if (virtual_size > mMaxVirtualSize)
	{
		mMaxVirtualSize = virtual_size;
	}
}

void LLViewerTexture::resetTextureStats(BOOL zero)
{
	if (zero)
	{
		mMaxVirtualSize = 0.0f;
	}
	else
	{
		mMaxVirtualSize -= mMaxVirtualSize * .10f; // decay by 5%/update
	}
}

//virtual 
F32 LLViewerTexture::getMaxVirtualSize()
{
	return mMaxVirtualSize ;
}

//virtual
void LLViewerTexture::addFace(LLFace* facep) 
{
	mFaceList.push_back(facep) ;
}

//virtual
void LLViewerTexture::removeFace(LLFace* facep) 
{
	mFaceList.remove(facep) ;
}

void LLViewerTexture::forceActive()
{
	mTextureState = ACTIVE ; 
}

void LLViewerTexture::setActive() 
{ 
	if(mTextureState != NO_DELETE)
	{
		mTextureState = ACTIVE ; 
	}
}

//set the texture to stay in memory
void LLViewerTexture::setNoDelete() 
{ 
	mTextureState = NO_DELETE ;
}

void LLViewerTexture::generateGLTexture() 
{	
	if(mGLTexturep.isNull())
	{
		mGLTexturep = new LLImageGL(mFullWidth, mFullHeight, mComponents, mUseMipMaps) ;
	}
}

LLImageGL* LLViewerTexture::getGLTexture() const
{
	llassert_always(mGLTexturep.notNull()) ;
	
	return mGLTexturep ;
}

BOOL LLViewerTexture::createGLTexture() 
{
	if(mGLTexturep.isNull())
	{
		generateGLTexture() ;
	}

	return mGLTexturep->createGLTexture() ;
}

BOOL LLViewerTexture::createGLTexture(S32 discard_level, const LLImageRaw* imageraw, S32 usename)
{
	llassert_always(mGLTexturep.notNull()) ;	

	BOOL ret = mGLTexturep->createGLTexture(discard_level, imageraw, usename) ;
	
	if(ret)
	{
		mFullWidth = mGLTexturep->getCurrentWidth() ;
		mFullHeight = mGLTexturep->getCurrentHeight() ; 
		mComponents = mGLTexturep->getComponents() ;
	}

	return ret ;
}

void LLViewerTexture::setExplicitFormat(LLGLint internal_format, LLGLenum primary_format, LLGLenum type_format, BOOL swap_bytes)
{
	llassert_always(mGLTexturep.notNull()) ;
	
	mGLTexturep->setExplicitFormat(internal_format, primary_format, type_format, swap_bytes) ;
}
void LLViewerTexture::setAddressMode(LLTexUnit::eTextureAddressMode mode)
{
	llassert_always(mGLTexturep.notNull()) ;
	mGLTexturep->setAddressMode(mode) ;
}
void LLViewerTexture::setFilteringOption(LLTexUnit::eTextureFilterOptions option)
{
	llassert_always(mGLTexturep.notNull()) ;
	mGLTexturep->setFilteringOption(option) ;
}

//virtual
S32	LLViewerTexture::getWidth(S32 discard_level) const
{
	llassert_always(mGLTexturep.notNull()) ;
	return mGLTexturep->getWidth(discard_level) ;
}

//virtual
S32	LLViewerTexture::getHeight(S32 discard_level) const
{
	llassert_always(mGLTexturep.notNull()) ;
	return mGLTexturep->getHeight(discard_level) ;
}

S32 LLViewerTexture::getMaxDiscardLevel() const
{
	llassert_always(mGLTexturep.notNull()) ;
	return mGLTexturep->getMaxDiscardLevel() ;
}
S32 LLViewerTexture::getDiscardLevel() const
{
	llassert_always(mGLTexturep.notNull()) ;
	return mGLTexturep->getDiscardLevel() ;
}
S8  LLViewerTexture::getComponents() const 
{ 
	llassert_always(mGLTexturep.notNull()) ;
	
	return mGLTexturep->getComponents() ;
}

LLGLuint LLViewerTexture::getTexName() const 
{ 
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getTexName() ; 
}

BOOL LLViewerTexture::hasGLTexture() const 
{
	if(mGLTexturep.notNull())
	{
		return mGLTexturep->getHasGLTexture() ;
	}
	return FALSE ;
}

BOOL LLViewerTexture::getBoundRecently() const
{
	if(mGLTexturep.notNull())
	{
		return mGLTexturep->getBoundRecently() ;
	}
	return FALSE ;
}

LLTexUnit::eTextureType LLViewerTexture::getTarget(void) const
{
	llassert_always(mGLTexturep.notNull()) ;
	return mGLTexturep->getTarget() ;
}

BOOL LLViewerTexture::setSubImage(const LLImageRaw* imageraw, S32 x_pos, S32 y_pos, S32 width, S32 height)
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->setSubImage(imageraw, x_pos, y_pos, width, height) ;
}

BOOL LLViewerTexture::setSubImage(const U8* datap, S32 data_width, S32 data_height, S32 x_pos, S32 y_pos, S32 width, S32 height)
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->setSubImage(datap, data_width, data_height, x_pos, y_pos, width, height) ;
}

void LLViewerTexture::setGLTextureCreated (bool initialized)
{
	llassert_always(mGLTexturep.notNull()) ;

	mGLTexturep->setGLTextureCreated (initialized) ;
}

LLTexUnit::eTextureAddressMode LLViewerTexture::getAddressMode(void) const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getAddressMode() ;
}

S32 LLViewerTexture::getTextureMemory() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->mTextureMemory ;
}

LLGLenum LLViewerTexture::getPrimaryFormat() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getPrimaryFormat() ;
}

BOOL LLViewerTexture::getIsAlphaMask() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getIsAlphaMask() ;
}

BOOL LLViewerTexture::getMask(const LLVector2 &tc)
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getMask(tc) ;
}

F32 LLViewerTexture::getTimePassedSinceLastBound()
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getTimePassedSinceLastBound() ;
}
BOOL LLViewerTexture::getMissed() const 
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getMissed() ;
}

BOOL LLViewerTexture::isValidForSculpt(S32 discard_level, S32 image_width, S32 image_height, S32 ncomponents) 
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->isValidForSculpt(discard_level, image_width, image_height, ncomponents) ;
}

BOOL LLViewerTexture::readBackRaw(S32 discard_level, LLImageRaw* imageraw, bool compressed_ok) const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->readBackRaw(discard_level, imageraw, compressed_ok) ;
}

U32 LLViewerTexture::getTexelsInAtlas() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getTexelsInAtlas() ;
}

U32 LLViewerTexture::getTexelsInGLTexture() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getTexelsInGLTexture() ;
}

BOOL LLViewerTexture::isGLTextureCreated() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->isGLTextureCreated() ;
}

S32  LLViewerTexture::getDiscardLevelInAtlas() const
{
	llassert_always(mGLTexturep.notNull()) ;

	return mGLTexturep->getDiscardLevelInAtlas() ;
}

void LLViewerTexture::destroyGLTexture() 
{
	if(mGLTexturep.notNull() && mGLTexturep->getHasGLTexture())
	{
		mGLTexturep->destroyGLTexture() ;
		mTextureState = DELETED ;	
	}	
}

//virtual 
void LLViewerTexture::updateBindStatsForTester()
{
	if(LLViewerTextureManager::sTesterp)
	{
		LLViewerTextureManager::sTesterp->updateTextureBindingStats(this) ;
	}
}

//----------------------------------------------------------------------------------------------
//end of LLViewerTexture
//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
//start of LLViewerFetchedTexture
//----------------------------------------------------------------------------------------------

//static
F32 LLViewerFetchedTexture::maxDecodePriority()
{
	return 2000000.f;
}

LLViewerFetchedTexture::LLViewerFetchedTexture(const LLUUID& id, BOOL usemipmaps)
	: LLViewerTexture(id, usemipmaps)
{
	init(TRUE) ;
	generateGLTexture() ;
}
	
LLViewerFetchedTexture::LLViewerFetchedTexture(const LLImageRaw* raw, BOOL usemipmaps)
	: LLViewerTexture(raw, usemipmaps)
{
	init(TRUE) ;
}
	
LLViewerFetchedTexture::LLViewerFetchedTexture(const std::string& full_path, const LLUUID& id, BOOL usemipmaps)
	: LLViewerTexture(id, usemipmaps),
	mLocalFileName(full_path)
{
	init(TRUE) ;
	generateGLTexture() ;
}

void LLViewerFetchedTexture::init(bool firstinit)
{
	mOrigWidth = 0;
	mOrigHeight = 0;
	mNeedsAux = FALSE;
	mRequestedDiscardLevel = -1;
	mRequestedDownloadPriority = 0.f;
	mFullyLoaded = FALSE;
	mDesiredDiscardLevel = MAX_DISCARD_LEVEL + 1;
	mMinDesiredDiscardLevel = MAX_DISCARD_LEVEL + 1;
	
	mDecodingAux = FALSE;

	mKnownDrawWidth = 0;
	mKnownDrawHeight = 0;

	if (firstinit)
	{
		mDecodePriority = 0.f;
		mInImageList = 0;
	}

	// Only set mIsMissingAsset true when we know for certain that the database
	// does not contain this image.
	mIsMissingAsset = FALSE;

	mNeedsCreateTexture = FALSE;
	
	mIsRawImageValid = FALSE;
	mRawDiscardLevel = INVALID_DISCARD_LEVEL;
	mMinDiscardLevel = 0;

	mHasFetcher = FALSE;
	mIsFetching = FALSE;
	mFetchState = 0;
	mFetchPriority = 0;
	mDownloadProgress = 0.f;
	mFetchDeltaTime = 999999.f;
	mDecodeFrame = 0;
	mVisibleFrame = 0;
	mForSculpt = FALSE ;
	mIsFetched = FALSE ;
}

LLViewerFetchedTexture::~LLViewerFetchedTexture()
{
	//*NOTE getTextureFetch can return NULL when Viewer is shutting down.
	// This is due to LLWearableList is singleton and is destroyed after 
	// LLAppViewer::cleanup() was called. (see ticket EXT-177)
	if (mHasFetcher && LLAppViewer::getTextureFetch())
	{
		LLAppViewer::getTextureFetch()->deleteRequest(getID(), true);
	}
	cleanup();	
}

//virtual 
S8 LLViewerFetchedTexture::getType() const
{
	return LLViewerTexture::FETCHED_TEXTURE ;
}

void LLViewerFetchedTexture::cleanup()
{
	for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
		iter != mLoadedCallbackList.end(); )
	{
		LLLoadedCallbackEntry *entryp = *iter++;
		// We never finished loading the image.  Indicate failure.
		// Note: this allows mLoadedCallbackUserData to be cleaned up.
		entryp->mCallback( FALSE, this, NULL, NULL, 0, TRUE, entryp->mUserData );
		delete entryp;
	}
	mLoadedCallbackList.clear();
	mNeedsAux = FALSE;
	
	// Clean up image data
	destroyRawImage();
}

void LLViewerFetchedTexture::setForSculpt()
{
	mForSculpt = TRUE ;
}

BOOL LLViewerFetchedTexture::isDeleted()  
{ 
	return mTextureState == DELETED ; 
}

BOOL LLViewerFetchedTexture::isInactive()  
{ 
	return mTextureState == INACTIVE ; 
}

BOOL LLViewerFetchedTexture::isDeletionCandidate()  
{ 
	return mTextureState == DELETION_CANDIDATE ; 
}

void LLViewerFetchedTexture::setDeletionCandidate()  
{ 
	if(mGLTexturep.notNull() && mGLTexturep->getTexName() && (mTextureState == INACTIVE))
	{
		mTextureState = DELETION_CANDIDATE ;		
	}
}

//set the texture inactive
void LLViewerFetchedTexture::setInactive()
{
	if(mTextureState == ACTIVE && mGLTexturep.notNull() && mGLTexturep->getTexName() && !mGLTexturep->getBoundRecently())
	{
		mTextureState = INACTIVE ; 
	}
}

// virtual
void LLViewerFetchedTexture::dump()
{
	LLViewerTexture::dump();

	llinfos << "LLViewerFetchedTexture"
			<< " mIsMissingAsset " << (S32)mIsMissingAsset
			<< " mFullWidth " << mFullWidth
			<< " mFullHeight " << mFullHeight
			<< " mOrigWidth" << mOrigWidth
			<< " mOrigHeight" << mOrigHeight
			<< llendl;
}

///////////////////////////////////////////////////////////////////////////////
// ONLY called from LLViewerFetchedTextureList
void LLViewerFetchedTexture::destroyTexture() 
{
	if(LLImageGL::sGlobalTextureMemoryInBytes < sMaxDesiredTextureMemInBytes)//not ready to release unused memory.
	{
		return ;
	}
	if (mNeedsCreateTexture)//return if in the process of generating a new texture.
	{
		return ;
	}
	
	destroyGLTexture() ;
	mFullyLoaded = FALSE ;
}

// ONLY called from LLViewerTextureList
BOOL LLViewerFetchedTexture::createTexture(S32 usename/*= 0*/)
{
	if (!mNeedsCreateTexture)
	{
		destroyRawImage();
		return FALSE;
	}
	mNeedsCreateTexture	= FALSE;
	if (mRawImage.isNull())
	{
		llerrs << "LLViewerTexture trying to create texture with no Raw Image" << llendl;
	}
// 	llinfos << llformat("IMAGE Creating (%d) [%d x %d] Bytes: %d ",
// 						mRawDiscardLevel, 
// 						mRawImage->getWidth(), mRawImage->getHeight(),mRawImage->getDataSize())
// 			<< mID.getString() << llendl;
	BOOL res = TRUE;
	if (!gNoRender)
	{
		// store original size only for locally-sourced images
		if (!mLocalFileName.empty())
		{
			mOrigWidth = mRawImage->getWidth();
			mOrigHeight = mRawImage->getHeight();

			// leave black border, do not scale image content
			mRawImage->expandToPowerOfTwo(MAX_IMAGE_SIZE, FALSE);
			
			mFullWidth = mRawImage->getWidth();
			mFullHeight = mRawImage->getHeight();
		}
		else
		{
			mOrigWidth = mFullWidth;
			mOrigHeight = mFullHeight;
		}

		bool size_okay = true;
		
		U32 raw_width = mRawImage->getWidth() << mRawDiscardLevel;
		U32 raw_height = mRawImage->getHeight() << mRawDiscardLevel;
		if( raw_width > MAX_IMAGE_SIZE || raw_height > MAX_IMAGE_SIZE )
		{
			llinfos << "Width or height is greater than " << MAX_IMAGE_SIZE << ": (" << raw_width << "," << raw_height << ")" << llendl;
			size_okay = false;
		}
		
		if (!LLImageGL::checkSize(mRawImage->getWidth(), mRawImage->getHeight()))
		{
			// A non power-of-two image was uploaded (through a non standard client)
			llinfos << "Non power of two width or height: (" << mRawImage->getWidth() << "," << mRawImage->getHeight() << ")" << llendl;
			size_okay = false;
		}
		
		if( !size_okay )
		{
			// An inappropriately-sized image was uploaded (through a non standard client)
			// We treat these images as missing assets which causes them to
			// be renderd as 'missing image' and to stop requesting data
			setIsMissingAsset();
			destroyRawImage();
			return FALSE;
		}
		
		if(!(res = insertToAtlas()))
		{
			res = mGLTexturep->createGLTexture(mRawDiscardLevel, mRawImage, usename);
			resetFaceAtlas() ;
		}
		setActive() ;
	}

	//
	// Iterate through the list of image loading callbacks to see
	// what sort of data they need.
	//
	// *TODO: Fix image callback code
	BOOL imageraw_callbacks = FALSE;
	for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
		iter != mLoadedCallbackList.end(); )
	{
		LLLoadedCallbackEntry *entryp = *iter++;
		if (entryp->mNeedsImageRaw)
		{
			imageraw_callbacks = TRUE;
			break;
		}
	}

	if (!imageraw_callbacks)
	{
		mNeedsAux = FALSE;
		destroyRawImage();
	}
	return res;
}

// Call with 0,0 to turn this feature off.
void LLViewerFetchedTexture::setKnownDrawSize(S32 width, S32 height)
{
	mKnownDrawWidth = width;
	mKnownDrawHeight = height;
	addTextureStats((F32)(width * height));
}

//virtual
void LLViewerFetchedTexture::processTextureStats()
{
	if(mFullyLoaded)//already loaded
	{
		return ;
	}

	if(!mFullWidth || !mFullHeight)
	{
		mDesiredDiscardLevel = 	getMaxDiscardLevel() ;
	}
	else
	{
		mDesiredDiscardLevel = 0;
		if (mFullWidth > MAX_IMAGE_SIZE_DEFAULT || mFullHeight > MAX_IMAGE_SIZE_DEFAULT)
		{
			mDesiredDiscardLevel = 1; // MAX_IMAGE_SIZE_DEFAULT = 1024 and max size ever is 2048
		}

		if(getDiscardLevel() >= 0 && (getDiscardLevel() <= mDesiredDiscardLevel))
		{
			mFullyLoaded = TRUE ;
		}
	}
}

//texture does not have any data, so we don't know the size of the image, treat it like 32 * 32.
F32 LLViewerFetchedTexture::calcDecodePriorityForUnknownTexture(F32 pixel_priority)
{
	static const F64 log_2 = log(2.0);

	F32 desired = (F32)(log(32.0/pixel_priority) / log_2);
	S32 ddiscard = MAX_DISCARD_LEVEL - (S32)desired + 1;
	ddiscard = llclamp(ddiscard, 1, 9);
	
	return ddiscard*100000.f;
}

F32 LLViewerFetchedTexture::calcDecodePriority()
{
#ifndef LL_RELEASE_FOR_DOWNLOAD
	if (mID == LLAppViewer::getTextureFetch()->mDebugID)
	{
		LLAppViewer::getTextureFetch()->mDebugCount++; // for setting breakpoints
	}
#endif
	
	if(mFullyLoaded)//already loaded for static texture
	{
		return -4.0f ; //alreay fetched
	}

	if (mNeedsCreateTexture)
	{
		return mDecodePriority; // no change while waiting to create
	}

	F32 priority;
	S32 cur_discard = getDiscardLevel();
	bool have_all_data = (cur_discard >= 0 && (cur_discard <= mDesiredDiscardLevel));
	F32 pixel_priority = fsqrtf(mMaxVirtualSize);
	const S32 MIN_NOT_VISIBLE_FRAMES = 30; // NOTE: this function is not called every frame
	mDecodeFrame++;
	if (pixel_priority > 0.f)
	{
		mVisibleFrame = mDecodeFrame;
	}
	
	if (mIsMissingAsset)
	{
		priority = 0.0f;
	}
	else if (mDesiredDiscardLevel > getMaxDiscardLevel())
	{
		// Don't decode anything we don't need
		priority = -1.0f;
	}
	else if (mBoostLevel == LLViewerTexture::BOOST_UI && !have_all_data)
	{
		priority = 1.f;
	}
	else if (pixel_priority <= 0.f && !have_all_data)
	{
		// Not on screen but we might want some data
		if (mBoostLevel > BOOST_HIGH)
		{
			// Always want high boosted images
			priority = 1.f;
		}
		else if (mVisibleFrame == 0 || (mDecodeFrame - mVisibleFrame > MIN_NOT_VISIBLE_FRAMES))
		{
			// Don't decode anything that isn't visible unless it's important
			priority = -2.0f;
		}
		else
		{
			// Leave the priority as-is
			return mDecodePriority;
		}
	}
	else if (cur_discard < 0)
	{
		priority = calcDecodePriorityForUnknownTexture(pixel_priority) ;
	}
	else if ((mMinDiscardLevel > 0) && (cur_discard <= mMinDiscardLevel))
	{
		// larger mips are corrupted
		priority = -3.0f;
	}
	else if (cur_discard <= mDesiredDiscardLevel)
	{
		priority = -4.0f;
	}
	else
	{
		// priority range = 100000-400000
		S32 ddiscard = cur_discard - mDesiredDiscardLevel;
		if (getDontDiscard())
		{
			ddiscard+=2;
		}
		else if (mGLTexturep.notNull() && !mGLTexturep->getBoundRecently() && mBoostLevel == 0)
		{
			ddiscard-=2;
		}
		ddiscard = llclamp(ddiscard, 0, 4);
		priority = ddiscard*100000.f;
	}
	if (priority > 0.0f)
	{
		pixel_priority = llclamp(pixel_priority, 0.0f, priority-1.f); // priority range = 100000-900000
		if ( mBoostLevel > BOOST_HIGH)
		{
			priority = 1000000.f + pixel_priority + 1000.f * mBoostLevel;
		}
		else
		{
			priority +=      0.f + pixel_priority + 1000.f * mBoostLevel;
		}
	}
	return priority;
}
//============================================================================

void LLViewerFetchedTexture::setDecodePriority(F32 priority)
{
	llassert(!mInImageList);
	mDecodePriority = priority;
}

bool LLViewerFetchedTexture::updateFetch()
{
	mFetchState = 0;
	mFetchPriority = 0;
	mFetchDeltaTime = 999999.f;
	mRequestDeltaTime = 999999.f;

#ifndef LL_RELEASE_FOR_DOWNLOAD
	if (mID == LLAppViewer::getTextureFetch()->mDebugID)
	{
		LLAppViewer::getTextureFetch()->mDebugCount++; // for setting breakpoints
	}
#endif

	if (mNeedsCreateTexture)
	{
		// We may be fetching still (e.g. waiting on write)
		// but don't check until we've processed the raw data we have
		return false;
	}
	if (mIsMissingAsset)
	{
		llassert_always(!mHasFetcher);
		return false; // skip
	}
	if (!mLoadedCallbackList.empty() && mRawImage.notNull())
	{
		return false; // process any raw image data in callbacks before replacing
	}
	
	S32 current_discard = getDiscardLevel() ;
	S32 desired_discard = getDesiredDiscardLevel();
	F32 decode_priority = getDecodePriority();
	decode_priority = llmax(decode_priority, 0.0f);
	
	if (mIsFetching)
	{
		// Sets mRawDiscardLevel, mRawImage, mAuxRawImage
		S32 fetch_discard = current_discard;
		if (mRawImage.notNull()) sRawCount--;
		if (mAuxRawImage.notNull()) sAuxCount--;
		bool finished = LLAppViewer::getTextureFetch()->getRequestFinished(getID(), fetch_discard, mRawImage, mAuxRawImage);
		if (mRawImage.notNull()) sRawCount++;
		if (mAuxRawImage.notNull()) sAuxCount++;
		if (finished)
		{
			mIsFetching = FALSE;
		}
		else
		{
			mFetchState = LLAppViewer::getTextureFetch()->getFetchState(mID, mDownloadProgress, mRequestedDownloadPriority,
																		mFetchPriority, mFetchDeltaTime, mRequestDeltaTime);
		}
		
		// We may have data ready regardless of whether or not we are finished (e.g. waiting on write)
		if (mRawImage.notNull())
		{
			if(LLViewerTextureManager::sTesterp)
			{
				mIsFetched = TRUE ;
				LLViewerTextureManager::sTesterp->updateTextureLoadingStats(this, mRawImage, LLAppViewer::getTextureFetch()->isFromLocalCache(mID)) ;
			}
			mRawDiscardLevel = fetch_discard;
			if ((mRawImage->getDataSize() > 0 && mRawDiscardLevel >= 0) &&
				(current_discard < 0 || mRawDiscardLevel < current_discard))
			{
				if (getComponents() != mRawImage->getComponents())
				{
					// We've changed the number of components, so we need to move any
					// objects using this pool to a different pool.
					mComponents = mRawImage->getComponents();
					mGLTexturep->setComponents(mComponents) ;

					gTextureList.dirtyImage(this);
				}			
				mIsRawImageValid = TRUE;
				gTextureList.mCreateTextureList.insert(this);
				mNeedsCreateTexture = TRUE;
				mFullWidth = mRawImage->getWidth() << mRawDiscardLevel;
				mFullHeight = mRawImage->getHeight() << mRawDiscardLevel;
			}
			else
			{
				// Data is ready but we don't need it
				// (received it already while fetcher was writing to disk)
				destroyRawImage();
				return false; // done
			}
		}
		
		if (!mIsFetching)
		{
			if ((decode_priority > 0) && (mRawDiscardLevel < 0 || mRawDiscardLevel == INVALID_DISCARD_LEVEL))
			{
				// We finished but received no data
				if (current_discard < 0)
				{
					setIsMissingAsset();
					desired_discard = -1;
				}
				else
				{
					llwarns << mID << ": Setting min discard to " << current_discard << llendl;
					mMinDiscardLevel = current_discard;
					desired_discard = current_discard;
				}
				destroyRawImage();
			}
			else if (mRawImage.isNull())
			{
				// We have data, but our fetch failed to return raw data
				// *TODO: FIgure out why this is happening and fix it
				destroyRawImage();
			}
		}
		else
		{
			LLAppViewer::getTextureFetch()->updateRequestPriority(mID, decode_priority);
		}
	}

	bool make_request = true;
	
	if (decode_priority <= 0)
	{
		make_request = false;
	}
	else if (mNeedsCreateTexture || mIsMissingAsset)
	{
		make_request = false;
	}
	else if (current_discard >= 0 && current_discard <= mMinDiscardLevel)
	{
		make_request = false;
	}
	else
	{
		if (mIsFetching)
		{
			if (mRequestedDiscardLevel <= desired_discard)
			{
				make_request = false;
			}
		}
		else
		{
			if (current_discard >= 0 && current_discard <= desired_discard)
			{
				make_request = false;
			}
		}
	}
	
	if (make_request)
	{
		S32 w=0, h=0, c=0;
		if (current_discard >= 0)
		{
			w = mGLTexturep->getWidth(0);
			h = mGLTexturep->getHeight(0);
			c = mComponents;
		}
		if (!mDontDiscard)
		{
			if (mBoostLevel == 0)
			{
				desired_discard = llmax(desired_discard, current_discard-1);
			}
			else
			{
				desired_discard = llmax(desired_discard, current_discard-2);
			}
		}

		// bypass texturefetch directly by pulling from LLTextureCache
		bool fetch_request_created = false;
		if (mLocalFileName.empty())
		{
			fetch_request_created = LLAppViewer::getTextureFetch()->createRequest(getID(), getTargetHost(), decode_priority,
											 w, h, c, desired_discard,
											 needsAux());
		}
		else
		{
			fetch_request_created = LLAppViewer::getTextureFetch()->createRequest(mLocalFileName, getID(),getTargetHost(), decode_priority,
											 w, h, c, desired_discard,
											 needsAux());
		}

		if (fetch_request_created)
		{
			mHasFetcher = TRUE;
			mIsFetching = TRUE;
			mRequestedDiscardLevel = desired_discard;
			mFetchState = LLAppViewer::getTextureFetch()->getFetchState(mID, mDownloadProgress, mRequestedDownloadPriority,
													   mFetchPriority, mFetchDeltaTime, mRequestDeltaTime);
		}

		// if createRequest() failed, we're finishing up a request for this UUID,
		// wait for it to complete
	}
	else if (mHasFetcher && !mIsFetching)
	{
		// Only delete requests that haven't receeived any network data for a while
		const F32 FETCH_IDLE_TIME = 5.f;
		if (mLastPacketTimer.getElapsedTimeF32() > FETCH_IDLE_TIME)
		{
// 			llinfos << "Deleting request: " << getID() << " Discard: " << current_discard << " <= min:" << mMinDiscardLevel << " or priority == 0: " << decode_priority << llendl;
			LLAppViewer::getTextureFetch()->deleteRequest(getID(), true);
			mHasFetcher = FALSE;
		}
	}
	
	llassert_always(mRawImage.notNull() || (!mNeedsCreateTexture && !mIsRawImageValid));
	
	return mIsFetching ? true : false;
}

void LLViewerFetchedTexture::setIsMissingAsset()
{
	llwarns << mLocalFileName << " " << mID << ": Marking image as missing" << llendl;
	if (mHasFetcher)
	{
		LLAppViewer::getTextureFetch()->deleteRequest(getID(), true);
		mHasFetcher = FALSE;
		mIsFetching = FALSE;
		mFetchState = 0;
		mFetchPriority = 0;
	}
	mIsMissingAsset = TRUE;
}

void LLViewerFetchedTexture::setLoadedCallback( loaded_callback_func loaded_callback,
									   S32 discard_level, BOOL keep_imageraw, BOOL needs_aux, void* userdata)
{
	//
	// Don't do ANYTHING here, just add it to the global callback list
	//
	if (mLoadedCallbackList.empty())
	{
		// Put in list to call this->doLoadedCallbacks() periodically
		gTextureList.mCallbackList.insert(this);
	}
	LLLoadedCallbackEntry* entryp = new LLLoadedCallbackEntry(loaded_callback, discard_level, keep_imageraw, userdata);
	mLoadedCallbackList.push_back(entryp);
	mNeedsAux |= needs_aux;
	if (mNeedsAux && mAuxRawImage.isNull() && getDiscardLevel() >= 0)
	{
		// We need aux data, but we've already loaded the image, and it didn't have any
		llwarns << "No aux data available for callback for image:" << getID() << llendl;
	}
}

bool LLViewerFetchedTexture::doLoadedCallbacks()
{
	if (mNeedsCreateTexture)
	{
		return false;
	}

	bool res = false;
	
	if (isMissingAsset())
	{
		for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
			iter != mLoadedCallbackList.end(); )
		{
			LLLoadedCallbackEntry *entryp = *iter++;
			// We never finished loading the image.  Indicate failure.
			// Note: this allows mLoadedCallbackUserData to be cleaned up.
			entryp->mCallback(FALSE, this, NULL, NULL, 0, TRUE, entryp->mUserData);
			delete entryp;
		}
		mLoadedCallbackList.clear();

		// Remove ourself from the global list of textures with callbacks
		gTextureList.mCallbackList.erase(this);
	}

	S32 gl_discard = getDiscardLevel();

	// If we don't have a legit GL image, set it to be lower than the worst discard level
	if (gl_discard == -1)
	{
		gl_discard = MAX_DISCARD_LEVEL + 1;
	}

	//
	// Determine the quality levels of textures that we can provide to callbacks
	// and whether we need to do decompression/readback to get it
	//
	S32 current_raw_discard = MAX_DISCARD_LEVEL + 1; // We can always do a readback to get a raw discard
	S32 best_raw_discard = gl_discard;	// Current GL quality level
	S32 current_aux_discard = MAX_DISCARD_LEVEL + 1;
	S32 best_aux_discard = MAX_DISCARD_LEVEL + 1;

	if (mIsRawImageValid)
	{
		// If we have an existing raw image, we have a baseline for the raw and auxiliary quality levels.
		best_raw_discard = llmin(best_raw_discard, mRawDiscardLevel);
		best_aux_discard = llmin(best_aux_discard, mRawDiscardLevel); // We always decode the aux when we decode the base raw
		current_aux_discard = llmin(current_aux_discard, best_aux_discard);
	}
	else
	{
		// We have no data at all, we need to get it
		// Do this by forcing the best aux discard to be 0.
		best_aux_discard = 0;
	}


	//
	// See if any of the callbacks would actually run using the data that we can provide,
	// and also determine if we need to perform any readbacks or decodes.
	//
	bool run_gl_callbacks = false;
	bool run_raw_callbacks = false;
	bool need_readback = false;

	for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
		iter != mLoadedCallbackList.end(); )
	{
		LLLoadedCallbackEntry *entryp = *iter++;
		if (entryp->mNeedsImageRaw)
		{
			if (mNeedsAux)
			{
				//
				// Need raw and auxiliary channels
				//
				if (entryp->mLastUsedDiscard > current_aux_discard)
				{
					// We have useful data, run the callbacks
					run_raw_callbacks = true;
				}
			}
			else
			{
				if (entryp->mLastUsedDiscard > current_raw_discard)
				{
					// We have useful data, just run the callbacks
					run_raw_callbacks = true;
				}
				else if (entryp->mLastUsedDiscard > best_raw_discard)
				{
					// We can readback data, and then run the callbacks
					need_readback = true;
					run_raw_callbacks = true;
				}
			}
		}
		else
		{
			// Needs just GL
			if (entryp->mLastUsedDiscard > gl_discard)
			{
				// We have enough data, run this callback requiring GL data
				run_gl_callbacks = true;
			}
		}
	}

	//
	// Do a readback if required, OR start off a texture decode
	//
	if (need_readback && (getMaxDiscardLevel() > gl_discard))
	{
		// Do a readback to get the GL data into the raw image
		// We have GL data.

		destroyRawImage();
		readBackRawImage(gl_discard);
		llassert_always(mRawImage.notNull());
		llassert_always(!mNeedsAux || mAuxRawImage.notNull());
	}

	//
	// Run raw/auxiliary data callbacks
	//
	if (run_raw_callbacks && mIsRawImageValid && (mRawDiscardLevel <= getMaxDiscardLevel()))
	{
		// Do callbacks which require raw image data.
		//llinfos << "doLoadedCallbacks raw for " << getID() << llendl;

		// Call each party interested in the raw data.
		for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
			iter != mLoadedCallbackList.end(); )
		{
			callback_list_t::iterator curiter = iter++;
			LLLoadedCallbackEntry *entryp = *curiter;
			if (entryp->mNeedsImageRaw && (entryp->mLastUsedDiscard > mRawDiscardLevel))
			{
				// If we've loaded all the data there is to load or we've loaded enough
				// to satisfy the interested party, then this is the last time that
				// we're going to call them.

				llassert_always(mRawImage.notNull());
				if(mNeedsAux && mAuxRawImage.isNull())
				{
					llwarns << "Raw Image with no Aux Data for callback" << llendl;
				}
				BOOL final = mRawDiscardLevel <= entryp->mDesiredDiscard ? TRUE : FALSE;
				//llinfos << "Running callback for " << getID() << llendl;
				//llinfos << mRawImage->getWidth() << "x" << mRawImage->getHeight() << llendl;
				if (final)
				{
					//llinfos << "Final!" << llendl;
				}
				entryp->mLastUsedDiscard = mRawDiscardLevel;
				entryp->mCallback(TRUE, this, mRawImage, mAuxRawImage, mRawDiscardLevel, final, entryp->mUserData);
				if (final)
				{
					iter = mLoadedCallbackList.erase(curiter);
					delete entryp;
				}
				res = true;
			}
		}
	}

	//
	// Run GL callbacks
	//
	if (run_gl_callbacks && (gl_discard <= getMaxDiscardLevel()))
	{
		//llinfos << "doLoadedCallbacks GL for " << getID() << llendl;

		// Call the callbacks interested in GL data.
		for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
			iter != mLoadedCallbackList.end(); )
		{
			callback_list_t::iterator curiter = iter++;
			LLLoadedCallbackEntry *entryp = *curiter;
			if (!entryp->mNeedsImageRaw && (entryp->mLastUsedDiscard > gl_discard))
			{
				BOOL final = gl_discard <= entryp->mDesiredDiscard ? TRUE : FALSE;
				entryp->mLastUsedDiscard = gl_discard;
				entryp->mCallback(TRUE, this, NULL, NULL, gl_discard, final, entryp->mUserData);
				if (final)
				{
					iter = mLoadedCallbackList.erase(curiter);
					delete entryp;
				}
				res = true;
			}
		}
	}

	//
	// If we have no callbacks, take us off of the image callback list.
	//
	if (mLoadedCallbackList.empty())
	{
		gTextureList.mCallbackList.erase(this);
	}

	// Done with any raw image data at this point (will be re-created if we still have callbacks)
	destroyRawImage();
	
	return res;
}

//virtual
void LLViewerFetchedTexture::forceImmediateUpdate()
{
	//only immediately update a deleted texture which is now being re-used.
	if(!isDeleted())
	{
		return ;
	}
	//if already called forceImmediateUpdate()
	if(mInImageList && mDecodePriority == LLViewerFetchedTexture::maxDecodePriority())
	{
		return ;
	}

	gTextureList.forceImmediateUpdate(this) ;
	return ;
}

// Was in LLImageGL
LLImageRaw* LLViewerFetchedTexture::readBackRawImage(S8 discard_level)
{
	llassert_always(mGLTexturep.notNull()) ;
	llassert_always(discard_level >= 0);
	llassert_always(mComponents > 0);
	if (mRawImage.notNull())
	{
		llerrs << "called with existing mRawImage" << llendl;
		mRawImage = NULL;
	}
	mRawImage = new LLImageRaw(mGLTexturep->getWidth(discard_level), mGLTexturep->getHeight(discard_level), mComponents);
	sRawCount++;
	mRawDiscardLevel = discard_level;
	mGLTexturep->readBackRaw(mRawDiscardLevel, mRawImage, false);
	mIsRawImageValid = TRUE;
	
	return mRawImage;
}

void LLViewerFetchedTexture::destroyRawImage()
{
	if (mRawImage.notNull()) sRawCount--;
	if (mAuxRawImage.notNull()) sAuxCount--;
	mRawImage = NULL;
	mAuxRawImage = NULL;
	mIsRawImageValid = FALSE;
	mRawDiscardLevel = INVALID_DISCARD_LEVEL;
}

//----------------------------------------------------------------------------------------------
//atlasing
//----------------------------------------------------------------------------------------------
void LLViewerFetchedTexture::resetFaceAtlas()
{
	//Nothing should be done here.
}

//invalidate all atlas slots for this image.
void LLViewerFetchedTexture::invalidateAtlas(BOOL rebuild_geom)
{
	for(ll_face_list_t::iterator iter = mFaceList.begin(); iter != mFaceList.end(); ++iter)
	{
		if(*iter)
		{
			LLFace* facep = (LLFace*)*iter ;
			facep->removeAtlas() ;
			if(rebuild_geom && facep->getDrawable() && facep->getDrawable()->getSpatialGroup())
			{
				facep->getDrawable()->getSpatialGroup()->setState(LLSpatialGroup::GEOM_DIRTY);
			}
		}
	}
}

BOOL LLViewerFetchedTexture::insertToAtlas()
{
	if(!LLViewerTexture::sUseTextureAtlas)
	{
		return FALSE ;
	}
	if(mFaceList.size() < 1)
	{
		return FALSE ;
	}						
	if(mGLTexturep->getDiscardLevelInAtlas() > 0 && mRawDiscardLevel >= mGLTexturep->getDiscardLevelInAtlas())
	{
		return FALSE ;
	}
	if(!LLTextureAtlasManager::getInstance()->canAddToAtlas(mRawImage->getWidth(), mRawImage->getHeight(), mRawImage->getComponents(), mGLTexturep->getTexTarget()))
	{
		return FALSE ;
	}

	BOOL ret = TRUE ;//if ret is set to false, will generate a gl texture for this image.
	S32 raw_w = mRawImage->getWidth() ;
	S32 raw_h = mRawImage->getHeight() ;
	F32 xscale = 1.0f, yscale = 1.0f ;
	LLPointer<LLTextureAtlasSlot> slot_infop;
	LLTextureAtlasSlot* cur_slotp ;//no need to be smart pointer.
	LLSpatialGroup* groupp ;
	LLFace* facep;

	//if the atlas slot pointers for some faces are null, process them later.
	ll_face_list_t waiting_list ;

	for(ll_face_list_t::iterator iter = mFaceList.begin(); iter != mFaceList.end(); ++iter)
	{
		if(*iter)
		{
			facep = (LLFace*)*iter ;
			
			//face can not use atlas.
			if(!facep->canUseAtlas())
			{
				if(facep->getAtlasInfo())
				{
					facep->removeAtlas() ;	
				}
				ret = FALSE ;
				continue ;
			}

			//the atlas slot is updated
			slot_infop = facep->getAtlasInfo() ;
			groupp = facep->getDrawable()->getSpatialGroup() ;	

			if(slot_infop) 
			{
				if(slot_infop->getSpatialGroup() != groupp)
				{
					if((cur_slotp = groupp->getCurUpdatingSlot(this))) //switch slot
					{
						facep->setAtlasInfo(cur_slotp) ;
						facep->setAtlasInUse(TRUE) ;
						continue ;
					}
					else //do not forget to update slot_infop->getSpatialGroup().
					{
						LLSpatialGroup* gp = slot_infop->getSpatialGroup() ;
						gp->setCurUpdatingTime(gFrameCount) ;
						gp->setCurUpdatingTexture(this) ;
						gp->setCurUpdatingSlot(slot_infop) ;
					}
				}
				else //same group
				{
					if(gFrameCount && slot_infop->getUpdatedTime() == gFrameCount)//slot is just updated
					{
						facep->setAtlasInUse(TRUE) ;
						continue ;
					}
				}
			}				
			else
			{
				//if the slot is null, wait to process them later.
				waiting_list.push_back(facep) ;
				continue ;
			}
						
			//----------
			//insert to atlas
			if(!slot_infop->getAtlas()->insertSubTexture(mGLTexturep, mRawDiscardLevel, mRawImage, slot_infop->getSlotCol(), slot_infop->getSlotRow()))			
			{
				
				//the texture does not qualify to add to atlas, do not bother to try for other faces.
				//invalidateAtlas();
				return FALSE ;
			}
			
			//update texture scale		
			slot_infop->getAtlas()->getTexCoordScale(raw_w, raw_h, xscale, yscale) ;
			slot_infop->setTexCoordScale(xscale, yscale) ;
			slot_infop->setValid() ;
			slot_infop->setUpdatedTime(gFrameCount) ;
			
			//update spatial group atlas info
			groupp->setCurUpdatingTime(gFrameCount) ;
			groupp->setCurUpdatingTexture(this) ;
			groupp->setCurUpdatingSlot(slot_infop) ;

			//make the face to switch to the atlas.
			facep->setAtlasInUse(TRUE) ;
		}
	}

	//process the waiting_list
	for(ll_face_list_t::iterator iter = waiting_list.begin(); iter != waiting_list.end(); ++iter)
	{
		facep = (LLFace*)*iter ;	
		groupp = facep->getDrawable()->getSpatialGroup() ;

		//check if this texture already inserted to atlas for this group
		if((cur_slotp = groupp->getCurUpdatingSlot(this)))
		{
			facep->setAtlasInfo(cur_slotp) ;
			facep->setAtlasInUse(TRUE) ;		
			continue ;
		}

		//need to reserve a slot from atlas
		slot_infop = LLTextureAtlasManager::getInstance()->reserveAtlasSlot(llmax(mFullWidth, mFullHeight), getComponents(), groupp, this) ;	

		facep->setAtlasInfo(slot_infop) ;
		
		groupp->setCurUpdatingTime(gFrameCount) ;
		groupp->setCurUpdatingTexture(this) ;
		groupp->setCurUpdatingSlot(slot_infop) ;

		//slot allocation failed.
		if(!slot_infop || !slot_infop->getAtlas())
		{			
			ret = FALSE ;
			facep->setAtlasInUse(FALSE) ;
			continue ;
		}
				
		//insert to atlas
		if(!slot_infop->getAtlas()->insertSubTexture(mGLTexturep, mRawDiscardLevel, mRawImage, slot_infop->getSlotCol(), slot_infop->getSlotRow()))
		{
			//the texture does not qualify to add to atlas, do not bother to try for other faces.
			ret = FALSE ;
			//invalidateAtlas();
			break ; 
		}
		
		//update texture scale		
		slot_infop->getAtlas()->getTexCoordScale(raw_w, raw_h, xscale, yscale) ;
		slot_infop->setTexCoordScale(xscale, yscale) ;
		slot_infop->setValid() ;
		slot_infop->setUpdatedTime(gFrameCount) ;

		//make the face to switch to the atlas.
		facep->setAtlasInUse(TRUE) ;
	}
	
	return ret ;
}

//----------------------------------------------------------------------------------------------
//end of LLViewerFetchedTexture
//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
//start of LLViewerLODTexture
//----------------------------------------------------------------------------------------------
LLViewerLODTexture::LLViewerLODTexture(const LLUUID& id, BOOL usemipmaps)
	: LLViewerFetchedTexture(id, usemipmaps)
{
	init(TRUE) ;
}

LLViewerLODTexture::LLViewerLODTexture(const std::string& full_path, const LLUUID& id, BOOL usemipmaps)
	: LLViewerFetchedTexture(full_path, id, usemipmaps)
{
	init(TRUE) ;
}

void LLViewerLODTexture::init(bool firstinit)
{
	mTexelsPerImage = 64.f*64.f;
	mDiscardVirtualSize = 0.f;
	mCalculatedDiscardLevel = -1.f;
}

//virtual 
S8 LLViewerLODTexture::getType() const
{
	return LLViewerTexture::LOD_TEXTURE ;
}

// This is gauranteed to get called periodically for every texture
//virtual
void LLViewerLODTexture::processTextureStats()
{
	// Generate the request priority and render priority
	if (mDontDiscard || !mUseMipMaps)
	{
		mDesiredDiscardLevel = 0;
		if (mFullWidth > MAX_IMAGE_SIZE_DEFAULT || mFullHeight > MAX_IMAGE_SIZE_DEFAULT)
			mDesiredDiscardLevel = 1; // MAX_IMAGE_SIZE_DEFAULT = 1024 and max size ever is 2048
	}
	else if (mBoostLevel < LLViewerTexture::BOOST_HIGH && mMaxVirtualSize <= 10.f)
	{
		// If the image has not been significantly visible in a while, we don't want it
		mDesiredDiscardLevel = llmin(mMinDesiredDiscardLevel, (S8)(MAX_DISCARD_LEVEL + 1));
	}
	else if (!mFullWidth  || !mFullHeight)
	{
		mDesiredDiscardLevel = 	getMaxDiscardLevel() ;
	}
	else
	{
		//static const F64 log_2 = log(2.0);
		static const F64 log_4 = log(4.0);

		S32 fullwidth = llmin(mFullWidth,(S32)MAX_IMAGE_SIZE_DEFAULT);
		S32 fullheight = llmin(mFullHeight,(S32)MAX_IMAGE_SIZE_DEFAULT);
		mTexelsPerImage = (F32)fullwidth * fullheight;

		F32 discard_level = 0.f;

		// If we know the output width and height, we can force the discard
		// level to the correct value, and thus not decode more texture
		// data than we need to.
		/*if (mBoostLevel == LLViewerTexture::BOOST_UI ||
			mBoostLevel == LLViewerTexture::BOOST_PREVIEW ||
			mBoostLevel == LLViewerTexture::BOOST_AVATAR_SELF)	// what about AVATAR_BAKED_SELF?
		{
			discard_level = 0; // full res
		}
		else*/ if (mKnownDrawWidth && mKnownDrawHeight)
		{
			S32 draw_texels = mKnownDrawWidth * mKnownDrawHeight;

			// Use log_4 because we're in square-pixel space, so an image
			// with twice the width and twice the height will have mTexelsPerImage
			// 4 * draw_size
			discard_level = (F32)(log(mTexelsPerImage/draw_texels) / log_4);
		}
		else
		{
			if ((mCalculatedDiscardLevel >= 0.f) &&
				(llabs(mMaxVirtualSize - mDiscardVirtualSize) < mMaxVirtualSize*.20f))
			{
				// < 20% change in virtual size = no change in desired discard
				discard_level = mCalculatedDiscardLevel; 
			}
			else
			{
				// Calculate the required scale factor of the image using pixels per texel
				discard_level = (F32)(log(mTexelsPerImage/mMaxVirtualSize) / log_4);
				mDiscardVirtualSize = mMaxVirtualSize;
				mCalculatedDiscardLevel = discard_level;
			}
		}
		if (mBoostLevel < LLViewerTexture::BOOST_HIGH)
		{
			static const F32 discard_bias = -.5f; // Must be < 1 or highest discard will never load!
			discard_level += discard_bias;
			discard_level += sDesiredDiscardBias;
			discard_level *= sDesiredDiscardScale; // scale
		}
		discard_level = floorf(discard_level);
// 		discard_level -= (gTextureList.mVideoMemorySetting>>1); // more video ram = higher detail

		F32 min_discard = 0.f;
		if (mFullWidth > MAX_IMAGE_SIZE_DEFAULT || mFullHeight > MAX_IMAGE_SIZE_DEFAULT)
			min_discard = 1.f; // MAX_IMAGE_SIZE_DEFAULT = 1024 and max size ever is 2048

		discard_level = llclamp(discard_level, min_discard, (F32)MAX_DISCARD_LEVEL);
		
		// Can't go higher than the max discard level
		mDesiredDiscardLevel = llmin(getMaxDiscardLevel() + 1, (S32)discard_level);
		// Clamp to min desired discard
		mDesiredDiscardLevel = llmin(mMinDesiredDiscardLevel, mDesiredDiscardLevel);

		//
		// At this point we've calculated the quality level that we want,
		// if possible.  Now we check to see if we have it, and take the
		// proper action if we don't.
		//

		BOOL increase_discard = FALSE;
		S32 current_discard = getDiscardLevel();
		if ((sDesiredDiscardBias > 0.0f) &&
			(current_discard >= 0 && mDesiredDiscardLevel >= current_discard))
		{
			if ( BYTES_TO_MEGA_BYTES(sBoundTextureMemoryInBytes) > sMaxBoundTextureMemInMegaBytes * texmem_middle_bound_scale)			
			{
				// Limit the amount of GL memory bound each frame
				if (mDesiredDiscardLevel > current_discard)
				{
					increase_discard = TRUE;
				}
			}
			if ( BYTES_TO_MEGA_BYTES(sTotalTextureMemoryInBytes) > sMaxTotalTextureMemInMegaBytes*texmem_middle_bound_scale)			
			{
				// Only allow GL to have 2x the video card memory
				if (!mGLTexturep->getBoundRecently())
				{
					increase_discard = TRUE;
				}
			}
			if (increase_discard)
			{
				// 			llinfos << "DISCARDED: " << mID << " Discard: " << current_discard << llendl;
				sBoundTextureMemoryInBytes -= mGLTexturep->mTextureMemory;
				sTotalTextureMemoryInBytes -= mGLTexturep->mTextureMemory;
				// Increase the discard level (reduce the texture res)
				S32 new_discard = current_discard+1;
				mGLTexturep->setDiscardLevel(new_discard);
				sBoundTextureMemoryInBytes += mGLTexturep->mTextureMemory;
				sTotalTextureMemoryInBytes += mGLTexturep->mTextureMemory;
				if(LLViewerTextureManager::sTesterp)
				{
					LLViewerTextureManager::sTesterp->setStablizingTime() ;
				}
			}
		}
	}
}

//----------------------------------------------------------------------------------------------
//end of LLViewerLODTexture
//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
//start of LLViewerMediaTexture
//----------------------------------------------------------------------------------------------
//static
void LLViewerMediaTexture::updateClass()
{
	static const F32 MAX_INACTIVE_TIME = 30.f ;

	for(media_map_t::iterator iter = sMediaMap.begin() ; iter != sMediaMap.end(); )
	{
		LLViewerMediaTexture* mediap = iter->second;	

		//
		//Note: delay some time to delete the media textures to stop endlessly creating and immediately removing media texture.
		//
		if(mediap->getNumRefs() == 1 && mediap->getLastReferencedTimer()->getElapsedTimeF32() > MAX_INACTIVE_TIME) //one by sMediaMap
		{
			media_map_t::iterator cur = iter++ ;
			sMediaMap.erase(cur) ;
		}
		else
		{
			++iter ;
		}
	}
}

//static 
void LLViewerMediaTexture::removeMediaImplFromTexture(const LLUUID& media_id) 
{
	LLViewerMediaTexture* media_tex = findMediaTexture(media_id) ;
	if(media_tex)
	{
		media_tex->invalidateMediaImpl() ;
	}
}

//static
void LLViewerMediaTexture::cleanup()
{
	sMediaMap.clear() ;
}

//static
LLViewerMediaTexture* LLViewerMediaTexture::findMediaTexture(const LLUUID& media_id)
{
	media_map_t::iterator iter = sMediaMap.find(media_id);
	if(iter == sMediaMap.end())
	{
		return NULL;
	}

	LLViewerMediaTexture* media_tex = iter->second ;
	media_tex->setMediaImpl() ;
	media_tex->getLastReferencedTimer()->reset() ;

	return media_tex;
}

LLViewerMediaTexture::LLViewerMediaTexture(const LLUUID& id, BOOL usemipmaps, LLImageGL* gl_image) 
	: LLViewerTexture(id, usemipmaps),
	mMediaImplp(NULL),
	mUpdateVirtualSizeTime(0)
{
	sMediaMap.insert(std::make_pair(id, this));

	mGLTexturep = gl_image ;
	if(mGLTexturep.isNull())
	{
		generateGLTexture() ;
	}
	mGLTexturep->setNeedsAlphaAndPickMask(FALSE) ;

	mIsPlaying = FALSE ;

	setMediaImpl() ;
}

//virtual 
LLViewerMediaTexture::~LLViewerMediaTexture() 
{	
}

void LLViewerMediaTexture::reinit(BOOL usemipmaps /* = TRUE */)
{
	mGLTexturep = NULL ;
	init(false);
	mUseMipMaps = usemipmaps ;
	getLastReferencedTimer()->reset() ;

	generateGLTexture() ;
	mGLTexturep->setNeedsAlphaAndPickMask(FALSE) ;
}

void LLViewerMediaTexture::setUseMipMaps(BOOL mipmap) 
{
	mUseMipMaps = mipmap;

	if(mGLTexturep.notNull())
	{
		mGLTexturep->setUseMipMaps(mipmap) ;
	}
}

//virtual 
S8 LLViewerMediaTexture::getType() const
{
	return LLViewerTexture::MEDIA_TEXTURE ;
}

void LLViewerMediaTexture::invalidateMediaImpl() 
{
	mMediaImplp = NULL ;
}

void LLViewerMediaTexture::setMediaImpl()
{
	if(!mMediaImplp)
	{
		mMediaImplp = LLViewerMedia::getMediaImplFromTextureID(mID) ;
	}
}

//return true if all faces to reference to this media texture are found
//Note: mMediaFaceList is valid only for the current instant 
//      because it does not check the face validity after the current frame.
BOOL LLViewerMediaTexture::findFaces()
{	
	mMediaFaceList.clear() ;

	BOOL ret = TRUE ;

	//for parcel media
	LLViewerTexture* tex = gTextureList.findImage(mID) ;	
	if(tex)
	{
		const ll_face_list_t* face_list = tex->getFaceList() ;
		for(ll_face_list_t::const_iterator iter = face_list->begin(); iter != face_list->end(); ++iter)
		{
			mMediaFaceList.push_back(*iter) ;
		}
	}
	
	if(!mMediaImplp)
	{
		return TRUE ; 
	}

	//for media on a face.
	const std::list< LLVOVolume* >* obj_list = mMediaImplp->getObjectList() ;
	std::list< LLVOVolume* >::const_iterator iter = obj_list->begin() ;
	for(; iter != obj_list->end(); ++iter)
	{
		LLVOVolume* obj = *iter ;
		if(obj->mDrawable.isNull())
		{
			ret = FALSE ;
			continue ;
		}

		S32 face_id = -1 ;
		while((face_id = obj->getFaceIndexWithMediaImpl(mMediaImplp, face_id)) > -1)
		{
			LLFace* facep = obj->mDrawable->getFace(face_id) ;
			if(facep)
			{
				mMediaFaceList.push_back(facep) ;
			}
			else
			{
				ret = FALSE ;
			}
		}
	}

	return ret ;
}

void LLViewerMediaTexture::initVirtualSize()
{
	if(mIsPlaying)
	{
		return ;
	}

	findFaces() ;
	for(std::list< LLFace* >::iterator iter = mMediaFaceList.begin(); iter!= mMediaFaceList.end(); ++iter)
	{
		addTextureStats((*iter)->getVirtualSize()) ;
	}
}

void LLViewerMediaTexture::addMediaToFace(LLFace* facep) 
{
	if(!mIsPlaying)
	{
		return ; //no need to add the face because the media is not in playing.
	}

	switchTexture(facep) ;
}
	
void LLViewerMediaTexture::removeMediaFromFace(LLFace* facep) 
{
	if(!mIsPlaying)
	{
		return ; //no need to remove the face because the media is not in playing.
	}
	if(!facep)
	{
		return ;
	}

	mIsPlaying = FALSE ; //set to remove the media from the face.
	switchTexture(facep) ;
	mIsPlaying = TRUE ; //set the flag back.

	if(mFaceList.empty()) //no face referencing to this media
	{
		stopPlaying() ;
	}
}

//virtual 
void LLViewerMediaTexture::addFace(LLFace* facep) 
{
	LLViewerTexture::addFace(facep) ;

	const LLTextureEntry* te = facep->getTextureEntry() ;
	if(te)
	{
		LLViewerTexture* tex = gTextureList.findImage(te->getID()) ;
		if(tex)
		{
			mTextureList.push_back(tex) ;//increase the reference number by one for tex to avoid deleting it.
			return ;
		}
	}
	llerrs << "The face does not have a valid texture before media texture." << llendl ;
}

//virtual 
void LLViewerMediaTexture::removeFace(LLFace* facep) 
{
	LLViewerTexture::removeFace(facep) ;

	const LLTextureEntry* te = facep->getTextureEntry() ;
	if(te)
	{
		LLViewerTexture* tex = gTextureList.findImage(te->getID()) ;
		if(tex)
		{
			for(std::list< LLPointer<LLViewerTexture> >::iterator iter = mTextureList.begin();
				iter != mTextureList.end(); ++iter)
			{
				if(*iter == tex)
				{
					mTextureList.erase(iter) ; //decrease the reference number for tex by one.
					return ;
				}
			}

			//
			//we have some trouble here: the texture of the face is changed.
			//we need to find the former texture, and remove it from the list to avoid memory leaking.
			if(mFaceList.empty())
			{
				mTextureList.clear() ;
				return ;
			}
			S32 end = mFaceList.size() ;
			std::vector<const LLTextureEntry*> te_list(end) ;
			S32 i = 0 ;			
			for(ll_face_list_t::iterator iter = mFaceList.begin(); iter != mFaceList.end(); ++iter)
			{
				te_list[i++] = (*iter)->getTextureEntry() ;//all textures are in use.
			}
			for(std::list< LLPointer<LLViewerTexture> >::iterator iter = mTextureList.begin();
				iter != mTextureList.end(); ++iter)
			{
				for(i = 0 ; i < end ; i++)
				{
					if(te_list[i] && te_list[i]->getID() == (*iter)->getID())//the texture is in use.
					{
						te_list[i] = NULL ;
						break ;
					}
				}
				if(i == end) //no hit for this texture, remove it.
				{
					mTextureList.erase(iter) ; //decrease the reference number for tex by one.
					return ;
				}
			}
		}
	}
	llerrs << "mTextureList texture reference number is corrupted." << llendl ;
}

void LLViewerMediaTexture::stopPlaying()
{
	if(mMediaImplp)
	{
		mMediaImplp->stop() ;
	}
	mIsPlaying = FALSE ;			
}

void LLViewerMediaTexture::switchTexture(LLFace* facep)
{
	if(facep)
	{
		//check if another media is playing on this face.
		if(facep->getTexture() && facep->getTexture() != this 
			&& facep->getTexture()->getType() == LLViewerTexture::MEDIA_TEXTURE)
		{
			if(mID == facep->getTexture()->getID()) //this is a parcel media
			{
				return ; //let the prim media win.
			}
		}

		if(mIsPlaying) //old textures switch to the media texture
		{
			facep->switchTexture(this) ;
		}
		else //switch to old textures.
		{
			const LLTextureEntry* te = facep->getTextureEntry() ;
			if(te)
			{
				LLViewerTexture* tex = gTextureList.findImage(te->getID()) ;
				facep->switchTexture(tex) ;
			}
		}
	}
}

void LLViewerMediaTexture::setPlaying(BOOL playing) 
{
	if(!mMediaImplp)
	{
		return ; 
	}
	if(!playing && !mIsPlaying)
	{
		return ; //media is already off
	}

	if(playing == mIsPlaying && !mMediaImplp->isUpdated())
	{
		return ; //nothing has changed since last time.
	}	

	mIsPlaying = playing ;
	if(mIsPlaying) //is about to play this media
	{
		if(findFaces())
		{
			//about to update all faces.
			mMediaImplp->setUpdated(FALSE) ;
		}

		if(mMediaFaceList.empty())//no face pointing to this media
		{
			stopPlaying() ;
			return ;
		}

		for(std::list< LLFace* >::iterator iter = mMediaFaceList.begin(); iter!= mMediaFaceList.end(); ++iter)
		{
			switchTexture(*iter) ;
		}
	}
	else //stop playing this media
	{
		if(mFaceList.empty())
		{
			return ;
		}

		ll_face_list_t::iterator cur ;
		for(ll_face_list_t::iterator iter = mFaceList.begin(); iter!= mFaceList.end(); )
		{
			cur = iter++ ; 
			switchTexture(*cur) ; //cur could be removed in this function.
		}
	}
	return ;
}

//virtual 
F32 LLViewerMediaTexture::getMaxVirtualSize() 
{	
	if(LLFrameTimer::getFrameCount() == mUpdateVirtualSizeTime)
	{
		return mMaxVirtualSize ;
	}
	mUpdateVirtualSizeTime = LLFrameTimer::getFrameCount() ;

	if(mNeedsResetMaxVirtualSize)
	{
		mMaxVirtualSize = 0.f ;//reset
		mNeedsResetMaxVirtualSize = FALSE ;
	}

	if(mIsPlaying) //media is playing
	{
		if(mFaceList.size() > 0) 
		{				
			for(std::list<LLFace*>::iterator iter = mFaceList.begin(); iter != mFaceList.end(); ++iter)
			{
				LLFace* facep = *iter ;
				if(facep->getDrawable()->isRecentlyVisible())
				{
					addTextureStats(facep->getVirtualSize()) ;
				}
			}	
		}
	}
	else //media is not in playing
	{
		findFaces() ;
	
		if(!mMediaFaceList.empty())
		{
			for(std::list< LLFace* >::iterator iter = mMediaFaceList.begin(); iter!= mMediaFaceList.end(); ++iter)
			{
				LLFace* facep = *iter ;
				if(facep->getDrawable()->isRecentlyVisible())
				{
					addTextureStats(facep->getVirtualSize()) ;
				}
			}
		}
	}

	mNeedsResetMaxVirtualSize = TRUE ;

	return mMaxVirtualSize ;
}
//----------------------------------------------------------------------------------------------
//end of LLViewerMediaTexture
//----------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------
//start of LLTexturePipelineTester
//----------------------------------------------------------------------------------------------
LLTexturePipelineTester::LLTexturePipelineTester() :
	LLMetricPerformanceTester("TextureTester", FALSE) 
{
	addMetricString("TotalBytesLoaded") ;
	addMetricString("TotalBytesLoadedFromCache") ;
	addMetricString("TotalBytesLoadedForLargeImage") ;
	addMetricString("TotalBytesLoadedForSculpties") ;
	addMetricString("StartFetchingTime") ;
	addMetricString("TotalGrayTime") ;
	addMetricString("TotalStablizingTime") ;
	addMetricString("StartTimeLoadingSculpties") ;
	addMetricString("EndTimeLoadingSculpties") ;

	addMetricString("Time") ;
	addMetricString("TotalBytesBound") ;
	addMetricString("TotalBytesBoundForLargeImage") ;
	addMetricString("PercentageBytesBound") ;
	
	mTotalBytesLoaded = 0 ;
	mTotalBytesLoadedFromCache = 0 ;	
	mTotalBytesLoadedForLargeImage = 0 ;
	mTotalBytesLoadedForSculpties = 0 ;

	reset() ;
}

LLTexturePipelineTester::~LLTexturePipelineTester()
{
	LLViewerTextureManager::sTesterp = NULL ;
}

void LLTexturePipelineTester::update()
{
	mLastTotalBytesUsed = mTotalBytesUsed ;
	mLastTotalBytesUsedForLargeImage = mTotalBytesUsedForLargeImage ;
	mTotalBytesUsed = 0 ;
	mTotalBytesUsedForLargeImage = 0 ;
	
	if(LLAppViewer::getTextureFetch()->getNumRequests() > 0) //fetching list is not empty
	{
		if(mPause)
		{
			//start a new fetching session
			reset() ;
			mStartFetchingTime = LLImageGL::sLastFrameTime ;
			mPause = FALSE ;
		}

		//update total gray time		
		if(mUsingDefaultTexture)
		{
			mUsingDefaultTexture = FALSE ;
			mTotalGrayTime = LLImageGL::sLastFrameTime - mStartFetchingTime ;		
		}

		//update the stablizing timer.
		updateStablizingTime() ;

		outputTestResults() ;
	}
	else if(!mPause)
	{
		//stop the current fetching session
		mPause = TRUE ;
		outputTestResults() ;
		reset() ;
	}		
}
	
void LLTexturePipelineTester::reset() 
{
	mPause = TRUE ;

	mUsingDefaultTexture = FALSE ;
	mStartStablizingTime = 0.0f ;
	mEndStablizingTime = 0.0f ;

	mTotalBytesUsed = 0 ;
	mTotalBytesUsedForLargeImage = 0 ;
	mLastTotalBytesUsed = 0 ;
	mLastTotalBytesUsedForLargeImage = 0 ;
	
	mStartFetchingTime = 0.0f ;
	
	mTotalGrayTime = 0.0f ;
	mTotalStablizingTime = 0.0f ;

	mStartTimeLoadingSculpties = 1.0f ;
	mEndTimeLoadingSculpties = 0.0f ;
}

//virtual 
void LLTexturePipelineTester::outputTestRecord(LLSD *sd) 
{	
	(*sd)[mCurLabel]["TotalBytesLoaded"]              = (LLSD::Integer)mTotalBytesLoaded ;
	(*sd)[mCurLabel]["TotalBytesLoadedFromCache"]     = (LLSD::Integer)mTotalBytesLoadedFromCache ;
	(*sd)[mCurLabel]["TotalBytesLoadedForLargeImage"] = (LLSD::Integer)mTotalBytesLoadedForLargeImage ;
	(*sd)[mCurLabel]["TotalBytesLoadedForSculpties"]  = (LLSD::Integer)mTotalBytesLoadedForSculpties ;

	(*sd)[mCurLabel]["StartFetchingTime"]             = (LLSD::Real)mStartFetchingTime ;
	(*sd)[mCurLabel]["TotalGrayTime"]                 = (LLSD::Real)mTotalGrayTime ;
	(*sd)[mCurLabel]["TotalStablizingTime"]           = (LLSD::Real)mTotalStablizingTime ;

	(*sd)[mCurLabel]["StartTimeLoadingSculpties"]     = (LLSD::Real)mStartTimeLoadingSculpties ;
	(*sd)[mCurLabel]["EndTimeLoadingSculpties"]       = (LLSD::Real)mEndTimeLoadingSculpties ;

	(*sd)[mCurLabel]["Time"]                          = LLImageGL::sLastFrameTime ;
	(*sd)[mCurLabel]["TotalBytesBound"]               = (LLSD::Integer)mLastTotalBytesUsed ;
	(*sd)[mCurLabel]["TotalBytesBoundForLargeImage"]  = (LLSD::Integer)mLastTotalBytesUsedForLargeImage ;
	(*sd)[mCurLabel]["PercentageBytesBound"]          = (LLSD::Real)(100.f * mLastTotalBytesUsed / mTotalBytesLoaded) ;
}

void LLTexturePipelineTester::updateTextureBindingStats(const LLViewerTexture* imagep) 
{
	U32 mem_size = (U32)imagep->getTextureMemory() ;
	mTotalBytesUsed += mem_size ; 

	if(MIN_LARGE_IMAGE_AREA <= (U32)(mem_size / (U32)imagep->getComponents()))
	{
		mTotalBytesUsedForLargeImage += mem_size ;
	}
}
	
void LLTexturePipelineTester::updateTextureLoadingStats(const LLViewerFetchedTexture* imagep, const LLImageRaw* raw_imagep, BOOL from_cache) 
{
	U32 data_size = (U32)raw_imagep->getDataSize() ;
	mTotalBytesLoaded += data_size ;

	if(from_cache)
	{
		mTotalBytesLoadedFromCache += data_size ;
	}

	if(MIN_LARGE_IMAGE_AREA <= (U32)(data_size / (U32)raw_imagep->getComponents()))
	{
		mTotalBytesLoadedForLargeImage += data_size ;
	}

	if(imagep->isForSculpt())
	{
		mTotalBytesLoadedForSculpties += data_size ;

		if(mStartTimeLoadingSculpties > mEndTimeLoadingSculpties)
		{
			mStartTimeLoadingSculpties = LLImageGL::sLastFrameTime ;
		}
		mEndTimeLoadingSculpties = LLImageGL::sLastFrameTime ;
	}
}

void LLTexturePipelineTester::updateGrayTextureBinding()
{
	mUsingDefaultTexture = TRUE ;
}

void LLTexturePipelineTester::setStablizingTime()
{
	if(mStartStablizingTime <= mStartFetchingTime)
	{
		mStartStablizingTime = LLImageGL::sLastFrameTime ;
	}
	mEndStablizingTime = LLImageGL::sLastFrameTime ;
}

void LLTexturePipelineTester::updateStablizingTime()
{
	if(mStartStablizingTime > mStartFetchingTime)
	{
		F32 t = mEndStablizingTime - mStartStablizingTime ;

		if(t > 0.0001f && (t - mTotalStablizingTime) < 0.0001f)
		{
			//already stablized
			mTotalStablizingTime = LLImageGL::sLastFrameTime - mStartStablizingTime ;

			//cancel the timer
			mStartStablizingTime = 0.f ;
			mEndStablizingTime = 0.f ;
		}
		else
		{
			mTotalStablizingTime = t ;
		}
	}
	mTotalStablizingTime = 0.f ;
}

//virtual 
void LLTexturePipelineTester::compareTestSessions(std::ofstream* os) 
{	
	LLTexturePipelineTester::LLTextureTestSession* base_sessionp = dynamic_cast<LLTexturePipelineTester::LLTextureTestSession*>(mBaseSessionp) ;
	LLTexturePipelineTester::LLTextureTestSession* current_sessionp = dynamic_cast<LLTexturePipelineTester::LLTextureTestSession*>(mCurrentSessionp) ;
	if(!base_sessionp || !current_sessionp)
	{
		llerrs << "type of test session does not match!" << llendl ;
	}

	//compare and output the comparison
	*os << llformat("%s\n", mName.c_str()) ;
	*os << llformat("AggregateResults\n") ;

	compareTestResults(os, "TotalFetchingTime", base_sessionp->mTotalFetchingTime, current_sessionp->mTotalFetchingTime) ;
	compareTestResults(os, "TotalGrayTime", base_sessionp->mTotalGrayTime, current_sessionp->mTotalGrayTime) ;
	compareTestResults(os, "TotalStablizingTime", base_sessionp->mTotalStablizingTime, current_sessionp->mTotalStablizingTime);
	compareTestResults(os, "StartTimeLoadingSculpties", base_sessionp->mStartTimeLoadingSculpties, current_sessionp->mStartTimeLoadingSculpties) ;		
	compareTestResults(os, "TotalTimeLoadingSculpties", base_sessionp->mTotalTimeLoadingSculpties, current_sessionp->mTotalTimeLoadingSculpties) ;
	
	compareTestResults(os, "TotalBytesLoaded", base_sessionp->mTotalBytesLoaded, current_sessionp->mTotalBytesLoaded) ;
	compareTestResults(os, "TotalBytesLoadedFromCache", base_sessionp->mTotalBytesLoadedFromCache, current_sessionp->mTotalBytesLoadedFromCache) ;
	compareTestResults(os, "TotalBytesLoadedForLargeImage", base_sessionp->mTotalBytesLoadedForLargeImage, current_sessionp->mTotalBytesLoadedForLargeImage) ;
	compareTestResults(os, "TotalBytesLoadedForSculpties", base_sessionp->mTotalBytesLoadedForSculpties, current_sessionp->mTotalBytesLoadedForSculpties) ;
		
	*os << llformat("InstantResults\n") ;
	S32 size = llmin(base_sessionp->mInstantPerformanceListCounter, current_sessionp->mInstantPerformanceListCounter) ;
	for(S32 i = 0 ; i < size ; i++)
	{
		*os << llformat("Time(B-T)-%.4f-%.4f\n", base_sessionp->mInstantPerformanceList[i].mTime, current_sessionp->mInstantPerformanceList[i].mTime) ;

		compareTestResults(os, "AverageBytesUsedPerSecond", base_sessionp->mInstantPerformanceList[i].mAverageBytesUsedPerSecond,
			current_sessionp->mInstantPerformanceList[i].mAverageBytesUsedPerSecond) ;
			
		compareTestResults(os, "AverageBytesUsedForLargeImagePerSecond", base_sessionp->mInstantPerformanceList[i].mAverageBytesUsedForLargeImagePerSecond,
			current_sessionp->mInstantPerformanceList[i].mAverageBytesUsedForLargeImagePerSecond) ;
			
		compareTestResults(os, "AveragePercentageBytesUsedPerSecond", base_sessionp->mInstantPerformanceList[i].mAveragePercentageBytesUsedPerSecond,
			current_sessionp->mInstantPerformanceList[i].mAveragePercentageBytesUsedPerSecond) ;
	}
	
	if(size < base_sessionp->mInstantPerformanceListCounter)
	{
		for(S32 i = size ; i < base_sessionp->mInstantPerformanceListCounter ; i++)
		{
			*os << llformat("Time(B-T)-%.4f- \n", base_sessionp->mInstantPerformanceList[i].mTime) ;

			*os << llformat(", AverageBytesUsedPerSecond, %d, N/A \n", base_sessionp->mInstantPerformanceList[i].mAverageBytesUsedPerSecond) ;
			*os << llformat(", AverageBytesUsedForLargeImagePerSecond, %d, N/A \n", base_sessionp->mInstantPerformanceList[i].mAverageBytesUsedForLargeImagePerSecond) ;				
			*os << llformat(", AveragePercentageBytesUsedPerSecond, %.4f, N/A \n", base_sessionp->mInstantPerformanceList[i].mAveragePercentageBytesUsedPerSecond) ;			
		}
	}
	else if(size < current_sessionp->mInstantPerformanceListCounter)
	{
		for(S32 i = size ; i < current_sessionp->mInstantPerformanceListCounter ; i++)
		{
			*os << llformat("Time(B-T)- -%.4f\n", current_sessionp->mInstantPerformanceList[i].mTime) ;

			*os << llformat(", AverageBytesUsedPerSecond, N/A, %d\n", current_sessionp->mInstantPerformanceList[i].mAverageBytesUsedPerSecond) ;
			*os << llformat(", AverageBytesUsedForLargeImagePerSecond, N/A, %d\n", current_sessionp->mInstantPerformanceList[i].mAverageBytesUsedForLargeImagePerSecond) ;				
			*os << llformat(", AveragePercentageBytesUsedPerSecond, N/A, %.4f\n", current_sessionp->mInstantPerformanceList[i].mAveragePercentageBytesUsedPerSecond) ;			
		}
	}
}

//virtual 
LLMetricPerformanceTester::LLTestSession* LLTexturePipelineTester::loadTestSession(LLSD* log)
{
	LLTexturePipelineTester::LLTextureTestSession* sessionp = new LLTexturePipelineTester::LLTextureTestSession() ;
	if(!sessionp)
	{
		return NULL ;
	}
	
	F32 total_fetching_time = 0.f ;
	F32 total_gray_time = 0.f ;
	F32 total_stablizing_time = 0.f ;
	F32 total_loading_sculpties_time = 0.f ;

	F32 start_fetching_time = -1.f ;
	F32 start_fetching_sculpties_time = 0.f ;

	F32 last_time = 0.0f ;
	S32 frame_count = 0 ;

	sessionp->mInstantPerformanceListCounter = 0 ;
	sessionp->mInstantPerformanceList.resize(128) ;
	sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedPerSecond = 0 ;
	sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedForLargeImagePerSecond = 0 ;
	sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAveragePercentageBytesUsedPerSecond = 0.f ;
	sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mTime = 0.f ;
	
	//load a session
	BOOL in_log = (*log).has(mCurLabel) ;
	while(in_log)
	{
		LLSD::String label = mCurLabel ;		
		incLabel() ;
		in_log = (*log).has(mCurLabel) ;

		if(sessionp->mInstantPerformanceListCounter >= (S32)sessionp->mInstantPerformanceList.size())
		{
			sessionp->mInstantPerformanceList.resize(sessionp->mInstantPerformanceListCounter + 128) ;
		}
		
		//time
		F32 start_time = (*log)[label]["StartFetchingTime"].asReal() ;
		F32 cur_time   = (*log)[label]["Time"].asReal() ;
		if(start_time - start_fetching_time > 0.0001f) //fetching has paused for a while
		{
			sessionp->mTotalFetchingTime += total_fetching_time ;
			sessionp->mTotalGrayTime += total_gray_time ;
			sessionp->mTotalStablizingTime += total_stablizing_time ;

			sessionp->mStartTimeLoadingSculpties = start_fetching_sculpties_time ; 
			sessionp->mTotalTimeLoadingSculpties += total_loading_sculpties_time ;

			start_fetching_time = start_time ;
			total_fetching_time = 0.0f ;
			total_gray_time = 0.f ;
			total_stablizing_time = 0.f ;
			total_loading_sculpties_time = 0.f ;
		}
		else
		{
			total_fetching_time = cur_time - start_time ;
			total_gray_time = (*log)[label]["TotalGrayTime"].asReal() ;
			total_stablizing_time = (*log)[label]["TotalStablizingTime"].asReal() ;

			total_loading_sculpties_time = (*log)[label]["EndTimeLoadingSculpties"].asReal() - (*log)[label]["StartTimeLoadingSculpties"].asReal() ;
			if(start_fetching_sculpties_time < 0.f && total_loading_sculpties_time > 0.f)
			{
				start_fetching_sculpties_time = (*log)[label]["StartTimeLoadingSculpties"].asReal() ;
			}			
		}
		
		//total loaded bytes
		sessionp->mTotalBytesLoaded = (*log)[label]["TotalBytesLoaded"].asInteger() ; 
		sessionp->mTotalBytesLoadedFromCache = (*log)[label]["TotalBytesLoadedFromCache"].asInteger() ;
		sessionp->mTotalBytesLoadedForLargeImage = (*log)[label]["TotalBytesLoadedForLargeImage"].asInteger() ;
		sessionp->mTotalBytesLoadedForSculpties = (*log)[label]["TotalBytesLoadedForSculpties"].asInteger() ; 

		//instant metrics			
		sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedPerSecond +=
			(*log)[label]["TotalBytesBound"].asInteger() ;
		sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedForLargeImagePerSecond +=
			(*log)[label]["TotalBytesBoundForLargeImage"].asInteger() ;
		sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAveragePercentageBytesUsedPerSecond +=
			(*log)[label]["PercentageBytesBound"].asReal() ;
		frame_count++ ;
		if(cur_time - last_time >= 1.0f)
		{
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedPerSecond /= frame_count ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedForLargeImagePerSecond /= frame_count ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAveragePercentageBytesUsedPerSecond /= frame_count ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mTime = last_time ;

			frame_count = 0 ;
			last_time = cur_time ;
			sessionp->mInstantPerformanceListCounter++ ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedPerSecond = 0 ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAverageBytesUsedForLargeImagePerSecond = 0 ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mAveragePercentageBytesUsedPerSecond = 0.f ;
			sessionp->mInstantPerformanceList[sessionp->mInstantPerformanceListCounter].mTime = 0.f ;
		}		
	}

	sessionp->mTotalFetchingTime += total_fetching_time ;
	sessionp->mTotalGrayTime += total_gray_time ;
	sessionp->mTotalStablizingTime += total_stablizing_time ;

	if(sessionp->mStartTimeLoadingSculpties < 0.f)
	{
		sessionp->mStartTimeLoadingSculpties = start_fetching_sculpties_time ; 
	}
	sessionp->mTotalTimeLoadingSculpties += total_loading_sculpties_time ;

	return sessionp;
}

LLTexturePipelineTester::LLTextureTestSession::LLTextureTestSession() 
{
	reset() ;
}
LLTexturePipelineTester::LLTextureTestSession::~LLTextureTestSession() 
{
}
void LLTexturePipelineTester::LLTextureTestSession::reset() 
{
	mTotalFetchingTime = 0.0f ;

	mTotalGrayTime = 0.0f ;
	mTotalStablizingTime = 0.0f ;

	mStartTimeLoadingSculpties = 0.0f ; 
	mTotalTimeLoadingSculpties = 0.0f ;

	mTotalBytesLoaded = 0 ; 
	mTotalBytesLoadedFromCache = 0 ;
	mTotalBytesLoadedForLargeImage = 0 ;
	mTotalBytesLoadedForSculpties = 0 ; 

	mInstantPerformanceListCounter = 0 ;
}
//----------------------------------------------------------------------------------------------
//end of LLTexturePipelineTester
//----------------------------------------------------------------------------------------------