summaryrefslogtreecommitdiffstats
path: root/cc/gl_renderer.cc
blob: 63a35d286dc2f1695ce2fba049627ff8f70d1ecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
// Copyright 2010 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/gl_renderer.h"

#include <set>
#include <string>
#include <vector>

#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "base/strings/string_split.h"
#include "build/build_config.h"
#include "cc/base/math_util.h"
#include "cc/compositor_frame.h"
#include "cc/compositor_frame_metadata.h"
#include "cc/context_provider.h"
#include "cc/damage_tracker.h"
#include "cc/geometry_binding.h"
#include "cc/gl_frame_data.h"
#include "cc/layer_quad.h"
#include "cc/output_surface.h"
#include "cc/priority_calculator.h"
#include "cc/proxy.h"
#include "cc/render_pass.h"
#include "cc/render_surface_filters.h"
#include "cc/scoped_resource.h"
#include "cc/single_thread_proxy.h"
#include "cc/stream_video_draw_quad.h"
#include "cc/texture_draw_quad.h"
#include "cc/video_layer_impl.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/gpu/GrContext.h"
#include "third_party/skia/include/gpu/GrTexture.h"
#include "third_party/skia/include/gpu/SkGpuDevice.h"
#include "third_party/skia/include/gpu/SkGrTexturePixelRef.h"
#include "ui/gfx/quad_f.h"
#include "ui/gfx/rect_conversions.h"

using WebKit::WebGraphicsContext3D;
using WebKit::WebGraphicsMemoryAllocation;

