summaryrefslogtreecommitdiff
path: root/cogl/cogl-journal.c
blob: f7c6b2dae4da841171697fc4fbe69eb505906d1e (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
/*
 * Cogl
 *
 * An object oriented GL/GLES Abstraction/Utility Layer
 *
 * Copyright (C) 2007,2008,2009 Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "cogl.h"
#include "cogl-debug.h"
#include "cogl-internal.h"
#include "cogl-context-private.h"
#include "cogl-journal-private.h"
#include "cogl-texture-private.h"
#include "cogl-pipeline-private.h"
#include "cogl-pipeline-opengl-private.h"
#include "cogl-vertex-buffer-private.h"
#include "cogl-framebuffer-private.h"
#include "cogl-profile.h"
#include "cogl-attribute-private.h"
#include "cogl-point-in-poly-private.h"
#include "cogl-private.h"

#include <string.h>
#include <gmodule.h>
#include <math.h>

/* The structs for the journal entries are over-allocated to store the
   texture coordinates for each layer */
#define GET_JOURNAL_ENTRY_SIZE_FOR_N_LAYERS(N_LAYERS) \
  (sizeof (CoglJournalEntry) + (N_LAYERS * 4 - 1) * sizeof (float))

/* XXX NB:
 * Once in the vertex array, the journal's vertex data is arranged as follows:
 * 4 vertices per quad:
 *    2 or 3 GLfloats per position (3 when doing software transforms)
 *    4 RGBA GLubytes,
 *    2 GLfloats per tex coord * n_layers
 *
 * Where n_layers corresponds to the number of pipeline layers enabled
 *
 * To avoid frequent changes in the stride of our vertex data we always pad
 * n_layers to be >= 2
 *
 * There will be four vertices per quad in the vertex array
 *
 * When we are transforming quads in software we need to also track the z
 * coordinate of transformed vertices.
 *
 * So for a given number of layers this gets the stride in 32bit words:
 */
#define SW_TRANSFORM      (!(COGL_DEBUG_ENABLED \
                             (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
#define POS_STRIDE        (SW_TRANSFORM ? 3 : 2) /* number of 32bit words */
#define N_POS_COMPONENTS  POS_STRIDE
#define COLOR_STRIDE      1 /* number of 32bit words */
#define TEX_STRIDE        2 /* number of 32bit words */
#define MIN_LAYER_PADING  2
#define GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS(N_LAYERS) \
  (POS_STRIDE + COLOR_STRIDE + \
   TEX_STRIDE * (N_LAYERS < MIN_LAYER_PADING ? MIN_LAYER_PADING : N_LAYERS))

/* If a batch is longer than this threshold then we'll assume it's not
   worth doing software clipping and it's cheaper to program the GPU
   to do the clip */
#define COGL_JOURNAL_HARDWARE_CLIP_THRESHOLD 8

typedef struct _CoglJournalFlushState
{
  CoglJournal         *journal;

  CoglFramebuffer     *framebuffer;

  CoglAttributeBuffer *attribute_buffer;
  GArray              *attributes;
  int                  current_attribute;

  gsize                stride;
  size_t               array_offset;
  GLuint               current_vertex;

  CoglIndices         *indices;
  gsize                indices_type_size;

  CoglMatrixStack     *modelview_stack;
  CoglMatrixStack     *projection_stack;

  CoglPipeline        *source;
} CoglJournalFlushState;

typedef struct _CoglJournalIter
{
  CoglJournalEntry *entry;
  int batch_num;
} CoglJournalIter;

typedef void (*CoglJournalBatchCallback) (CoglJournal *journal,
                                          const CoglJournalIter *start,
                                          int n_entries,
                                          void *data);
typedef gboolean (*CoglJournalBatchTest) (const CoglJournalIter *iter0,
                                          const CoglJournalIter *iter1);

static void _cogl_journal_free (CoglJournal *journal);

static void entry_to_screen_polygon (const CoglJournalEntry *entry,
                                     float *poly);

COGL_OBJECT_DEFINE (Journal, journal);

static void
_cogl_journal_free (CoglJournal *journal)
{
  int i;

  _cogl_journal_discard (journal);

  if (journal->batches)
    g_array_free (journal->batches, TRUE);

  for (i = 0; i < COGL_JOURNAL_VBO_POOL_SIZE; i++)
    if (journal->vbo_pool[i])
      cogl_object_unref (journal->vbo_pool[i]);

  g_slice_free (CoglJournal, journal);
}

CoglJournal *
_cogl_journal_new (void)
{
  CoglJournal *journal = g_slice_new0 (CoglJournal);

  journal->batches = g_array_new (FALSE, FALSE, sizeof (CoglJournalBatch));

  return _cogl_journal_object_new (journal);
}

static void
_cogl_journal_dump_logged_quad (CoglJournalEntry *entry)
{
  int i;

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  g_print ("n_layers = %d; rgba=0x%02X%02X%02X%02X\n",
           entry->n_layers,
           entry->color[0], entry->color[1], entry->color[2], entry->color[3]);

  for (i = 0; i < 2; i++)
    {
      float *v = entry->position + i * 2;
      int j;

      g_print ("v%d: x = %f, y = %f", i, v[0], v[1]);

      for (j = 0; j < entry->n_layers; j++)
        {
          float *t = entry->tex_coords + 4 * j + 2 * i;
          g_print (", tx%d = %f, ty%d = %f", j, t[0], j, t[1]);
        }
      g_print ("\n");
    }
}

static void
_cogl_journal_dump_quad_vertices (guint8 *data, int n_layers)
{
  gsize stride = GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS (n_layers);
  int i;

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  g_print ("n_layers = %d; stride = %d; pos stride = %d; color stride = %d; "
           "tex stride = %d; stride in bytes = %d\n",
           n_layers, (int)stride, POS_STRIDE, COLOR_STRIDE,
           TEX_STRIDE, (int)stride * 4);

  for (i = 0; i < 4; i++)
    {
      float *v = (float *)data + (i * stride);
      guint8 *c = data + (POS_STRIDE * 4) + (i * stride * 4);
      int j;

      if (G_UNLIKELY (COGL_DEBUG_ENABLED
                      (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
        g_print ("v%d: x = %f, y = %f, rgba=0x%02X%02X%02X%02X",
                 i, v[0], v[1], c[0], c[1], c[2], c[3]);
      else
        g_print ("v%d: x = %f, y = %f, z = %f, rgba=0x%02X%02X%02X%02X",
                 i, v[0], v[1], v[2], c[0], c[1], c[2], c[3]);
      for (j = 0; j < n_layers; j++)
        {
          float *t = v + POS_STRIDE + COLOR_STRIDE + TEX_STRIDE * j;
          g_print (", tx%d = %f, ty%d = %f", j, t[0], j, t[1]);
        }
      g_print ("\n");
    }
}

static void
_cogl_journal_dump_quad_batch (guint8 *data, int n_layers, int n_quads)
{
  gsize byte_stride = GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS (n_layers) * 4;
  int i;

  g_print ("_cogl_journal_dump_quad_batch: n_layers = %d, n_quads = %d\n",
           n_layers, n_quads);
  for (i = 0; i < n_quads; i++)
    _cogl_journal_dump_quad_vertices (data + byte_stride * 2 * i, n_layers);
}

static void
_cogl_journal_iterator_init (CoglJournal *journal,
                             CoglJournalIter *iter)
{
  iter->batch_num = 0;
  iter->entry = COGL_TAILQ_FIRST (&g_array_index (journal->batches,
                                                  CoglJournalBatch,
                                                  0).entries);
}

static void
_cogl_journal_iterator_next (CoglJournal *journal,
                             CoglJournalIter *iter)
{
  if ((iter->entry = COGL_TAILQ_NEXT (iter->entry, batch)) == NULL)
    {
      CoglJournalBatch *batch =
        &g_array_index (journal->batches, CoglJournalBatch, ++iter->batch_num);
      iter->entry = COGL_TAILQ_FIRST (&batch->entries);
    }
}

static void
_cogl_journal_iterator_init_reverse (CoglJournal *journal,
                                     CoglJournalIter *iter)
{
  iter->batch_num = journal->batches->len - 1;
  iter->entry = COGL_TAILQ_LAST (&g_array_index (journal->batches,
                                                 CoglJournalBatch,
                                                 iter->batch_num).entries,
                                 CoglJournalEntryList);
}

static void
_cogl_journal_iterator_previous (CoglJournal *journal,
                                 CoglJournalIter *iter)
{
  iter->entry = COGL_TAILQ_PREV (iter->entry, CoglJournalEntryList, batch);

  if (iter->entry == NULL)
    {
      if (--iter->batch_num < 0)
        iter->entry = NULL;
      else
        {
          CoglJournalBatch *batch =
            &g_array_index (journal->batches,
                            CoglJournalBatch,
                            iter->batch_num);
          iter->entry = COGL_TAILQ_LAST (&batch->entries, CoglJournalEntryList);
        }
    }
}

static void
batch_and_call (CoglJournal *journal,
                const CoglJournalIter *iter_in,
                int n_entries,
                CoglJournalBatchTest can_batch_callback,
                CoglJournalBatchCallback batch_callback,
                void *data)
{
  int i;
  int batch_len = 1;
  CoglJournalIter batch_start = *iter_in;
  CoglJournalIter prev_iter = *iter_in;

  if (n_entries < 1)
    return;

  for (i = 1; i < n_entries; i++)
    {
      CoglJournalIter next_iter = prev_iter;

      _cogl_journal_iterator_next (journal, &next_iter);

      if (can_batch_callback (&prev_iter, &next_iter))
        {
          prev_iter = next_iter;
          batch_len++;
          continue;
        }

      batch_callback (journal, &batch_start, batch_len, data);

      batch_start = next_iter;
      batch_len = 1;
      prev_iter = next_iter;
    }

  /* The last batch... */
  batch_callback (journal, &batch_start, batch_len, data);
}

static void
_cogl_journal_flush_modelview_and_entries (CoglJournal *journal,
                                           const CoglJournalIter *batch_start,
                                           int batch_len,
                                           void *data)
{
  CoglJournalFlushState *state = data;
  CoglAttribute **attributes;
  CoglDrawFlags draw_flags = (COGL_DRAW_SKIP_JOURNAL_FLUSH |
                              COGL_DRAW_SKIP_PIPELINE_VALIDATION |
                              COGL_DRAW_SKIP_FRAMEBUFFER_FLUSH);

  COGL_STATIC_TIMER (time_flush_modelview_and_entries,
                     "flush: pipeline+entries", /* parent */
                     "flush: modelview+entries",
                     "The time spent flushing modelview + entries",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context, time_flush_modelview_and_entries);

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_BATCHING)))
    g_print ("BATCHING:     modelview batch len = %d\n", batch_len);

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
    {
      _cogl_matrix_stack_set (state->modelview_stack,
                              &batch_start->entry->model_view);
      _cogl_matrix_stack_flush_to_gl (state->modelview_stack,
                                      COGL_MATRIX_MODELVIEW);
    }

  attributes = (CoglAttribute **)state->attributes->data;
  _cogl_push_source (state->source, FALSE);

  if (!_cogl_pipeline_get_real_blend_enabled (state->source))
    draw_flags |= COGL_DRAW_COLOR_ATTRIBUTE_IS_OPAQUE;

#ifdef HAVE_COGL_GL
  if (ctx->driver == COGL_DRIVER_GL)
    {
      /* XXX: it's rather evil that we sneak in the GL_QUADS enum here... */
      _cogl_draw_attributes (GL_QUADS,
                             state->current_vertex, batch_len * 4,
                             attributes,
                             state->attributes->len,
                             draw_flags);
    }
  else
#endif /* HAVE_COGL_GL */
    {
      if (batch_len > 1)
        {
          _cogl_draw_indexed_attributes (COGL_VERTICES_MODE_TRIANGLES,
                                         state->current_vertex * 6 / 4,
                                         batch_len * 6,
                                         state->indices,
                                         attributes,
                                         state->attributes->len,
                                         draw_flags);

        }
      else
        {
          _cogl_draw_attributes (COGL_VERTICES_MODE_TRIANGLE_FAN,
                                 state->current_vertex, 4,
                                 attributes,
                                 state->attributes->len,
                                 draw_flags);
        }
    }

  /* DEBUGGING CODE XXX: This path will cause all rectangles to be
   * drawn with a coloured outline. Each batch will be rendered with
   * the same color. This may e.g. help with debugging texture slicing
   * issues, visually seeing what is batched and debugging blending
   * issues, plus it looks quite cool.
   */
  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_RECTANGLES)))
    {
      static CoglPipeline *outline = NULL;
      guint8 color_intensity;
      int i;
      CoglAttribute *loop_attributes[1];

      _COGL_GET_CONTEXT (ctxt, NO_RETVAL);

      if (outline == NULL)
        outline = cogl_pipeline_new ();

      /* The least significant three bits represent the three
         components so that the order of colours goes red, green,
         yellow, blue, magenta, cyan. Black and white are skipped. The
         next two bits give four scales of intensity for those colours
         in the order 0xff, 0xcc, 0x99, and 0x66. This gives a total
         of 24 colours. If there are more than 24 batches on the stage
         then it will wrap around */
      color_intensity = 0xff - 0x33 * (ctxt->journal_rectangles_color >> 3);
      cogl_pipeline_set_color4ub (outline,
                                  (ctxt->journal_rectangles_color & 1) ?
                                  color_intensity : 0,
                                  (ctxt->journal_rectangles_color & 2) ?
                                  color_intensity : 0,
                                  (ctxt->journal_rectangles_color & 4) ?
                                  color_intensity : 0,
                                  0xff);
      cogl_set_source (outline);

      loop_attributes[0] = attributes[0]; /* we just want the position */
      for (i = 0; i < batch_len; i++)
        _cogl_draw_attributes (COGL_VERTICES_MODE_LINE_LOOP,
                               4 * i + state->current_vertex, 4,
                               loop_attributes,
                               1,
                               draw_flags);

      /* Go to the next color */
      do
        ctxt->journal_rectangles_color = ((ctxt->journal_rectangles_color + 1) &
                                          ((1 << 5) - 1));
      /* We don't want to use black or white */
      while ((ctxt->journal_rectangles_color & 0x07) == 0
             || (ctxt->journal_rectangles_color & 0x07) == 0x07);
    }

  state->current_vertex += (4 * batch_len);

  cogl_pop_source ();

  COGL_TIMER_STOP (_cogl_uprof_context, time_flush_modelview_and_entries);
}

