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
|
/**
* @file llviewerimage.cpp
* @brief Object which handles a received image (and associated texture(s))
*
* Copyright (c) 2000-2007, Linden Research, Inc.
*
* 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://secondlife.com/developers/opensource/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://secondlife.com/developers/opensource/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.
*/
#include "llviewerprecompiledheaders.h"
#include "llviewerimage.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 "lltexturetable.h"
#include "llvfile.h"
#include "llvfs.h"
#include "message.h"
#include "lltimer.h"
// viewer includes
#include "lldrawpool.h"
#include "lltexturefetch.h"
#include "llviewerimagelist.h"
#include "llviewercontrol.h"
#include "pipeline.h"
#include "viewer.h"
///////////////////////////////////////////////////////////////////////////////
// statics
LLPointer<LLViewerImage> LLViewerImage::sMissingAssetImagep = NULL;
LLPointer<LLViewerImage> LLViewerImage::sWhiteImagep = NULL;
LLPointer<LLImageGL> LLViewerImage::sDefaultImagep = NULL;
LLPointer<LLViewerImage> LLViewerImage::sSmokeImagep = NULL;
LLPointer<LLImageGL> LLViewerImage::sNullImagep = NULL;
S32 LLViewerImage::sImageCount = 0;
LLTimer LLViewerImage::sEvaluationTimer;
F32 LLViewerImage::sDesiredDiscardBias = 0.f;
static F32 sDesiredDiscardBiasMin = -2.0f; // -max number of levels to improve image quality by
static F32 sDesiredDiscardBiasMax = 1.5f; // max number of levels to reduce image quality by
F32 LLViewerImage::sDesiredDiscardScale = 1.1f;
S32 LLViewerImage::sBoundTextureMemory = 0;
S32 LLViewerImage::sTotalTextureMemory = 0;
S32 LLViewerImage::sMaxBoundTextureMem = 0;
S32 LLViewerImage::sMaxTotalTextureMem = 0;
BOOL LLViewerImage::sDontLoadVolumeTextures = FALSE;
// static
void LLViewerImage::initClass()
{
sNullImagep = new LLImageGL(1,1,3,TRUE);
LLPointer<LLImageRaw> raw = new LLImageRaw(1,1,3);
raw->clear(0x77, 0x77, 0x77, 0xFF);
sNullImagep->createGLTexture(0, raw);
#if 1
LLViewerImage* imagep = new LLViewerImage(IMG_DEFAULT, TRUE);
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;
gImageList.addImage(imagep);
imagep->dontDiscard();
#else
sDefaultImagep = gImageList.getImage(IMG_DEFAULT, TRUE, TRUE);
#endif
sSmokeImagep = gImageList.getImage(IMG_SMOKE, TRUE, TRUE);
}
// static
void LLViewerImage::cleanupClass()
{
stop_glerror();
sNullImagep = NULL;
sDefaultImagep = NULL;
sSmokeImagep = NULL;
sMissingAssetImagep = NULL;
sWhiteImagep = NULL;
}
// 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 LLViewerImage::updateClass(const F32 velocity, const F32 angular_velocity)
{
sBoundTextureMemory = LLImageGL::sBoundTextureMemory;
sTotalTextureMemory = LLImageGL::sGlobalTextureMemory;
sMaxBoundTextureMem = gImageList.getMaxResidentTexMem();
sMaxTotalTextureMem = sMaxBoundTextureMem * 2;
if (sMaxBoundTextureMem > 64000000)
{
sMaxTotalTextureMem -= sMaxBoundTextureMem/4;
}
if (sMaxTotalTextureMem > (S32)gSysMemory.getPhysicalMemory() - min_non_tex_system_mem)
{
sMaxTotalTextureMem = (S32)gSysMemory.getPhysicalMemory() - min_non_tex_system_mem;
}
if (sBoundTextureMemory >= sMaxBoundTextureMem ||
sTotalTextureMemory >= sMaxTotalTextureMem)
{
// 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 &&
sBoundTextureMemory < sMaxBoundTextureMem*texmem_lower_bound_scale &&
sTotalTextureMemory < sMaxTotalTextureMem*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, sDesiredDiscardBiasMin, sDesiredDiscardBiasMax);
}
//----------------------------------------------------------------------------
const U32 LLViewerImage::sCurrentFileVersion = 1;
LLViewerImage::LLViewerImage(const LLUUID& id, BOOL usemipmaps)
: LLImageGL(usemipmaps),
mID(id)
{
init(true);
sImageCount++;
}
LLViewerImage::LLViewerImage(const U32 width, const U32 height, const U8 components, BOOL usemipmaps)
: LLImageGL(width, height, components, usemipmaps)
{
init(true);
mNeedsAux = FALSE;
// Create an empty image of the specified size and width
mID.generate();
mFullyLoaded = TRUE;
sImageCount++;
}
LLViewerImage::LLViewerImage(const LLImageRaw* raw, BOOL usemipmaps)
: LLImageGL(raw, usemipmaps)
{
init(true);
mNeedsAux = FALSE;
// Create an empty image of the specified size and width
mID.generate();
mFullyLoaded = TRUE;
sImageCount++;
}
void LLViewerImage::init(bool firstinit)
{
mFullWidth = 0;
mFullHeight = 0;
mNeedsAux = FALSE;
mTexelsPerImage = 64.f*64.f;
mMaxVirtualSize = 0.f;
mDiscardVirtualSize = 0.f;
mMaxCosAngle = -1.f;
mRequestedDiscardLevel = -1;
mRequestedDownloadPriority = 0.f;
mFullyLoaded = FALSE;
mDesiredDiscardLevel = MAX_DISCARD_LEVEL + 1;
mMinDesiredDiscardLevel = MAX_DISCARD_LEVEL + 1;
mCalculatedDiscardLevel = -1.f;
mDecodingAux = FALSE;
mKnownDrawWidth = 0;
mKnownDrawHeight = 0;
if (firstinit)
{
mDecodePriority = 0.f;
mInImageList = 0;
}
mIsMediaTexture = FALSE;
mBoostLevel = LLViewerImage::BOOST_NONE;
// 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;
mTargetHost = LLHost::invalid;
mHasFetcher = FALSE;
mIsFetching = FALSE;
mFetchState = 0;
mFetchPriority = 0;
mDownloadProgress = 0.f;
mFetchDeltaTime = 999999.f;
mDecodeFrame = 0;
mVisibleFrame = 0;
}
// virtual
void LLViewerImage::dump()
{
LLImageGL::dump();
llinfos << "LLViewerImage"
<< " mID " << mID
<< " mIsMissingAsset " << (S32)mIsMissingAsset
<< " mFullWidth " << mFullWidth
<< " mFullHeight " << mFullHeight
<< llendl;
}
///////////////////////////////////////////////////////////////////////////////
LLViewerImage::~LLViewerImage()
{
if (mHasFetcher)
{
gTextureFetch->deleteRequest(getID(), true);
}
// Explicitly call LLViewerImage::cleanup since we're in a destructor and cleanup is virtual
LLViewerImage::cleanup();
sImageCount--;
}
///////////////////////////////////////////////////////////////////////////////
void LLViewerImage::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();
// Clean up image data
destroyRawImage();
// LLImageGL::cleanup will get called more than once when this is used in the destructor.
LLImageGL::cleanup();
}
void LLViewerImage::reinit(BOOL usemipmaps /* = TRUE */)
{
LLViewerImage::cleanup();
LLImageGL::init(usemipmaps);
init(false);
setSize(0,0,0);
}
///////////////////////////////////////////////////////////////////////////////
// ONLY called from LLViewerImageList
BOOL LLViewerImage::createTexture(S32 usename/*= 0*/)
{
if (!mNeedsCreateTexture)
{
destroyRawImage();
return FALSE;
}
mNeedsCreateTexture = FALSE;
if (mRawImage.isNull())
{
llerrs << "LLViewerImage 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)
{
if (LLImageGL::checkSize(mRawImage->getWidth(), mRawImage->getHeight()))
{
res = LLImageGL::createGLTexture(mRawDiscardLevel, mRawImage, usename);
}
else
{
// A non power-of-two 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;
}
}
//
// 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)
{
destroyRawImage();
}
return res;
}
//============================================================================
void LLViewerImage::addTextureStats(F32 pixel_area,
F32 texel_area_ratio, // = 1.0
F32 cos_center_angle) const // = 1.0
{
F32 virtual_size = pixel_area / texel_area_ratio;
if (virtual_size > mMaxVirtualSize)
{
mMaxVirtualSize = virtual_size;
}
cos_center_angle = llclamp(cos_center_angle, -1.f, 1.f);
if (cos_center_angle > mMaxCosAngle)
{
mMaxCosAngle = cos_center_angle;
}
}
void LLViewerImage::resetTextureStats(BOOL zero)
{
if (zero)
{
mMaxVirtualSize = 0.0f;
mMaxCosAngle = -1.0f;
}
else
{
mMaxVirtualSize -= mMaxVirtualSize * .10f; // decay by 5%/update
mMaxCosAngle = -1.0f;
}
}
// This is gauranteed to get called periodically for every texture
void LLViewerImage::processTextureStats()
{
// Generate the request priority and render priority
if (mDontDiscard || !getUseMipMaps())
{
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 < LLViewerImage::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 && !mWidth) || (!mFullHeight && !mHeight))
{
mDesiredDiscardLevel = mMaxDiscardLevel;
}
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 == LLViewerImage::BOOST_UI ||
mBoostLevel == LLViewerImage::BOOST_PREVIEW ||
mBoostLevel == LLViewerImage::BOOST_AVATAR_SELF) // JAMESDEBUG 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 < LLViewerImage::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 -= (gImageList.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((S32)mMaxDiscardLevel+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 ( sBoundTextureMemory > sMaxBoundTextureMem*texmem_middle_bound_scale)
{
// Limit the amount of GL memory bound each frame
if (mDesiredDiscardLevel > current_discard)
{
increase_discard = TRUE;
}
}
if ( sTotalTextureMemory > sMaxTotalTextureMem*texmem_middle_bound_scale)
{
// Only allow GL to have 2x the video card memory
if (!getBoundRecently())
{
increase_discard = TRUE;
}
}
if (increase_discard)
{
// llinfos << "DISCARDED: " << mID << " Discard: " << current_discard << llendl;
sBoundTextureMemory -= mTextureMemory;
sTotalTextureMemory -= mTextureMemory;
// Increase the discard level (reduce the texture res)
S32 new_discard = current_discard+1;
setDiscardLevel(new_discard);
sBoundTextureMemory += mTextureMemory;
sTotalTextureMemory += mTextureMemory;
}
}
}
}
//============================================================================
F32 LLViewerImage::calcDecodePriority()
{
#ifndef LL_RELEASE_FOR_DOWNLOAD
if (mID == gTextureFetch->mDebugID)
{
gTextureFetch->mDebugCount++; // for setting breakpoints
}
#endif
if (mNeedsCreateTexture)
{
return mDecodePriority; // no change while waiting to create
}
F32 priority;
S32 cur_discard = getDiscardLevel();
F32 pixel_priority = fsqrtf(mMaxVirtualSize) * (1.f + mMaxCosAngle);
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 > mMaxDiscardLevel)
{
// Don't decode anything we don't need
priority = -1.0f;
}
else if (pixel_priority <= 0.f && (cur_discard < 0 || mDesiredDiscardLevel < cur_discard))
{
// 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)
{
// We don't have any data yet, so we don't know the size of the image, treat as 1024x1024
// priority = 900000.f;
static const F64 log_2 = log(2.0);
F32 desired = (F32)(log(1024.0/pixel_priority) / log_2);
S32 ddiscard = MAX_DISCARD_LEVEL - (S32)desired + 1;
ddiscard = llclamp(ddiscard, 1, 9);
priority = ddiscard*100000.f;
}
else if (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 (!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 += pixel_priority;
if ( mBoostLevel > BOOST_HIGH)
{
priority += 1000000.f + 1000.f * mBoostLevel;
}
else if ( mBoostLevel > 0)
{
priority += 0.f + 1000.f * mBoostLevel;
}
}
return priority;
}
// A value >= max value calculated above for normalization
//static
F32 LLViewerImage::maxDecodePriority()
{
return 2000000.f;
}
void LLViewerImage::setDecodePriority(F32 priority)
{
llassert(!mInImageList);
if (priority < 0.0f)
{
mDecodePriority = calcDecodePriority();
}
else
{
mDecodePriority = priority;
}
}
void LLViewerImage::setBoostLevel(S32 level)
{
mBoostLevel = level;
if (level >= LLViewerImage::BOOST_HIGH)
{
processTextureStats();
}
}
//============================================================================
bool LLViewerImage::updateFetch()
{
mFetchState = 0;
mFetchPriority = 0;
mFetchDeltaTime = 999999.f;
mRequestDeltaTime = 999999.f;
#ifndef LL_RELEASE_FOR_DOWNLOAD
if (mID == gTextureFetch->mDebugID)
{
gTextureFetch->mDebugCount++; // for setting breakpoints
}
#endif
if (mIsMediaTexture)
{
llassert_always(!mHasFetcher);
return false; // skip
}
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 (mFullyLoaded)
{
llassert_always(!mHasFetcher);
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();
if (mIsFetching)
{
// Sets mRawDiscardLevel, mRawImage, mAuxRawImage
S32 fetch_discard = current_discard;
bool finished = gTextureFetch->getRequestFinished(getID(), fetch_discard, mRawImage, mAuxRawImage);
if (finished)
{
mIsFetching = FALSE;
}
else
{
mFetchState = gTextureFetch->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())
{
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();
gImageList.dirtyImage(this);
}
mIsRawImageValid = TRUE;
gImageList.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 (mRawDiscardLevel < 0)
{
// We finished but received no data
if (current_discard < 0)
{
llwarns << mID << ": Marking image as missing" << llendl;
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 if (mDecodePriority >= 0.f)
{
gTextureFetch->updateRequestPriority(mID, mDecodePriority);
}
}
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 = getWidth(0);
h = getHeight(0);
c = getComponents();
}
if (!mDontDiscard)
{
if (mBoostLevel == 0)
{
desired_discard = llmax(desired_discard, current_discard-1);
}
else
{
desired_discard = llmax(desired_discard, current_discard-2);
}
}
if (gTextureFetch->createRequest(getID(),getTargetHost(), decode_priority,
w, h, c, desired_discard,
needsAux()))
{
mHasFetcher = TRUE;
mIsFetching = TRUE;
mRequestedDiscardLevel = desired_discard;
mFetchState = gTextureFetch->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;
gTextureFetch->deleteRequest(getID(), true);
mHasFetcher = FALSE;
}
}
llassert_always(mRawImage.notNull() || (!mNeedsCreateTexture && !mIsRawImageValid));
return mIsFetching ? true : false;
}
void LLViewerImage::setIsMissingAsset()
{
if (mHasFetcher)
{
gTextureFetch->deleteRequest(getID(), true);
mHasFetcher = FALSE;
mIsFetching = FALSE;
mFetchState = 0;
mFetchPriority = 0;
}
mIsMissingAsset = TRUE;
}
//============================================================================
void LLViewerImage::setLoadedCallback( loaded_callback_func loaded_callback, S32 discard_level, BOOL keep_imageraw, 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
gImageList.mCallbackList.insert(this);
}
LLLoadedCallbackEntry* entryp = new LLLoadedCallbackEntry(loaded_callback, discard_level, keep_imageraw, userdata);
mLoadedCallbackList.push_back(entryp);
}
bool LLViewerImage::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
gImageList.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 && (mMaxDiscardLevel > gl_discard))
{
// Do a readback to get the GL data into the raw image
// We have GL data.
destroyRawImage();
createRawImage(gl_discard, TRUE);
readBackRaw(gl_discard, mRawImage);
mIsRawImageValid = TRUE;
llassert_always(mRawImage.notNull());
llassert_always(!mNeedsAux || mAuxRawImage.notNull());
}
//
// Run raw/auxiliary data callbacks
//
if (run_raw_callbacks && mIsRawImageValid && (mRawDiscardLevel <= mMaxDiscardLevel))
{
// 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());
llassert_always(!mNeedsAux || mAuxRawImage.notNull());
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 <= mMaxDiscardLevel))
{
//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())
{
gImageList.mCallbackList.erase(this);
}
// Done with any raw image data at this point (will be re-created if we still have callbacks)
destroyRawImage();
return res;
}
//============================================================================
// Call with 0,0 to turn this feature off.
void LLViewerImage::setKnownDrawSize(S32 width, S32 height)
{
mKnownDrawWidth = width;
mKnownDrawHeight = height;
addTextureStats((F32)(width * height));
}
// virtual
BOOL LLViewerImage::bind(S32 stage) const
{
if (stage == -1)
{
return TRUE;
}
if (gNoRender)
{
return true;
}
BOOL res = bindTextureInternal(stage);
if (res)
{
//llassert_always(mIsMissingAsset == FALSE);
}
else
{
// On failure to bind, what should we set the currently bound texture to?
if (mIsMissingAsset && !sMissingAssetImagep.isNull() && (this != (LLImageGL *)sMissingAssetImagep))
{
res = sMissingAssetImagep->bind( stage );
}
if (!res && !sDefaultImagep.isNull() && (this != (LLImageGL *)sDefaultImagep))
{
// use default if we've got it
res = sDefaultImagep->bind(stage);
}
if (!res && !sNullImagep.isNull() && (this != (LLImageGL *)sNullImagep))
{
res = sNullImagep->bind(stage);
}
if (!res)
{
llwarns << "LLViewerImage::bindTexture failed." << llendl;
}
stop_glerror();
}
return res;
}
// Was in LLImageGL
LLImageRaw* LLViewerImage::createRawImage(S8 discard_level, BOOL allocate)
{
llassert(discard_level >= 0);
if (mRawImage.notNull())
{
llerrs << "createRawImage() called with existing mRawImage" << llendl;
mRawImage = NULL;
mAuxRawImage = NULL;
}
if (allocate && mComponents)
{
mRawImage = new LLImageRaw(getWidth(discard_level), getHeight(discard_level), mComponents);
mIsRawImageValid = TRUE;
}
else
{
mRawImage = new LLImageRaw;
mIsRawImageValid = FALSE;
}
mRawDiscardLevel = discard_level;
return mRawImage;
}
void LLViewerImage::destroyRawImage()
{
mRawImage = NULL;
mAuxRawImage = NULL;
mIsRawImageValid = FALSE;
mRawDiscardLevel = INVALID_DISCARD_LEVEL;
}
|