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
|
// Copyright 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 "cc/resources/resource_provider.h"
#include <algorithm>
#include <limits>
#include "base/containers/hash_tables.h"
#include "base/debug/trace_event.h"
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "cc/base/util.h"
#include "cc/output/gl_renderer.h" // For the GLC() macro.
#include "cc/resources/platform_color.h"
#include "cc/resources/returned_resource.h"
#include "cc/resources/shared_bitmap_manager.h"
#include "cc/resources/texture_uploader.h"
#include "cc/resources/transferable_resource.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/GrContext.h"
#include "ui/gfx/frame_time.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/gpu_memory_buffer.h"
using gpu::gles2::GLES2Interface;
namespace cc {
class IdAllocator {
public:
virtual ~IdAllocator() {}
virtual GLuint NextId() = 0;
protected:
IdAllocator(GLES2Interface* gl, size_t id_allocation_chunk_size)
: gl_(gl),
id_allocation_chunk_size_(id_allocation_chunk_size),
ids_(new GLuint[id_allocation_chunk_size]),
next_id_index_(id_allocation_chunk_size) {
DCHECK(id_allocation_chunk_size_);
}
GLES2Interface* gl_;
const size_t id_allocation_chunk_size_;
scoped_ptr<GLuint[]> ids_;
size_t next_id_index_;
};
namespace {
// Measured in seconds.
const double kSoftwareUploadTickRate = 0.000250;
const double kTextureUploadTickRate = 0.004;
GLenum TextureToStorageFormat(ResourceFormat format) {
GLenum storage_format = GL_RGBA8_OES;
switch (format) {
case RGBA_8888:
break;
case BGRA_8888:
storage_format = GL_BGRA8_EXT;
break;
case RGBA_4444:
case ALPHA_8:
case LUMINANCE_8:
case RGB_565:
case ETC1:
case RED_8:
NOTREACHED();
break;
}
return storage_format;
}
bool IsFormatSupportedForStorage(ResourceFormat format, bool use_bgra) {
switch (format) {
case RGBA_8888:
return true;
case BGRA_8888:
return use_bgra;
case RGBA_4444:
case ALPHA_8:
case LUMINANCE_8:
case RGB_565:
case ETC1:
case RED_8:
return false;
}
return false;
}
GrPixelConfig ToGrPixelConfig(ResourceFormat format) {
switch (format) {
case RGBA_8888:
return kRGBA_8888_GrPixelConfig;
case BGRA_8888:
return kBGRA_8888_GrPixelConfig;
case RGBA_4444:
return kRGBA_4444_GrPixelConfig;
default:
break;
}
DCHECK(false) << "Unsupported resource format.";
return kSkia8888_GrPixelConfig;
}
gfx::GpuMemoryBuffer::Format ToGpuMemoryBufferFormat(ResourceFormat format) {
switch (format) {
case RGBA_8888:
return gfx::GpuMemoryBuffer::Format::RGBA_8888;
case BGRA_8888:
return gfx::GpuMemoryBuffer::Format::BGRA_8888;
case RGBA_4444:
case ALPHA_8:
case LUMINANCE_8:
case RGB_565:
case ETC1:
case RED_8:
break;
}
NOTREACHED();
return gfx::GpuMemoryBuffer::Format::RGBA_8888;
}
class ScopedSetActiveTexture {
public:
ScopedSetActiveTexture(GLES2Interface* gl, GLenum unit)
: gl_(gl), unit_(unit) {
DCHECK_EQ(GL_TEXTURE0, ResourceProvider::GetActiveTextureUnit(gl_));
if (unit_ != GL_TEXTURE0)
GLC(gl_, gl_->ActiveTexture(unit_));
}
~ScopedSetActiveTexture() {
// Active unit being GL_TEXTURE0 is effectively the ground state.
if (unit_ != GL_TEXTURE0)
GLC(gl_, gl_->ActiveTexture(GL_TEXTURE0));
}
private:
GLES2Interface* gl_;
GLenum unit_;
};
class TextureIdAllocator : public IdAllocator {
public:
TextureIdAllocator(GLES2Interface* gl,
size_t texture_id_allocation_chunk_size)
: IdAllocator(gl, texture_id_allocation_chunk_size) {}
~TextureIdAllocator() override {
gl_->DeleteTextures(id_allocation_chunk_size_ - next_id_index_,
ids_.get() + next_id_index_);
}
// Overridden from IdAllocator:
GLuint NextId() override {
if (next_id_index_ == id_allocation_chunk_size_) {
gl_->GenTextures(id_allocation_chunk_size_, ids_.get());
next_id_index_ = 0;
}
return ids_[next_id_index_++];
}
private:
DISALLOW_COPY_AND_ASSIGN(TextureIdAllocator);
};
class BufferIdAllocator : public IdAllocator {
public:
BufferIdAllocator(GLES2Interface* gl, size_t buffer_id_allocation_chunk_size)
: IdAllocator(gl, buffer_id_allocation_chunk_size) {}
~BufferIdAllocator() override {
gl_->DeleteBuffers(id_allocation_chunk_size_ - next_id_index_,
ids_.get() + next_id_index_);
}
// Overridden from IdAllocator:
GLuint NextId() override {
if (next_id_index_ == id_allocation_chunk_size_) {
gl_->GenBuffers(id_allocation_chunk_size_, ids_.get());
next_id_index_ = 0;
}
return ids_[next_id_index_++];
}
private:
DISALLOW_COPY_AND_ASSIGN(BufferIdAllocator);
};
// Query object based fence implementation used to detect completion of copy
// texture operations. Fence has passed when query result is available.
class CopyTextureFence : public ResourceProvider::Fence {
public:
CopyTextureFence(gpu::gles2::GLES2Interface* gl, unsigned query_id)
: gl_(gl), query_id_(query_id) {}
// Overridden from ResourceProvider::Fence:
void Set() override {}
bool HasPassed() override {
unsigned available = 1;
gl_->GetQueryObjectuivEXT(
query_id_, GL_QUERY_RESULT_AVAILABLE_EXT, &available);
if (!available)
return false;
ProcessResult();
return true;
}
void Wait() override {
// ProcessResult() will wait for result to become available.
ProcessResult();
}
private:
~CopyTextureFence() override {}
void ProcessResult() {
unsigned time_elapsed_us = 0;
gl_->GetQueryObjectuivEXT(query_id_, GL_QUERY_RESULT_EXT, &time_elapsed_us);
UMA_HISTOGRAM_CUSTOM_COUNTS("Renderer4.CopyTextureLatency", time_elapsed_us,
0, 256000, 50);
}
gpu::gles2::GLES2Interface* gl_;
unsigned query_id_;
DISALLOW_COPY_AND_ASSIGN(CopyTextureFence);
};
} // namespace
ResourceProvider::Resource::Resource()
: child_id(0),
gl_id(0),
gl_pixel_buffer_id(0),
gl_upload_query_id(0),
gl_read_lock_query_id(0),
pixels(NULL),
lock_for_read_count(0),
imported_count(0),
exported_count(0),
dirty_image(false),
locked_for_write(false),
lost(false),
marked_for_deletion(false),
pending_set_pixels(false),
set_pixels_completion_forced(false),
allocated(false),
read_lock_fences_enabled(false),
has_shared_bitmap_id(false),
allow_overlay(false),
read_lock_fence(NULL),
size(),
origin(Internal),
target(0),
original_filter(0),
filter(0),
image_id(0),
bound_image_id(0),
texture_pool(0),
wrap_mode(0),
hint(TextureHintImmutable),
type(InvalidType),
format(RGBA_8888),
shared_bitmap(NULL),
gpu_memory_buffer(NULL) {
}
ResourceProvider::Resource::~Resource() {}
ResourceProvider::Resource::Resource(GLuint texture_id,
const gfx::Size& size,
Origin origin,
GLenum target,
GLenum filter,
GLenum texture_pool,
GLint wrap_mode,
TextureHint hint,
ResourceFormat format)
: child_id(0),
gl_id(texture_id),
gl_pixel_buffer_id(0),
gl_upload_query_id(0),
gl_read_lock_query_id(0),
pixels(NULL),
lock_for_read_count(0),
imported_count(0),
exported_count(0),
dirty_image(false),
locked_for_write(false),
lost(false),
marked_for_deletion(false),
pending_set_pixels(false),
set_pixels_completion_forced(false),
allocated(false),
read_lock_fences_enabled(false),
has_shared_bitmap_id(false),
allow_overlay(false),
read_lock_fence(NULL),
size(size),
origin(origin),
target(target),
original_filter(filter),
filter(filter),
image_id(0),
bound_image_id(0),
texture_pool(texture_pool),
wrap_mode(wrap_mode),
hint(hint),
type(GLTexture),
format(format),
shared_bitmap(NULL),
gpu_memory_buffer(NULL) {
DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT);
DCHECK_EQ(origin == Internal, !!texture_pool);
}
ResourceProvider::Resource::Resource(uint8_t* pixels,
SharedBitmap* bitmap,
const gfx::Size& size,
Origin origin,
GLenum filter,
GLint wrap_mode)
: child_id(0),
gl_id(0),
gl_pixel_buffer_id(0),
gl_upload_query_id(0),
gl_read_lock_query_id(0),
pixels(pixels),
lock_for_read_count(0),
imported_count(0),
exported_count(0),
dirty_image(false),
locked_for_write(false),
lost(false),
marked_for_deletion(false),
pending_set_pixels(false),
set_pixels_completion_forced(false),
allocated(false),
read_lock_fences_enabled(false),
has_shared_bitmap_id(!!bitmap),
allow_overlay(false),
read_lock_fence(NULL),
size(size),
origin(origin),
target(0),
original_filter(filter),
filter(filter),
image_id(0),
bound_image_id(0),
texture_pool(0),
wrap_mode(wrap_mode),
hint(TextureHintImmutable),
type(Bitmap),
format(RGBA_8888),
shared_bitmap(bitmap),
gpu_memory_buffer(NULL) {
DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT);
DCHECK(origin == Delegated || pixels);
if (bitmap)
shared_bitmap_id = bitmap->id();
}
ResourceProvider::Resource::Resource(const SharedBitmapId& bitmap_id,
const gfx::Size& size,
Origin origin,
GLenum filter,
GLint wrap_mode)
: child_id(0),
gl_id(0),
gl_pixel_buffer_id(0),
gl_upload_query_id(0),
gl_read_lock_query_id(0),
pixels(NULL),
lock_for_read_count(0),
imported_count(0),
exported_count(0),
dirty_image(false),
locked_for_write(false),
lost(false),
marked_for_deletion(false),
pending_set_pixels(false),
set_pixels_completion_forced(false),
allocated(false),
read_lock_fences_enabled(false),
has_shared_bitmap_id(true),
allow_overlay(false),
read_lock_fence(NULL),
size(size),
origin(origin),
target(0),
original_filter(filter),
filter(filter),
image_id(0),
bound_image_id(0),
texture_pool(0),
wrap_mode(wrap_mode),
hint(TextureHintImmutable),
type(Bitmap),
format(RGBA_8888),
shared_bitmap_id(bitmap_id),
shared_bitmap(NULL),
gpu_memory_buffer(NULL) {
DCHECK(wrap_mode == GL_CLAMP_TO_EDGE || wrap_mode == GL_REPEAT);
}
ResourceProvider::Child::Child() : marked_for_deletion(false) {}
ResourceProvider::Child::~Child() {}
scoped_ptr<ResourceProvider> ResourceProvider::Create(
OutputSurface* output_surface,
SharedBitmapManager* shared_bitmap_manager,
gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
BlockingTaskRunner* blocking_main_thread_task_runner,
int highp_threshold_min,
bool use_rgba_4444_texture_format,
size_t id_allocation_chunk_size) {
scoped_ptr<ResourceProvider> resource_provider(
new ResourceProvider(output_surface,
shared_bitmap_manager,
gpu_memory_buffer_manager,
blocking_main_thread_task_runner,
highp_threshold_min,
use_rgba_4444_texture_format,
id_allocation_chunk_size));
if (resource_provider->ContextGL())
resource_provider->InitializeGL();
else
resource_provider->InitializeSoftware();
DCHECK_NE(InvalidType, resource_provider->default_resource_type());
return resource_provider.Pass();
}
ResourceProvider::~ResourceProvider() {
while (!children_.empty())
DestroyChildInternal(children_.begin(), ForShutdown);
while (!resources_.empty())
DeleteResourceInternal(resources_.begin(), ForShutdown);
CleanUpGLIfNeeded();
}
bool ResourceProvider::InUseByConsumer(ResourceId id) {
Resource* resource = GetResource(id);
return resource->lock_for_read_count > 0 || resource->exported_count > 0 ||
resource->lost;
}
bool ResourceProvider::IsLost(ResourceId id) {
Resource* resource = GetResource(id);
return resource->lost;
}
bool ResourceProvider::AllowOverlay(ResourceId id) {
Resource* resource = GetResource(id);
return resource->allow_overlay;
}
ResourceProvider::ResourceId ResourceProvider::CreateResource(
const gfx::Size& size,
GLint wrap_mode,
TextureHint hint,
ResourceFormat format) {
DCHECK(!size.IsEmpty());
switch (default_resource_type_) {
case GLTexture:
return CreateGLTexture(size,
GL_TEXTURE_2D,
GL_TEXTURE_POOL_UNMANAGED_CHROMIUM,
wrap_mode,
hint,
format);
case Bitmap:
DCHECK_EQ(RGBA_8888, format);
return CreateBitmap(size, wrap_mode);
case InvalidType:
break;
}
LOG(FATAL) << "Invalid default resource type.";
return 0;
}
ResourceProvider::ResourceId ResourceProvider::CreateManagedResource(
const gfx::Size& size,
GLenum target,
GLint wrap_mode,
TextureHint hint,
ResourceFormat format) {
DCHECK(!size.IsEmpty());
switch (default_resource_type_) {
case GLTexture:
return CreateGLTexture(size,
target,
GL_TEXTURE_POOL_MANAGED_CHROMIUM,
wrap_mode,
hint,
format);
case Bitmap:
DCHECK_EQ(RGBA_8888, format);
return CreateBitmap(size, wrap_mode);
case InvalidType:
break;
}
LOG(FATAL) << "Invalid default resource type.";
return 0;
}
ResourceProvider::ResourceId ResourceProvider::CreateGLTexture(
const gfx::Size& size,
GLenum target,
GLenum texture_pool,
GLint wrap_mode,
TextureHint hint,
ResourceFormat format) {
DCHECK_LE(size.width(), max_texture_size_);
DCHECK_LE(size.height(), max_texture_size_);
DCHECK(thread_checker_.CalledOnValidThread());
ResourceId id = next_id_++;
Resource resource(0,
size,
Resource::Internal,
target,
GL_LINEAR,
texture_pool,
wrap_mode,
hint,
format);
resource.allocated = false;
resources_[id] = resource;
return id;
}
ResourceProvider::ResourceId ResourceProvider::CreateBitmap(
const gfx::Size& size, GLint wrap_mode) {
DCHECK(thread_checker_.CalledOnValidThread());
scoped_ptr<SharedBitmap> bitmap =
shared_bitmap_manager_->AllocateSharedBitmap(size);
uint8_t* pixels = bitmap->pixels();
DCHECK(pixels);
ResourceId id = next_id_++;
Resource resource(
pixels, bitmap.release(), size, Resource::Internal, GL_LINEAR, wrap_mode);
resource.allocated = true;
resources_[id] = resource;
return id;
}
ResourceProvider::ResourceId ResourceProvider::CreateResourceFromIOSurface(
const gfx::Size& size,
unsigned io_surface_id) {
DCHECK(thread_checker_.CalledOnValidThread());
ResourceId id = next_id_++;
Resource resource(0,
gfx::Size(),
Resource::Internal,
GL_TEXTURE_RECTANGLE_ARB,
GL_LINEAR,
GL_TEXTURE_POOL_UNMANAGED_CHROMIUM,
GL_CLAMP_TO_EDGE,
TextureHintImmutable,
RGBA_8888);
LazyCreate(&resource);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
gl->BindTexture(GL_TEXTURE_RECTANGLE_ARB, resource.gl_id);
gl->TexImageIOSurface2DCHROMIUM(
GL_TEXTURE_RECTANGLE_ARB, size.width(), size.height(), io_surface_id, 0);
resource.allocated = true;
resources_[id] = resource;
return id;
}
ResourceProvider::ResourceId ResourceProvider::CreateResourceFromTextureMailbox(
const TextureMailbox& mailbox,
scoped_ptr<SingleReleaseCallbackImpl> release_callback_impl) {
DCHECK(thread_checker_.CalledOnValidThread());
// Just store the information. Mailbox will be consumed in LockForRead().
ResourceId id = next_id_++;
DCHECK(mailbox.IsValid());
Resource& resource = resources_[id];
if (mailbox.IsTexture()) {
resource = Resource(0,
gfx::Size(),
Resource::External,
mailbox.target(),
mailbox.nearest_neighbor() ? GL_NEAREST : GL_LINEAR,
0,
GL_CLAMP_TO_EDGE,
TextureHintImmutable,
RGBA_8888);
} else {
DCHECK(mailbox.IsSharedMemory());
SharedBitmap* shared_bitmap = mailbox.shared_bitmap();
uint8_t* pixels = shared_bitmap->pixels();
DCHECK(pixels);
resource = Resource(pixels, shared_bitmap, mailbox.shared_memory_size(),
Resource::External, GL_LINEAR, GL_CLAMP_TO_EDGE);
}
resource.allocated = true;
resource.mailbox = mailbox;
resource.release_callback_impl =
base::Bind(&SingleReleaseCallbackImpl::Run,
base::Owned(release_callback_impl.release()));
resource.allow_overlay = mailbox.allow_overlay();
return id;
}
void ResourceProvider::DeleteResource(ResourceId id) {
DCHECK(thread_checker_.CalledOnValidThread());
ResourceMap::iterator it = resources_.find(id);
CHECK(it != resources_.end());
Resource* resource = &it->second;
DCHECK(!resource->marked_for_deletion);
DCHECK_EQ(resource->imported_count, 0);
DCHECK(resource->pending_set_pixels || !resource->locked_for_write);
if (resource->exported_count > 0 || resource->lock_for_read_count > 0) {
resource->marked_for_deletion = true;
return;
} else {
DeleteResourceInternal(it, Normal);
}
}
void ResourceProvider::DeleteResourceInternal(ResourceMap::iterator it,
DeleteStyle style) {
TRACE_EVENT0("cc", "ResourceProvider::DeleteResourceInternal");
Resource* resource = &it->second;
bool lost_resource = resource->lost;
DCHECK(resource->exported_count == 0 || style != Normal);
if (style == ForShutdown && resource->exported_count > 0)
lost_resource = true;
if (resource->image_id) {
DCHECK(resource->origin == Resource::Internal);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
GLC(gl, gl->DestroyImageCHROMIUM(resource->image_id));
}
if (resource->gl_upload_query_id) {
DCHECK(resource->origin == Resource::Internal);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
GLC(gl, gl->DeleteQueriesEXT(1, &resource->gl_upload_query_id));
}
if (resource->gl_read_lock_query_id) {
DCHECK(resource->origin == Resource::Internal);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
GLC(gl, gl->DeleteQueriesEXT(1, &resource->gl_read_lock_query_id));
}
if (resource->gl_pixel_buffer_id) {
DCHECK(resource->origin == Resource::Internal);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
GLC(gl, gl->DeleteBuffers(1, &resource->gl_pixel_buffer_id));
}
if (resource->origin == Resource::External) {
DCHECK(resource->mailbox.IsValid());
GLuint sync_point = resource->mailbox.sync_point();
if (resource->type == GLTexture) {
DCHECK(resource->mailbox.IsTexture());
lost_resource |= lost_output_surface_;
GLES2Interface* gl = ContextGL();
DCHECK(gl);
if (resource->gl_id) {
GLC(gl, gl->DeleteTextures(1, &resource->gl_id));
resource->gl_id = 0;
if (!lost_resource)
sync_point = gl->InsertSyncPointCHROMIUM();
}
} else {
DCHECK(resource->mailbox.IsSharedMemory());
resource->shared_bitmap = nullptr;
resource->pixels = nullptr;
}
resource->release_callback_impl.Run(
sync_point, lost_resource, blocking_main_thread_task_runner_);
}
if (resource->gl_id) {
GLES2Interface* gl = ContextGL();
DCHECK(gl);
GLC(gl, gl->DeleteTextures(1, &resource->gl_id));
resource->gl_id = 0;
}
if (resource->shared_bitmap) {
DCHECK(resource->origin != Resource::External);
DCHECK_EQ(Bitmap, resource->type);
delete resource->shared_bitmap;
resource->pixels = NULL;
}
if (resource->pixels) {
DCHECK(resource->origin == Resource::Internal);
delete[] resource->pixels;
resource->pixels = NULL;
}
if (resource->gpu_memory_buffer) {
DCHECK(resource->origin == Resource::Internal);
delete resource->gpu_memory_buffer;
resource->gpu_memory_buffer = NULL;
}
resources_.erase(it);
}
ResourceProvider::ResourceType ResourceProvider::GetResourceType(
ResourceId id) {
return GetResource(id)->type;
}
void ResourceProvider::SetPixels(ResourceId id,
const uint8_t* image,
const gfx::Rect& image_rect,
const gfx::Rect& source_rect,
const gfx::Vector2d& dest_offset) {
Resource* resource = GetResource(id);
DCHECK(!resource->locked_for_write);
DCHECK(!resource->lock_for_read_count);
DCHECK(resource->origin == Resource::Internal);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(ReadLockFenceHasPassed(resource));
LazyAllocate(resource);
if (resource->type == GLTexture) {
DCHECK(resource->gl_id);
DCHECK(!resource->pending_set_pixels);
DCHECK_EQ(resource->target, static_cast<GLenum>(GL_TEXTURE_2D));
GLES2Interface* gl = ContextGL();
DCHECK(gl);
DCHECK(texture_uploader_.get());
gl->BindTexture(GL_TEXTURE_2D, resource->gl_id);
texture_uploader_->Upload(image,
image_rect,
source_rect,
dest_offset,
resource->format,
resource->size);
} else {
DCHECK_EQ(Bitmap, resource->type);
DCHECK(resource->allocated);
DCHECK_EQ(RGBA_8888, resource->format);
DCHECK(source_rect.x() >= image_rect.x());
DCHECK(source_rect.y() >= image_rect.y());
DCHECK(source_rect.right() <= image_rect.right());
DCHECK(source_rect.bottom() <= image_rect.bottom());
SkImageInfo source_info =
SkImageInfo::MakeN32Premul(source_rect.width(), source_rect.height());
size_t image_row_bytes = image_rect.width() * 4;
gfx::Vector2d source_offset = source_rect.origin() - image_rect.origin();
image += source_offset.y() * image_row_bytes + source_offset.x() * 4;
ScopedWriteLockSoftware lock(this, id);
SkCanvas dest(lock.sk_bitmap());
dest.writePixels(source_info, image, image_row_bytes, dest_offset.x(),
dest_offset.y());
}
}
size_t ResourceProvider::NumBlockingUploads() {
if (!texture_uploader_)
return 0;
return texture_uploader_->NumBlockingUploads();
}
void ResourceProvider::MarkPendingUploadsAsNonBlocking() {
if (!texture_uploader_)
return;
texture_uploader_->MarkPendingUploadsAsNonBlocking();
}
size_t ResourceProvider::EstimatedUploadsPerTick() {
if (!texture_uploader_)
return 1u;
double textures_per_second = texture_uploader_->EstimatedTexturesPerSecond();
size_t textures_per_tick = floor(
kTextureUploadTickRate * textures_per_second);
return textures_per_tick ? textures_per_tick : 1u;
}
void ResourceProvider::FlushUploads() {
if (!texture_uploader_)
return;
texture_uploader_->Flush();
}
void ResourceProvider::ReleaseCachedData() {
if (!texture_uploader_)
return;
texture_uploader_->ReleaseCachedQueries();
}
base::TimeTicks ResourceProvider::EstimatedUploadCompletionTime(
size_t uploads_per_tick) {
if (lost_output_surface_)
return base::TimeTicks();
// Software resource uploads happen on impl thread, so don't bother batching
// them up and trying to wait for them to complete.
if (!texture_uploader_) {
return gfx::FrameTime::Now() + base::TimeDelta::FromMicroseconds(
base::Time::kMicrosecondsPerSecond * kSoftwareUploadTickRate);
}
base::TimeDelta upload_one_texture_time =
base::TimeDelta::FromMicroseconds(
base::Time::kMicrosecondsPerSecond * kTextureUploadTickRate) /
uploads_per_tick;
size_t total_uploads = NumBlockingUploads() + uploads_per_tick;
return gfx::FrameTime::Now() + upload_one_texture_time * total_uploads;
}
ResourceProvider::Resource* ResourceProvider::GetResource(ResourceId id) {
DCHECK(thread_checker_.CalledOnValidThread());
ResourceMap::iterator it = resources_.find(id);
CHECK(it != resources_.end());
return &it->second;
}
const ResourceProvider::Resource* ResourceProvider::LockForRead(ResourceId id) {
Resource* resource = GetResource(id);
DCHECK(!resource->locked_for_write ||
resource->set_pixels_completion_forced) <<
"locked for write: " << resource->locked_for_write <<
" pixels completion forced: " << resource->set_pixels_completion_forced;
DCHECK_EQ(resource->exported_count, 0);
// Uninitialized! Call SetPixels or LockForWrite first.
DCHECK(resource->allocated);
LazyCreate(resource);
if (resource->type == GLTexture && !resource->gl_id) {
DCHECK(resource->origin != Resource::Internal);
DCHECK(resource->mailbox.IsTexture());
// Mailbox sync_points must be processed by a call to
// WaitSyncPointIfNeeded() prior to calling LockForRead().
DCHECK(!resource->mailbox.sync_point());
GLES2Interface* gl = ContextGL();
DCHECK(gl);
resource->gl_id = texture_id_allocator_->NextId();
GLC(gl, gl->BindTexture(resource->target, resource->gl_id));
GLC(gl,
gl->ConsumeTextureCHROMIUM(resource->mailbox.target(),
resource->mailbox.name()));
}
if (!resource->pixels && resource->has_shared_bitmap_id &&
shared_bitmap_manager_) {
scoped_ptr<SharedBitmap> bitmap =
shared_bitmap_manager_->GetSharedBitmapFromId(
resource->size, resource->shared_bitmap_id);
if (bitmap) {
resource->shared_bitmap = bitmap.release();
resource->pixels = resource->shared_bitmap->pixels();
}
}
resource->lock_for_read_count++;
if (resource->read_lock_fences_enabled) {
if (current_read_lock_fence_.get())
current_read_lock_fence_->Set();
resource->read_lock_fence = current_read_lock_fence_;
}
return resource;
}
void ResourceProvider::UnlockForRead(ResourceId id) {
DCHECK(thread_checker_.CalledOnValidThread());
ResourceMap::iterator it = resources_.find(id);
CHECK(it != resources_.end());
Resource* resource = &it->second;
DCHECK_GT(resource->lock_for_read_count, 0);
DCHECK_EQ(resource->exported_count, 0);
resource->lock_for_read_count--;
if (resource->marked_for_deletion && !resource->lock_for_read_count) {
if (!resource->child_id) {
// The resource belongs to this ResourceProvider, so it can be destroyed.
DeleteResourceInternal(it, Normal);
} else {
ChildMap::iterator child_it = children_.find(resource->child_id);
ResourceIdArray unused;
unused.push_back(id);
DeleteAndReturnUnusedResourcesToChild(child_it, Normal, unused);
}
}
}
ResourceProvider::Resource* ResourceProvider::LockForWrite(ResourceId id) {
Resource* resource = GetResource(id);
DCHECK(CanLockForWrite(id));
resource->locked_for_write = true;
return resource;
}
bool ResourceProvider::CanLockForWrite(ResourceId id) {
Resource* resource = GetResource(id);
return !resource->locked_for_write && !resource->lock_for_read_count &&
!resource->exported_count && resource->origin == Resource::Internal &&
!resource->lost && ReadLockFenceHasPassed(resource);
}
void ResourceProvider::UnlockForWrite(ResourceProvider::Resource* resource) {
DCHECK(resource->locked_for_write);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(resource->origin == Resource::Internal);
resource->locked_for_write = false;
}
ResourceProvider::ScopedReadLockGL::ScopedReadLockGL(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id)
: resource_provider_(resource_provider),
resource_id_(resource_id),
texture_id_(resource_provider->LockForRead(resource_id)->gl_id) {
DCHECK(texture_id_);
}
ResourceProvider::ScopedReadLockGL::~ScopedReadLockGL() {
resource_provider_->UnlockForRead(resource_id_);
}
ResourceProvider::ScopedSamplerGL::ScopedSamplerGL(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id,
GLenum filter)
: ScopedReadLockGL(resource_provider, resource_id),
unit_(GL_TEXTURE0),
target_(resource_provider->BindForSampling(resource_id, unit_, filter)) {
}
ResourceProvider::ScopedSamplerGL::ScopedSamplerGL(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id,
GLenum unit,
GLenum filter)
: ScopedReadLockGL(resource_provider, resource_id),
unit_(unit),
target_(resource_provider->BindForSampling(resource_id, unit_, filter)) {
}
ResourceProvider::ScopedSamplerGL::~ScopedSamplerGL() {
}
ResourceProvider::ScopedWriteLockGL::ScopedWriteLockGL(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id)
: resource_provider_(resource_provider),
resource_(resource_provider->LockForWrite(resource_id)) {
resource_provider_->LazyAllocate(resource_);
texture_id_ = resource_->gl_id;
DCHECK(texture_id_);
}
ResourceProvider::ScopedWriteLockGL::~ScopedWriteLockGL() {
resource_provider_->UnlockForWrite(resource_);
}
void ResourceProvider::PopulateSkBitmapWithResource(
SkBitmap* sk_bitmap, const Resource* resource) {
DCHECK_EQ(RGBA_8888, resource->format);
SkImageInfo info = SkImageInfo::MakeN32Premul(resource->size.width(),
resource->size.height());
sk_bitmap->installPixels(info, resource->pixels, info.minRowBytes());
}
ResourceProvider::ScopedReadLockSoftware::ScopedReadLockSoftware(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id)
: resource_provider_(resource_provider),
resource_id_(resource_id) {
const Resource* resource = resource_provider->LockForRead(resource_id);
wrap_mode_ = resource->wrap_mode;
ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource);
}
ResourceProvider::ScopedReadLockSoftware::~ScopedReadLockSoftware() {
resource_provider_->UnlockForRead(resource_id_);
}
ResourceProvider::ScopedWriteLockSoftware::ScopedWriteLockSoftware(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id)
: resource_provider_(resource_provider),
resource_(resource_provider->LockForWrite(resource_id)) {
ResourceProvider::PopulateSkBitmapWithResource(&sk_bitmap_, resource_);
DCHECK(valid());
}
ResourceProvider::ScopedWriteLockSoftware::~ScopedWriteLockSoftware() {
DCHECK(thread_checker_.CalledOnValidThread());
resource_provider_->UnlockForWrite(resource_);
}
ResourceProvider::ScopedWriteLockGpuMemoryBuffer::
ScopedWriteLockGpuMemoryBuffer(ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id)
: resource_provider_(resource_provider),
resource_(resource_provider->LockForWrite(resource_id)),
gpu_memory_buffer_manager_(resource_provider->gpu_memory_buffer_manager_),
gpu_memory_buffer_(nullptr),
size_(resource_->size),
format_(resource_->format) {
DCHECK_EQ(GLTexture, resource_->type);
std::swap(gpu_memory_buffer_, resource_->gpu_memory_buffer);
}
ResourceProvider::ScopedWriteLockGpuMemoryBuffer::
~ScopedWriteLockGpuMemoryBuffer() {
DCHECK(thread_checker_.CalledOnValidThread());
resource_provider_->UnlockForWrite(resource_);
if (!gpu_memory_buffer_)
return;
if (!resource_->image_id) {
GLES2Interface* gl = resource_provider_->ContextGL();
DCHECK(gl);
#if defined(OS_CHROMEOS)
// TODO(reveman): GL_COMMANDS_ISSUED_CHROMIUM is used for synchronization
// on ChromeOS to avoid some performance issues. This only works with
// shared memory backed buffers. crbug.com/436314
DCHECK_EQ(gpu_memory_buffer_->GetHandle().type, gfx::SHARED_MEMORY_BUFFER);
#endif
resource_->image_id =
gl->CreateImageCHROMIUM(gpu_memory_buffer_->AsClientBuffer(),
size_.width(),
size_.height(),
GL_RGBA);
}
std::swap(resource_->gpu_memory_buffer, gpu_memory_buffer_);
resource_->allocated = true;
resource_->dirty_image = true;
// GpuMemoryBuffer provides direct access to the memory used by the GPU.
// Read lock fences are required to ensure that we're not trying to map a
// buffer that is currently in-use by the GPU.
resource_->read_lock_fences_enabled = true;
}
gfx::GpuMemoryBuffer*
ResourceProvider::ScopedWriteLockGpuMemoryBuffer::GetGpuMemoryBuffer() {
if (!gpu_memory_buffer_) {
scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer =
gpu_memory_buffer_manager_->AllocateGpuMemoryBuffer(
size_, ToGpuMemoryBufferFormat(format_), gfx::GpuMemoryBuffer::MAP);
gpu_memory_buffer_ = gpu_memory_buffer.release();
}
return gpu_memory_buffer_;
}
ResourceProvider::ScopedWriteLockGr::ScopedWriteLockGr(
ResourceProvider* resource_provider,
ResourceProvider::ResourceId resource_id)
: resource_provider_(resource_provider),
resource_(resource_provider->LockForWrite(resource_id)) {
}
ResourceProvider::ScopedWriteLockGr::~ScopedWriteLockGr() {
DCHECK(thread_checker_.CalledOnValidThread());
resource_provider_->UnlockForWrite(resource_);
}
SkSurface* ResourceProvider::ScopedWriteLockGr::GetSkSurface(
bool use_distance_field_text,
bool can_use_lcd_text) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(resource_->locked_for_write);
bool create_surface =
!resource_->sk_surface.get() ||
!SurfaceHasMatchingProperties(use_distance_field_text, can_use_lcd_text);
if (create_surface) {
resource_provider_->LazyAllocate(resource_);
GrBackendTextureDesc desc;
desc.fFlags = kRenderTarget_GrBackendTextureFlag;
desc.fWidth = resource_->size.width();
desc.fHeight = resource_->size.height();
desc.fConfig = ToGrPixelConfig(resource_->format);
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
desc.fTextureHandle = resource_->gl_id;
class GrContext* gr_context = resource_provider_->GrContext();
skia::RefPtr<GrTexture> gr_texture =
skia::AdoptRef(gr_context->wrapBackendTexture(desc));
if (!gr_texture)
return nullptr;
uint32_t flags = use_distance_field_text
? SkSurfaceProps::kUseDistanceFieldFonts_Flag
: 0;
// Use unknown pixel geometry to disable LCD text.
SkSurfaceProps surface_props(flags, kUnknown_SkPixelGeometry);
if (can_use_lcd_text) {
// LegacyFontHost will get LCD text and skia figures out what type to use.
surface_props =
SkSurfaceProps(flags, SkSurfaceProps::kLegacyFontHost_InitType);
}
resource_->sk_surface = skia::AdoptRef(SkSurface::NewRenderTargetDirect(
gr_texture->asRenderTarget(), &surface_props));
}
return resource_->sk_surface.get();
}
bool ResourceProvider::ScopedWriteLockGr::SurfaceHasMatchingProperties(
bool use_distance_field_text,
bool can_use_lcd_text) const {
const SkSurface* surface = resource_->sk_surface.get();
bool surface_uses_distance_field_text =
surface->props().isUseDistanceFieldFonts();
bool surface_can_use_lcd_text =
surface->props().pixelGeometry() != kUnknown_SkPixelGeometry;
return use_distance_field_text == surface_uses_distance_field_text &&
can_use_lcd_text == surface_can_use_lcd_text;
}
ResourceProvider::SynchronousFence::SynchronousFence(
gpu::gles2::GLES2Interface* gl)
: gl_(gl), has_synchronized_(true) {
}
ResourceProvider::SynchronousFence::~SynchronousFence() {
}
void ResourceProvider::SynchronousFence::Set() {
has_synchronized_ = false;
}
bool ResourceProvider::SynchronousFence::HasPassed() {
if (!has_synchronized_) {
has_synchronized_ = true;
Synchronize();
}
return true;
}
void ResourceProvider::SynchronousFence::Wait() {
HasPassed();
}
void ResourceProvider::SynchronousFence::Synchronize() {
TRACE_EVENT0("cc", "ResourceProvider::SynchronousFence::Synchronize");
gl_->Finish();
}
ResourceProvider::ResourceProvider(
OutputSurface* output_surface,
SharedBitmapManager* shared_bitmap_manager,
gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
BlockingTaskRunner* blocking_main_thread_task_runner,
int highp_threshold_min,
bool use_rgba_4444_texture_format,
size_t id_allocation_chunk_size)
: output_surface_(output_surface),
shared_bitmap_manager_(shared_bitmap_manager),
gpu_memory_buffer_manager_(gpu_memory_buffer_manager),
blocking_main_thread_task_runner_(blocking_main_thread_task_runner),
lost_output_surface_(false),
highp_threshold_min_(highp_threshold_min),
next_id_(1),
next_child_(1),
default_resource_type_(InvalidType),
use_texture_storage_ext_(false),
use_texture_format_bgra_(false),
use_texture_usage_hint_(false),
use_compressed_texture_etc1_(false),
yuv_resource_format_(LUMINANCE_8),
max_texture_size_(0),
best_texture_format_(RGBA_8888),
use_rgba_4444_texture_format_(use_rgba_4444_texture_format),
id_allocation_chunk_size_(id_allocation_chunk_size),
use_sync_query_(false) {
DCHECK(output_surface_->HasClient());
DCHECK(id_allocation_chunk_size_);
}
void ResourceProvider::InitializeSoftware() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_NE(Bitmap, default_resource_type_);
CleanUpGLIfNeeded();
default_resource_type_ = Bitmap;
// Pick an arbitrary limit here similar to what hardware might.
max_texture_size_ = 16 * 1024;
best_texture_format_ = RGBA_8888;
}
void ResourceProvider::InitializeGL() {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!texture_uploader_);
DCHECK_NE(GLTexture, default_resource_type_);
DCHECK(!texture_id_allocator_);
DCHECK(!buffer_id_allocator_);
default_resource_type_ = GLTexture;
const ContextProvider::Capabilities& caps =
output_surface_->context_provider()->ContextCapabilities();
bool use_bgra = caps.gpu.texture_format_bgra8888;
use_texture_storage_ext_ = caps.gpu.texture_storage;
use_texture_format_bgra_ = caps.gpu.texture_format_bgra8888;
use_texture_usage_hint_ = caps.gpu.texture_usage;
use_compressed_texture_etc1_ = caps.gpu.texture_format_etc1;
yuv_resource_format_ = caps.gpu.texture_rg ? RED_8 : LUMINANCE_8;
use_sync_query_ = caps.gpu.sync_query;
GLES2Interface* gl = ContextGL();
DCHECK(gl);
texture_uploader_ = TextureUploader::Create(gl);
max_texture_size_ = 0; // Context expects cleared value.
GLC(gl, gl->GetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size_));
best_texture_format_ = PlatformColor::BestTextureFormat(use_bgra);
texture_id_allocator_.reset(
new TextureIdAllocator(gl, id_allocation_chunk_size_));
buffer_id_allocator_.reset(
new BufferIdAllocator(gl, id_allocation_chunk_size_));
}
void ResourceProvider::CleanUpGLIfNeeded() {
GLES2Interface* gl = ContextGL();
if (default_resource_type_ != GLTexture) {
// We are not in GL mode, but double check before returning.
DCHECK(!gl);
DCHECK(!texture_uploader_);
return;
}
DCHECK(gl);
#if DCHECK_IS_ON()
// Check that all GL resources has been deleted.
for (ResourceMap::const_iterator itr = resources_.begin();
itr != resources_.end();
++itr) {
DCHECK_NE(GLTexture, itr->second.type);
}
#endif // DCHECK_IS_ON()
texture_uploader_ = nullptr;
texture_id_allocator_ = nullptr;
buffer_id_allocator_ = nullptr;
gl->Finish();
}
int ResourceProvider::CreateChild(const ReturnCallback& return_callback) {
DCHECK(thread_checker_.CalledOnValidThread());
Child child_info;
child_info.return_callback = return_callback;
int child = next_child_++;
children_[child] = child_info;
return child;
}
void ResourceProvider::DestroyChild(int child_id) {
ChildMap::iterator it = children_.find(child_id);
DCHECK(it != children_.end());
DestroyChildInternal(it, Normal);
}
void ResourceProvider::DestroyChildInternal(ChildMap::iterator it,
DeleteStyle style) {
DCHECK(thread_checker_.CalledOnValidThread());
Child& child = it->second;
DCHECK(style == ForShutdown || !child.marked_for_deletion);
ResourceIdArray resources_for_child;
for (ResourceIdMap::iterator child_it = child.child_to_parent_map.begin();
child_it != child.child_to_parent_map.end();
++child_it) {
ResourceId id = child_it->second;
resources_for_child.push_back(id);
}
// If the child is going away, don't consider any resources in use.
child.in_use_resources.clear();
child.marked_for_deletion = true;
DeleteAndReturnUnusedResourcesToChild(it, style, resources_for_child);
}
const ResourceProvider::ResourceIdMap& ResourceProvider::GetChildToParentMap(
int child) const {
DCHECK(thread_checker_.CalledOnValidThread());
ChildMap::const_iterator it = children_.find(child);
DCHECK(it != children_.end());
DCHECK(!it->second.marked_for_deletion);
return it->second.child_to_parent_map;
}
void ResourceProvider::PrepareSendToParent(const ResourceIdArray& resources,
TransferableResourceArray* list) {
DCHECK(thread_checker_.CalledOnValidThread());
GLES2Interface* gl = ContextGL();
bool need_sync_point = false;
for (ResourceIdArray::const_iterator it = resources.begin();
it != resources.end();
++it) {
TransferableResource resource;
TransferResource(gl, *it, &resource);
if (!resource.mailbox_holder.sync_point && !resource.is_software)
need_sync_point = true;
++resources_.find(*it)->second.exported_count;
list->push_back(resource);
}
if (need_sync_point) {
GLuint sync_point = gl->InsertSyncPointCHROMIUM();
for (TransferableResourceArray::iterator it = list->begin();
it != list->end();
++it) {
if (!it->mailbox_holder.sync_point)
it->mailbox_holder.sync_point = sync_point;
}
}
}
void ResourceProvider::ReceiveFromChild(
int child, const TransferableResourceArray& resources) {
DCHECK(thread_checker_.CalledOnValidThread());
GLES2Interface* gl = ContextGL();
Child& child_info = children_.find(child)->second;
DCHECK(!child_info.marked_for_deletion);
for (TransferableResourceArray::const_iterator it = resources.begin();
it != resources.end();
++it) {
ResourceIdMap::iterator resource_in_map_it =
child_info.child_to_parent_map.find(it->id);
if (resource_in_map_it != child_info.child_to_parent_map.end()) {
Resource& resource = resources_[resource_in_map_it->second];
resource.marked_for_deletion = false;
resource.imported_count++;
continue;
}
if ((!it->is_software && !gl) ||
(it->is_software && !shared_bitmap_manager_)) {
TRACE_EVENT0("cc", "ResourceProvider::ReceiveFromChild dropping invalid");
ReturnedResourceArray to_return;
to_return.push_back(it->ToReturnedResource());
child_info.return_callback.Run(to_return,
blocking_main_thread_task_runner_);
continue;
}
ResourceId local_id = next_id_++;
Resource& resource = resources_[local_id];
if (it->is_software) {
resource = Resource(it->mailbox_holder.mailbox,
it->size,
Resource::Delegated,
GL_LINEAR,
it->is_repeated ? GL_REPEAT : GL_CLAMP_TO_EDGE);
} else {
resource = Resource(0,
it->size,
Resource::Delegated,
it->mailbox_holder.texture_target,
it->filter,
0,
it->is_repeated ? GL_REPEAT : GL_CLAMP_TO_EDGE,
TextureHintImmutable,
it->format);
resource.mailbox = TextureMailbox(it->mailbox_holder.mailbox,
it->mailbox_holder.texture_target,
it->mailbox_holder.sync_point);
}
resource.child_id = child;
// Don't allocate a texture for a child.
resource.allocated = true;
resource.imported_count = 1;
resource.allow_overlay = it->allow_overlay;
child_info.parent_to_child_map[local_id] = it->id;
child_info.child_to_parent_map[it->id] = local_id;
}
}
void ResourceProvider::DeclareUsedResourcesFromChild(
int child,
const ResourceIdArray& resources_from_child) {
DCHECK(thread_checker_.CalledOnValidThread());
ChildMap::iterator child_it = children_.find(child);
DCHECK(child_it != children_.end());
Child& child_info = child_it->second;
DCHECK(!child_info.marked_for_deletion);
child_info.in_use_resources.clear();
for (size_t i = 0; i < resources_from_child.size(); ++i) {
ResourceIdMap::iterator it =
child_info.child_to_parent_map.find(resources_from_child[i]);
DCHECK(it != child_info.child_to_parent_map.end());
ResourceId local_id = it->second;
DCHECK(!resources_[local_id].marked_for_deletion);
child_info.in_use_resources.insert(local_id);
}
ResourceIdArray unused;
for (ResourceIdMap::iterator it = child_info.child_to_parent_map.begin();
it != child_info.child_to_parent_map.end();
++it) {
ResourceId local_id = it->second;
bool resource_is_in_use = child_info.in_use_resources.count(local_id) > 0;
if (!resource_is_in_use)
unused.push_back(local_id);
}
DeleteAndReturnUnusedResourcesToChild(child_it, Normal, unused);
}
// static
bool ResourceProvider::CompareResourceMapIteratorsByChildId(
const std::pair<ReturnedResource, ResourceMap::iterator>& a,
const std::pair<ReturnedResource, ResourceMap::iterator>& b) {
const ResourceMap::iterator& a_it = a.second;
const ResourceMap::iterator& b_it = b.second;
const Resource& a_resource = a_it->second;
const Resource& b_resource = b_it->second;
return a_resource.child_id < b_resource.child_id;
}
void ResourceProvider::ReceiveReturnsFromParent(
const ReturnedResourceArray& resources) {
DCHECK(thread_checker_.CalledOnValidThread());
GLES2Interface* gl = ContextGL();
int child_id = 0;
ResourceIdArray resources_for_child;
std::vector<std::pair<ReturnedResource, ResourceMap::iterator>>
sorted_resources;
for (ReturnedResourceArray::const_iterator it = resources.begin();
it != resources.end();
++it) {
ResourceId local_id = it->id;
ResourceMap::iterator map_iterator = resources_.find(local_id);
// Resource was already lost (e.g. it belonged to a child that was
// destroyed).
if (map_iterator == resources_.end())
continue;
sorted_resources.push_back(
std::pair<ReturnedResource, ResourceMap::iterator>(*it, map_iterator));
}
std::sort(sorted_resources.begin(),
sorted_resources.end(),
CompareResourceMapIteratorsByChildId);
ChildMap::iterator child_it = children_.end();
for (size_t i = 0; i < sorted_resources.size(); ++i) {
ReturnedResource& returned = sorted_resources[i].first;
ResourceMap::iterator& map_iterator = sorted_resources[i].second;
ResourceId local_id = map_iterator->first;
Resource* resource = &map_iterator->second;
CHECK_GE(resource->exported_count, returned.count);
resource->exported_count -= returned.count;
resource->lost |= returned.lost;
if (resource->exported_count)
continue;
// Need to wait for the current read lock fence to pass before we can
// recycle this resource.
if (resource->read_lock_fences_enabled) {
if (current_read_lock_fence_.get())
current_read_lock_fence_->Set();
resource->read_lock_fence = current_read_lock_fence_;
}
if (returned.sync_point) {
DCHECK(!resource->has_shared_bitmap_id);
if (resource->origin == Resource::Internal) {
DCHECK(resource->gl_id);
GLC(gl, gl->WaitSyncPointCHROMIUM(returned.sync_point));
} else {
DCHECK(!resource->gl_id);
resource->mailbox.set_sync_point(returned.sync_point);
}
}
if (!resource->marked_for_deletion)
continue;
if (!resource->child_id) {
// The resource belongs to this ResourceProvider, so it can be destroyed.
DeleteResourceInternal(map_iterator, Normal);
continue;
}
DCHECK(resource->origin == Resource::Delegated);
// Delete the resource and return it to the child it came from one.
if (resource->child_id != child_id) {
if (child_id) {
DCHECK_NE(resources_for_child.size(), 0u);
DCHECK(child_it != children_.end());
DeleteAndReturnUnusedResourcesToChild(
child_it, Normal, resources_for_child);
resources_for_child.clear();
}
child_it = children_.find(resource->child_id);
DCHECK(child_it != children_.end());
child_id = resource->child_id;
}
resources_for_child.push_back(local_id);
}
if (child_id) {
DCHECK_NE(resources_for_child.size(), 0u);
DCHECK(child_it != children_.end());
DeleteAndReturnUnusedResourcesToChild(
child_it, Normal, resources_for_child);
}
}
void ResourceProvider::TransferResource(GLES2Interface* gl,
ResourceId id,
TransferableResource* resource) {
Resource* source = GetResource(id);
DCHECK(!source->locked_for_write);
DCHECK(!source->lock_for_read_count);
DCHECK(source->origin != Resource::External || source->mailbox.IsValid());
DCHECK(source->allocated);
resource->id = id;
resource->format = source->format;
resource->mailbox_holder.texture_target = source->target;
resource->filter = source->filter;
resource->size = source->size;
resource->is_repeated = (source->wrap_mode == GL_REPEAT);
resource->allow_overlay = source->allow_overlay;
if (source->type == Bitmap) {
resource->mailbox_holder.mailbox = source->shared_bitmap_id;
resource->is_software = true;
} else if (!source->mailbox.IsValid()) {
LazyCreate(source);
DCHECK(source->gl_id);
DCHECK(source->origin == Resource::Internal);
GLC(gl,
gl->BindTexture(resource->mailbox_holder.texture_target,
source->gl_id));
if (source->image_id) {
DCHECK(source->dirty_image);
BindImageForSampling(source);
}
// This is a resource allocated by the compositor, we need to produce it.
// Don't set a sync point, the caller will do it.
GLC(gl, gl->GenMailboxCHROMIUM(resource->mailbox_holder.mailbox.name));
GLC(gl,
gl->ProduceTextureCHROMIUM(resource->mailbox_holder.texture_target,
resource->mailbox_holder.mailbox.name));
source->mailbox = TextureMailbox(resource->mailbox_holder);
} else {
DCHECK(source->mailbox.IsTexture());
if (source->image_id && source->dirty_image) {
DCHECK(source->gl_id);
DCHECK(source->origin == Resource::Internal);
GLC(gl,
gl->BindTexture(resource->mailbox_holder.texture_target,
source->gl_id));
BindImageForSampling(source);
}
// This is either an external resource, or a compositor resource that we
// already exported. Make sure to forward the sync point that we were given.
resource->mailbox_holder.mailbox = source->mailbox.mailbox();
resource->mailbox_holder.texture_target = source->mailbox.target();
resource->mailbox_holder.sync_point = source->mailbox.sync_point();
source->mailbox.set_sync_point(0);
}
}
void ResourceProvider::DeleteAndReturnUnusedResourcesToChild(
ChildMap::iterator child_it,
DeleteStyle style,
const ResourceIdArray& unused) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(child_it != children_.end());
Child* child_info = &child_it->second;
if (unused.empty() && !child_info->marked_for_deletion)
return;
ReturnedResourceArray to_return;
GLES2Interface* gl = ContextGL();
bool need_sync_point = false;
for (size_t i = 0; i < unused.size(); ++i) {
ResourceId local_id = unused[i];
ResourceMap::iterator it = resources_.find(local_id);
CHECK(it != resources_.end());
Resource& resource = it->second;
DCHECK(!resource.locked_for_write);
DCHECK_EQ(0u, child_info->in_use_resources.count(local_id));
DCHECK(child_info->parent_to_child_map.count(local_id));
ResourceId child_id = child_info->parent_to_child_map[local_id];
DCHECK(child_info->child_to_parent_map.count(child_id));
bool is_lost =
resource.lost || (resource.type == GLTexture && lost_output_surface_);
if (resource.exported_count > 0 || resource.lock_for_read_count > 0) {
if (style != ForShutdown) {
// Defer this until we receive the resource back from the parent or
// the read lock is released.
resource.marked_for_deletion = true;
continue;
}
// We still have an exported_count, so we'll have to lose it.
is_lost = true;
}
if (gl && resource.filter != resource.original_filter) {
DCHECK(resource.target);
DCHECK(resource.gl_id);
GLC(gl, gl->BindTexture(resource.target, resource.gl_id));
GLC(gl,
gl->TexParameteri(resource.target,
GL_TEXTURE_MIN_FILTER,
resource.original_filter));
GLC(gl,
gl->TexParameteri(resource.target,
GL_TEXTURE_MAG_FILTER,
resource.original_filter));
}
ReturnedResource returned;
returned.id = child_id;
returned.sync_point = resource.mailbox.sync_point();
if (!returned.sync_point && resource.type == GLTexture)
need_sync_point = true;
returned.count = resource.imported_count;
returned.lost = is_lost;
to_return.push_back(returned);
child_info->parent_to_child_map.erase(local_id);
child_info->child_to_parent_map.erase(child_id);
resource.imported_count = 0;
DeleteResourceInternal(it, style);
}
if (need_sync_point) {
DCHECK(gl);
GLuint sync_point = gl->InsertSyncPointCHROMIUM();
for (size_t i = 0; i < to_return.size(); ++i) {
if (!to_return[i].sync_point)
to_return[i].sync_point = sync_point;
}
}
if (!to_return.empty())
child_info->return_callback.Run(to_return,
blocking_main_thread_task_runner_);
if (child_info->marked_for_deletion &&
child_info->parent_to_child_map.empty()) {
DCHECK(child_info->child_to_parent_map.empty());
children_.erase(child_it);
}
}
void ResourceProvider::AcquirePixelBuffer(ResourceId id) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::AcquirePixelBuffer");
Resource* resource = GetResource(id);
DCHECK(resource->origin == Resource::Internal);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(!resource->image_id);
DCHECK_NE(ETC1, resource->format);
DCHECK_EQ(GLTexture, resource->type);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
if (!resource->gl_pixel_buffer_id)
resource->gl_pixel_buffer_id = buffer_id_allocator_->NextId();
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
resource->gl_pixel_buffer_id);
unsigned bytes_per_pixel = BitsPerPixel(resource->format) / 8;
gl->BufferData(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
resource->size.height() *
RoundUp(bytes_per_pixel * resource->size.width(), 4u),
NULL,
GL_DYNAMIC_DRAW);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
}
void ResourceProvider::ReleasePixelBuffer(ResourceId id) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::ReleasePixelBuffer");
Resource* resource = GetResource(id);
DCHECK(resource->origin == Resource::Internal);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(!resource->image_id);
// The pixel buffer can be released while there is a pending "set pixels"
// if completion has been forced. Any shared memory associated with this
// pixel buffer will not be freed until the waitAsyncTexImage2DCHROMIUM
// command has been processed on the service side. It is also safe to
// reuse any query id associated with this resource before they complete
// as each new query has a unique submit count.
if (resource->pending_set_pixels) {
DCHECK(resource->set_pixels_completion_forced);
resource->pending_set_pixels = false;
resource->locked_for_write = false;
}
DCHECK_EQ(GLTexture, resource->type);
if (!resource->gl_pixel_buffer_id)
return;
GLES2Interface* gl = ContextGL();
DCHECK(gl);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
resource->gl_pixel_buffer_id);
gl->BufferData(
GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0, NULL, GL_DYNAMIC_DRAW);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
}
uint8_t* ResourceProvider::MapPixelBuffer(ResourceId id, int* stride) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::MapPixelBuffer");
Resource* resource = GetResource(id);
DCHECK(resource->origin == Resource::Internal);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(!resource->image_id);
*stride = 0;
DCHECK_EQ(GLTexture, resource->type);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
DCHECK(resource->gl_pixel_buffer_id);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
resource->gl_pixel_buffer_id);
uint8_t* image = static_cast<uint8_t*>(gl->MapBufferCHROMIUM(
GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, GL_WRITE_ONLY));
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
// Buffer is required to be 4-byte aligned.
CHECK(!(reinterpret_cast<intptr_t>(image) & 3));
return image;
}
void ResourceProvider::UnmapPixelBuffer(ResourceId id) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::UnmapPixelBuffer");
Resource* resource = GetResource(id);
DCHECK(resource->origin == Resource::Internal);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(!resource->image_id);
DCHECK_EQ(GLTexture, resource->type);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
DCHECK(resource->gl_pixel_buffer_id);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
resource->gl_pixel_buffer_id);
gl->UnmapBufferCHROMIUM(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
}
GLenum ResourceProvider::BindForSampling(ResourceId resource_id,
GLenum unit,
GLenum filter) {
DCHECK(thread_checker_.CalledOnValidThread());
GLES2Interface* gl = ContextGL();
ResourceMap::iterator it = resources_.find(resource_id);
DCHECK(it != resources_.end());
Resource* resource = &it->second;
DCHECK(resource->lock_for_read_count);
DCHECK(!resource->locked_for_write || resource->set_pixels_completion_forced);
ScopedSetActiveTexture scoped_active_tex(gl, unit);
GLenum target = resource->target;
GLC(gl, gl->BindTexture(target, resource->gl_id));
if (filter != resource->filter) {
GLC(gl, gl->TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter));
GLC(gl, gl->TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter));
resource->filter = filter;
}
if (resource->image_id && resource->dirty_image)
BindImageForSampling(resource);
return target;
}
void ResourceProvider::BeginSetPixels(ResourceId id) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::BeginSetPixels");
Resource* resource = GetResource(id);
DCHECK(!resource->pending_set_pixels);
LazyCreate(resource);
DCHECK(resource->origin == Resource::Internal);
DCHECK(resource->gl_id || resource->allocated);
DCHECK(ReadLockFenceHasPassed(resource));
DCHECK(!resource->image_id);
bool allocate = !resource->allocated;
resource->allocated = true;
LockForWrite(id);
DCHECK_EQ(GLTexture, resource->type);
DCHECK(resource->gl_id);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
DCHECK(resource->gl_pixel_buffer_id);
DCHECK_EQ(resource->target, static_cast<GLenum>(GL_TEXTURE_2D));
gl->BindTexture(GL_TEXTURE_2D, resource->gl_id);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM,
resource->gl_pixel_buffer_id);
if (!resource->gl_upload_query_id)
gl->GenQueriesEXT(1, &resource->gl_upload_query_id);
gl->BeginQueryEXT(GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM,
resource->gl_upload_query_id);
if (allocate) {
gl->AsyncTexImage2DCHROMIUM(GL_TEXTURE_2D,
0, /* level */
GLInternalFormat(resource->format),
resource->size.width(),
resource->size.height(),
0, /* border */
GLDataFormat(resource->format),
GLDataType(resource->format),
NULL);
} else {
gl->AsyncTexSubImage2DCHROMIUM(GL_TEXTURE_2D,
0, /* level */
0, /* x */
0, /* y */
resource->size.width(),
resource->size.height(),
GLDataFormat(resource->format),
GLDataType(resource->format),
NULL);
}
gl->EndQueryEXT(GL_ASYNC_PIXEL_UNPACK_COMPLETED_CHROMIUM);
gl->BindBuffer(GL_PIXEL_UNPACK_TRANSFER_BUFFER_CHROMIUM, 0);
resource->pending_set_pixels = true;
resource->set_pixels_completion_forced = false;
}
void ResourceProvider::ForceSetPixelsToComplete(ResourceId id) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::ForceSetPixelsToComplete");
Resource* resource = GetResource(id);
DCHECK(resource->locked_for_write);
DCHECK(resource->pending_set_pixels);
DCHECK(!resource->set_pixels_completion_forced);
if (resource->gl_id) {
GLES2Interface* gl = ContextGL();
GLC(gl, gl->BindTexture(GL_TEXTURE_2D, resource->gl_id));
GLC(gl, gl->WaitAsyncTexImage2DCHROMIUM(GL_TEXTURE_2D));
GLC(gl, gl->BindTexture(GL_TEXTURE_2D, 0));
}
resource->set_pixels_completion_forced = true;
}
bool ResourceProvider::DidSetPixelsComplete(ResourceId id) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
"ResourceProvider::DidSetPixelsComplete");
Resource* resource = GetResource(id);
DCHECK(resource->locked_for_write);
DCHECK(resource->pending_set_pixels);
if (resource->gl_id) {
GLES2Interface* gl = ContextGL();
DCHECK(gl);
DCHECK(resource->gl_upload_query_id);
GLuint complete = 1;
gl->GetQueryObjectuivEXT(
resource->gl_upload_query_id, GL_QUERY_RESULT_AVAILABLE_EXT, &complete);
if (!complete)
return false;
}
resource->pending_set_pixels = false;
UnlockForWrite(resource);
// Async set pixels commands are not necessarily processed in-sequence with
// drawing commands. Read lock fences are required to ensure that async
// commands don't access the resource while used for drawing.
resource->read_lock_fences_enabled = true;
return true;
}
void ResourceProvider::CreateForTesting(ResourceId id) {
LazyCreate(GetResource(id));
}
GLenum ResourceProvider::TargetForTesting(ResourceId id) {
Resource* resource = GetResource(id);
return resource->target;
}
void ResourceProvider::LazyCreate(Resource* resource) {
if (resource->type != GLTexture || resource->origin != Resource::Internal)
return;
if (resource->gl_id)
return;
DCHECK(resource->texture_pool);
DCHECK(resource->origin == Resource::Internal);
DCHECK(!resource->mailbox.IsValid());
resource->gl_id = texture_id_allocator_->NextId();
GLES2Interface* gl = ContextGL();
DCHECK(gl);
// Create and set texture properties. Allocation is delayed until needed.
GLC(gl, gl->BindTexture(resource->target, resource->gl_id));
GLC(gl,
gl->TexParameteri(
resource->target, GL_TEXTURE_MIN_FILTER, resource->original_filter));
GLC(gl,
gl->TexParameteri(
resource->target, GL_TEXTURE_MAG_FILTER, resource->original_filter));
GLC(gl,
gl->TexParameteri(
resource->target, GL_TEXTURE_WRAP_S, resource->wrap_mode));
GLC(gl,
gl->TexParameteri(
resource->target, GL_TEXTURE_WRAP_T, resource->wrap_mode));
GLC(gl,
gl->TexParameteri(
resource->target, GL_TEXTURE_POOL_CHROMIUM, resource->texture_pool));
if (use_texture_usage_hint_ && (resource->hint & TextureHintFramebuffer)) {
GLC(gl,
gl->TexParameteri(resource->target,
GL_TEXTURE_USAGE_ANGLE,
GL_FRAMEBUFFER_ATTACHMENT_ANGLE));
}
}
void ResourceProvider::AllocateForTesting(ResourceId id) {
LazyAllocate(GetResource(id));
}
void ResourceProvider::LazyAllocate(Resource* resource) {
DCHECK(resource);
if (resource->allocated)
return;
LazyCreate(resource);
if (!resource->gl_id)
return;
resource->allocated = true;
GLES2Interface* gl = ContextGL();
gfx::Size& size = resource->size;
DCHECK_EQ(resource->target, static_cast<GLenum>(GL_TEXTURE_2D));
ResourceFormat format = resource->format;
GLC(gl, gl->BindTexture(GL_TEXTURE_2D, resource->gl_id));
if (use_texture_storage_ext_ &&
IsFormatSupportedForStorage(format, use_texture_format_bgra_) &&
(resource->hint & TextureHintImmutable)) {
GLenum storage_format = TextureToStorageFormat(format);
GLC(gl,
gl->TexStorage2DEXT(
GL_TEXTURE_2D, 1, storage_format, size.width(), size.height()));
} else {
// ETC1 does not support preallocation.
if (format != ETC1) {
GLC(gl,
gl->TexImage2D(GL_TEXTURE_2D,
0,
GLInternalFormat(format),
size.width(),
size.height(),
0,
GLDataFormat(format),
GLDataType(format),
NULL));
}
}
}
void ResourceProvider::BindImageForSampling(Resource* resource) {
GLES2Interface* gl = ContextGL();
DCHECK(resource->gl_id);
DCHECK(resource->image_id);
// Release image currently bound to texture.
if (resource->bound_image_id)
gl->ReleaseTexImage2DCHROMIUM(resource->target, resource->bound_image_id);
gl->BindTexImage2DCHROMIUM(resource->target, resource->image_id);
resource->bound_image_id = resource->image_id;
resource->dirty_image = false;
}
void ResourceProvider::CopyResource(ResourceId source_id, ResourceId dest_id) {
TRACE_EVENT0("cc", "ResourceProvider::CopyResource");
Resource* source_resource = GetResource(source_id);
DCHECK(!source_resource->lock_for_read_count);
DCHECK(source_resource->origin == Resource::Internal);
DCHECK_EQ(source_resource->exported_count, 0);
DCHECK_EQ(GLTexture, source_resource->type);
DCHECK(source_resource->allocated);
LazyCreate(source_resource);
Resource* dest_resource = GetResource(dest_id);
DCHECK(!dest_resource->locked_for_write);
DCHECK(!dest_resource->lock_for_read_count);
DCHECK(dest_resource->origin == Resource::Internal);
DCHECK_EQ(dest_resource->exported_count, 0);
DCHECK_EQ(GLTexture, dest_resource->type);
LazyAllocate(dest_resource);
DCHECK_EQ(source_resource->type, dest_resource->type);
DCHECK_EQ(source_resource->format, dest_resource->format);
DCHECK(source_resource->size == dest_resource->size);
GLES2Interface* gl = ContextGL();
DCHECK(gl);
if (source_resource->image_id && source_resource->dirty_image) {
gl->BindTexture(source_resource->target, source_resource->gl_id);
BindImageForSampling(source_resource);
}
if (use_sync_query_) {
if (!source_resource->gl_read_lock_query_id)
gl->GenQueriesEXT(1, &source_resource->gl_read_lock_query_id);
#if defined(OS_CHROMEOS)
// TODO(reveman): This avoids a performance problem on some ChromeOS
// devices. This needs to be removed to support native GpuMemoryBuffer
// implementations. crbug.com/436314
gl->BeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM,
source_resource->gl_read_lock_query_id);
#else
gl->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM,
source_resource->gl_read_lock_query_id);
#endif
}
DCHECK(!dest_resource->image_id);
dest_resource->allocated = true;
gl->CopyTextureCHROMIUM(dest_resource->target,
source_resource->gl_id,
dest_resource->gl_id,
0,
GLInternalFormat(dest_resource->format),
GLDataType(dest_resource->format));
if (source_resource->gl_read_lock_query_id) {
// End query and create a read lock fence that will prevent access to
// source resource until CopyTextureCHROMIUM command has completed.
#if defined(OS_CHROMEOS)
gl->EndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
#else
gl->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM);
#endif
source_resource->read_lock_fence = make_scoped_refptr(
new CopyTextureFence(gl, source_resource->gl_read_lock_query_id));
} else {
// Create a SynchronousFence when CHROMIUM_sync_query extension is missing.
// Try to use one synchronous fence for as many CopyResource operations as
// possible as that reduce the number of times we have to synchronize with
// the GL.
if (!synchronous_fence_.get() || synchronous_fence_->has_synchronized())
synchronous_fence_ = make_scoped_refptr(new SynchronousFence(gl));
source_resource->read_lock_fence = synchronous_fence_;
source_resource->read_lock_fence->Set();
}
}
void ResourceProvider::WaitSyncPointIfNeeded(ResourceId id) {
Resource* resource = GetResource(id);
DCHECK_EQ(resource->exported_count, 0);
DCHECK(resource->allocated);
if (resource->type != GLTexture || resource->gl_id)
return;
if (!resource->mailbox.sync_point())
return;
DCHECK(resource->mailbox.IsValid());
GLES2Interface* gl = ContextGL();
DCHECK(gl);
GLC(gl, gl->WaitSyncPointCHROMIUM(resource->mailbox.sync_point()));
resource->mailbox.set_sync_point(0);
}
void ResourceProvider::WaitReadLockIfNeeded(ResourceId id) {
Resource* resource = GetResource(id);
DCHECK_EQ(resource->exported_count, 0);
if (!resource->read_lock_fence.get())
return;
resource->read_lock_fence->Wait();
}
GLint ResourceProvider::GetActiveTextureUnit(GLES2Interface* gl) {
GLint active_unit = 0;
gl->GetIntegerv(GL_ACTIVE_TEXTURE, &active_unit);
return active_unit;
}
GLES2Interface* ResourceProvider::ContextGL() const {
ContextProvider* context_provider = output_surface_->context_provider();
return context_provider ? context_provider->ContextGL() : NULL;
}
class GrContext* ResourceProvider::GrContext() const {
ContextProvider* context_provider = output_surface_->context_provider();
return context_provider ? context_provider->GrContext() : NULL;
}
} // namespace cc
|