static gboolean
compare_entry_modelviews (const CoglJournalIter *iter0,
                          const CoglJournalIter *iter1)
{
  /* Batch together quads with the same model view matrix */

  /* FIXME: this is nasty, there are much nicer ways to track this
   * (at the add_quad_vertices level) without resorting to a memcmp!
   *
   * E.g. If the cogl-current-matrix code maintained an "age" for
   * the modelview matrix we could simply check in add_quad_vertices
   * if the age has increased, and if so record the change as a
   * boolean in the journal.
   */

  if (memcmp (&iter0->entry->model_view, &iter1->entry->model_view,
              sizeof (GLfloat) * 16) == 0)
    return TRUE;
  else
    return FALSE;
}

/* At this point we have a run of quads that we know have compatible
 * pipelines, but they may not all have the same modelview matrix */
static void
_cogl_journal_flush_pipeline_and_entries (CoglJournal *journal,
                                          const CoglJournalIter *batch_start,
                                          int batch_len,
                                          void *data)
{
  CoglJournalFlushState *state = data;
  COGL_STATIC_TIMER (time_flush_pipeline_entries,
                     "flush: texcoords+pipeline+entries", /* parent */
                     "flush: pipeline+entries",
                     "The time spent flushing pipeline + entries",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context, time_flush_pipeline_entries);

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_BATCHING)))
    g_print ("BATCHING:    pipeline batch len = %d\n", batch_len);

  state->source = g_array_index (journal->batches, CoglJournalBatch,
                                 batch_start->batch_num).pipeline;

  /* If we haven't transformed the quads in software then we need to also break
   * up batches according to changes in the modelview matrix... */
  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
    {
      batch_and_call (journal,
                      batch_start,
                      batch_len,
                      compare_entry_modelviews,
                      _cogl_journal_flush_modelview_and_entries,
                      data);
    }
  else
    _cogl_journal_flush_modelview_and_entries (journal,
                                               batch_start,
                                               batch_len,
                                               data);

  COGL_TIMER_STOP (_cogl_uprof_context, time_flush_pipeline_entries);
}

static gboolean
compare_entry_pipelines (const CoglJournalIter *iter0,
                         const CoglJournalIter *iter1)
{
  /* batch rectangles using compatible pipelines */

  /* If the entries are in the same batch then they have the same
     pipeline */
  return iter0->batch_num == iter1->batch_num;
}