namespace cc {

namespace {

// TODO(epenner): This should probably be moved to output surface.
//
// This implements a simple fence based on client side swaps.
// This is to isolate the ResourceProvider from 'frames' which
// it shouldn't need to care about, while still allowing us to
// enforce good texture recycling behavior strictly throughout
// the compositor (don't recycle a texture while it's in use).
class SimpleSwapFence : public ResourceProvider::Fence {
 public:
  SimpleSwapFence() : has_passed_(false) {}
  virtual bool HasPassed() OVERRIDE { return has_passed_; }
  void SetHasPassed() { has_passed_ = true; }
 private:
  virtual ~SimpleSwapFence() {}
  bool has_passed_;
};

bool NeedsIOSurfaceReadbackWorkaround() {
#if defined(OS_MACOSX)
  return true;
#else
  return false;
#endif
}

}  // anonymous namespace

scoped_ptr<GLRenderer> GLRenderer::Create(RendererClient* client,
                                          OutputSurface* output_surface,
                                          ResourceProvider* resource_provider) {
  scoped_ptr<GLRenderer> renderer(
      new GLRenderer(client, output_surface, resource_provider));
  if (!renderer->Initialize())
    return scoped_ptr<GLRenderer>();

  return renderer.Pass();
}

GLRenderer::GLRenderer(RendererClient* client,
                       OutputSurface* output_surface,
                       ResourceProvider* resource_provider)
    : DirectRenderer(client, resource_provider),
      offscreen_framebuffer_id_(0),
      shared_geometry_quad_(gfx::RectF(-0.5f, -0.5f, 1.0f, 1.0f)),
      output_surface_(output_surface),
      context_(output_surface->context3d()),
      is_viewport_changed_(false),
      is_backbuffer_discarded_(false),
      discard_backbuffer_when_not_visible_(false),
      is_using_bind_uniform_(false),
      visible_(true),
      is_scissor_enabled_(false) {
  DCHECK(context_);
}

bool GLRenderer::Initialize() {
  if (!context_->makeContextCurrent())
    return false;

  context_->setContextLostCallback(this);
  context_->pushGroupMarkerEXT("CompositorContext");

  std::string extensions_string =
      UTF16ToASCII(context_->getString(GL_EXTENSIONS));
  std::vector<std::string> extensions_list;
  base::SplitString(extensions_string, ' ', &extensions_list);
  std::set<std::string> extensions(extensions_list.begin(),
                                   extensions_list.end());

  if (Settings().acceleratePainting &&
      extensions.count("GL_EXT_texture_format_BGRA8888") &&
      extensions.count("GL_EXT_read_format_bgra"))
    capabilities_.using_accelerated_painting = true;
  else
    capabilities_.using_accelerated_painting = false;

  capabilities_.using_partial_swap =
      Settings().partialSwapEnabled &&
      extensions.count("GL_CHROMIUM_post_sub_buffer");

  // Use the swapBuffers callback only with the threaded proxy.
  if (client_->HasImplThread())
    capabilities_.using_swap_complete_callback =
        extensions.count("GL_CHROMIUM_swapbuffers_complete_callback");
  if (capabilities_.using_swap_complete_callback)
    context_->setSwapBuffersCompleteCallbackCHROMIUM(this);

  capabilities_.using_set_visibility =
      extensions.count("GL_CHROMIUM_set_visibility");

  if (extensions.count("GL_CHROMIUM_iosurface"))
    DCHECK(extensions.count("GL_ARB_texture_rectangle"));

  capabilities_.using_gpu_memory_manager =
      extensions.count("GL_CHROMIUM_gpu_memory_manager") &&
      Settings().useMemoryManagement;
  if (capabilities_.using_gpu_memory_manager)
    context_->setMemoryAllocationChangedCallbackCHROMIUM(this);

  capabilities_.using_egl_image = extensions.count("GL_OES_EGL_image_external");

  capabilities_.max_texture_size = resource_provider_->max_texture_size();
  capabilities_.best_texture_format = resource_provider_->best_texture_format();

  // The updater can access textures while the GLRenderer is using them.
  capabilities_.allow_partial_texture_updates = true;

  // Check for texture fast paths. Currently we always use MO8 textures,
  // so we only need to avoid POT textures if we have an NPOT fast-path.
  capabilities_.avoid_pow2_textures =
      extensions.count("GL_CHROMIUM_fast_NPOT_MO8_textures");

  capabilities_.using_offscreen_context3d = true;

  is_using_bind_uniform_ =
      extensions.count("GL_CHROMIUM_bind_uniform_location");

  // Make sure scissoring starts as disabled.
  GLC(context_, context_->disable(GL_SCISSOR_TEST));
  DCHECK(!is_scissor_enabled_);

  if (!InitializeSharedObjects())
    return false;

  // Make sure the viewport and context gets initialized, even if it is to zero.
  ViewportChanged();
  return true;
}

GLRenderer::~GLRenderer() {
  context_->setSwapBuffersCompleteCallbackCHROMIUM(NULL);
  context_->setMemoryAllocationChangedCallbackCHROMIUM(NULL);
  context_->setContextLostCallback(NULL);
  CleanupSharedObjects();
}

const RendererCapabilities& GLRenderer::Capabilities() const {
  return capabilities_;
}

WebGraphicsContext3D* GLRenderer::Context() { return context_; }

void GLRenderer::DebugGLCall(WebGraphicsContext3D* context,
                             const char* command,
                             const char* file,
                             int line) {
  unsigned long error = context->getError();
  if (error != GL_NO_ERROR)
    LOG(ERROR) << "GL command failed: File: " << file << "\n\tLine " << line
               << "\n\tcommand: " << command << ", error "
               << static_cast<int>(error) << "\n";
}

void GLRenderer::SetVisible(bool visible) {
  if (visible_ == visible)
    return;
  visible_ = visible;

  EnforceMemoryPolicy();

  // TODO: Replace setVisibilityCHROMIUM with an extension to explicitly manage
  // front/backbuffers
  // crbug.com/116049
  if (capabilities_.using_set_visibility)
    context_->setVisibilityCHROMIUM(visible);
}

void GLRenderer::SendManagedMemoryStats(size_t bytes_visible,
                                        size_t bytes_visible_and_nearby,
                                        size_t bytes_allocated) {
  WebKit::WebGraphicsManagedMemoryStats stats;
  stats.bytesVisible = bytes_visible;
  stats.bytesVisibleAndNearby = bytes_visible_and_nearby;
  stats.bytesAllocated = bytes_allocated;
  stats.backbufferRequested = !is_backbuffer_discarded_;
  context_->sendManagedMemoryStatsCHROMIUM(&stats);
}

void GLRenderer::ReleaseRenderPassTextures() { render_pass_textures_.clear(); }

void GLRenderer::ViewportChanged() { is_viewport_changed_ = true; }

void GLRenderer::ClearFramebuffer(DrawingFrame& frame) {
  // On DEBUG builds, opaque render passes are cleared to blue to easily see
  // regions that were not drawn on the screen.
  if (frame.current_render_pass->has_transparent_background)
    GLC(context_, context_->clearColor(0, 0, 0, 0));
  else
    GLC(context_, context_->clearColor(0, 0, 1, 1));

#ifdef NDEBUG
  if (frame.current_render_pass->has_transparent_background)
#endif
    context_->clear(GL_COLOR_BUFFER_BIT);
}

void GLRenderer::BeginDrawingFrame(DrawingFrame& frame) {
  // FIXME: Remove this once backbuffer is automatically recreated on first use
  EnsureBackbuffer();

  if (ViewportSize().IsEmpty())
    return;

  TRACE_EVENT0("cc", "GLRenderer::drawLayers");
  if (is_viewport_changed_) {
    // Only reshape when we know we are going to draw. Otherwise, the reshape
    // can leave the window at the wrong size if we never draw and the proper
    // viewport size is never set.
    is_viewport_changed_ = false;
    output_surface_->Reshape(gfx::Size(ViewportWidth(), ViewportHeight()));
  }

  MakeContextCurrent();
  // Bind the common vertex attributes used for drawing all the layers.
  shared_geometry_->PrepareForDraw();

  GLC(context_, context_->disable(GL_DEPTH_TEST));
  GLC(context_, context_->disable(GL_CULL_FACE));
  GLC(context_, context_->colorMask(true, true, true, true));
  GLC(context_, context_->enable(GL_BLEND));
  blend_shadow_ = true;
  GLC(context_, context_->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
  GLC(Context(), Context()->activeTexture(GL_TEXTURE0));
  program_shadow_ = 0;
}

void GLRenderer::DoNoOp() {
  GLC(context_, context_->bindFramebuffer(GL_FRAMEBUFFER, 0));
  GLC(context_, context_->flush());
}

void GLRenderer::DoDrawQuad(DrawingFrame& frame, const DrawQuad* quad) {
  DCHECK(quad->rect.Contains(quad->visible_rect));
  if (quad->material != DrawQuad::TEXTURE_CONTENT) {
    FlushTextureQuadCache();
  }

  switch (quad->material) {
    case DrawQuad::INVALID:
      NOTREACHED();
      break;
    case DrawQuad::CHECKERBOARD:
      DrawCheckerboardQuad(frame, CheckerboardDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::DEBUG_BORDER:
      DrawDebugBorderQuad(frame, DebugBorderDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::IO_SURFACE_CONTENT:
      DrawIOSurfaceQuad(frame, IOSurfaceDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::RENDER_PASS:
      DrawRenderPassQuad(frame, RenderPassDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::SOLID_COLOR:
      DrawSolidColorQuad(frame, SolidColorDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::STREAM_VIDEO_CONTENT:
      DrawStreamVideoQuad(frame, StreamVideoDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::TEXTURE_CONTENT:
      EnqueueTextureQuad(frame, TextureDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::TILED_CONTENT:
      DrawTileQuad(frame, TileDrawQuad::MaterialCast(quad));
      break;
    case DrawQuad::YUV_VIDEO_CONTENT:
      DrawYUVVideoQuad(frame, YUVVideoDrawQuad::MaterialCast(quad));
      break;
  }
}

void GLRenderer::DrawCheckerboardQuad(const DrawingFrame& frame,
                                      const CheckerboardDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());

  const TileCheckerboardProgram* program = GetTileCheckerboardProgram();
  DCHECK(program && (program->initialized() || IsContextLost()));
  SetUseProgram(program->program());

  SkColor color = quad->color;
  GLC(Context(),
      Context()->uniform4f(program->fragment_shader().colorLocation(),
                           SkColorGetR(color) * (1.0f / 255.0f),
                           SkColorGetG(color) * (1.0f / 255.0f),
                           SkColorGetB(color) * (1.0f / 255.0f),
                           1));

  const int checkerboard_width = 16;
  float frequency = 1.0f / checkerboard_width;

  gfx::Rect tile_rect = quad->rect;
  float tex_offset_x = tile_rect.x() % checkerboard_width;
  float tex_offset_y = tile_rect.y() % checkerboard_width;
  float tex_scale_x = tile_rect.width();
  float tex_scale_y = tile_rect.height();
  GLC(Context(),
      Context()->uniform4f(program->fragment_shader().texTransformLocation(),
                           tex_offset_x,
                           tex_offset_y,
                           tex_scale_x,
                           tex_scale_y));

  GLC(Context(),
      Context()->uniform1f(program->fragment_shader().frequencyLocation(),
                           frequency));

  SetShaderOpacity(quad->opacity(), program->fragment_shader().alphaLocation());
  DrawQuadGeometry(frame,
                   quad->quadTransform(),
                   quad->rect,
                   program->vertex_shader().matrixLocation());
}

void GLRenderer::DrawDebugBorderQuad(const DrawingFrame& frame,
                                     const DebugBorderDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());

  static float gl_matrix[16];
  const DebugBorderProgram* program = GetDebugBorderProgram();
  DCHECK(program && (program->initialized() || IsContextLost()));
  SetUseProgram(program->program());

  // Use the full quad_rect for debug quads to not move the edges based on
  // partial swaps.
  gfx::Rect layer_rect = quad->rect;
  gfx::Transform render_matrix = quad->quadTransform();
  render_matrix.Translate(0.5f * layer_rect.width() + layer_rect.x(),
                          0.5f * layer_rect.height() + layer_rect.y());
  render_matrix.Scale(layer_rect.width(), layer_rect.height());
  GLRenderer::ToGLMatrix(&gl_matrix[0],
                         frame.projection_matrix * render_matrix);
  GLC(Context(),
      Context()->uniformMatrix4fv(
          program->vertex_shader().matrixLocation(), 1, false, &gl_matrix[0]));

  SkColor color = quad->color;
  float alpha = SkColorGetA(color) * (1.0f / 255.0f);

  GLC(Context(),
      Context()->uniform4f(program->fragment_shader().colorLocation(),
                           (SkColorGetR(color) * (1.0f / 255.0f)) * alpha,
                           (SkColorGetG(color) * (1.0f / 255.0f)) * alpha,
                           (SkColorGetB(color) * (1.0f / 255.0f)) * alpha,
                           alpha));

  GLC(Context(), Context()->lineWidth(quad->width));

  // The indices for the line are stored in the same array as the triangle
  // indices.
  GLC(Context(),
      Context()->drawElements(GL_LINE_LOOP, 4, GL_UNSIGNED_SHORT, 0));
}

static inline SkBitmap ApplyFilters(GLRenderer* renderer,
                                    const WebKit::WebFilterOperations& filters,
                                    ScopedResource* source_texture_resource) {
  if (filters.isEmpty())
    return SkBitmap();

  ContextProvider* offscreen_contexts =
      renderer->resource_provider()->offscreen_context_provider();
  if (!offscreen_contexts || !offscreen_contexts->GrContext())
    return SkBitmap();

  ResourceProvider::ScopedWriteLockGL lock(renderer->resource_provider(),
                                           source_texture_resource->id());

  // Flush the compositor context to ensure that textures there are available
  // in the shared context.  Do this after locking/creating the compositor
  // texture.
  renderer->resource_provider()->Flush();

  // Make sure skia uses the correct GL context.
  offscreen_contexts->Context3d()->makeContextCurrent();

  SkBitmap source =
      RenderSurfaceFilters::Apply(filters,
                                  lock.texture_id(),
                                  source_texture_resource->size(),
                                  offscreen_contexts->GrContext());

  // Flush skia context so that all the rendered stuff appears on the
  // texture.
  offscreen_contexts->GrContext()->flush();

  // Flush the GL context so rendering results from this context are
  // visible in the compositor's context.
  offscreen_contexts->Context3d()->flush();

  // Use the compositor's GL context again.
  renderer->resource_provider()->GraphicsContext3D()->makeContextCurrent();
  return source;
}

static SkBitmap ApplyImageFilter(GLRenderer* renderer,
                                 SkImageFilter* filter,
                                 ScopedResource* source_texture_resource) {
  if (!filter)
    return SkBitmap();

  ContextProvider* offscreen_contexts =
      renderer->resource_provider()->offscreen_context_provider();
  if (!offscreen_contexts || !offscreen_contexts->GrContext())
    return SkBitmap();

  ResourceProvider::ScopedWriteLockGL lock(renderer->resource_provider(),
                                           source_texture_resource->id());

  // Flush the compositor context to ensure that textures there are available
  // in the shared context.  Do this after locking/creating the compositor
  // texture.
  renderer->resource_provider()->Flush();

  // Make sure skia uses the correct GL context.
  offscreen_contexts->Context3d()->makeContextCurrent();

  // Wrap the source texture in a Ganesh platform texture.
  GrBackendTextureDesc backend_texture_description;
  backend_texture_description.fWidth = source_texture_resource->size().width();
  backend_texture_description.fHeight =
      source_texture_resource->size().height();
  backend_texture_description.fConfig = kSkia8888_GrPixelConfig;
  backend_texture_description.fTextureHandle = lock.texture_id();
  backend_texture_description.fOrigin = kTopLeft_GrSurfaceOrigin;
  skia::RefPtr<GrTexture> texture =
      skia::AdoptRef(offscreen_contexts->GrContext()->wrapBackendTexture(
          backend_texture_description));

  // Place the platform texture inside an SkBitmap.
  SkBitmap source;
  source.setConfig(SkBitmap::kARGB_8888_Config,
                   source_texture_resource->size().width(),
                   source_texture_resource->size().height());
  skia::RefPtr<SkGrPixelRef> pixel_ref =
      skia::AdoptRef(new SkGrPixelRef(texture.get()));
  source.setPixelRef(pixel_ref.get());

  // Create a scratch texture for backing store.
  GrTextureDesc desc;
  desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
  desc.fSampleCnt = 0;
  desc.fWidth = source.width();
  desc.fHeight = source.height();
  desc.fConfig = kSkia8888_GrPixelConfig;
  desc.fOrigin = kTopLeft_GrSurfaceOrigin;
  GrAutoScratchTexture scratchTexture(
      offscreen_contexts->GrContext(), desc, GrContext::kExact_ScratchTexMatch);
  skia::RefPtr<GrTexture> backing_store =
      skia::AdoptRef(scratchTexture.detach());

  // Create a device and canvas using that backing store.
  SkGpuDevice device(offscreen_contexts->GrContext(), backing_store.get());
  SkCanvas canvas(&device);

  // Draw the source bitmap through the filter to the canvas.
  SkPaint paint;
  paint.setImageFilter(filter);
  canvas.clear(SK_ColorTRANSPARENT);
  canvas.drawSprite(source, 0, 0, &paint);

  // Flush skia context so that all the rendered stuff appears on the
  // texture.
  offscreen_contexts->GrContext()->flush();

  // Flush the GL context so rendering results from this context are
  // visible in the compositor's context.
  offscreen_contexts->Context3d()->flush();

  // Use the compositor's GL context again.
  renderer->resource_provider()->GraphicsContext3D()->makeContextCurrent();

  return device.accessBitmap(false);
}

scoped_ptr<ScopedResource> GLRenderer::DrawBackgroundFilters(
    DrawingFrame& frame,
    const RenderPassDrawQuad* quad,
    const gfx::Transform& contents_device_transform,
    const gfx::Transform& contents_device_transform_inverse) {
  // This method draws a background filter, which applies a filter to any pixels
  // behind the quad and seen through its background.  The algorithm works as
  // follows:
  // 1. Compute a bounding box around the pixels that will be visible through
  // the quad.
  // 2. Read the pixels in the bounding box into a buffer R.
  // 3. Apply the background filter to R, so that it is applied in the pixels'
  // coordinate space.
  // 4. Apply the quad's inverse transform to map the pixels in R into the
  // quad's content space. This implicitly clips R by the content bounds of the
  // quad since the destination texture has bounds matching the quad's content.
  // 5. Draw the background texture for the contents using the same transform as
  // used to draw the contents itself. This is done without blending to replace
  // the current background pixels with the new filtered background.
  // 6. Draw the contents of the quad over drop of the new background with
  // blending, as per usual. The filtered background pixels will show through
  // any non-opaque pixels in this draws.
  //
  // Pixel copies in this algorithm occur at steps 2, 3, 4, and 5.

  // FIXME: When this algorithm changes, update
  // LayerTreeHost::prioritizeTextures() accordingly.

  const WebKit::WebFilterOperations& filters = quad->background_filters;
  if (filters.isEmpty())
    return scoped_ptr<ScopedResource>();

  // FIXME: We only allow background filters on an opaque render surface because
  // other surfaces may contain translucent pixels, and the contents behind
  // those translucent pixels wouldn't have the filter applied.
  if (frame.current_render_pass->has_transparent_background)
    return scoped_ptr<ScopedResource>();
  DCHECK(!frame.current_texture);

  // FIXME: Do a single readback for both the surface and replica and cache the
  // filtered results (once filter textures are not reused).
  gfx::Rect device_rect = gfx::ToEnclosingRect(MathUtil::MapClippedRect(
      contents_device_transform, SharedGeometryQuad().BoundingBox()));

  int top, right, bottom, left;
  filters.getOutsets(top, right, bottom, left);
  device_rect.Inset(-left, -top, -right, -bottom);

  device_rect.Intersect(frame.current_render_pass->output_rect);

  scoped_ptr<ScopedResource> device_background_texture =
      ScopedResource::create(resource_provider_);
  if (!GetFramebufferTexture(device_background_texture.get(), device_rect))
    return scoped_ptr<ScopedResource>();

  SkBitmap filtered_device_background =
      ApplyFilters(this, filters, device_background_texture.get());
  if (!filtered_device_background.getTexture())
    return scoped_ptr<ScopedResource>();

  GrTexture* texture =
      reinterpret_cast<GrTexture*>(filtered_device_background.getTexture());
  int filtered_device_background_texture_id = texture->getTextureHandle();

  scoped_ptr<ScopedResource> background_texture =
      ScopedResource::create(resource_provider_);
  if (!background_texture->Allocate(quad->rect.size(),
                                    GL_RGBA,
                                    ResourceProvider::TextureUsageFramebuffer))
    return scoped_ptr<ScopedResource>();

  const RenderPass* target_render_pass = frame.current_render_pass;
  bool using_background_texture =
      UseScopedTexture(frame, background_texture.get(), quad->rect);

  if (using_background_texture) {
    // Copy the readback pixels from device to the background texture for the
    // surface.
    gfx::Transform device_to_framebuffer_transform;
    device_to_framebuffer_transform.Translate(
        quad->rect.width() * 0.5f + quad->rect.x(),
        quad->rect.height() * 0.5f + quad->rect.y());
    device_to_framebuffer_transform.Scale(quad->rect.width(),
                                          quad->rect.height());
    device_to_framebuffer_transform.PreconcatTransform(
        contents_device_transform_inverse);

#ifndef NDEBUG
    GLC(Context(), Context()->clearColor(0, 0, 1, 1));
    Context()->clear(GL_COLOR_BUFFER_BIT);
#endif

    CopyTextureToFramebuffer(frame,
                             filtered_device_background_texture_id,
                             device_rect,
                             device_to_framebuffer_transform);
  }

  UseRenderPass(frame, target_render_pass);

  if (!using_background_texture)
    return scoped_ptr<ScopedResource>();
  return background_texture.Pass();
}

void GLRenderer::DrawRenderPassQuad(DrawingFrame& frame,
                                    const RenderPassDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());

  CachedResource* contents_texture =
      render_pass_textures_.get(quad->render_pass_id);
  if (!contents_texture || !contents_texture->id())
    return;

  gfx::Transform quad_rect_matrix;
  QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
  gfx::Transform contents_device_transform =
      frame.window_matrix * frame.projection_matrix * quad_rect_matrix;
  contents_device_transform.FlattenTo2d();

  // Can only draw surface if device matrix is invertible.
  gfx::Transform contents_device_transform_inverse(
      gfx::Transform::kSkipInitialization);
  if (!contents_device_transform.GetInverse(&contents_device_transform_inverse))
    return;

  scoped_ptr<ScopedResource> background_texture =
      DrawBackgroundFilters(frame,
                            quad,
                            contents_device_transform,
                            contents_device_transform_inverse);

  // FIXME: Cache this value so that we don't have to do it for both the surface
  // and its replica.  Apply filters to the contents texture.
  SkBitmap filter_bitmap;
  if (quad->filter) {
    filter_bitmap =
        ApplyImageFilter(this, quad->filter.get(), contents_texture);
  } else {
    filter_bitmap = ApplyFilters(this, quad->filters, contents_texture);
  }

  // Draw the background texture if there is one.
  if (background_texture) {
    DCHECK(background_texture->size() == quad->rect.size());
    ResourceProvider::ScopedReadLockGL lock(resource_provider_,
                                            background_texture->id());
    CopyTextureToFramebuffer(
        frame, lock.texture_id(), quad->rect, quad->quadTransform());
  }

  bool clipped = false;
  gfx::QuadF device_quad = MathUtil::MapQuad(
      contents_device_transform, SharedGeometryQuad(), &clipped);
  DCHECK(!clipped);
  LayerQuad deviceLayerBounds(gfx::QuadF(device_quad.BoundingBox()));
  LayerQuad device_layer_edges(device_quad);

  // Use anti-aliasing programs only when necessary.
  bool use_aa = (!device_quad.IsRectilinear() ||
                 !device_quad.BoundingBox().IsExpressibleAsRect());
  if (use_aa) {
    deviceLayerBounds.InflateAntiAliasingDistance();
    device_layer_edges.InflateAntiAliasingDistance();
  }

  scoped_ptr<ResourceProvider::ScopedReadLockGL> mask_resource_lock;
  unsigned mask_texture_id = 0;
  if (quad->mask_resource_id) {
    mask_resource_lock.reset(new ResourceProvider::ScopedReadLockGL(
        resource_provider_, quad->mask_resource_id));
    mask_texture_id = mask_resource_lock->texture_id();
  }

  // FIXME: use the background_texture and blend the background in with this
  // draw instead of having a separate copy of the background texture.

  scoped_ptr<ResourceProvider::ScopedReadLockGL> contents_resource_lock;
  if (filter_bitmap.getTexture()) {
    GrTexture* texture =
        reinterpret_cast<GrTexture*>(filter_bitmap.getTexture());
    Context()->bindTexture(GL_TEXTURE_2D, texture->getTextureHandle());
  } else
    contents_resource_lock = make_scoped_ptr(
        new ResourceProvider::ScopedSamplerGL(resource_provider_,
                                              contents_texture->id(),
                                              GL_TEXTURE_2D,
                                              GL_LINEAR));

  int shader_quad_location = -1;
  int shader_edge_location = -1;
  int shader_mask_sampler_location = -1;
  int shader_mask_tex_coord_scale_location = -1;
  int shader_mask_tex_coord_offset_location = -1;
  int shader_matrix_location = -1;
  int shader_alpha_location = -1;
  int shader_tex_transform_location = -1;
  int shader_tex_scale_location = -1;

  if (use_aa && mask_texture_id) {
    const RenderPassMaskProgramAA* program = GetRenderPassMaskProgramAA();
    SetUseProgram(program->program());
    GLC(Context(),
        Context()->uniform1i(program->fragment_shader().samplerLocation(), 0));

    shader_quad_location = program->vertex_shader().pointLocation();
    shader_edge_location = program->fragment_shader().edgeLocation();
    shader_mask_sampler_location =
        program->fragment_shader().maskSamplerLocation();
    shader_mask_tex_coord_scale_location =
        program->fragment_shader().maskTexCoordScaleLocation();
    shader_mask_tex_coord_offset_location =
        program->fragment_shader().maskTexCoordOffsetLocation();
    shader_matrix_location = program->vertex_shader().matrixLocation();
    shader_alpha_location = program->fragment_shader().alphaLocation();
    shader_tex_scale_location = program->vertex_shader().texScaleLocation();
  } else if (!use_aa && mask_texture_id) {
    const RenderPassMaskProgram* program = GetRenderPassMaskProgram();
    SetUseProgram(program->program());
    GLC(Context(),
        Context()->uniform1i(program->fragment_shader().samplerLocation(), 0));

    shader_mask_sampler_location =
        program->fragment_shader().maskSamplerLocation();
    shader_mask_tex_coord_scale_location =
        program->fragment_shader().maskTexCoordScaleLocation();
    shader_mask_tex_coord_offset_location =
        program->fragment_shader().maskTexCoordOffsetLocation();
    shader_matrix_location = program->vertex_shader().matrixLocation();
    shader_alpha_location = program->fragment_shader().alphaLocation();
    shader_tex_transform_location =
        program->vertex_shader().texTransformLocation();
  } else if (use_aa && !mask_texture_id) {
    const RenderPassProgramAA* program = GetRenderPassProgramAA();
    SetUseProgram(program->program());
    GLC(Context(),
        Context()->uniform1i(program->fragment_shader().samplerLocation(), 0));

    shader_quad_location = program->vertex_shader().pointLocation();
    shader_edge_location = program->fragment_shader().edgeLocation();
    shader_matrix_location = program->vertex_shader().matrixLocation();
    shader_alpha_location = program->fragment_shader().alphaLocation();
    shader_tex_scale_location = program->vertex_shader().texScaleLocation();
  } else {
    const RenderPassProgram* program = GetRenderPassProgram();
    SetUseProgram(program->program());
    GLC(Context(),
        Context()->uniform1i(program->fragment_shader().samplerLocation(), 0));

    shader_matrix_location = program->vertex_shader().matrixLocation();
    shader_alpha_location = program->fragment_shader().alphaLocation();
    shader_tex_transform_location =
        program->vertex_shader().texTransformLocation();
  }

  float tex_scale_x =
      quad->rect.width() / static_cast<float>(contents_texture->size().width());
  float tex_scale_y = quad->rect.height() /
                      static_cast<float>(contents_texture->size().height());
  DCHECK_LE(tex_scale_x, 1.0f);
  DCHECK_LE(tex_scale_y, 1.0f);

  if (shader_tex_transform_location != -1) {
    GLC(Context(),
        Context()->uniform4f(shader_tex_transform_location,
                             0.0f,
                             0.0f,
                             tex_scale_x,
                             tex_scale_y));
  } else if (shader_tex_scale_location != -1) {
    GLC(Context(),
        Context()->uniform2f(
            shader_tex_scale_location, tex_scale_x, tex_scale_y));
  } else {
    DCHECK(IsContextLost());
  }

  if (shader_mask_sampler_location != -1) {
    DCHECK(shader_mask_tex_coord_scale_location != 1);
    DCHECK(shader_mask_tex_coord_offset_location != 1);
    GLC(Context(), Context()->activeTexture(GL_TEXTURE1));
    GLC(Context(), Context()->uniform1i(shader_mask_sampler_location, 1));
    GLC(Context(),
        Context()->uniform2f(shader_mask_tex_coord_offset_location,
                             quad->mask_uv_rect.x(),
                             quad->mask_uv_rect.y()));
    GLC(Context(),
        Context()->uniform2f(shader_mask_tex_coord_scale_location,
                             quad->mask_uv_rect.width() / tex_scale_x,
                             quad->mask_uv_rect.height() / tex_scale_y));
    resource_provider_->BindForSampling(
        quad->mask_resource_id, GL_TEXTURE_2D, GL_LINEAR);
    GLC(Context(), Context()->activeTexture(GL_TEXTURE0));
  }

  if (shader_edge_location != -1) {
    float edge[24];
    device_layer_edges.ToFloatArray(edge);
    deviceLayerBounds.ToFloatArray(&edge[12]);
    GLC(Context(), Context()->uniform3fv(shader_edge_location, 8, edge));
  }

  // Map device space quad to surface space. contents_device_transform has no 3d
  // component since it was flattened, so we don't need to project.
  gfx::QuadF surface_quad = MathUtil::MapQuad(contents_device_transform_inverse,
                                              device_layer_edges.ToQuadF(),
                                              &clipped);
  DCHECK(!clipped);

  SetShaderOpacity(quad->opacity(), shader_alpha_location);
  SetShaderQuadF(surface_quad, shader_quad_location);
  DrawQuadGeometry(
      frame, quad->quadTransform(), quad->rect, shader_matrix_location);

  // Flush the compositor context before the filter bitmap goes out of
  // scope, so the draw gets processed before the filter texture gets deleted.
  if (filter_bitmap.getTexture())
    context_->flush();
}

struct SolidColorProgramUniforms {
  unsigned program;
  unsigned matrix_location;
  unsigned color_location;
  unsigned point_location;
  unsigned tex_scale_location;
  unsigned edge_location;
};

template<class T>
static void SolidColorUniformLocation(T program,
                                      SolidColorProgramUniforms* uniforms) {
  uniforms->program = program->program();
  uniforms->matrix_location = program->vertex_shader().matrixLocation();
  uniforms->color_location = program->fragment_shader().colorLocation();
  uniforms->point_location = program->vertex_shader().pointLocation();
  uniforms->tex_scale_location = program->vertex_shader().texScaleLocation();
  uniforms->edge_location = program->fragment_shader().edgeLocation();
}

bool GLRenderer::SetupQuadForAntialiasing(
    const gfx::Transform& device_transform,
    const DrawQuad* quad,
    gfx::QuadF* local_quad,
    float edge[24]) const {
  gfx::Rect tile_rect = quad->visible_rect;

  bool clipped = false;
  gfx::QuadF device_layer_quad = MathUtil::MapQuad(
      device_transform, gfx::QuadF(quad->visibleContentRect()), &clipped);
  DCHECK(!clipped);

  // TODO(reveman): Axis-aligned is not enough to avoid anti-aliasing.
  // Bounding rectangle for quad also needs to be expressible as an integer
  // rectangle. crbug.com/169374
  bool is_axis_aligned_in_target = device_layer_quad.IsRectilinear();
  bool use_aa = !clipped && !is_axis_aligned_in_target && quad->IsEdge();

  if (!use_aa)
    return false;

  LayerQuad device_layer_bounds(gfx::QuadF(device_layer_quad.BoundingBox()));
  device_layer_bounds.InflateAntiAliasingDistance();

  LayerQuad device_layer_edges(device_layer_quad);
  device_layer_edges.InflateAntiAliasingDistance();

  device_layer_edges.ToFloatArray(edge);
  device_layer_bounds.ToFloatArray(&edge[12]);

  gfx::PointF bottom_right = tile_rect.bottom_right();
  gfx::PointF bottom_left = tile_rect.bottom_left();
  gfx::PointF top_left = tile_rect.origin();
  gfx::PointF top_right = tile_rect.top_right();

  // Map points to device space.
  bottom_right = MathUtil::MapPoint(device_transform, bottom_right, &clipped);
  DCHECK(!clipped);
  bottom_left = MathUtil::MapPoint(device_transform, bottom_left, &clipped);
  DCHECK(!clipped);
  top_left = MathUtil::MapPoint(device_transform, top_left, &clipped);
  DCHECK(!clipped);
  top_right = MathUtil::MapPoint(device_transform, top_right, &clipped);
  DCHECK(!clipped);

  LayerQuad::Edge bottom_edge(bottom_right, bottom_left);
  LayerQuad::Edge left_edge(bottom_left, top_left);
  LayerQuad::Edge top_edge(top_left, top_right);
  LayerQuad::Edge right_edge(top_right, bottom_right);

  // Only apply anti-aliasing to edges not clipped by culling or scissoring.
  if (quad->IsTopEdge() && tile_rect.y() == quad->rect.y())
    top_edge = device_layer_edges.top();
  if (quad->IsLeftEdge() && tile_rect.x() == quad->rect.x())
    left_edge = device_layer_edges.left();
  if (quad->IsRightEdge() && tile_rect.right() == quad->rect.right())
    right_edge = device_layer_edges.right();
  if (quad->IsBottomEdge() && tile_rect.bottom() == quad->rect.bottom())
    bottom_edge = device_layer_edges.bottom();

  float sign = gfx::QuadF(tile_rect).IsCounterClockwise() ? -1 : 1;
  bottom_edge.scale(sign);
  left_edge.scale(sign);
  top_edge.scale(sign);
  right_edge.scale(sign);

  // Create device space quad.
  LayerQuad device_quad(left_edge, top_edge, right_edge, bottom_edge);

  // Map device space quad to local space. deviceTransform has no 3d
  // component since it was flattened, so we don't need to project.  We should
  // have already checked that the transform was uninvertible above.
  gfx::Transform inverse_device_transform(
      gfx::Transform::kSkipInitialization);
  bool did_invert = device_transform.GetInverse(&inverse_device_transform);
  DCHECK(did_invert);
  *local_quad = MathUtil::MapQuad(
      inverse_device_transform, device_quad.ToQuadF(), &clipped);
  // We should not DCHECK(!clipped) here, because anti-aliasing inflation may
  // cause deviceQuad to become clipped. To our knowledge this scenario does
  // not need to be handled differently than the unclipped case.

  return true;
}

void GLRenderer::DrawSolidColorQuad(const DrawingFrame& frame,
                                    const SolidColorDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());
  gfx::Rect tile_rect = quad->visible_rect;

  gfx::Transform device_transform =
      frame.window_matrix * frame.projection_matrix * quad->quadTransform();
  device_transform.FlattenTo2d();
  if (!device_transform.IsInvertible())
    return;

  gfx::QuadF local_quad = gfx::QuadF(gfx::RectF(tile_rect));
  float edge[24];
  bool use_aa = SetupQuadForAntialiasing(
      device_transform, quad, &local_quad, edge);

  SolidColorProgramUniforms uniforms;
  if (use_aa)
    SolidColorUniformLocation(GetSolidColorProgramAA(), &uniforms);
  else
    SolidColorUniformLocation(GetSolidColorProgram(), &uniforms);
  SetUseProgram(uniforms.program);

  SkColor color = quad->color;
  float opacity = quad->opacity();
  float alpha = (SkColorGetA(color) * (1.0f / 255.0f)) * opacity;

  GLC(Context(),
      Context()->uniform4f(uniforms.color_location,
                           (SkColorGetR(color) * (1.0f / 255.0f)) * alpha,
                           (SkColorGetG(color) * (1.0f / 255.0f)) * alpha,
                           (SkColorGetB(color) * (1.0f / 255.0f)) * alpha,
                           alpha));

  if (use_aa)
    GLC(Context(), Context()->uniform3fv(uniforms.edge_location, 8, edge));

  // Enable blending when the quad properties require it or if we decided
  // to use antialiasing.
  SetBlendEnabled(quad->ShouldDrawWithBlending() || use_aa);

  // Normalize to tileRect.
  local_quad.Scale(1.0f / tile_rect.width(), 1.0f / tile_rect.height());

  SetShaderQuadF(local_quad, uniforms.point_location);

  // The transform and vertex data are used to figure out the extents that the
  // un-antialiased quad should have and which vertex this is and the float
  // quad passed in via uniform is the actual geometry that gets used to draw
  // it. This is why this centered rect is used and not the original quadRect.
  gfx::RectF centered_rect(gfx::PointF(-0.5f * tile_rect.width(),
                                       -0.5f * tile_rect.height()),
                           tile_rect.size());
  DrawQuadGeometry(frame, quad->quadTransform(),
                   centered_rect, uniforms.matrix_location);
}

struct TileProgramUniforms {
  unsigned program;
  unsigned sampler_location;
  unsigned vertex_tex_transform_location;
  unsigned fragment_tex_transform_location;
  unsigned edge_location;
  unsigned matrix_location;
  unsigned alpha_location;
  unsigned point_location;
};

template <class T>
static void TileUniformLocation(T program, TileProgramUniforms* uniforms) {
  uniforms->program = program->program();
  uniforms->vertex_tex_transform_location =
      program->vertex_shader().vertexTexTransformLocation();
  uniforms->matrix_location = program->vertex_shader().matrixLocation();
  uniforms->point_location = program->vertex_shader().pointLocation();

  uniforms->sampler_location = program->fragment_shader().samplerLocation();
  uniforms->alpha_location = program->fragment_shader().alphaLocation();
  uniforms->fragment_tex_transform_location =
      program->fragment_shader().fragmentTexTransformLocation();
  uniforms->edge_location = program->fragment_shader().edgeLocation();
}

void GLRenderer::DrawTileQuad(const DrawingFrame& frame,
                              const TileDrawQuad* quad) {
  gfx::Rect tile_rect = quad->visible_rect;

  gfx::RectF tex_coord_rect = quad->tex_coord_rect;
  float tex_to_geom_scale_x = quad->rect.width() / tex_coord_rect.width();
  float tex_to_geom_scale_y = quad->rect.height() / tex_coord_rect.height();

  // tex_coord_rect corresponds to quad_rect, but quadVisibleRect may be
  // smaller than quad_rect due to occlusion or clipping. Adjust
  // tex_coord_rect to match.
  gfx::Vector2d top_left_diff = tile_rect.origin() - quad->rect.origin();
  gfx::Vector2d bottom_right_diff =
      tile_rect.bottom_right() - quad->rect.bottom_right();
  tex_coord_rect.Inset(top_left_diff.x() / tex_to_geom_scale_x,
                       top_left_diff.y() / tex_to_geom_scale_y,
                       -bottom_right_diff.x() / tex_to_geom_scale_x,
                       -bottom_right_diff.y() / tex_to_geom_scale_y);

  gfx::RectF clamp_geom_rect(tile_rect);
  gfx::RectF clamp_tex_rect(tex_coord_rect);
  // Clamp texture coordinates to avoid sampling outside the layer
  // by deflating the tile region half a texel or half a texel
  // minus epsilon for one pixel layers. The resulting clamp region
  // is mapped to the unit square by the vertex shader and mapped
  // back to normalized texture coordinates by the fragment shader
  // after being clamped to 0-1 range.
  const float epsilon = 1.0f / 1024.0f;
  float tex_clamp_x = std::min(0.5f, 0.5f * clamp_tex_rect.width() - epsilon);
  float tex_clamp_y = std::min(0.5f, 0.5f * clamp_tex_rect.height() - epsilon);
  float geom_clamp_x = std::min(tex_clamp_x * tex_to_geom_scale_x,
                                0.5f * clamp_geom_rect.width() - epsilon);
  float geom_clamp_y = std::min(tex_clamp_y * tex_to_geom_scale_y,
                                0.5f * clamp_geom_rect.height() - epsilon);
  clamp_geom_rect.Inset(geom_clamp_x, geom_clamp_y, geom_clamp_x, geom_clamp_y);
  clamp_tex_rect.Inset(tex_clamp_x, tex_clamp_y, tex_clamp_x, tex_clamp_y);

  // Map clamping rectangle to unit square.
  float vertex_tex_translate_x = -clamp_geom_rect.x() / clamp_geom_rect.width();
  float vertex_tex_translate_y =
      -clamp_geom_rect.y() / clamp_geom_rect.height();
  float vertex_tex_scale_x = tile_rect.width() / clamp_geom_rect.width();
  float vertex_tex_scale_y = tile_rect.height() / clamp_geom_rect.height();

  // Map to normalized texture coordinates.
  gfx::Size texture_size = quad->texture_size;
  float fragment_tex_translate_x = clamp_tex_rect.x() / texture_size.width();
  float fragment_tex_translate_y = clamp_tex_rect.y() / texture_size.height();
  float fragment_tex_scale_x = clamp_tex_rect.width() / texture_size.width();
  float fragment_tex_scale_y = clamp_tex_rect.height() / texture_size.height();

  gfx::Transform device_transform =
      frame.window_matrix * frame.projection_matrix * quad->quadTransform();
  device_transform.FlattenTo2d();
  if (!device_transform.IsInvertible())
    return;

  gfx::QuadF local_quad = gfx::QuadF(gfx::RectF(tile_rect));
  float edge[24];
  bool use_aa = SetupQuadForAntialiasing(
      device_transform, quad, &local_quad, edge);

  TileProgramUniforms uniforms;
  if (use_aa) {
    if (quad->swizzle_contents)
      TileUniformLocation(GetTileProgramSwizzleAA(), &uniforms);
    else
      TileUniformLocation(GetTileProgramAA(), &uniforms);
  } else {
    if (quad->ShouldDrawWithBlending()) {
      if (quad->swizzle_contents)
        TileUniformLocation(GetTileProgramSwizzle(), &uniforms);
      else
        TileUniformLocation(GetTileProgram(), &uniforms);
    } else {
      if (quad->swizzle_contents)
        TileUniformLocation(GetTileProgramSwizzleOpaque(), &uniforms);
      else
        TileUniformLocation(GetTileProgramOpaque(), &uniforms);
    }
  }

  SetUseProgram(uniforms.program);
  GLC(Context(), Context()->uniform1i(uniforms.sampler_location, 0));
  bool scaled = (tex_to_geom_scale_x != 1.f || tex_to_geom_scale_y != 1.f);
  GLenum filter = (use_aa || scaled ||
                   !quad->quadTransform().IsIdentityOrIntegerTranslation())
                  ? GL_LINEAR
                  : GL_NEAREST;
  ResourceProvider::ScopedSamplerGL quad_resource_lock(
      resource_provider_, quad->resource_id, GL_TEXTURE_2D, filter);

  if (use_aa) {
    GLC(Context(), Context()->uniform3fv(uniforms.edge_location, 8, edge));

    GLC(Context(),
        Context()->uniform4f(uniforms.vertex_tex_transform_location,
                             vertex_tex_translate_x,
                             vertex_tex_translate_y,
                             vertex_tex_scale_x,
                             vertex_tex_scale_y));
    GLC(Context(),
        Context()->uniform4f(uniforms.fragment_tex_transform_location,
                             fragment_tex_translate_x,
                             fragment_tex_translate_y,
                             fragment_tex_scale_x,
                             fragment_tex_scale_y));
  } else {
    // Move fragment shader transform to vertex shader. We can do this while
    // still producing correct results as fragment_tex_transform_location
    // should always be non-negative when tiles are transformed in a way
    // that could result in sampling outside the layer.
    vertex_tex_scale_x *= fragment_tex_scale_x;
    vertex_tex_scale_y *= fragment_tex_scale_y;
    vertex_tex_translate_x *= fragment_tex_scale_x;
    vertex_tex_translate_y *= fragment_tex_scale_y;
    vertex_tex_translate_x += fragment_tex_translate_x;
    vertex_tex_translate_y += fragment_tex_translate_y;

    GLC(Context(),
        Context()->uniform4f(uniforms.vertex_tex_transform_location,
                             vertex_tex_translate_x,
                             vertex_tex_translate_y,
                             vertex_tex_scale_x,
                             vertex_tex_scale_y));
  }

  // Enable blending when the quad properties require it or if we decided
  // to use antialiasing.
  SetBlendEnabled(quad->ShouldDrawWithBlending() || use_aa);

  // Normalize to tile_rect.
  local_quad.Scale(1.0f / tile_rect.width(), 1.0f / tile_rect.height());

  SetShaderOpacity(quad->opacity(), uniforms.alpha_location);
  SetShaderQuadF(local_quad, uniforms.point_location);

  // The transform and vertex data are used to figure out the extents that the
  // un-antialiased quad should have and which vertex this is and the float
  // quad passed in via uniform is the actual geometry that gets used to draw
  // it. This is why this centered rect is used and not the original quad_rect.
  gfx::RectF centeredRect(
      gfx::PointF(-0.5f * tile_rect.width(), -0.5f * tile_rect.height()),
      tile_rect.size());
  DrawQuadGeometry(
      frame, quad->quadTransform(), centeredRect, uniforms.matrix_location);
}

void GLRenderer::DrawYUVVideoQuad(const DrawingFrame& frame,
                                  const YUVVideoDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());

  const VideoYUVProgram* program = GetVideoYUVProgram();
  DCHECK(program && (program->initialized() || IsContextLost()));

  const VideoLayerImpl::FramePlane& y_plane = quad->y_plane;
  const VideoLayerImpl::FramePlane& u_plane = quad->u_plane;
  const VideoLayerImpl::FramePlane& v_plane = quad->v_plane;

  GLC(Context(), Context()->activeTexture(GL_TEXTURE1));
  ResourceProvider::ScopedSamplerGL y_plane_lock(
      resource_provider_, y_plane.resource_id, GL_TEXTURE_2D, GL_LINEAR);
  GLC(Context(), Context()->activeTexture(GL_TEXTURE2));
  ResourceProvider::ScopedSamplerGL u_plane_lock(
      resource_provider_, u_plane.resource_id, GL_TEXTURE_2D, GL_LINEAR);
  GLC(Context(), Context()->activeTexture(GL_TEXTURE3));
  ResourceProvider::ScopedSamplerGL v_plane_lock(
      resource_provider_, v_plane.resource_id, GL_TEXTURE_2D, GL_LINEAR);

  SetUseProgram(program->program());

  GLC(Context(),
      Context()->uniform2f(program->vertex_shader().texScaleLocation(),
                           quad->tex_scale.width(),
                           quad->tex_scale.height()));
  GLC(Context(),
      Context()->uniform1i(program->fragment_shader().yTextureLocation(), 1));
  GLC(Context(),
      Context()->uniform1i(program->fragment_shader().uTextureLocation(), 2));
  GLC(Context(),
      Context()->uniform1i(program->fragment_shader().vTextureLocation(), 3));

  // These values are magic numbers that are used in the transformation from YUV
  // to RGB color values.  They are taken from the following webpage:
  // http://www.fourcc.org/fccyvrgb.php
  float yuv_to_rgb[9] = {
      1.164f, 1.164f, 1.164f,
      0.0f, -.391f, 2.018f,
      1.596f, -.813f, 0.0f,
  };
  GLC(Context(),
      Context()->uniformMatrix3fv(
          program->fragment_shader().yuvMatrixLocation(), 1, 0, yuv_to_rgb));

  // These values map to 16, 128, and 128 respectively, and are computed
  // as a fraction over 256 (e.g. 16 / 256 = 0.0625).
  // They are used in the YUV to RGBA conversion formula:
  //   Y - 16   : Gives 16 values of head and footroom for overshooting
  //   U - 128  : Turns unsigned U into signed U [-128,127]
  //   V - 128  : Turns unsigned V into signed V [-128,127]
  float yuv_adjust[3] = { -0.0625f, -0.5f, -0.5f, };
  GLC(Context(),
      Context()->uniform3fv(
          program->fragment_shader().yuvAdjLocation(), 1, yuv_adjust));

  SetShaderOpacity(quad->opacity(), program->fragment_shader().alphaLocation());
  DrawQuadGeometry(frame,
                   quad->quadTransform(),
                   quad->rect,
                   program->vertex_shader().matrixLocation());

  // Reset active texture back to texture 0.
  GLC(Context(), Context()->activeTexture(GL_TEXTURE0));
}

void GLRenderer::DrawStreamVideoQuad(const DrawingFrame& frame,
                                     const StreamVideoDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());

  static float gl_matrix[16];

  DCHECK(capabilities_.using_egl_image);

  const VideoStreamTextureProgram* program = GetVideoStreamTextureProgram();
  SetUseProgram(program->program());

  ToGLMatrix(&gl_matrix[0], quad->matrix);
  GLC(Context(),
      Context()->uniformMatrix4fv(
          program->vertex_shader().texMatrixLocation(), 1, false, gl_matrix));

  GLC(Context(),
      Context()->bindTexture(GL_TEXTURE_EXTERNAL_OES, quad->texture_id));

  GLC(Context(),
      Context()->uniform1i(program->fragment_shader().samplerLocation(), 0));

  SetShaderOpacity(quad->opacity(), program->fragment_shader().alphaLocation());
  DrawQuadGeometry(frame,
                   quad->quadTransform(),
                   quad->rect,
                   program->vertex_shader().matrixLocation());
}

struct TextureProgramBinding {
  template <class Program>
  void Set(Program* program, WebKit::WebGraphicsContext3D* context) {
    DCHECK(program && (program->initialized() || context->isContextLost()));
    program_id = program->program();
    sampler_location = program->fragment_shader().samplerLocation();
    matrix_location = program->vertex_shader().matrixLocation();
    alpha_location = program->fragment_shader().alphaLocation();
  }
  int program_id;
  int sampler_location;
  int matrix_location;
  int alpha_location;
};

struct TexTransformTextureProgramBinding : TextureProgramBinding {
  template <class Program>
  void Set(Program* program, WebKit::WebGraphicsContext3D* context) {
    TextureProgramBinding::Set(program, context);
    tex_transform_location = program->vertex_shader().texTransformLocation();
    vertex_opacity_location = program->vertex_shader().vertexOpacityLocation();
  }
  int tex_transform_location;
  int vertex_opacity_location;
};

void GLRenderer::FlushTextureQuadCache() {
  // Check to see if we have anything to draw.
  if (draw_cache_.program_id == 0)
    return;

  // Set the correct blending mode.
  SetBlendEnabled(draw_cache_.needs_blending);

  // Bind the program to the GL state.
  SetUseProgram(draw_cache_.program_id);

  // Bind the correct texture sampler location.
  GLC(Context(), Context()->uniform1i(draw_cache_.sampler_location, 0));

  // Assume the current active textures is 0.
  ResourceProvider::ScopedReadLockGL locked_quad(resource_provider_,
                                                 draw_cache_.resource_id);
  GLC(Context(),
      Context()->bindTexture(GL_TEXTURE_2D, locked_quad.texture_id()));

  // set up premultiplied alpha.
  if (!draw_cache_.use_premultiplied_alpha) {
    // As it turns out, the premultiplied alpha blending function (ONE,
    // ONE_MINUS_SRC_ALPHA) will never cause the alpha channel to be set to
    // anything less than 1.0f if it is initialized to that value! Therefore,
    // premultipliedAlpha being false is the first situation we can generally
    // see an alpha channel less than 1.0f coming out of the compositor. This is
    // causing platform differences in some layout tests (see
    // https://bugs.webkit.org/show_bug.cgi?id=82412), so in this situation, use
    // a separate blend function for the alpha channel to avoid modifying it.
    // Don't use colorMask for this as it has performance implications on some
    // platforms.
    GLC(Context(),
        Context()->blendFuncSeparate(
            GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE));
  }

  COMPILE_ASSERT(sizeof(Float4) == 4 * sizeof(float), struct_is_densely_packed);
  COMPILE_ASSERT(sizeof(Float16) == 16 * sizeof(float),
                 struct_is_densely_packed);

  // Upload the tranforms for both points and uvs.
  GLC(context_,
      context_->uniformMatrix4fv(
          static_cast<int>(draw_cache_.matrix_location),
          static_cast<int>(draw_cache_.matrix_data.size()),
          false,
          reinterpret_cast<float*>(&draw_cache_.matrix_data.front())));
  GLC(context_,
      context_->uniform4fv(
          static_cast<int>(draw_cache_.uv_xform_location),
          static_cast<int>(draw_cache_.uv_xform_data.size()),
          reinterpret_cast<float*>(&draw_cache_.uv_xform_data.front())));
  GLC(context_,
      context_->uniform1fv(
          static_cast<int>(draw_cache_.vertex_opacity_location),
          static_cast<int>(draw_cache_.vertex_opacity_data.size()),
          static_cast<float*>(&draw_cache_.vertex_opacity_data.front())));

  // Draw the quads!
  GLC(context_,
      context_->drawElements(GL_TRIANGLES,
                             6 * draw_cache_.matrix_data.size(),
                             GL_UNSIGNED_SHORT,
                             0));

  // Clean up after ourselves (reset state set above).
  if (!draw_cache_.use_premultiplied_alpha)
    GLC(context_, context_->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));

  // Clear the cache.
  draw_cache_.program_id = 0;
  draw_cache_.uv_xform_data.resize(0);
  draw_cache_.vertex_opacity_data.resize(0);
  draw_cache_.matrix_data.resize(0);
}

void GLRenderer::EnqueueTextureQuad(const DrawingFrame& frame,
                                    const TextureDrawQuad* quad) {
  // Choose the correct texture program binding
  TexTransformTextureProgramBinding binding;
  if (quad->flipped)
    binding.Set(GetTextureProgramFlip(), Context());
  else
    binding.Set(GetTextureProgram(), Context());

  int resource_id = quad->resource_id;

  if (draw_cache_.program_id != binding.program_id ||
      draw_cache_.resource_id != resource_id ||
      draw_cache_.use_premultiplied_alpha != quad->premultiplied_alpha ||
      draw_cache_.needs_blending != quad->ShouldDrawWithBlending() ||
      draw_cache_.matrix_data.size() >= 8) {
    FlushTextureQuadCache();
    draw_cache_.program_id = binding.program_id;
    draw_cache_.resource_id = resource_id;
    draw_cache_.use_premultiplied_alpha = quad->premultiplied_alpha;
    draw_cache_.needs_blending = quad->ShouldDrawWithBlending();

    draw_cache_.uv_xform_location = binding.tex_transform_location;
    draw_cache_.vertex_opacity_location = binding.vertex_opacity_location;
    draw_cache_.matrix_location = binding.matrix_location;
    draw_cache_.sampler_location = binding.sampler_location;
  }

  // Generate the uv-transform
  gfx::PointF uv0 = quad->uv_top_left;
  gfx::PointF uv1 = quad->uv_bottom_right;
  Float4 uv = { { uv0.x(), uv0.y(), uv1.x() - uv0.x(), uv1.y() - uv0.y() } };
  draw_cache_.uv_xform_data.push_back(uv);

  // Generate the vertex opacity
  const float opacity = quad->opacity();
  draw_cache_.vertex_opacity_data.push_back(quad->vertex_opacity[0] * opacity);
  draw_cache_.vertex_opacity_data.push_back(quad->vertex_opacity[1] * opacity);
  draw_cache_.vertex_opacity_data.push_back(quad->vertex_opacity[2] * opacity);
  draw_cache_.vertex_opacity_data.push_back(quad->vertex_opacity[3] * opacity);

  // Generate the transform matrix
  gfx::Transform quad_rect_matrix;
  QuadRectTransform(&quad_rect_matrix, quad->quadTransform(), quad->rect);
  quad_rect_matrix = frame.projection_matrix * quad_rect_matrix;

  Float16 m;
  quad_rect_matrix.matrix().asColMajorf(m.data);
  draw_cache_.matrix_data.push_back(m);
}

void GLRenderer::DrawTextureQuad(const DrawingFrame& frame,
                                 const TextureDrawQuad* quad) {
  TexTransformTextureProgramBinding binding;
  if (quad->flipped)
    binding.Set(GetTextureProgramFlip(), Context());
  else
    binding.Set(GetTextureProgram(), Context());
  SetUseProgram(binding.program_id);
  GLC(Context(), Context()->uniform1i(binding.sampler_location, 0));
  gfx::PointF uv0 = quad->uv_top_left;
  gfx::PointF uv1 = quad->uv_bottom_right;
  GLC(Context(),
      Context()->uniform4f(binding.tex_transform_location,
                           uv0.x(),
                           uv0.y(),
                           uv1.x() - uv0.x(),
                           uv1.y() - uv0.y()));

  GLC(Context(),
      Context()->uniform1fv(
          binding.vertex_opacity_location, 4, quad->vertex_opacity));

  ResourceProvider::ScopedSamplerGL quad_resource_lock(
      resource_provider_, quad->resource_id, GL_TEXTURE_2D, GL_LINEAR);

  if (!quad->premultiplied_alpha) {
    // As it turns out, the premultiplied alpha blending function (ONE,
    // ONE_MINUS_SRC_ALPHA) will never cause the alpha channel to be set to
    // anything less than 1.0f if it is initialized to that value! Therefore,
    // premultipliedAlpha being false is the first situation we can generally
    // see an alpha channel less than 1.0f coming out of the compositor. This is
    // causing platform differences in some layout tests (see
    // https://bugs.webkit.org/show_bug.cgi?id=82412), so in this situation, use
    // a separate blend function for the alpha channel to avoid modifying it.
    // Don't use colorMask for this as it has performance implications on some
    // platforms.
    GLC(Context(),
        Context()->blendFuncSeparate(
            GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE));
  }

  DrawQuadGeometry(
      frame, quad->quadTransform(), quad->rect, binding.matrix_location);

  if (!quad->premultiplied_alpha)
    GLC(context_, context_->blendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}

void GLRenderer::DrawIOSurfaceQuad(const DrawingFrame& frame,
                                   const IOSurfaceDrawQuad* quad) {
  SetBlendEnabled(quad->ShouldDrawWithBlending());

  TexTransformTextureProgramBinding binding;
  binding.Set(GetTextureIOSurfaceProgram(), Context());

  SetUseProgram(binding.program_id);
  GLC(Context(), Context()->uniform1i(binding.sampler_location, 0));
  if (quad->orientation == IOSurfaceDrawQuad::FLIPPED) {
    GLC(Context(),
        Context()->uniform4f(binding.tex_transform_location,
                             0,
                             quad->io_surface_size.height(),
                             quad->io_surface_size.width(),
                             quad->io_surface_size.height() * -1.0f));
  } else {
    GLC(Context(),
        Context()->uniform4f(binding.tex_transform_location,
                             0,
                             0,
                             quad->io_surface_size.width(),
                             quad->io_surface_size.height()));
  }

  const float vertex_opacity[] = { quad->opacity(), quad->opacity(),
                                   quad->opacity(), quad->opacity() };
  GLC(Context(),
      Context()->uniform1fv(
          binding.vertex_opacity_location, 4, vertex_opacity));

  GLC(Context(),
      Context()->bindTexture(GL_TEXTURE_RECTANGLE_ARB,
                             quad->io_surface_texture_id));

  DrawQuadGeometry(
      frame, quad->quadTransform(), quad->rect, binding.matrix_location);

  GLC(Context(), Context()->bindTexture(GL_TEXTURE_RECTANGLE_ARB, 0));
}

void GLRenderer::FinishDrawingFrame(DrawingFrame& frame) {
  current_framebuffer_lock_.reset();
  swap_buffer_rect_.Union(gfx::ToEnclosingRect(frame.root_damage_rect));

  GLC(context_, context_->disable(GL_BLEND));
  blend_shadow_ = false;

  if (Settings().compositorFrameMessage) {
    CompositorFrame compositor_frame;
    compositor_frame.metadata = client_->MakeCompositorFrameMetadata();
    output_surface_->SendFrameToParentCompositor(&compositor_frame);
  }
}

void GLRenderer::FinishDrawingQuadList() { FlushTextureQuadCache(); }

bool GLRenderer::FlippedFramebuffer() const { return true; }

void GLRenderer::EnsureScissorTestEnabled() {
  if (is_scissor_enabled_)
    return;

  FlushTextureQuadCache();
  GLC(context_, context_->enable(GL_SCISSOR_TEST));
  is_scissor_enabled_ = true;
}

void GLRenderer::EnsureScissorTestDisabled() {
  if (!is_scissor_enabled_)
    return;

  FlushTextureQuadCache();
  GLC(context_, context_->disable(GL_SCISSOR_TEST));
  is_scissor_enabled_ = false;
}

void GLRenderer::ToGLMatrix(float* gl_matrix, const gfx::Transform& transform) {
  transform.matrix().asColMajorf(gl_matrix);
}

void GLRenderer::SetShaderQuadF(const gfx::QuadF& quad, int quad_location) {
  if (quad_location == -1)
    return;

  float point[8];
  point[0] = quad.p1().x();
  point[1] = quad.p1().y();
  point[2] = quad.p2().x();
  point[3] = quad.p2().y();
  point[4] = quad.p3().x();
  point[5] = quad.p3().y();
  point[6] = quad.p4().x();
  point[7] = quad.p4().y();
  GLC(context_, context_->uniform2fv(quad_location, 4, point));
}

void GLRenderer::SetShaderOpacity(float opacity, int alpha_location) {
  if (alpha_location != -1)
    GLC(context_, context_->uniform1f(alpha_location, opacity));
}

void GLRenderer::SetBlendEnabled(bool enabled) {
  if (enabled == blend_shadow_)
    return;

  if (enabled)
    GLC(context_, context_->enable(GL_BLEND));
  else
    GLC(context_, context_->disable(GL_BLEND));
  blend_shadow_ = enabled;
}

void GLRenderer::SetUseProgram(unsigned program) {
  if (program == program_shadow_)
    return;
  GLC(context_, context_->useProgram(program));
  program_shadow_ = program;
}

void GLRenderer::DrawQuadGeometry(const DrawingFrame& frame,
                                  const gfx::Transform& draw_transform,
                                  const gfx::RectF& quad_rect,
                                  int matrix_location) {
  gfx::Transform quad_rect_matrix;
  QuadRectTransform(&quad_rect_matrix, draw_transform, quad_rect);
  static float gl_matrix[16];
  ToGLMatrix(&gl_matrix[0], frame.projection_matrix * quad_rect_matrix);
  GLC(context_,
      context_->uniformMatrix4fv(matrix_location, 1, false, &gl_matrix[0]));

  GLC(context_, context_->drawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0));
}

void GLRenderer::CopyTextureToFramebuffer(const DrawingFrame& frame,
                                          int texture_id,
                                          gfx::Rect rect,
                                          const gfx::Transform& draw_matrix) {
  const RenderPassProgram* program = GetRenderPassProgram();

  GLC(Context(), Context()->bindTexture(GL_TEXTURE_2D, texture_id));

  SetUseProgram(program->program());
  GLC(Context(),
      Context()->uniform1i(program->fragment_shader().samplerLocation(), 0));
  GLC(Context(),
      Context()->uniform4f(program->vertex_shader().texTransformLocation(),
                           0.0f,
                           0.0f,
                           1.0f,
                           1.0f));
  SetShaderOpacity(1, program->fragment_shader().alphaLocation());
  DrawQuadGeometry(
      frame, draw_matrix, rect, program->vertex_shader().matrixLocation());
}

void GLRenderer::Finish() {
  TRACE_EVENT0("cc", "GLRenderer::finish");
  context_->finish();
}

bool GLRenderer::SwapBuffers() {
  DCHECK(visible_);
  DCHECK(!is_backbuffer_discarded_);

  TRACE_EVENT0("cc", "GLRenderer::swapBuffers");
  // We're done! Time to swapbuffers!

  if (capabilities_.using_partial_swap) {
    // If supported, we can save significant bandwidth by only swapping the
    // damaged/scissored region (clamped to the viewport)
    swap_buffer_rect_.Intersect(gfx::Rect(gfx::Point(), ViewportSize()));
    int flipped_y_pos_of_rect_bottom =
        ViewportHeight() - swap_buffer_rect_.y() - swap_buffer_rect_.height();
    output_surface_->PostSubBuffer(gfx::Rect(swap_buffer_rect_.x(),
                                             flipped_y_pos_of_rect_bottom,
                                             swap_buffer_rect_.width(),
                                             swap_buffer_rect_.height()));
  } else {
    output_surface_->SwapBuffers();
  }

  swap_buffer_rect_ = gfx::Rect();

  // We don't have real fences, so we mark read fences as passed
  // assuming a double-buffered GPU pipeline. A texture can be
  // written to after one full frame has past since it was last read.
  if (last_swap_fence_)
    static_cast<SimpleSwapFence*>(last_swap_fence_.get())->SetHasPassed();
  last_swap_fence_ = resource_provider_->GetReadLockFence();
  resource_provider_->SetReadLockFence(new SimpleSwapFence());

  return true;
}

void GLRenderer::ReceiveCompositorFrameAck(const CompositorFrameAck& ack) {
  onSwapBuffersComplete();
}

void GLRenderer::onSwapBuffersComplete() { client_->OnSwapBuffersComplete(); }

void GLRenderer::onMemoryAllocationChanged(
    WebGraphicsMemoryAllocation allocation) {
  // Just ignore the memory manager when it says to set the limit to zero
  // bytes. This will happen when the memory manager thinks that the renderer
  // is not visible (which the renderer knows better).
  if (allocation.bytesLimitWhenVisible) {
    ManagedMemoryPolicy policy(
        allocation.bytesLimitWhenVisible,
        PriorityCutoff(allocation.priorityCutoffWhenVisible),
        allocation.bytesLimitWhenNotVisible,
        PriorityCutoff(allocation.priorityCutoffWhenNotVisible));

    if (allocation.enforceButDoNotKeepAsPolicy)
      client_->EnforceManagedMemoryPolicy(policy);
    else
      client_->SetManagedMemoryPolicy(policy);
  }

  bool old_discard_backbuffer_when_not_visible =
      discard_backbuffer_when_not_visible_;
  discard_backbuffer_when_not_visible_ = !allocation.suggestHaveBackbuffer;
  EnforceMemoryPolicy();
  if (allocation.enforceButDoNotKeepAsPolicy)
    discard_backbuffer_when_not_visible_ =
        old_discard_backbuffer_when_not_visible;
}

ManagedMemoryPolicy::PriorityCutoff GLRenderer::PriorityCutoff(
    WebKit::WebGraphicsMemoryAllocation::PriorityCutoff priority_cutoff) {
  // This is simple a 1:1 map, the names differ only because the WebKit names
  // should be to match the cc names.
  switch (priority_cutoff) {
    case WebKit::WebGraphicsMemoryAllocation::PriorityCutoffAllowNothing:
      return ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
    case WebKit::WebGraphicsMemoryAllocation::PriorityCutoffAllowVisibleOnly:
      return ManagedMemoryPolicy::CUTOFF_ALLOW_REQUIRED_ONLY;
    case WebKit::WebGraphicsMemoryAllocation::
        PriorityCutoffAllowVisibleAndNearby:
      return ManagedMemoryPolicy::CUTOFF_ALLOW_NICE_TO_HAVE;
    case WebKit::WebGraphicsMemoryAllocation::PriorityCutoffAllowEverything:
      return ManagedMemoryPolicy::CUTOFF_ALLOW_EVERYTHING;
  }
  NOTREACHED();
  return ManagedMemoryPolicy::CUTOFF_ALLOW_NOTHING;
}

void GLRenderer::EnforceMemoryPolicy() {
  if (!visible_) {
    TRACE_EVENT0("cc", "GLRenderer::enforceMemoryPolicy dropping resources");
    ReleaseRenderPassTextures();
    if (discard_backbuffer_when_not_visible_)
      DiscardBackbuffer();
    resource_provider_->ReleaseCachedData();
    GLC(context_, context_->flush());
  }
}

void GLRenderer::DiscardBackbuffer() {
  if (is_backbuffer_discarded_)
    return;

  output_surface_->DiscardBackbuffer();

  is_backbuffer_discarded_ = true;

  // Damage tracker needs a full reset every time framebuffer is discarded.
  client_->SetFullRootLayerDamage();
}

void GLRenderer::EnsureBackbuffer() {
  if (!is_backbuffer_discarded_)
    return;

  output_surface_->EnsureBackbuffer();
  is_backbuffer_discarded_ = false;
}

void GLRenderer::onContextLost() { client_->DidLoseOutputSurface(); }

void GLRenderer::GetFramebufferPixels(void* pixels, gfx::Rect rect) {
  DCHECK(rect.right() <= ViewportWidth());
  DCHECK(rect.bottom() <= ViewportHeight());

  if (!pixels)
    return;

  MakeContextCurrent();

  bool do_workaround = NeedsIOSurfaceReadbackWorkaround();

  GLuint temporary_texture = 0;
  GLuint temporary_fbo = 0;

  if (do_workaround) {
    // On Mac OS X, calling glReadPixels against an FBO whose color attachment
    // is an IOSurface-backed texture causes corruption of future glReadPixels
    // calls, even those on different OpenGL contexts. It is believed that this
    // is the root cause of top crasher
    // http://crbug.com/99393. <rdar://problem/10949687>

    temporary_texture = context_->createTexture();
    GLC(context_, context_->bindTexture(GL_TEXTURE_2D, temporary_texture));
    GLC(context_,
        context_->texParameteri(
            GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
    GLC(context_,
        context_->texParameteri(
            GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
    GLC(context_,
        context_->texParameteri(
            GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
    GLC(context_,
        context_->texParameteri(
            GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
    // Copy the contents of the current (IOSurface-backed) framebuffer into a
    // temporary texture.
    GLC(context_,
        context_->copyTexImage2D(GL_TEXTURE_2D,
                                 0,
                                 GL_RGBA,
                                 0,
                                 0,
                                 ViewportSize().width(),
                                 ViewportSize().height(),
                                 0));
    temporary_fbo = context_->createFramebuffer();
    // Attach this texture to an FBO, and perform the readback from that FBO.
    GLC(context_, context_->bindFramebuffer(GL_FRAMEBUFFER, temporary_fbo));
    GLC(context_,
        context_->framebufferTexture2D(GL_FRAMEBUFFER,
                                       GL_COLOR_ATTACHMENT0,
                                       GL_TEXTURE_2D,
                                       temporary_texture,
                                       0));

    DCHECK(context_->checkFramebufferStatus(GL_FRAMEBUFFER) ==
           GL_FRAMEBUFFER_COMPLETE);
  }

  scoped_array<uint8_t> src_pixels(
      new uint8_t[rect.width() * rect.height() * 4]);
  GLC(context_,
      context_->readPixels(rect.x(),
                           ViewportSize().height() - rect.bottom(),
                           rect.width(),
                           rect.height(),
                           GL_RGBA,
                           GL_UNSIGNED_BYTE,
                           src_pixels.get()));

  uint8_t* dest_pixels = static_cast<uint8_t*>(pixels);
  size_t row_bytes = rect.width() * 4;
  int num_rows = rect.height();
  size_t total_bytes = num_rows * row_bytes;
  for (size_t dest_y = 0; dest_y < total_bytes; dest_y += row_bytes) {
    // Flip Y axis.
    size_t src_y = total_bytes - dest_y - row_bytes;
    // Swizzle BGRA -> RGBA.
    for (size_t x = 0; x < row_bytes; x += 4) {
      dest_pixels[dest_y + (x + 0)] = src_pixels.get()[src_y + (x + 2)];
      dest_pixels[dest_y + (x + 1)] = src_pixels.get()[src_y + (x + 1)];
      dest_pixels[dest_y + (x + 2)] = src_pixels.get()[src_y + (x + 0)];
      dest_pixels[dest_y + (x + 3)] = src_pixels.get()[src_y + (x + 3)];
    }
  }

  if (do_workaround) {
    // Clean up.
    GLC(context_, context_->bindFramebuffer(GL_FRAMEBUFFER, 0));
    GLC(context_, context_->bindTexture(GL_TEXTURE_2D, 0));
    GLC(context_, context_->deleteFramebuffer(temporary_fbo));
    GLC(context_, context_->deleteTexture(temporary_texture));
  }

  EnforceMemoryPolicy();
}

bool GLRenderer::GetFramebufferTexture(ScopedResource* texture,
                                       gfx::Rect device_rect) {
  DCHECK(!texture->id() || (texture->size() == device_rect.size() &&
                            texture->format() == GL_RGB));

  if (!texture->id() && !texture->Allocate(device_rect.size(),
                                           GL_RGB,
                                           ResourceProvider::TextureUsageAny))
    return false;

  ResourceProvider::ScopedWriteLockGL lock(resource_provider_, texture->id());
  GLC(context_, context_->bindTexture(GL_TEXTURE_2D, lock.texture_id()));
  GLC(context_,
      context_->copyTexImage2D(GL_TEXTURE_2D,
                               0,
                               texture->format(),
                               device_rect.x(),
                               device_rect.y(),
                               device_rect.width(),
                               device_rect.height(),
                               0));
  return true;
}

bool GLRenderer::UseScopedTexture(DrawingFrame& frame,
                                  const ScopedResource* texture,
                                  gfx::Rect viewport_rect) {
  DCHECK(texture->id());
  frame.current_render_pass = 0;
  frame.current_texture = texture;

  return BindFramebufferToTexture(frame, texture, viewport_rect);
}

void GLRenderer::BindFramebufferToOutputSurface(DrawingFrame& frame) {
  current_framebuffer_lock_.reset();
  output_surface_->BindFramebuffer();
}

bool GLRenderer::BindFramebufferToTexture(DrawingFrame& frame,
                                          const ScopedResource* texture,
                                          gfx::Rect framebuffer_rect) {
  DCHECK(texture->id());

  GLC(context_,
      context_->bindFramebuffer(GL_FRAMEBUFFER, offscreen_framebuffer_id_));
  current_framebuffer_lock_ =
      make_scoped_ptr(new ResourceProvider::ScopedWriteLockGL(
          resource_provider_, texture->id()));
  unsigned texture_id = current_framebuffer_lock_->texture_id();
  GLC(context_,
      context_->framebufferTexture2D(
          GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0));

  DCHECK(context_->checkFramebufferStatus(GL_FRAMEBUFFER) ==
         GL_FRAMEBUFFER_COMPLETE || IsContextLost());

  InitializeMatrices(frame, framebuffer_rect, false);
  SetDrawViewportSize(framebuffer_rect.size());

  return true;
}

void GLRenderer::SetScissorTestRect(gfx::Rect scissor_rect) {
  EnsureScissorTestEnabled();

  // Don't unnecessarily ask the context to change the scissor, because it
  // may cause undesired GPU pipeline flushes.
  if (scissor_rect == scissor_rect_)
    return;

  scissor_rect_ = scissor_rect;
  FlushTextureQuadCache();
  GLC(context_,
      context_->scissor(scissor_rect.x(),
                        scissor_rect.y(),
                        scissor_rect.width(),
                        scissor_rect.height()));
}

void GLRenderer::SetDrawViewportSize(gfx::Size viewport_size) {
  GLC(context_,
      context_->viewport(0, 0, viewport_size.width(), viewport_size.height()));
}

bool GLRenderer::MakeContextCurrent() { return context_->makeContextCurrent(); }

bool GLRenderer::InitializeSharedObjects() {
  TRACE_EVENT0("cc", "GLRenderer::initializeSharedObjects");
  MakeContextCurrent();

  // Create an FBO for doing offscreen rendering.
  GLC(context_, offscreen_framebuffer_id_ = context_->createFramebuffer());

  // We will always need these programs to render, so create the programs
  // eagerly so that the shader compilation can start while we do other work.
  // Other programs are created lazily on first access.
  shared_geometry_ =
      make_scoped_ptr(new GeometryBinding(context_, QuadVertexRect()));
  render_pass_program_ = make_scoped_ptr(new RenderPassProgram(context_));
  tile_program_ = make_scoped_ptr(new TileProgram(context_));
  tile_program_opaque_ = make_scoped_ptr(new TileProgramOpaque(context_));

  GLC(context_, context_->flush());

  return true;
}

const GLRenderer::TileCheckerboardProgram*
GLRenderer::GetTileCheckerboardProgram() {
  if (!tile_checkerboard_program_) {
    tile_checkerboard_program_ =
        make_scoped_ptr(new TileCheckerboardProgram(context_));
  }
  if (!tile_checkerboard_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::checkerboardProgram::initalize");
    tile_checkerboard_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_checkerboard_program_.get();
}

const GLRenderer::DebugBorderProgram* GLRenderer::GetDebugBorderProgram() {
  if (!debug_border_program_)
    debug_border_program_ = make_scoped_ptr(new DebugBorderProgram(context_));
  if (!debug_border_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::debugBorderProgram::initialize");
    debug_border_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return debug_border_program_.get();
}

const GLRenderer::SolidColorProgram* GLRenderer::GetSolidColorProgram() {
  if (!solid_color_program_)
    solid_color_program_ = make_scoped_ptr(new SolidColorProgram(context_));
  if (!solid_color_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::solidColorProgram::initialize");
    solid_color_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return solid_color_program_.get();
}

const GLRenderer::SolidColorProgramAA* GLRenderer::GetSolidColorProgramAA() {
  if (!solid_color_program_aa_) {
    solid_color_program_aa_ =
        make_scoped_ptr(new SolidColorProgramAA(context_));
  }
  if (!solid_color_program_aa_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::solidColorProgramAA::initialize");
    solid_color_program_aa_->Initialize(context_, is_using_bind_uniform_);
  }
  return solid_color_program_aa_.get();
}

const GLRenderer::RenderPassProgram* GLRenderer::GetRenderPassProgram() {
  DCHECK(render_pass_program_);
  if (!render_pass_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::renderPassProgram::initialize");
    render_pass_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return render_pass_program_.get();
}

const GLRenderer::RenderPassProgramAA* GLRenderer::GetRenderPassProgramAA() {
  if (!render_pass_program_aa_)
    render_pass_program_aa_ =
        make_scoped_ptr(new RenderPassProgramAA(context_));
  if (!render_pass_program_aa_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::renderPassProgramAA::initialize");
    render_pass_program_aa_->Initialize(context_, is_using_bind_uniform_);
  }
  return render_pass_program_aa_.get();
}

const GLRenderer::RenderPassMaskProgram*
GLRenderer::GetRenderPassMaskProgram() {
  if (!render_pass_mask_program_)
    render_pass_mask_program_ =
        make_scoped_ptr(new RenderPassMaskProgram(context_));
  if (!render_pass_mask_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::renderPassMaskProgram::initialize");
    render_pass_mask_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return render_pass_mask_program_.get();
}

const GLRenderer::RenderPassMaskProgramAA*
GLRenderer::GetRenderPassMaskProgramAA() {
  if (!render_pass_mask_program_aa_)
    render_pass_mask_program_aa_ =
        make_scoped_ptr(new RenderPassMaskProgramAA(context_));
  if (!render_pass_mask_program_aa_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::renderPassMaskProgramAA::initialize");
    render_pass_mask_program_aa_->Initialize(context_, is_using_bind_uniform_);
  }
  return render_pass_mask_program_aa_.get();
}

const GLRenderer::TileProgram* GLRenderer::GetTileProgram() {
  DCHECK(tile_program_);
  if (!tile_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::tileProgram::initialize");
    tile_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_program_.get();
}

const GLRenderer::TileProgramOpaque* GLRenderer::GetTileProgramOpaque() {
  DCHECK(tile_program_opaque_);
  if (!tile_program_opaque_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::tileProgramOpaque::initialize");
    tile_program_opaque_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_program_opaque_.get();
}

const GLRenderer::TileProgramAA* GLRenderer::GetTileProgramAA() {
  if (!tile_program_aa_)
    tile_program_aa_ = make_scoped_ptr(new TileProgramAA(context_));
  if (!tile_program_aa_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::tileProgramAA::initialize");
    tile_program_aa_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_program_aa_.get();
}

const GLRenderer::TileProgramSwizzle* GLRenderer::GetTileProgramSwizzle() {
  if (!tile_program_swizzle_)
    tile_program_swizzle_ = make_scoped_ptr(new TileProgramSwizzle(context_));
  if (!tile_program_swizzle_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::tileProgramSwizzle::initialize");
    tile_program_swizzle_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_program_swizzle_.get();
}

const GLRenderer::TileProgramSwizzleOpaque*
GLRenderer::GetTileProgramSwizzleOpaque() {
  if (!tile_program_swizzle_opaque_)
    tile_program_swizzle_opaque_ =
        make_scoped_ptr(new TileProgramSwizzleOpaque(context_));
  if (!tile_program_swizzle_opaque_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::tileProgramSwizzleOpaque::initialize");
    tile_program_swizzle_opaque_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_program_swizzle_opaque_.get();
}

const GLRenderer::TileProgramSwizzleAA* GLRenderer::GetTileProgramSwizzleAA() {
  if (!tile_program_swizzle_aa_)
    tile_program_swizzle_aa_ =
        make_scoped_ptr(new TileProgramSwizzleAA(context_));
  if (!tile_program_swizzle_aa_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::tileProgramSwizzleAA::initialize");
    tile_program_swizzle_aa_->Initialize(context_, is_using_bind_uniform_);
  }
  return tile_program_swizzle_aa_.get();
}

const GLRenderer::TextureProgram* GLRenderer::GetTextureProgram() {
  if (!texture_program_)
    texture_program_ = make_scoped_ptr(new TextureProgram(context_));
  if (!texture_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::textureProgram::initialize");
    texture_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return texture_program_.get();
}

const GLRenderer::TextureProgramFlip* GLRenderer::GetTextureProgramFlip() {
  if (!texture_program_flip_)
    texture_program_flip_ = make_scoped_ptr(new TextureProgramFlip(context_));
  if (!texture_program_flip_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::textureProgramFlip::initialize");
    texture_program_flip_->Initialize(context_, is_using_bind_uniform_);
  }
  return texture_program_flip_.get();
}

const GLRenderer::TextureIOSurfaceProgram*
GLRenderer::GetTextureIOSurfaceProgram() {
  if (!texture_io_surface_program_)
    texture_io_surface_program_ =
        make_scoped_ptr(new TextureIOSurfaceProgram(context_));
  if (!texture_io_surface_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::textureIOSurfaceProgram::initialize");
    texture_io_surface_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return texture_io_surface_program_.get();
}

const GLRenderer::VideoYUVProgram* GLRenderer::GetVideoYUVProgram() {
  if (!video_yuv_program_)
    video_yuv_program_ = make_scoped_ptr(new VideoYUVProgram(context_));
  if (!video_yuv_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::videoYUVProgram::initialize");
    video_yuv_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return video_yuv_program_.get();
}

const GLRenderer::VideoStreamTextureProgram*
GLRenderer::GetVideoStreamTextureProgram() {
  if (!Capabilities().using_egl_image)
    return NULL;
  if (!video_stream_texture_program_)
    video_stream_texture_program_ =
        make_scoped_ptr(new VideoStreamTextureProgram(context_));
  if (!video_stream_texture_program_->initialized()) {
    TRACE_EVENT0("cc", "GLRenderer::streamTextureProgram::initialize");
    video_stream_texture_program_->Initialize(context_, is_using_bind_uniform_);
  }
  return video_stream_texture_program_.get();
}

void GLRenderer::CleanupSharedObjects() {
  MakeContextCurrent();

  shared_geometry_.reset();

  if (tile_program_)
    tile_program_->Cleanup(context_);
  if (tile_program_opaque_)
    tile_program_opaque_->Cleanup(context_);
  if (tile_program_swizzle_)
    tile_program_swizzle_->Cleanup(context_);
  if (tile_program_swizzle_opaque_)
    tile_program_swizzle_opaque_->Cleanup(context_);
  if (tile_program_aa_)
    tile_program_aa_->Cleanup(context_);
  if (tile_program_swizzle_aa_)
    tile_program_swizzle_aa_->Cleanup(context_);
  if (tile_checkerboard_program_)
    tile_checkerboard_program_->Cleanup(context_);

  if (render_pass_mask_program_)
    render_pass_mask_program_->Cleanup(context_);
  if (render_pass_program_)
    render_pass_program_->Cleanup(context_);
  if (render_pass_mask_program_aa_)
    render_pass_mask_program_aa_->Cleanup(context_);
  if (render_pass_program_aa_)
    render_pass_program_aa_->Cleanup(context_);

  if (texture_program_)
    texture_program_->Cleanup(context_);
  if (texture_program_flip_)
    texture_program_flip_->Cleanup(context_);
  if (texture_io_surface_program_)
    texture_io_surface_program_->Cleanup(context_);

  if (video_yuv_program_)
    video_yuv_program_->Cleanup(context_);
  if (video_stream_texture_program_)
    video_stream_texture_program_->Cleanup(context_);

  if (debug_border_program_)
    debug_border_program_->Cleanup(context_);
  if (solid_color_program_)
    solid_color_program_->Cleanup(context_);
  if (solid_color_program_aa_)
    solid_color_program_aa_->Cleanup(context_);

  if (offscreen_framebuffer_id_)
    GLC(context_, context_->deleteFramebuffer(offscreen_framebuffer_id_));

  ReleaseRenderPassTextures();
}

bool GLRenderer::IsContextLost() {
  return (context_->getGraphicsResetStatusARB() != GL_NO_ERROR);
}

}  // namespace cc