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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/common/gpu/gpu_memory_allocation.h"
#include "content/common/gpu/gpu_memory_manager.h"
#include "content/common/gpu/gpu_memory_manager_client.h"
#include "content/common/gpu/gpu_memory_tracking.h"
#include "ui/gfx/size_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(COMPILER_GCC)
namespace BASE_HASH_NAMESPACE {
template<>
struct hash<content::GpuMemoryManagerClient*> {
uint64 operator()(content::GpuMemoryManagerClient* ptr) const {
return hash<uint64>()(reinterpret_cast<uint64>(ptr));
}
};
} // namespace BASE_HASH_NAMESPACE
#endif // COMPILER
class FakeMemoryTracker : public gpu::gles2::MemoryTracker {
public:
virtual void TrackMemoryAllocatedChange(
size_t /* old_size */,
size_t /* new_size */,
gpu::gles2::MemoryTracker::Pool /* pool */) OVERRIDE {
}
virtual bool EnsureGPUMemoryAvailable(size_t /* size_needed */) OVERRIDE {
return true;
}
private:
virtual ~FakeMemoryTracker() {
}
};
namespace content {
// This class is used to collect all stub assignments during a
// Manage() call.
class ClientAssignmentCollector {
public:
struct ClientMemoryStat {
GpuMemoryAllocation allocation;
};
typedef base::hash_map<GpuMemoryManagerClient*, ClientMemoryStat>
ClientMemoryStatMap;
static const ClientMemoryStatMap& GetClientStatsForLastManage() {
return client_memory_stats_for_last_manage_;
}
static void ClearAllStats() {
client_memory_stats_for_last_manage_.clear();
}
static void AddClientStat(GpuMemoryManagerClient* client,
const GpuMemoryAllocation& allocation) {
DCHECK(!client_memory_stats_for_last_manage_.count(client));
client_memory_stats_for_last_manage_[client].allocation = allocation;
}
private:
static ClientMemoryStatMap client_memory_stats_for_last_manage_;
};
ClientAssignmentCollector::ClientMemoryStatMap
ClientAssignmentCollector::client_memory_stats_for_last_manage_;
class FakeClient : public GpuMemoryManagerClient {
public:
GpuMemoryManager* memmgr_;
GpuMemoryAllocation allocation_;
uint64 total_gpu_memory_;
gfx::Size surface_size_;
GpuMemoryManagerClient* share_group_;
scoped_refptr<gpu::gles2::MemoryTracker> memory_tracker_;
scoped_ptr<GpuMemoryTrackingGroup> tracking_group_;
scoped_ptr<GpuMemoryManagerClientState> client_state_;
// This will create a client with no surface
FakeClient(GpuMemoryManager* memmgr, GpuMemoryManagerClient* share_group)
: memmgr_(memmgr)
, total_gpu_memory_(0)
, share_group_(share_group)
, memory_tracker_(NULL)
, tracking_group_(NULL) {
if (!share_group_) {
memory_tracker_ = new FakeMemoryTracker();
tracking_group_.reset(
memmgr_->CreateTrackingGroup(0, memory_tracker_));
}
client_state_.reset(memmgr_->CreateClientState(this, false, true));
}
// This will create a client with a surface
FakeClient(GpuMemoryManager* memmgr,
int32 surface_id,
bool visible)
: memmgr_(memmgr)
, total_gpu_memory_(0)
, share_group_(NULL)
, memory_tracker_(NULL)
, tracking_group_(NULL) {
memory_tracker_ = new FakeMemoryTracker();
tracking_group_.reset(
memmgr_->CreateTrackingGroup(0, memory_tracker_));
client_state_.reset(memmgr_->CreateClientState(
this, surface_id != 0, visible));
}
virtual ~FakeClient() {
client_state_.reset();
tracking_group_.reset();
memory_tracker_ = NULL;
}
virtual void SetMemoryAllocation(const GpuMemoryAllocation& alloc) OVERRIDE {
allocation_ = alloc;
ClientAssignmentCollector::AddClientStat(this, alloc);
}
virtual bool GetTotalGpuMemory(uint64* bytes) OVERRIDE {
if (total_gpu_memory_) {
*bytes = total_gpu_memory_;
return true;
}
return false;
}
void SetTotalGpuMemory(uint64 bytes) { total_gpu_memory_ = bytes; }
virtual gpu::gles2::MemoryTracker* GetMemoryTracker() const OVERRIDE {
if (share_group_)
return share_group_->GetMemoryTracker();
return memory_tracker_.get();
}
virtual gfx::Size GetSurfaceSize() const OVERRIDE {
return surface_size_;
}
void SetSurfaceSize(gfx::Size size) { surface_size_ = size; }
void SetVisible(bool visible) {
client_state_->SetVisible(visible);
}
void SetManagedMemoryStats(const GpuManagedMemoryStats& stats) {
client_state_->SetManagedMemoryStats(stats);
}
uint64 BytesWhenVisible() const {
return allocation_.renderer_allocation.bytes_limit_when_visible;
}
uint64 BytesWhenNotVisible() const {
return allocation_.renderer_allocation.bytes_limit_when_not_visible;
}
};
class GpuMemoryManagerTest : public testing::Test {
protected:
static const uint64 kFrontbufferLimitForTest = 3;
GpuMemoryManagerTest()
: memmgr_(0, kFrontbufferLimitForTest) {
memmgr_.TestingDisableScheduleManage();
memmgr_.TestingSetUseNonuniformMemoryPolicy(false);
}
virtual void SetUp() {
}
static int32 GenerateUniqueSurfaceId() {
static int32 surface_id_ = 1;
return surface_id_++;
}
bool IsAllocationForegroundForSurfaceYes(
const GpuMemoryAllocation& alloc) {
return alloc.browser_allocation.suggest_have_frontbuffer &&
!alloc.renderer_allocation.have_backbuffer_when_not_visible &&
alloc.renderer_allocation.bytes_limit_when_visible >=
GetMinimumClientAllocation();
}
bool IsAllocationBackgroundForSurfaceYes(
const GpuMemoryAllocation& alloc) {
return alloc.browser_allocation.suggest_have_frontbuffer &&
!alloc.renderer_allocation.have_backbuffer_when_not_visible &&
alloc.renderer_allocation.bytes_limit_when_not_visible <=
memmgr_.GetCurrentNonvisibleAvailableGpuMemory();
}
bool IsAllocationHibernatedForSurfaceYes(
const GpuMemoryAllocation& alloc) {
return !alloc.browser_allocation.suggest_have_frontbuffer &&
!alloc.renderer_allocation.have_backbuffer_when_not_visible &&
alloc.renderer_allocation.bytes_limit_when_not_visible <=
memmgr_.GetCurrentNonvisibleAvailableGpuMemory();
}
bool IsAllocationForegroundForSurfaceNo(
const GpuMemoryAllocation& alloc) {
return !alloc.browser_allocation.suggest_have_frontbuffer &&
!alloc.renderer_allocation.have_backbuffer_when_not_visible &&
alloc.renderer_allocation.bytes_limit_when_visible ==
GetMinimumClientAllocation();
}
bool IsAllocationBackgroundForSurfaceNo(
const GpuMemoryAllocation& alloc) {
return !alloc.browser_allocation.suggest_have_frontbuffer &&
!alloc.renderer_allocation.have_backbuffer_when_not_visible &&
alloc.renderer_allocation.bytes_limit_when_visible ==
GetMinimumClientAllocation();
}
bool IsAllocationHibernatedForSurfaceNo(
const GpuMemoryAllocation& alloc) {
return !alloc.browser_allocation.suggest_have_frontbuffer &&
!alloc.renderer_allocation.have_backbuffer_when_not_visible &&
alloc.renderer_allocation.bytes_limit_when_visible == 0;
}
void Manage() {
ClientAssignmentCollector::ClearAllStats();
memmgr_.Manage();
}
uint64 CalcAvailableFromGpuTotal(uint64 bytes) {
return GpuMemoryManager::CalcAvailableFromGpuTotal(bytes);
}
uint64 CalcAvailableFromViewportArea(int viewport_area) {
return GpuMemoryManager::CalcAvailableFromViewportArea(viewport_area);
}
uint64 CalcAvailableClamped(uint64 bytes) {
bytes = std::max(bytes, memmgr_.GetDefaultAvailableGpuMemory());
bytes = std::min(bytes, memmgr_.GetMaximumTotalGpuMemory());
return bytes;
}
uint64 GetAvailableGpuMemory() {
return memmgr_.GetAvailableGpuMemory();
}
uint64 GetMaximumClientAllocation() {
return memmgr_.GetMaximumClientAllocation();
}
uint64 GetMinimumClientAllocation() {
return memmgr_.GetMinimumClientAllocation();
}
GpuMemoryManager memmgr_;
};
class GpuMemoryManagerTestNonuniform : public GpuMemoryManagerTest {
protected:
void SetClientStats(
FakeClient* client,
uint64 required,
uint64 nicetohave) {
client->SetManagedMemoryStats(
GpuManagedMemoryStats(required, nicetohave, 0, false));
}
};
// Test GpuMemoryManager::Manage basic functionality.
// Expect memory allocation to set suggest_have_frontbuffer/backbuffer
// according to visibility and last used time for stubs with surface.
// Expect memory allocation to be shared according to share groups for stubs
// without a surface.
TEST_F(GpuMemoryManagerTest, TestManageBasicFunctionality) {
// Test stubs with surface.
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), false);
Manage();
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
// Test stubs without surface, with share group of 1 stub.
FakeClient stub3(&memmgr_, &stub1), stub4(&memmgr_, &stub2);
Manage();
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub4.allocation_));
// Test stub without surface, with share group of multiple stubs.
FakeClient stub5(&memmgr_ , &stub2);
Manage();
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub4.allocation_));
}
// Test GpuMemoryManager::Manage functionality: changing visibility.
// Expect memory allocation to set suggest_have_frontbuffer/backbuffer
// according to visibility and last used time for stubs with surface.
// Expect memory allocation to be shared according to share groups for stubs
// without a surface.
TEST_F(GpuMemoryManagerTest, TestManageChangingVisibility) {
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), false);
FakeClient stub3(&memmgr_, &stub1), stub4(&memmgr_, &stub2);
FakeClient stub5(&memmgr_ , &stub2);
Manage();
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub4.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub5.allocation_));
stub1.SetVisible(false);
stub2.SetVisible(true);
Manage();
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub4.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub5.allocation_));
}
// Test GpuMemoryManager::Manage functionality: Test more than threshold number
// of visible stubs.
// Expect all allocations to continue to have frontbuffer.
TEST_F(GpuMemoryManagerTest, TestManageManyVisibleStubs) {
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), true),
stub3(&memmgr_, GenerateUniqueSurfaceId(), true),
stub4(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub5(&memmgr_ , &stub1), stub6(&memmgr_ , &stub2);
FakeClient stub7(&memmgr_ , &stub2);
Manage();
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub3.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub4.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub5.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub6.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub7.allocation_));
}
// Test GpuMemoryManager::Manage functionality: Test more than threshold number
// of not visible stubs.
// Expect the stubs surpassing the threshold to not have a backbuffer.
TEST_F(GpuMemoryManagerTest, TestManageManyNotVisibleStubs) {
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), true),
stub3(&memmgr_, GenerateUniqueSurfaceId(), true),
stub4(&memmgr_, GenerateUniqueSurfaceId(), true);
stub4.SetVisible(false);
stub3.SetVisible(false);
stub2.SetVisible(false);
stub1.SetVisible(false);
FakeClient stub5(&memmgr_ , &stub1), stub6(&memmgr_ , &stub4);
FakeClient stub7(&memmgr_ , &stub1);
Manage();
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub3.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub4.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub5.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub6.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub7.allocation_));
}
// Test GpuMemoryManager::Manage functionality: Test changing the last used
// time of stubs when doing so causes change in which stubs surpass threshold.
// Expect frontbuffer to be dropped for the older stub.
TEST_F(GpuMemoryManagerTest, TestManageChangingLastUsedTime) {
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), true),
stub3(&memmgr_, GenerateUniqueSurfaceId(), true),
stub4(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub5(&memmgr_ , &stub3), stub6(&memmgr_ , &stub4);
FakeClient stub7(&memmgr_ , &stub3);
// Make stub4 be the least-recently-used client
stub4.SetVisible(false);
stub3.SetVisible(false);
stub2.SetVisible(false);
stub1.SetVisible(false);
Manage();
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub3.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub4.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub5.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub6.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub7.allocation_));
// Make stub3 become the least-recently-used client.
stub2.SetVisible(true);
stub2.SetVisible(false);
stub4.SetVisible(true);
stub4.SetVisible(false);
Manage();
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub3.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub4.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub5.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub6.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub7.allocation_));
}
// Test GpuMemoryManager::Manage functionality: Test changing importance of
// enough stubs so that every stub in share group crosses threshold.
// Expect memory allocation of the stubs without surface to share memory
// allocation with the most visible stub in share group.
TEST_F(GpuMemoryManagerTest, TestManageChangingImportanceShareGroup) {
FakeClient stub_ignore_a(&memmgr_, GenerateUniqueSurfaceId(), true),
stub_ignore_b(&memmgr_, GenerateUniqueSurfaceId(), false),
stub_ignore_c(&memmgr_, GenerateUniqueSurfaceId(), false);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), false),
stub2(&memmgr_, GenerateUniqueSurfaceId(), false);
FakeClient stub3(&memmgr_, &stub2), stub4(&memmgr_, &stub2);
// stub1 and stub2 keep their non-hibernated state because they're
// either visible or the 2 most recently used clients (through the
// first three checks).
stub1.SetVisible(true);
stub2.SetVisible(true);
Manage();
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub4.allocation_));
stub1.SetVisible(false);
Manage();
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationForegroundForSurfaceNo(stub4.allocation_));
stub2.SetVisible(false);
Manage();
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub4.allocation_));
// stub_ignore_b will cause stub1 to become hibernated (because
// stub_ignore_a, stub_ignore_b, and stub2 are all non-hibernated and more
// important).
stub_ignore_b.SetVisible(true);
stub_ignore_b.SetVisible(false);
Manage();
EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationBackgroundForSurfaceNo(stub4.allocation_));
// stub_ignore_c will cause stub2 to become hibernated (because
// stub_ignore_a, stub_ignore_b, and stub_ignore_c are all non-hibernated
// and more important).
stub_ignore_c.SetVisible(true);
stub_ignore_c.SetVisible(false);
Manage();
EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub1.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceYes(stub2.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub3.allocation_));
EXPECT_TRUE(IsAllocationHibernatedForSurfaceNo(stub4.allocation_));
}
// Test GpuMemoryAllocation memory allocation bonuses:
// When the number of visible tabs is small, each tab should get a
// gpu_resource_size_in_bytes allocation value that is greater than
// GetMinimumClientAllocation(), and when the number of tabs is large,
// each should get exactly GetMinimumClientAllocation() and not less.
TEST_F(GpuMemoryManagerTest, TestForegroundStubsGetBonusAllocation) {
size_t max_stubs_before_no_bonus = static_cast<size_t>(
GetAvailableGpuMemory() / (GetMinimumClientAllocation() + 1));
std::vector<FakeClient*> stubs;
for (size_t i = 0; i < max_stubs_before_no_bonus; ++i) {
stubs.push_back(
new FakeClient(&memmgr_, GenerateUniqueSurfaceId(), true));
}
Manage();
for (size_t i = 0; i < stubs.size(); ++i) {
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stubs[i]->allocation_));
EXPECT_GT(
stubs[i]->allocation_.renderer_allocation.bytes_limit_when_visible,
GetMinimumClientAllocation());
}
FakeClient extra_stub(&memmgr_, GenerateUniqueSurfaceId(), true);
Manage();
for (size_t i = 0; i < stubs.size(); ++i) {
EXPECT_TRUE(IsAllocationForegroundForSurfaceYes(stubs[i]->allocation_));
EXPECT_EQ(
stubs[i]->allocation_.renderer_allocation.bytes_limit_when_visible,
GetMinimumClientAllocation());
}
for (size_t i = 0; i < max_stubs_before_no_bonus; ++i) {
delete stubs[i];
}
}
// Test GpuMemoryManager::UpdateAvailableGpuMemory functionality
TEST_F(GpuMemoryManagerTest, TestUpdateAvailableGpuMemory) {
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), false),
stub3(&memmgr_, GenerateUniqueSurfaceId(), true),
stub4(&memmgr_, GenerateUniqueSurfaceId(), false);
#if defined(OS_ANDROID)
// We use the largest visible surface size to calculate the limit
stub1.SetSurfaceSize(gfx::Size(1024, 512)); // Surface size
stub2.SetSurfaceSize(gfx::Size(2048, 512)); // Larger but not visible.
stub3.SetSurfaceSize(gfx::Size(512, 512)); // Visible but smaller.
stub4.SetSurfaceSize(gfx::Size(512, 512)); // Not visible and smaller.
Manage();
uint64 bytes_expected = CalcAvailableFromViewportArea(1024*512);
#else
// We take the lowest GPU's total memory as the limit
uint64 expected = 400 * 1024 * 1024;
stub1.SetTotalGpuMemory(expected); // GPU Memory
stub2.SetTotalGpuMemory(expected - 1024 * 1024); // Smaller but not visible.
stub3.SetTotalGpuMemory(expected + 1024 * 1024); // Visible but larger.
stub4.SetTotalGpuMemory(expected + 1024 * 1024); // Not visible and larger.
Manage();
uint64 bytes_expected = CalcAvailableFromGpuTotal(expected);
#endif
EXPECT_EQ(GetAvailableGpuMemory(), CalcAvailableClamped(bytes_expected));
}
// Test GpuMemoryAllocation comparison operators: Iterate over all possible
// combinations of gpu_resource_size_in_bytes, suggest_have_backbuffer, and
// suggest_have_frontbuffer, and make sure allocations with equal values test
// equal and non equal values test not equal.
TEST_F(GpuMemoryManagerTest, GpuMemoryAllocationCompareTests) {
std::vector<int> gpu_resource_size_in_bytes_values;
gpu_resource_size_in_bytes_values.push_back(0);
gpu_resource_size_in_bytes_values.push_back(1);
gpu_resource_size_in_bytes_values.push_back(12345678);
std::vector<GpuMemoryAllocation::BufferAllocation>
suggested_buffer_allocation_values;
suggested_buffer_allocation_values.push_back(
GpuMemoryAllocation::kHasFrontbuffer);
suggested_buffer_allocation_values.push_back(
GpuMemoryAllocation::kHasFrontbuffer);
suggested_buffer_allocation_values.push_back(
GpuMemoryAllocation::kHasNoFrontbuffer);
suggested_buffer_allocation_values.push_back(
GpuMemoryAllocation::kHasNoFrontbuffer);
for (size_t i = 0; i != gpu_resource_size_in_bytes_values.size(); ++i) {
for (size_t j = 0; j != suggested_buffer_allocation_values.size(); ++j) {
uint64 sz = gpu_resource_size_in_bytes_values[i];
GpuMemoryAllocation::BufferAllocation buffer_allocation =
suggested_buffer_allocation_values[j];
GpuMemoryAllocation allocation(sz, buffer_allocation);
EXPECT_TRUE(allocation.Equals(
GpuMemoryAllocation(sz, buffer_allocation)));
EXPECT_FALSE(allocation.Equals(
GpuMemoryAllocation(sz+1, buffer_allocation)));
for (size_t k = 0; k != suggested_buffer_allocation_values.size(); ++k) {
GpuMemoryAllocation::BufferAllocation buffer_allocation_other =
suggested_buffer_allocation_values[k];
if (buffer_allocation == buffer_allocation_other) continue;
EXPECT_FALSE(allocation.Equals(
GpuMemoryAllocation(sz, buffer_allocation_other)));
}
}
}
}
// Test GpuMemoryManager Stub Memory Stats functionality:
// Creates various surface/non-surface stubs and switches stub visibility and
// tests to see that stats data structure values are correct.
TEST_F(GpuMemoryManagerTest, StubMemoryStatsForLastManageTests) {
ClientAssignmentCollector::ClientMemoryStatMap stats;
Manage();
stats = ClientAssignmentCollector::GetClientStatsForLastManage();
EXPECT_EQ(stats.size(), 0ul);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
Manage();
stats = ClientAssignmentCollector::GetClientStatsForLastManage();
uint64 stub1allocation1 =
stats[&stub1].allocation.renderer_allocation.bytes_limit_when_visible;
EXPECT_EQ(stats.size(), 1ul);
EXPECT_GT(stub1allocation1, 0ul);
FakeClient stub2(&memmgr_, &stub1);
Manage();
stats = ClientAssignmentCollector::GetClientStatsForLastManage();
EXPECT_EQ(stats.count(&stub1), 1ul);
uint64 stub1allocation2 =
stats[&stub1].allocation.renderer_allocation.bytes_limit_when_visible;
EXPECT_EQ(stats.count(&stub2), 1ul);
uint64 stub2allocation2 =
stats[&stub2].allocation.renderer_allocation.bytes_limit_when_visible;
EXPECT_EQ(stats.size(), 2ul);
EXPECT_GT(stub1allocation2, 0ul);
EXPECT_GT(stub2allocation2, 0ul);
if (stub1allocation2 != GetMaximumClientAllocation())
EXPECT_LT(stub1allocation2, stub1allocation1);
FakeClient stub3(&memmgr_, GenerateUniqueSurfaceId(), true);
Manage();
stats = ClientAssignmentCollector::GetClientStatsForLastManage();
uint64 stub1allocation3 =
stats[&stub1].allocation.renderer_allocation.bytes_limit_when_visible;
uint64 stub2allocation3 =
stats[&stub2].allocation.renderer_allocation.bytes_limit_when_visible;
uint64 stub3allocation3 =
stats[&stub3].allocation.renderer_allocation.bytes_limit_when_visible;
EXPECT_EQ(stats.size(), 3ul);
EXPECT_GT(stub1allocation3, 0ul);
EXPECT_GT(stub2allocation3, 0ul);
EXPECT_GT(stub3allocation3, 0ul);
if (stub1allocation3 != GetMaximumClientAllocation())
EXPECT_LT(stub1allocation3, stub1allocation2);
stub1.SetVisible(false);
Manage();
stats = ClientAssignmentCollector::GetClientStatsForLastManage();
uint64 stub1allocation4 =
stats[&stub1].allocation.renderer_allocation.bytes_limit_when_visible;
uint64 stub2allocation4 =
stats[&stub2].allocation.renderer_allocation.bytes_limit_when_visible;
uint64 stub3allocation4 =
stats[&stub3].allocation.renderer_allocation.bytes_limit_when_visible;
EXPECT_EQ(stats.size(), 3ul);
EXPECT_GT(stub1allocation4, 0ul);
EXPECT_GE(stub2allocation4, 0ul);
EXPECT_GT(stub3allocation4, 0ul);
if (stub3allocation3 != GetMaximumClientAllocation())
EXPECT_GT(stub3allocation4, stub3allocation3);
}
// Test GpuMemoryManager's managed memory tracking
TEST_F(GpuMemoryManagerTest, TestManagedUsageTracking) {
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true),
stub2(&memmgr_, GenerateUniqueSurfaceId(), false);
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Set memory allocations and verify the results are reflected.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 5, false));
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 7, false));
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Remove a visible client
stub1.client_state_.reset();
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
stub1.client_state_.reset(memmgr_.CreateClientState(&stub1, true, true));
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 5, false));
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Remove a nonvisible client
stub2.client_state_.reset();
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_nonvisible_);
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_nonvisible_);
stub2.client_state_.reset(memmgr_.CreateClientState(&stub2, true, false));
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(0ul, memmgr_.bytes_allocated_managed_nonvisible_);
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 7, false));
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Create and then destroy some stubs, and verify their allocations go away.
{
FakeClient stub3(&memmgr_, GenerateUniqueSurfaceId(), true),
stub4(&memmgr_, GenerateUniqueSurfaceId(), false);
stub3.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 1, false));
stub4.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 2, false));
EXPECT_EQ(6ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(9ul, memmgr_.bytes_allocated_managed_nonvisible_);
}
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Do no-op changes to stubs' visibility and make sure nothing chnages.
stub1.SetVisible(true);
stub2.SetVisible(false);
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Change visbility state.
stub1.SetVisible(false);
stub2.SetVisible(true);
EXPECT_EQ(7ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(5ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Increase allocation amounts.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 6, false));
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 8, false));
EXPECT_EQ(8ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(6ul, memmgr_.bytes_allocated_managed_nonvisible_);
// Decrease allocation amounts.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 4, false));
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(0, 0, 6, false));
EXPECT_EQ(6ul, memmgr_.bytes_allocated_managed_visible_);
EXPECT_EQ(4ul, memmgr_.bytes_allocated_managed_nonvisible_);
}
// Test GpuMemoryManager's background cutoff threshoulds
TEST_F(GpuMemoryManagerTest, TestBackgroundCutoff) {
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
// stub1's requirements are not <=16, so it should just dump
// everything when it goes invisible.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(17, 24, 18, false));
Manage();
EXPECT_EQ(0ul, stub1.BytesWhenNotVisible());
// stub1 now fits, so it should have a full budget.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(16, 24, 18, false));
Manage();
EXPECT_EQ(memmgr_.bytes_nonvisible_available_gpu_memory_,
memmgr_.GetCurrentNonvisibleAvailableGpuMemory());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub1.BytesWhenNotVisible());
// Background stub1.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(16, 24, 16, false));
stub1.SetVisible(false);
// Add stub2 that uses almost enough memory to evict
// stub1, but not quite.
FakeClient stub2(&memmgr_, GenerateUniqueSurfaceId(), true);
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(16, 50, 48, false));
Manage();
EXPECT_EQ(memmgr_.bytes_nonvisible_available_gpu_memory_,
memmgr_.GetCurrentNonvisibleAvailableGpuMemory());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub1.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub2.BytesWhenNotVisible());
// Increase stub2 to force stub1 to be evicted.
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(16, 50, 49, false));
Manage();
EXPECT_EQ(0ul,
stub1.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub2.BytesWhenNotVisible());
}
// Test GpuMemoryManager's background MRU behavior
TEST_F(GpuMemoryManagerTest, TestBackgroundMru) {
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub2(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub3(&memmgr_, GenerateUniqueSurfaceId(), true);
// When all are visible, they should all be allowed to have memory
// should they become nonvisible.
stub1.SetManagedMemoryStats(GpuManagedMemoryStats(7, 24, 7, false));
stub2.SetManagedMemoryStats(GpuManagedMemoryStats(7, 24, 7, false));
stub3.SetManagedMemoryStats(GpuManagedMemoryStats(7, 24, 7, false));
Manage();
EXPECT_EQ(memmgr_.bytes_nonvisible_available_gpu_memory_,
memmgr_.GetCurrentNonvisibleAvailableGpuMemory());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub1.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub2.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub3.BytesWhenNotVisible());
// Background stubs 1 and 2, and they should fit
stub2.SetVisible(false);
stub1.SetVisible(false);
Manage();
EXPECT_EQ(memmgr_.bytes_nonvisible_available_gpu_memory_,
memmgr_.GetCurrentNonvisibleAvailableGpuMemory());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub1.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub2.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub3.BytesWhenNotVisible());
// Now background stub 3, and it should cause stub 2 to be
// evicted because it was set non-visible first
stub3.SetVisible(false);
Manage();
EXPECT_EQ(memmgr_.bytes_nonvisible_available_gpu_memory_,
memmgr_.GetCurrentNonvisibleAvailableGpuMemory());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub1.BytesWhenNotVisible());
EXPECT_EQ(0ul,
stub2.BytesWhenNotVisible());
EXPECT_EQ(memmgr_.GetCurrentNonvisibleAvailableGpuMemory(),
stub3.BytesWhenNotVisible());
}
// Test GpuMemoryManager's tracking of unmanaged (e.g, WebGL) memory.
TEST_F(GpuMemoryManagerTest, TestUnmanagedTracking) {
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
memmgr_.TestingSetUnmanagedLimitStep(16);
memmgr_.TestingSetMinimumClientAllocation(8);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
// Expect that the one stub get the maximum tab allocation.
Manage();
EXPECT_EQ(memmgr_.GetMaximumClientAllocation(),
stub1.BytesWhenVisible());
// Now allocate some unmanaged memory and make sure the amount
// goes down.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
0,
48,
gpu::gles2::MemoryTracker::kUnmanaged);
Manage();
EXPECT_GT(memmgr_.GetMaximumClientAllocation(),
stub1.BytesWhenVisible());
// Now allocate the entire FB worth of unmanaged memory, and
// make sure that we stay stuck at the minimum tab allocation.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
48,
64,
gpu::gles2::MemoryTracker::kUnmanaged);
Manage();
EXPECT_EQ(memmgr_.GetMinimumClientAllocation(),
stub1.BytesWhenVisible());
// Far-oversubscribe the entire FB, and make sure we stay at
// the minimum allocation, and don't blow up.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
64,
999,
gpu::gles2::MemoryTracker::kUnmanaged);
Manage();
EXPECT_EQ(memmgr_.GetMinimumClientAllocation(),
stub1.BytesWhenVisible());
// Delete all tracked memory so we don't hit leak checks.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
999,
0,
gpu::gles2::MemoryTracker::kUnmanaged);
}
// Test nonvisible MRU behavior (the most recently used nonvisible clients
// keep their contents).
TEST_F(GpuMemoryManagerTestNonuniform, BackgroundMru) {
// Set memory manager constants for this test
memmgr_.TestingSetUseNonuniformMemoryPolicy(true);
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
memmgr_.TestingSetMinimumClientAllocation(8);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub2(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub3(&memmgr_, GenerateUniqueSurfaceId(), true);
// When all are visible, they should all be allowed to have memory
// should they become nonvisible.
SetClientStats(&stub1, 6, 23);
SetClientStats(&stub2, 6, 23);
SetClientStats(&stub3, 6, 23);
Manage();
EXPECT_GE(stub1.BytesWhenVisible(), 20u);
EXPECT_GE(stub2.BytesWhenVisible(), 20u);
EXPECT_GE(stub3.BytesWhenVisible(), 20u);
EXPECT_LT(stub1.BytesWhenVisible(), 22u);
EXPECT_LT(stub2.BytesWhenVisible(), 22u);
EXPECT_LT(stub3.BytesWhenVisible(), 22u);
EXPECT_GE(stub1.BytesWhenNotVisible(), 6u);
EXPECT_GE(stub2.BytesWhenNotVisible(), 6u);
EXPECT_GE(stub3.BytesWhenNotVisible(), 6u);
// Background stubs 1 and 2, and they should fit. All stubs should
// have their full nicetohave budget should they become visible.
stub2.SetVisible(false);
stub1.SetVisible(false);
Manage();
EXPECT_GE(stub1.BytesWhenVisible(), 23u);
EXPECT_GE(stub2.BytesWhenVisible(), 23u);
EXPECT_GE(stub3.BytesWhenVisible(), 23u);
EXPECT_LT(stub1.BytesWhenVisible(), 32u);
EXPECT_LT(stub2.BytesWhenVisible(), 32u);
EXPECT_LT(stub3.BytesWhenVisible(), 32u);
EXPECT_GE(stub1.BytesWhenNotVisible(), 6u);
EXPECT_GE(stub2.BytesWhenNotVisible(), 6u);
EXPECT_GE(stub3.BytesWhenNotVisible(), 6u);
// Now background stub 3, and it should cause stub 2 to be
// evicted because it was set non-visible first
stub3.SetVisible(false);
Manage();
EXPECT_GE(stub1.BytesWhenNotVisible(), 6u);
EXPECT_EQ(stub2.BytesWhenNotVisible(), 0u);
EXPECT_GE(stub3.BytesWhenNotVisible(), 6u);
}
// Test that once a backgrounded client has dropped its resources, it
// doesn't get them back until it becomes visible again.
TEST_F(GpuMemoryManagerTestNonuniform, BackgroundDiscardPersistent) {
// Set memory manager constants for this test
memmgr_.TestingSetUseNonuniformMemoryPolicy(true);
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
memmgr_.TestingSetMinimumClientAllocation(8);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
FakeClient stub2(&memmgr_, GenerateUniqueSurfaceId(), true);
// Both clients should be able to keep their contents should one of
// them become nonvisible.
SetClientStats(&stub1, 10, 20);
SetClientStats(&stub2, 10, 20);
Manage();
EXPECT_GE(stub1.BytesWhenNotVisible(), 10u);
EXPECT_GE(stub2.BytesWhenNotVisible(), 10u);
// If they both go nonvisible, then only the most recently used client
// should keep its contents.
stub1.SetVisible(false);
stub2.SetVisible(false);
Manage();
EXPECT_EQ(stub1.BytesWhenNotVisible(), 0u);
EXPECT_GE(stub2.BytesWhenNotVisible(), 10u);
// When becoming visible, stub 2 should get its contents back, and
// retain them next time it is made nonvisible.
stub2.SetVisible(true);
Manage();
EXPECT_GE(stub2.BytesWhenNotVisible(), 10u);
stub2.SetVisible(false);
Manage();
EXPECT_GE(stub2.BytesWhenNotVisible(), 10u);
}
// Test tracking of unmanaged (e.g, WebGL) memory.
TEST_F(GpuMemoryManagerTestNonuniform, UnmanagedTracking) {
// Set memory manager constants for this test
memmgr_.TestingSetUseNonuniformMemoryPolicy(true);
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
memmgr_.TestingSetMinimumClientAllocation(8);
memmgr_.TestingSetUnmanagedLimitStep(16);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
// Expect that the one stub get its nicetohave level.
SetClientStats(&stub1, 16, 32);
Manage();
EXPECT_GE(stub1.BytesWhenVisible(), 32u);
// Now allocate some unmanaged memory and make sure the amount
// goes down.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
0,
48,
gpu::gles2::MemoryTracker::kUnmanaged);
Manage();
EXPECT_LT(stub1.BytesWhenVisible(), 24u);
// Now allocate the entire FB worth of unmanaged memory, and
// make sure that we stay stuck at the minimum tab allocation.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
48,
64,
gpu::gles2::MemoryTracker::kUnmanaged);
Manage();
EXPECT_EQ(stub1.BytesWhenVisible(), 8u);
// Far-oversubscribe the entire FB, and make sure we stay at
// the minimum allocation, and don't blow up.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
64,
999,
gpu::gles2::MemoryTracker::kUnmanaged);
Manage();
EXPECT_EQ(stub1.BytesWhenVisible(), 8u);
// Delete all tracked memory so we don't hit leak checks.
memmgr_.TrackMemoryAllocatedChange(
stub1.tracking_group_.get(),
999,
0,
gpu::gles2::MemoryTracker::kUnmanaged);
}
// Test the default allocation levels are used.
TEST_F(GpuMemoryManagerTestNonuniform, DefaultAllocation) {
// Set memory manager constants for this test
memmgr_.TestingSetUseNonuniformMemoryPolicy(true);
memmgr_.TestingSetAvailableGpuMemory(64);
memmgr_.TestingSetNonvisibleAvailableGpuMemory(16);
memmgr_.TestingSetMinimumClientAllocation(8);
memmgr_.TestingSetDefaultClientAllocation(16);
FakeClient stub1(&memmgr_, GenerateUniqueSurfaceId(), true);
// Expect that a client which has not sent stats receive the
// default allocation.
Manage();
EXPECT_EQ(stub1.BytesWhenVisible(),
memmgr_.GetDefaultClientAllocation());
EXPECT_EQ(stub1.BytesWhenNotVisible(), 0u);
}
} // namespace content
|