/* Since the stride may not reflect the number of texture layers in use
 * (due to padding) we deal with texture coordinate offsets separately
 * from vertex and color offsets... */
static void
_cogl_journal_flush_texcoord_vbo_offsets_and_entries (
                                          CoglJournal *journal,
                                          const CoglJournalIter *batch_start,
                                          int batch_len,
                                          void *data)
{
  CoglJournalFlushState *state = data;
  int                    i;
  COGL_STATIC_TIMER (time_flush_texcoord_pipeline_entries,
                     "flush: vbo+texcoords+pipeline+entries", /* parent */
                     "flush: texcoords+pipeline+entries",
                     "The time spent flushing texcoord offsets + pipeline "
                     "+ entries",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context, time_flush_texcoord_pipeline_entries);

  /* NB: attributes 0 and 1 are position and color */

  for (i = 2; i < state->attributes->len; i++)
    cogl_object_unref (g_array_index (state->attributes, CoglAttribute *, i));

  g_array_set_size (state->attributes, batch_start->entry->n_layers + 2);

  for (i = 0; i < batch_start->entry->n_layers; i++)
    {
      CoglAttribute **attribute_entry =
        &g_array_index (state->attributes, CoglAttribute *, i + 2);
      const char *names[] = {
          "cogl_tex_coord0_in",
          "cogl_tex_coord1_in",
          "cogl_tex_coord2_in",
          "cogl_tex_coord3_in",
          "cogl_tex_coord4_in",
          "cogl_tex_coord5_in",
          "cogl_tex_coord6_in",
          "cogl_tex_coord7_in"
      };
      char *name;

      /* XXX NB:
       * Our journal's vertex data is arranged as follows:
       * 4 vertices per quad:
       *    2 or 3 floats per position (3 when doing software transforms)
       *    4 RGBA bytes,
       *    2 floats per tex coord * n_layers
       * (though n_layers may be padded; see definition of
       *  GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS for details)
       */
      name = i < 8 ? (char *)names[i] :
        g_strdup_printf ("cogl_tex_coord%d_in", i);

      /* XXX: it may be worth having some form of static initializer for
       * attributes... */
      *attribute_entry =
        cogl_attribute_new (state->attribute_buffer,
                            name,
                            state->stride,
                            state->array_offset +
                            (POS_STRIDE + COLOR_STRIDE) * 4 +
                            TEX_STRIDE * 4 * i,
                            2,
                            COGL_ATTRIBUTE_TYPE_FLOAT);

      if (i >= 8)
        g_free (name);
    }

  batch_and_call (journal,
                  batch_start,
                  batch_len,
                  compare_entry_pipelines,
                  _cogl_journal_flush_pipeline_and_entries,
                  data);
  COGL_TIMER_STOP (_cogl_uprof_context, time_flush_texcoord_pipeline_entries);
}

static gboolean
compare_entry_n_layers (const CoglJournalIter *iter0,
                        const CoglJournalIter *iter1)
{
  if (iter0->entry->n_layers == iter1->entry->n_layers)
    return TRUE;
  else
    return FALSE;
}

/* At this point we know the stride has changed from the previous batch
 * of journal entries */
static void
_cogl_journal_flush_vbo_offsets_and_entries (CoglJournal *journal,
                                             const CoglJournalIter *batch_start,
                                             int batch_len,
                                             void *data)
{
  CoglJournalFlushState   *state = data;
  gsize                    stride;
  int                      i;
  CoglAttribute          **attribute_entry;
  COGL_STATIC_TIMER (time_flush_vbo_texcoord_pipeline_entries,
                     "flush: clip+vbo+texcoords+pipeline+entries", /* parent */
                     "flush: vbo+texcoords+pipeline+entries",
                     "The time spent flushing vbo + texcoord offsets + "
                     "pipeline + entries",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context,
                    time_flush_vbo_texcoord_pipeline_entries);

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_BATCHING)))
    g_print ("BATCHING:   vbo offset batch len = %d\n", batch_len);

  /* XXX NB:
   * Our journal's vertex data is arranged as follows:
   * 4 vertices per quad:
   *    2 or 3 GLfloats per position (3 when doing software transforms)
   *    4 RGBA GLubytes,
   *    2 GLfloats per tex coord * n_layers
   * (though n_layers may be padded; see definition of
   *  GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS for details)
   */
  stride = GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS (batch_start->entry->n_layers);
  stride *= sizeof (float);
  state->stride = stride;

  for (i = 0; i < state->attributes->len; i++)
    cogl_object_unref (g_array_index (state->attributes, CoglAttribute *, i));

  g_array_set_size (state->attributes, 2);

  attribute_entry = &g_array_index (state->attributes, CoglAttribute *, 0);
  *attribute_entry = cogl_attribute_new (state->attribute_buffer,
                                         "cogl_position_in",
                                         stride,
                                         state->array_offset,
                                         N_POS_COMPONENTS,
                                         COGL_ATTRIBUTE_TYPE_FLOAT);

  attribute_entry = &g_array_index (state->attributes, CoglAttribute *, 1);
  *attribute_entry =
    cogl_attribute_new (state->attribute_buffer,
                        "cogl_color_in",
                        stride,
                        state->array_offset + (POS_STRIDE * 4),
                        4,
                        COGL_ATTRIBUTE_TYPE_UNSIGNED_BYTE);

  if (ctx->driver != COGL_DRIVER_GL)
    state->indices = cogl_get_rectangle_indices (batch_len);

  /* We only create new Attributes when the stride within the
   * AttributeBuffer changes. (due to a change in the number of pipeline
   * layers) While the stride remains constant we walk forward through
   * the above AttributeBuffer using a vertex offset passed to
   * cogl_draw_attributes
   */
  state->current_vertex = 0;

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_JOURNAL)))
    {
      guint8 *verts;

      /* Mapping a buffer for read is probably a really bad thing to
         do but this will only happen during debugging so it probably
         doesn't matter */
      verts = ((guint8 *)cogl_buffer_map (COGL_BUFFER (state->attribute_buffer),
                                          COGL_BUFFER_ACCESS_READ, 0) +
               state->array_offset);

      _cogl_journal_dump_quad_batch (verts,
                                     batch_start->entry->n_layers,
                                     batch_len);

      cogl_buffer_unmap (COGL_BUFFER (state->attribute_buffer));
    }

  batch_and_call (journal,
                  batch_start,
                  batch_len,
                  compare_entry_n_layers,
                  _cogl_journal_flush_texcoord_vbo_offsets_and_entries,
                  data);

  /* progress forward through the VBO containing all our vertices */
  state->array_offset += (stride * 4 * batch_len);
  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_JOURNAL)))
    g_print ("new vbo offset = %lu\n", (unsigned long)state->array_offset);

  COGL_TIMER_STOP (_cogl_uprof_context,
                   time_flush_vbo_texcoord_pipeline_entries);
}

static gboolean
compare_entry_strides (const CoglJournalIter *iter0,
                       const CoglJournalIter *iter1)
{
  /* Currently the only thing that affects the stride for our vertex arrays
   * is the number of pipeline layers. We need to update our VBO offsets
   * whenever the stride changes. */
  /* TODO: We should be padding the n_layers == 1 case as if it were
   * n_layers == 2 so we can reduce the need to split batches. */
  if (iter0->entry->n_layers == iter1->entry->n_layers ||
      (iter0->entry->n_layers <= MIN_LAYER_PADING &&
       iter1->entry->n_layers <= MIN_LAYER_PADING))
    return TRUE;
  else
    return FALSE;
}

/* At this point we know the batch has a unique clip stack */
static void
_cogl_journal_flush_clip_stacks_and_entries (CoglJournal *journal,
                                             const CoglJournalIter *batch_start,
                                             int batch_len,
                                             void *data)
{
  CoglJournalFlushState *state = data;

  COGL_STATIC_TIMER (time_flush_clip_stack_pipeline_entries,
                     "Journal Flush", /* parent */
                     "flush: clip+vbo+texcoords+pipeline+entries",
                     "The time spent flushing clip + vbo + texcoord offsets + "
                     "pipeline + entries",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context,
                    time_flush_clip_stack_pipeline_entries);

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_BATCHING)))
    g_print ("BATCHING:  clip stack batch len = %d\n", batch_len);

  _cogl_clip_stack_flush (batch_start->entry->clip_stack, state->framebuffer);

  _cogl_matrix_stack_push (state->modelview_stack);

  /* If we have transformed all our quads at log time then we ensure
   * no further model transform is applied by loading the identity
   * matrix here. We need to do this after flushing the clip stack
   * because the clip stack flushing code can modify the matrix */
  if (G_LIKELY (!(COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM))))
    {
      _cogl_matrix_stack_load_identity (state->modelview_stack);
      _cogl_matrix_stack_flush_to_gl (state->modelview_stack,
                                      COGL_MATRIX_MODELVIEW);
    }

  /* Setting up the clip state can sometimes also flush the projection
     matrix so we should flush it again. This will be a no-op if the
     clip code didn't modify the projection */
  _cogl_matrix_stack_flush_to_gl (state->projection_stack,
                                  COGL_MATRIX_PROJECTION);

  batch_and_call (journal,
                  batch_start,
                  batch_len,
                  compare_entry_strides,
                  _cogl_journal_flush_vbo_offsets_and_entries, /* callback */
                  data);

  _cogl_matrix_stack_pop (state->modelview_stack);

  COGL_TIMER_STOP (_cogl_uprof_context,
                   time_flush_clip_stack_pipeline_entries);
}

static gboolean
calculate_translation (const CoglMatrix *a,
                       const CoglMatrix *b,
                       float *tx_p,
                       float *ty_p)
{
  float tx, ty;
  int x, y;

  /* Assuming we had the original matrix in this form:
   *
   *      [ a₁₁, a₁₂, a₁₃, a₁₄ ]
   *      [ a₂₁, a₂₂, a₂₃, a₂₄ ]
   *  a = [ a₃₁, a₃₂, a₃₃, a₃₄ ]
   *      [ a₄₁, a₄₂, a₄₃, a₄₄ ]
   *
   * then a translation of that matrix would be a multiplication by a
   * matrix of this form:
   *
   *      [ 1, 0, 0, x ]
   *      [ 0, 1, 0, y ]
   *  t = [ 0, 0, 1, 0 ]
   *      [ 0, 0, 0, 1 ]
   *
   * That would give us a matrix of this form.
   *
   *              [ a₁₁, a₁₂, a₁₃, a₁₁ x + a₁₂ y + a₁₄ ]
   *              [ a₂₁, a₂₂, a₂₃, a₂₁ x + a₂₂ y + a₂₄ ]
   *  b = a ⋅ t = [ a₃₁, a₃₂, a₃₃, a₃₁ x + a₃₂ y + a₃₄ ]
   *              [ a₄₁, a₄₂, a₄₃, a₄₁ x + a₄₂ y + a₄₄ ]
   *
   * We can use the two equations from the top left of the matrix to
   * work out the x and y translation given the two matrices:
   *
   *  b₁₄ = a₁₁x + a₁₂y + a₁₄
   *  b₂₄ = a₂₁x + a₂₂y + a₂₄
   *
   * Rearranging gives us:
   *
   *        a₁₂ b₂₄ - a₂₄ a₁₂
   *        -----------------  +  a₁₄ - b₁₄
   *              a₂₂
   *  x =  ---------------------------------
   *                a₁₂ a₂₁
   *                -------  -  a₁₁
   *                  a₂₂
   *
   *      b₂₄ - a₂₁x - a₂₄
   *  y = ----------------
   *            a₂₂
   *
   * Once we've worked out what x and y would be if this was a valid
   * translation then we can simply verify that the rest of the matrix
   * matches up.
   */

  /* The leftmost 3x4 part of the matrix shouldn't change by a
     translation so we can just compare it directly */
  for (y = 0; y < 4; y++)
    for (x = 0; x < 3; x++)
      if ((&a->xx)[x * 4 + y] != (&b->xx)[x * 4 + y])
        return FALSE;

  tx = (((a->xy * b->yw - a->yw * a->xy) / a->yy + a->xw - b->xw) /
        ((a->xy * a->yx) / a->yy - a->xx));
  ty = (b->yw - a->yx * tx - a->yw) / a->yy;

#define APPROX_EQUAL(a, b) (fabsf ((a) - (b)) < 1e-6f)

  /* Check whether the 4th column of the matrices match up to the
     calculation */
  if (!APPROX_EQUAL (b->xw, a->xx * tx + a->xy * ty + a->xw) ||
      !APPROX_EQUAL (b->yw, a->yx * tx + a->yy * ty + a->yw) ||
      !APPROX_EQUAL (b->zw, a->zx * tx + a->zy * ty + a->zw) ||
      !APPROX_EQUAL (b->ww, a->wx * tx + a->wy * ty + a->ww))
    return FALSE;

#undef APPROX_EQUAL

  *tx_p = tx;
  *ty_p = ty;

  return TRUE;
}

typedef struct
{
  float x_1, y_1;
  float x_2, y_2;
} ClipBounds;

static gboolean
can_software_clip_entry (CoglPipeline *entry_pipeline,
                         CoglJournalEntry *journal_entry,
                         CoglPipeline *prev_pipeline,
                         CoglClipStack *clip_stack,
                         ClipBounds *clip_bounds_out)
{
  CoglClipStack *clip_entry;
  int layer_num;

  clip_bounds_out->x_1 = -G_MAXFLOAT;
  clip_bounds_out->y_1 = -G_MAXFLOAT;
  clip_bounds_out->x_2 = G_MAXFLOAT;
  clip_bounds_out->y_2 = G_MAXFLOAT;

  /* Check the pipeline is usable. We can short-cut here for
     entries using the same pipeline as the previous entry */
  if (prev_pipeline == NULL || entry_pipeline != prev_pipeline)
    {
      /* If the pipeline has a user program then we can't reliably modify
         the texture coordinates */
      if (cogl_pipeline_get_user_program (entry_pipeline))
        return FALSE;

      /* If any of the pipeline layers have a texture matrix then we can't
         reliably modify the texture coordinates */
      for (layer_num = cogl_pipeline_get_n_layers (entry_pipeline) - 1;
           layer_num >= 0;
           layer_num--)
        if (_cogl_pipeline_layer_has_user_matrix (entry_pipeline, layer_num))
          return FALSE;
    }

  /* Now we need to verify that each clip entry's matrix is just a
     translation of the journal entry's modelview matrix. We can
     also work out the bounds of the clip in modelview space using
     this translation */
  for (clip_entry = clip_stack; clip_entry; clip_entry = clip_entry->parent)
    {
      float rect_x1, rect_y1, rect_x2, rect_y2;
      CoglClipStackRect *clip_rect;
      float tx, ty;

      clip_rect = (CoglClipStackRect *) clip_entry;

      if (!calculate_translation (&clip_rect->matrix,
                                  &journal_entry->model_view,
                                  &tx, &ty))
        return FALSE;

      if (clip_rect->x0 < clip_rect->x1)
        {
          rect_x1 = clip_rect->x0;
          rect_x2 = clip_rect->x1;
        }
      else
        {
          rect_x1 = clip_rect->x1;
          rect_x2 = clip_rect->x0;
        }
      if (clip_rect->y0 < clip_rect->y1)
        {
          rect_y1 = clip_rect->y0;
          rect_y2 = clip_rect->y1;
        }
      else
        {
          rect_y1 = clip_rect->y1;
          rect_y2 = clip_rect->y0;
        }

      clip_bounds_out->x_1 = MAX (clip_bounds_out->x_1, rect_x1 - tx);
      clip_bounds_out->y_1 = MAX (clip_bounds_out->y_1, rect_y1 - ty);
      clip_bounds_out->x_2 = MIN (clip_bounds_out->x_2, rect_x2 - tx);
      clip_bounds_out->y_2 = MIN (clip_bounds_out->y_2, rect_y2 - ty);
    }

  if (clip_bounds_out->x_2 <= clip_bounds_out->x_1 ||
      clip_bounds_out->y_2 <= clip_bounds_out->y_1)
    memset (clip_bounds_out, 0, sizeof (ClipBounds));

  return TRUE;
}

static void
_cogl_journal_calculate_transformed_vertices (CoglJournalEntry *entry)
{
  entry->transformed_verts[0] = entry->position[0];
  entry->transformed_verts[1] = entry->position[1];
  entry->transformed_verts[2] = 0;

  entry->transformed_verts[3] = entry->position[0];
  entry->transformed_verts[4] = entry->position[3];
  entry->transformed_verts[5] = 0;

  entry->transformed_verts[6] = entry->position[2];
  entry->transformed_verts[7] = entry->position[3];
  entry->transformed_verts[8] = 0;

  entry->transformed_verts[9] = entry->position[2];
  entry->transformed_verts[10] = entry->position[1];
  entry->transformed_verts[11] = 0;

  cogl_matrix_transform_points (&entry->model_view,
                                2, /* n_components */
                                sizeof (float) * 3, /* stride_in */
                                entry->transformed_verts, /* points_in */
                                /* strideout */
                                sizeof (float) * 3,
                                entry->transformed_verts, /* points_out */
                                4 /* n_points */);
}

static void
software_clip_entry (CoglJournalEntry *journal_entry,
                     ClipBounds *clip_bounds)
{
  float rx1, ry1, rx2, ry2;
  float vx1, vy1, vx2, vy2;
  int layer_num;

  /* Remove the clip on the entry */
  _cogl_clip_stack_unref (journal_entry->clip_stack);
  journal_entry->clip_stack = NULL;

  vx1 = journal_entry->position[0];
  vy1 = journal_entry->position[1];
  vx2 = journal_entry->position[2];
  vy2 = journal_entry->position[3];

  if (vx1 < vx2)
    {
      rx1 = vx1;
      rx2 = vx2;
    }
  else
    {
      rx1 = vx2;
      rx2 = vx1;
    }
  if (vy1 < vy2)
    {
      ry1 = vy1;
      ry2 = vy2;
    }
  else
    {
      ry1 = vy2;
      ry2 = vy1;
    }

  rx1 = CLAMP (rx1, clip_bounds->x_1, clip_bounds->x_2);
  ry1 = CLAMP (ry1, clip_bounds->y_1, clip_bounds->y_2);
  rx2 = CLAMP (rx2, clip_bounds->x_1, clip_bounds->x_2);
  ry2 = CLAMP (ry2, clip_bounds->y_1, clip_bounds->y_2);

  /* Check if the rectangle intersects the clip at all */
  if (rx1 == rx2 || ry1 == ry2)
    {
      /* Will set all of the vertex data to 0 in the hope that this will
         create a degenerate rectangle and the GL driver will be able to
         clip it quickly */
      if (G_UNLIKELY (COGL_DEBUG_ENABLED
                      (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
        memset (journal_entry->position, 0, sizeof (float) * 4);
      else
        memset (journal_entry->transformed_verts, 0, sizeof (float) * 12);
    }
  else
    {
      if (vx1 > vx2)
        {
          float t = rx1;
          rx1 = rx2;
          rx2 = t;
        }
      if (vy1 > vy2)
        {
          float t = ry1;
          ry1 = ry2;
          ry2 = t;
        }

      journal_entry->position[0] = rx1;
      journal_entry->position[1] = ry1;
      journal_entry->position[2] = rx2;
      journal_entry->position[3] = ry2;

      /* Convert the rectangle coordinates to a fraction of the original
         rectangle */
      rx1 = (rx1 - vx1) / (vx2 - vx1);
      ry1 = (ry1 - vy1) / (vy2 - vy1);
      rx2 = (rx2 - vx1) / (vx2 - vx1);
      ry2 = (ry2 - vy1) / (vy2 - vy1);

      for (layer_num = 0; layer_num < journal_entry->n_layers; layer_num++)
        {
          float *t = journal_entry->tex_coords + layer_num * 4;
          float tx1 = t[0], ty1 = t[1];
          float tx2 = t[2], ty2 = t[3];
          t[0] = rx1 * (tx2 - tx1) + tx1;
          t[1] = ry1 * (ty2 - ty1) + ty1;
          t[2] = rx2 * (tx2 - tx1) + tx1;
          t[3] = ry2 * (ty2 - ty1) + ty1;
        }

      /* The transformed vertices need to be recalculated. FIXME:
         clipping should probably be done earlier to avoid this, but
         then it can't know the length of the batch which affects the
         decision of whether to clip. */
      if (G_LIKELY (!COGL_DEBUG_ENABLED
                    (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
        _cogl_journal_calculate_transformed_vertices (journal_entry);
    }
}

static void
maybe_software_clip_entries (CoglJournal *journal,
                             const CoglJournalIter *batch_start,
                             int batch_len,
                             CoglJournalFlushState *state)
{
  CoglJournalIter iter;
  CoglPipeline *prev_pipeline;
  CoglClipStack *clip_stack, *clip_entry;
  int entry_num;

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  /* This tries to find cases where the entry is logged with a clip
     but it would be faster to modify the vertex and texture
     coordinates rather than flush the clip so that it can batch
     better */

  /* If the batch is reasonably long then it's worthwhile programming
     the GPU to do the clip */
  if (batch_len >= COGL_JOURNAL_HARDWARE_CLIP_THRESHOLD)
    return;

  clip_stack = batch_start->entry->clip_stack;

  if (clip_stack == NULL)
    return;

  /* Verify that all of the clip stack entries are a simple rectangle
     clip */
  for (clip_entry = clip_stack; clip_entry; clip_entry = clip_entry->parent)
    if (clip_entry->type != COGL_CLIP_STACK_RECT)
      return;

  /* This scratch buffer is used to store the translation for each
     entry in the journal. We store it in a separate buffer because
     it's expensive to calculate but at this point we still don't know
     whether we can clip all of the entries so we don't want to do the
     rest of the dependant calculations until we're sure we can. */
  if (ctx->journal_clip_bounds == NULL)
    ctx->journal_clip_bounds = g_array_new (FALSE, FALSE, sizeof (ClipBounds));
  g_array_set_size (ctx->journal_clip_bounds, batch_len);

  prev_pipeline = NULL;

  for (entry_num = 0, iter = *batch_start;
       entry_num < batch_len;
       entry_num++, _cogl_journal_iterator_next (journal, &iter))
    {
      CoglPipeline *entry_pipeline;
      ClipBounds *clip_bounds = &g_array_index (ctx->journal_clip_bounds,
                                                ClipBounds, entry_num);

      entry_pipeline = g_array_index (journal->batches,
                                      CoglJournalBatch,
                                      iter.batch_num).pipeline;

      if (!can_software_clip_entry (entry_pipeline,
                                    iter.entry,
                                    prev_pipeline,
                                    clip_stack,
                                    clip_bounds))
        return;

      prev_pipeline = entry_pipeline;
    }

  /* If we make it here then we know we can software clip the entire batch */

  COGL_NOTE (CLIPPING, "Software clipping a batch of length %i", batch_len);

  for (entry_num = 0, iter = *batch_start;
       entry_num < batch_len;
       entry_num++, _cogl_journal_iterator_next (journal, &iter))
    {
      ClipBounds *clip_bounds = &g_array_index (ctx->journal_clip_bounds,
                                                ClipBounds, entry_num);

      software_clip_entry (iter.entry, clip_bounds);
    }

  return;
}

static void
_cogl_journal_maybe_software_clip_entries (CoglJournal *journal,
                                           const CoglJournalIter *batch_start,
                                           int batch_len,
                                           void *data)
{
  CoglJournalFlushState *state = data;

  COGL_STATIC_TIMER (time_check_software_clip,
                     "Journal Flush", /* parent */
                     "flush: software clipping",
                     "Time spent software clipping",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context,
                    time_check_software_clip);

  maybe_software_clip_entries (journal, batch_start, batch_len, state);

  COGL_TIMER_STOP (_cogl_uprof_context,
                   time_check_software_clip);
}

static gboolean
compare_entry_clip_stacks (const CoglJournalIter *iter0,
                           const CoglJournalIter *iter1)
{
  return iter0->entry->clip_stack == iter1->entry->clip_stack;
}

/* Gets a new vertex array from the pool. A reference is taken on the
   array so it can be treated as if it was just newly allocated */
static CoglAttributeBuffer *
create_attribute_buffer (CoglJournal *journal,
                         gsize n_bytes)
{
  CoglAttributeBuffer *vbo;

  /* If CoglBuffers are being emulated with malloc then there's not
     really any point in using the pool so we'll just allocate the
     buffer directly */
  if (!cogl_features_available (COGL_FEATURE_VBOS))
    return cogl_attribute_buffer_new (n_bytes, NULL);

  vbo = journal->vbo_pool[journal->next_vbo_in_pool];

  if (vbo == NULL)
    {
      vbo = cogl_attribute_buffer_new (n_bytes, NULL);
      journal->vbo_pool[journal->next_vbo_in_pool] = vbo;
    }
  else if (cogl_buffer_get_size (COGL_BUFFER (vbo)) < n_bytes)
    {
      /* If the buffer is too small then we'll just recreate it */
      cogl_object_unref (vbo);
      vbo = cogl_attribute_buffer_new (n_bytes, NULL);
      journal->vbo_pool[journal->next_vbo_in_pool] = vbo;
    }

  journal->next_vbo_in_pool = ((journal->next_vbo_in_pool + 1) %
                               COGL_JOURNAL_VBO_POOL_SIZE);

  return cogl_object_ref (vbo);
}

static CoglAttributeBuffer *
upload_vertices (CoglJournal *journal)
{
  CoglAttributeBuffer *attribute_buffer;
  CoglJournalIter iter;
  CoglBuffer *buffer;
  float *vout;
  int entry_num;
  int i;

  g_assert (journal->needed_vbo_len > 0);

  attribute_buffer =
    create_attribute_buffer (journal, journal->needed_vbo_len * 4);
  buffer = COGL_BUFFER (attribute_buffer);
  cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_STATIC);

  vout = _cogl_buffer_map_for_fill_or_fallback (buffer);

  /* Expand the number of vertices from 2 to 4 while uploading */
  for (entry_num = 0, _cogl_journal_iterator_init (journal, &iter);
       entry_num < journal->journal_len;
       entry_num++, _cogl_journal_iterator_next (journal, &iter))
    {
      size_t vb_stride =
        GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS (iter.entry->n_layers);

      /* Copy the color to all four of the vertices */
      for (i = 0; i < 4; i++)
        memcpy (vout + vb_stride * i + POS_STRIDE, iter.entry->color, 4);

      if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_SOFTWARE_TRANSFORM)))
        {
          vout[vb_stride * 0 + 0] = iter.entry->position[0];
          vout[vb_stride * 0 + 1] = iter.entry->position[1];
          vout[vb_stride * 1 + 0] = iter.entry->position[0];
          vout[vb_stride * 1 + 1] = iter.entry->position[3];
          vout[vb_stride * 2 + 0] = iter.entry->position[2];
          vout[vb_stride * 2 + 1] = iter.entry->position[3];
          vout[vb_stride * 3 + 0] = iter.entry->position[2];
          vout[vb_stride * 3 + 1] = iter.entry->position[1];
        }
      else
        {
          for (i = 0; i < 4; i++)
            memcpy (vout + vb_stride * i,
                    iter.entry->transformed_verts + i * 3,
                    sizeof (float) * 3);
        }

      for (i = 0; i < iter.entry->n_layers; i++)
        {
          const float *tin = iter.entry->tex_coords + i * 4;
          float *tout = vout + POS_STRIDE + COLOR_STRIDE;

          tout[vb_stride * 0 + 0 + i * 2] = tin[0];
          tout[vb_stride * 0 + 1 + i * 2] = tin[1];
          tout[vb_stride * 1 + 0 + i * 2] = tin[0];
          tout[vb_stride * 1 + 1 + i * 2] = tin[3];
          tout[vb_stride * 2 + 0 + i * 2] = tin[2];
          tout[vb_stride * 2 + 1 + i * 2] = tin[3];
          tout[vb_stride * 3 + 0 + i * 2] = tin[2];
          tout[vb_stride * 3 + 1 + i * 2] = tin[1];
        }

      vout += vb_stride * 4;
    }

  _cogl_buffer_unmap_for_fill_or_fallback (buffer);

  return attribute_buffer;
}

void
_cogl_journal_discard (CoglJournal *journal)
{
  int i;

  for (i = 0; i < journal->batches->len; i++)
    {
      CoglJournalBatch *batch =
        &g_array_index (journal->batches, CoglJournalBatch, i);
      CoglJournalEntry *entry, *tmp;

      COGL_TAILQ_FOREACH_SAFE (entry, &batch->entries, batch, tmp)
        {
          _cogl_clip_stack_unref (entry->clip_stack);
          g_slice_free1 (GET_JOURNAL_ENTRY_SIZE_FOR_N_LAYERS (entry->n_layers),
                         entry);
        }

      _cogl_pipeline_journal_unref (batch->pipeline);
    }

  g_array_set_size (journal->batches, 0);
  journal->needed_vbo_len = 0;
  journal->fast_read_pixel_count = 0;
  journal->journal_len = 0;
}

/* Note: A return value of FALSE doesn't mean 'no' it means
 * 'unknown' */
gboolean
_cogl_journal_all_entries_within_bounds (CoglJournal *journal,
                                         float clip_x0,
                                         float clip_y0,
                                         float clip_x1,
                                         float clip_y1)
{
  CoglJournalIter iter;
  CoglClipStack *clip_entry;
  CoglClipStack *reference = NULL;
  int bounds_x0;
  int bounds_y0;
  int bounds_x1;
  int bounds_y1;
  int i;

  if (journal->journal_len == 0)
    return TRUE;

  _cogl_journal_iterator_init (journal, &iter);

  /* Find the shortest clip_stack ancestry that leaves us in the
   * required bounds */
  for (clip_entry = iter.entry->clip_stack;
       clip_entry;
       clip_entry = clip_entry->parent)
    {
      _cogl_clip_stack_get_bounds (clip_entry,
                                   &bounds_x0, &bounds_y0,
                                   &bounds_x1, &bounds_y1);

      if (bounds_x0 >= clip_x0 && bounds_y0 >= clip_y0 &&
          bounds_x1 <= clip_x1 && bounds_y1 <= clip_y1)
        reference = clip_entry;
      else
        break;
    }

  if (!reference)
    return FALSE;

  /* For the remaining journal entries we will only verify they share
   * 'reference' as an ancestor in their clip stack since that's
   * enough to know that they would be within the required bounds.
   */
  for (i = 1; i < journal->journal_len; i++)
    {
      gboolean found_reference = FALSE;
      _cogl_journal_iterator_next (journal, &iter);

      for (clip_entry = iter.entry->clip_stack;
           clip_entry;
           clip_entry = clip_entry->parent)
        {
          if (clip_entry == reference)
            {
              found_reference = TRUE;
              break;
            }
        }

      if (!found_reference)
        return FALSE;
    }

  return TRUE;
}

/* XXX NB: When _cogl_journal_flush() returns all state relating
 * to pipelines, all glEnable flags and current matrix state
 * is undefined.
 */
void
_cogl_journal_flush (CoglJournal *journal,
                     CoglFramebuffer *framebuffer)
{
  CoglJournalFlushState state;
  int                   i;
  CoglMatrixStack      *modelview_stack;
  CoglJournalIter       first_iter;
  COGL_STATIC_TIMER (flush_timer,
                     "Mainloop", /* parent */
                     "Journal Flush",
                     "The time spent flushing the Cogl journal",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  if (journal->journal_len == 0)
    return;

  /* The entries in this journal may depend on images in other
   * framebuffers which may require that we flush the journals
   * associated with those framebuffers before we can flush
   * this journal... */
  _cogl_framebuffer_flush_dependency_journals (framebuffer);

  /* Note: we start the timer after flushing dependency journals so
   * that the timer isn't started recursively. */
  COGL_TIMER_START (_cogl_uprof_context, flush_timer);

  state.framebuffer = framebuffer;
  cogl_push_framebuffer (framebuffer);

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_BATCHING)))
    g_print ("BATCHING: journal len = %" G_GSIZE_FORMAT "\n",
             journal->journal_len);

  /* NB: the journal deals with flushing the modelview stack and clip
     state manually */
  _cogl_framebuffer_flush_state (framebuffer,
                                 framebuffer,
                                 COGL_FRAMEBUFFER_FLUSH_SKIP_MODELVIEW |
                                 COGL_FRAMEBUFFER_FLUSH_SKIP_CLIP_STATE);

  state.journal = journal;

  state.attributes = ctx->journal_flush_attributes_array;

  modelview_stack = _cogl_framebuffer_get_modelview_stack (framebuffer);
  state.modelview_stack = modelview_stack;
  state.projection_stack = _cogl_framebuffer_get_projection_stack (framebuffer);

  _cogl_journal_iterator_init (journal, &first_iter);

  if (G_UNLIKELY ((COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_SOFTWARE_CLIP)) == 0))
    {
      /* We do an initial walk of the journal to analyse the clip stack
         batches to see if we can do software clipping. We do this as a
         separate walk of the journal because we can modify entries and
         this may end up joining together clip stack batches in the next
         iteration. */
      batch_and_call (journal,
                      &first_iter, /* first entry */
                      /* max number of entries to consider */
                      journal->journal_len,
                      compare_entry_clip_stacks,
                      _cogl_journal_maybe_software_clip_entries, /* callback */
                      &state); /* data */
    }

  /* We upload the vertices after the clip stack pass in case it
     modifies the entries */
  state.attribute_buffer = upload_vertices (journal);
  state.array_offset = 0;

  /* batch_and_call() batches a list of journal entries according to some
   * given criteria and calls a callback once for each determined batch.
   *
   * The process of flushing the journal is staggered to reduce the amount
   * of driver/GPU state changes necessary:
   * 1) We split the entries according to the clip state.
   * 2) We split the entries according to the stride of the vertices:
   *      Each time the stride of our vertex data changes we need to call
   *      gl{Vertex,Color}Pointer to inform GL of new VBO offsets.
   *      Currently the only thing that affects the stride of our vertex data
   *      is the number of pipeline layers.
   * 3) We split the entries explicitly by the number of pipeline layers:
   *      We pad our vertex data when the number of layers is < 2 so that we
   *      can minimize changes in stride. Each time the number of layers
   *      changes we need to call glTexCoordPointer to inform GL of new VBO
   *      offsets.
   * 4) We then split according to compatible Cogl pipelines:
   *      This is where we flush pipeline state
   * 5) Finally we split according to modelview matrix changes:
   *      This is when we finally tell GL to draw something.
   *      Note: Splitting by modelview changes is skipped when are doing the
   *      vertex transformation in software at log time.
   */
  batch_and_call (journal,
                  &first_iter, /* first entry */
                  journal->journal_len, /* max number of entries to consider */
                  compare_entry_clip_stacks,
                  _cogl_journal_flush_clip_stacks_and_entries, /* callback */
                  &state); /* data */

  for (i = 0; i < state.attributes->len; i++)
    cogl_object_unref (g_array_index (state.attributes, CoglAttribute *, i));
  g_array_set_size (state.attributes, 0);

  cogl_object_unref (state.attribute_buffer);

  _cogl_journal_discard (journal);

  cogl_pop_framebuffer ();

  COGL_TIMER_STOP (_cogl_uprof_context, flush_timer);
}

static gboolean
add_framebuffer_deps_cb (CoglPipelineLayer *layer, void *user_data)
{
  CoglFramebuffer *framebuffer = user_data;
  CoglTexture *texture = _cogl_pipeline_layer_get_texture_real (layer);
  const GList *l;

  if (!texture)
    return TRUE;

  for (l = _cogl_texture_get_associated_framebuffers (texture); l; l = l->next)
    _cogl_framebuffer_add_dependency (framebuffer, l->data);

  return TRUE;
}

static void
_cogl_journal_add_entry_to_batch (CoglJournal *journal,
                                  CoglPipeline *pipeline,
                                  CoglJournalEntry *entry)
{
  CoglLooseRegionRectangle bounds;
  int batch_index;
  CoglJournalBatch *batch;
  float poly[16];
  int i;

  /* Calculate the screen-space bounding box of this entry */
  entry_to_screen_polygon (entry, poly);

  bounds.x_2 = bounds.x_1 = poly[0];
  bounds.y_2 = bounds.y_1 = poly[1];

  for (i = 1; i < 4; i++)
    {
      float x = poly[i * 4 + 0], y = poly[i * 4 + 1];

      if (x < bounds.x_1)
        bounds.x_1 = x;
      if (y < bounds.y_1)
        bounds.y_1 = y;
      if (x > bounds.x_2)
        bounds.x_2 = x;
      if (y > bounds.y_2)
        bounds.y_2 = y;
    }

  /* Search backwards through the list of lists for a matching
     pipeline */
  for (batch_index = journal->batches->len - 1;
       batch_index >= 0;
       batch_index--)
    {
      batch = &g_array_index (journal->batches,
                              CoglJournalBatch, batch_index);

      /* If the list is using a matching pipeline then we can use it */
      if (_cogl_pipeline_equal (batch->pipeline,
                                pipeline,
                                (COGL_PIPELINE_STATE_ALL &
                                 ~COGL_PIPELINE_STATE_COLOR),
                                COGL_PIPELINE_LAYER_STATE_ALL,
                                0))
        /* We have a matching list so we can just append this entry */
        goto found_list;

      /* Any further lists will be painted behind this one. Therefore
         we can only continue searching if the new entry does not
         intersect the current list */
      if (_cogl_loose_region_intersects (&batch->region, &bounds))
        /* The new entry intersects the list so we can't paint behind
           this one and we'll have to start a new list */
        break;
    }

  batch_index = journal->batches->len;
  g_array_set_size (journal->batches,
                    journal->batches->len + 1);
  batch = &g_array_index (journal->batches,
                          CoglJournalBatch, batch_index);

  batch->pipeline = _cogl_pipeline_journal_ref (pipeline);
  _cogl_loose_region_init (&batch->region);
  COGL_TAILQ_INIT (&batch->entries);

found_list:

  _cogl_loose_region_add_rectangle (&batch->region, &bounds);

  COGL_TAILQ_INSERT_TAIL (&batch->entries, entry, batch);
}

void
_cogl_journal_log_quad (CoglJournal  *journal,
                        const float  *position,
                        CoglPipeline *pipeline,
                        int           n_layers,
                        CoglTexture  *layer0_override_texture,
                        const float  *tex_coords,
                        unsigned int  tex_coords_len)
{
  guint32           disable_layers;
  CoglJournalEntry *entry;
  CoglPipeline     *source;
  CoglClipStack    *clip_stack;
  CoglPipelineFlushOptions flush_options;
  COGL_STATIC_TIMER (log_timer,
                     "Mainloop", /* parent */
                     "Journal Log",
                     "The time spent logging in the Cogl journal",
                     0 /* no application private data */);

  _COGL_GET_CONTEXT (ctx, NO_RETVAL);

  COGL_TIMER_START (_cogl_uprof_context, log_timer);

  /* The vertex data is logged into a separate array. The data needs
     to be copied into a vertex array before it's given to GL so we
     only store two vertices per quad and expand it to four while
     uploading. */

  /* We calculate the needed size of the vbo as we go because it
     depends on the number of layers in each entry and it's not easy
     calculate based on the length of the logged vertices array */
  journal->needed_vbo_len += GET_JOURNAL_VB_STRIDE_FOR_N_LAYERS (n_layers) * 4;

  entry = g_slice_alloc (GET_JOURNAL_ENTRY_SIZE_FOR_N_LAYERS (n_layers));

  _cogl_pipeline_get_colorubv (pipeline, entry->color);

  memcpy (entry->position, position, sizeof (float) * 4);
  memcpy (entry->tex_coords, tex_coords, sizeof (float) * 4 * n_layers);

  cogl_get_modelview_matrix (&entry->model_view);

  entry->n_layers = n_layers;

  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_JOURNAL)))
    {
      g_print ("Logged new quad:\n");
      _cogl_journal_dump_logged_quad (entry);
    }

  _cogl_journal_calculate_transformed_vertices (entry);

  source = pipeline;

  flush_options.flags = 0;
  if (G_UNLIKELY (cogl_pipeline_get_n_layers (pipeline) != n_layers))
    {
      disable_layers = (1 << n_layers) - 1;
      disable_layers = ~disable_layers;
      flush_options.disable_layers = disable_layers;
      flush_options.flags |= COGL_PIPELINE_FLUSH_DISABLE_MASK;
    }
  if (G_UNLIKELY (layer0_override_texture))
    {
      flush_options.flags |= COGL_PIPELINE_FLUSH_LAYER0_OVERRIDE;
      flush_options.layer0_override_texture = layer0_override_texture;
    }

  if (G_UNLIKELY (flush_options.flags))
    {
      source = cogl_pipeline_copy (pipeline);
      _cogl_pipeline_apply_overrides (source, &flush_options);
    }

  clip_stack = _cogl_framebuffer_get_clip_stack (cogl_get_draw_framebuffer ());
  entry->clip_stack = _cogl_clip_stack_ref (clip_stack);

  _cogl_journal_add_entry_to_batch (journal, source, entry);

  if (G_UNLIKELY (source != pipeline))
    cogl_handle_unref (source);

  journal->journal_len++;

  _cogl_pipeline_foreach_layer_internal (pipeline,
                                         add_framebuffer_deps_cb,
                                         cogl_get_draw_framebuffer ());

  /* XXX: It doesn't feel very nice that in this case we just assume
   * that the journal is associated with the current framebuffer. I
   * think a journal->framebuffer reference would seem nicer here but
   * the reason we don't have that currently is that it would
   * introduce a circular reference. */
  if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_BATCHING)))
    _cogl_framebuffer_flush_journal (cogl_get_draw_framebuffer ());

  COGL_TIMER_STOP (_cogl_uprof_context, log_timer);
}

static void
entry_to_screen_polygon (const CoglJournalEntry *entry,
                         float *poly)
{
  CoglMatrixStack *projection_stack;
  CoglMatrix projection;
  int i;
  float viewport[4];

  /* TODO: perhaps split the following out into a more generalized
   * _cogl_transform_points utility...
   */

  projection_stack =
    _cogl_framebuffer_get_projection_stack (cogl_get_draw_framebuffer ());
  _cogl_matrix_stack_get (projection_stack, &projection);

  cogl_matrix_project_points (&projection,
                              3, /* n_components */
                              sizeof (float) * 3, /* stride_in */
                              entry->transformed_verts, /* points_in */
                              /* strideout */
                              sizeof (float) * 4,
                              poly, /* points_out */
                              4 /* n_points */);

  cogl_framebuffer_get_viewport4fv (cogl_get_draw_framebuffer (), viewport);

/* Scale from OpenGL normalized device coordinates (ranging from -1 to 1)
 * to Cogl window/framebuffer coordinates (ranging from 0 to buffer-size) with
 * (0,0) being top left. */
#define VIEWPORT_TRANSFORM_X(x, vp_origin_x, vp_width) \
    (  ( ((x) + 1.0) * ((vp_width) / 2.0) ) + (vp_origin_x)  )
/* Note: for Y we first flip all coordinates around the X axis while in
 * normalized device coodinates */
#define VIEWPORT_TRANSFORM_Y(y, vp_origin_y, vp_height) \
    (  ( ((-(y)) + 1.0) * ((vp_height) / 2.0) ) + (vp_origin_y)  )

  /* Scale from normalized device coordinates (in range [-1,1]) to
   * window coordinates ranging [0,window-size] ... */
  for (i = 0; i < 4; i++)
    {
      float w = poly[4 * i + 3];

      /* Perform perspective division */
      poly[4 * i] /= w;
      poly[4 * i + 1] /= w;

      /* Apply viewport transform */
      poly[4 * i] = VIEWPORT_TRANSFORM_X (poly[4 * i],
                                          viewport[0], viewport[2]);
      poly[4 * i + 1] = VIEWPORT_TRANSFORM_Y (poly[4 * i + 1],
                                              viewport[1], viewport[3]);
    }

#undef VIEWPORT_TRANSFORM_X
#undef VIEWPORT_TRANSFORM_Y
}

static gboolean
try_checking_point_hits_entry_after_clipping (CoglPipeline *pipeline,
                                              CoglJournalEntry *entry,
                                              float x,
                                              float y,
                                              gboolean *hit)
{
  gboolean can_software_clip = TRUE;
  gboolean needs_software_clip = FALSE;
  CoglClipStack *clip_entry;

  *hit = TRUE;

  /* Verify that all of the clip stack entries are simple rectangle
   * clips */
  for (clip_entry = entry->clip_stack;
       clip_entry;
       clip_entry = clip_entry->parent)
    {
      if (x < clip_entry->bounds_x0 ||
          x >= clip_entry->bounds_x1 ||
          y < clip_entry->bounds_y0 ||
          y >= clip_entry->bounds_y1)
        {
          *hit = FALSE;
          return TRUE;
        }

      if (clip_entry->type == COGL_CLIP_STACK_WINDOW_RECT)
        {
          /* XXX: technically we could still run the software clip in
           * this case because for our purposes we know this clip
           * can be ignored now, but [can_]sofware_clip_entry() doesn't
           * know this and will bail out. */
          can_software_clip = FALSE;
        }
      else if (clip_entry->type == COGL_CLIP_STACK_RECT)
        {
          CoglClipStackRect *rect_entry = (CoglClipStackRect *)entry;

          if (rect_entry->can_be_scissor == FALSE)
            needs_software_clip = TRUE;
          /* If can_be_scissor is TRUE then we know it's screen
           * aligned and the hit test we did above has determined
           * that we are inside this clip. */
        }
      else
        return FALSE;
    }

  if (needs_software_clip)
    {
      ClipBounds clip_bounds;
      float poly[16];

      if (!can_software_clip)
        return FALSE;

      if (!can_software_clip_entry (pipeline, entry, NULL,
                                    entry->clip_stack, &clip_bounds))
        return FALSE;

      software_clip_entry (entry, &clip_bounds);
      entry_to_screen_polygon (entry, poly);

      *hit = _cogl_util_point_in_screen_poly (x, y, poly, sizeof (float) * 4, 4);
      return TRUE;
    }

  return TRUE;
}

gboolean
_cogl_journal_try_read_pixel (CoglJournal *journal,
                              int x,
                              int y,
                              CoglPixelFormat format,
                              guint8 *pixel,
                              gboolean *found_intersection)
{
  CoglJournalIter iter;
  int i;

  _COGL_GET_CONTEXT (ctx, FALSE);

  /* XXX: this number has been plucked out of thin air, but the idea
   * is that if so many pixels are being read from the same un-changed
   * journal than we expect that it will be more efficient to fail
   * here so we end up flushing and rendering the journal so that
   * further reads can directly read from the framebuffer. There will
   * be a bit more lag to flush the render but if there are going to
   * continue being lots of arbitrary single pixel reads they will end
   * up faster in the end. */
  if (journal->fast_read_pixel_count > 50)
    return FALSE;

  if (format != COGL_PIXEL_FORMAT_RGBA_8888_PRE &&
      format != COGL_PIXEL_FORMAT_RGBA_8888)
    return FALSE;

  *found_intersection = FALSE;

  /* The journal iterators don't work if the journal is empty */
  if (journal->journal_len <= 0)
    goto success;

  /* NB: The most recently added journal entry is the last entry, and
   * assuming this is a simple scene only comprised of opaque coloured
   * rectangles with no special pipelines involved (e.g. enabling
   * depth testing) then we can assume painter's algorithm for the
   * entries and so our fast read-pixel just needs to walk backwards
   * through the journal entries trying to intersect each entry with
   * the given point of interest. */
  for (i = 0, _cogl_journal_iterator_init_reverse (journal, &iter);
       i < journal->journal_len;
       i++, _cogl_journal_iterator_previous (journal, &iter))
    {
      CoglPipeline *pipeline;
      float poly[16];

      entry_to_screen_polygon (iter.entry, poly);

      if (!_cogl_util_point_in_screen_poly (x, y, poly, sizeof (float) * 4, 4))
        continue;

      pipeline = g_array_index (journal->batches,
                                CoglJournalBatch,
                                iter.batch_num).pipeline;

      /* FIXME: the journal should have a back pointer to the
       * associated framebuffer, because it should be possible to read
       * a pixel from arbitrary framebuffers without needing to
       * internally call _cogl_push/pop_framebuffer.
       */
      if (iter.entry->clip_stack)
        {
          gboolean hit;

          if (!try_checking_point_hits_entry_after_clipping (pipeline,
                                                             iter.entry,
                                                             x, y, &hit))
            return FALSE; /* hit couldn't be determined */

          if (!hit)
            continue;
        }

      *found_intersection = TRUE;

      /* If we find that the rectangle the point of interest
       * intersects has any state more complex than a constant opaque
       * color then we bail out. */
      if (!_cogl_pipeline_equal (ctx->opaque_color_pipeline,
                                 pipeline,
                                 (COGL_PIPELINE_STATE_ALL &
                                  ~COGL_PIPELINE_STATE_COLOR),
                                 COGL_PIPELINE_LAYER_STATE_ALL,
                                 0))
        return FALSE;


      /* we currently only care about cases where the premultiplied or
       * unpremultipled colors are equivalent... */
      if (iter.entry->color[3] != 0xff)
        return FALSE;

      pixel[0] = iter.entry->color[0];
      pixel[1] = iter.entry->color[1];
      pixel[2] = iter.entry->color[2];
      pixel[3] = iter.entry->color[3];

      goto success;
    }

success:
  journal->fast_read_pixel_count++;
  return TRUE;
}