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
|
/* $Id: renderspu_cocoa_helper.m $ */
/** @file
* VirtualBox OpenGL Cocoa Window System Helper Implementation.
*/
/*
* Copyright (C) 2009-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include "renderspu_cocoa_helper.h"
#import <Cocoa/Cocoa.h>
#undef PVM
#include "chromium.h" /* For the visual bits of chromium */
#include <iprt/thread.h>
#include <iprt/string.h>
#include <iprt/mem.h>
#include <iprt/time.h>
#include <iprt/assert.h>
#include <cr_vreg.h>
#include <cr_error.h>
#include <cr_blitter.h>
#ifdef VBOX_WITH_CRDUMPER_THUMBNAIL
# include <cr_pixeldata.h>
#endif
#include "renderspu.h"
/** @page pg_opengl_cocoa OpenGL - Cocoa Window System Helper
*
* How this works:
* In general it is not so easy like on the other platforms, cause Cocoa
* doesn't support any clipping of already painted stuff. In Mac OS X there is
* the concept of translucent canvas's e.g. windows and there it is just
* painted what should be visible to the user. Unfortunately this isn't the
* concept of chromium. Therefor I reroute all OpenGL operation from the guest
* to a frame buffer object (FBO). This is a OpenGL extension, which is
* supported by all OS X versions we support (AFAIC tell). Of course the guest
* doesn't know that and we have to make sure that the OpenGL state always is
* in the right state to paint into the FBO and not to the front/back buffer.
* Several functions below (like cocoaBindFramebufferEXT, cocoaGetIntegerv,
* ...) doing this. When a swap or finish is triggered by the guest, the
* content (which is already bound to an texture) is painted on the screen
* within a separate OpenGL context. This allows the usage of the same
* resources (texture ids, buffers ...) but at the same time having an
* different internal OpenGL state. Another advantage is that we can paint a
* thumbnail of the current output in a much more smaller (GPU accelerated
* scale) version on a third context and use glReadPixels to get the actual
* data. glReadPixels is a very slow operation, but as we just use a much more
* smaller image, we can handle it (anyway this is only done 5 times per
* second).
*
* Other things to know:
* - If the guest request double buffering, we have to make sure there are two
* buffers. We use the same FBO with 2 color attachments. Also glDrawBuffer
* and glReadBuffer is intercepted to make sure it is painted/read to/from
* the correct buffers. On swap our buffers are swapped and not the
* front/back buffer.
* - If the guest request a depth/stencil buffer, a combined render buffer for
* this is created.
* - If the size of the guest OpenGL window changes, all FBO's, textures, ...
* need to be recreated.
* - We need to track any changes to the parent window
* (create/destroy/move/resize). The various classes like OverlayHelperView,
* OverlayWindow, ... are there for.
* - The HGCM service runs on a other thread than the Main GUI. Keeps this
* always in mind (see e.g. performSelectorOnMainThread in renderFBOToView)
* - We make heavy use of late binding. We can not be sure that the GUI (or any
* other third party GUI), overwrite our NSOpenGLContext. So we always ask if
* this is our own one, before use. Really neat concept of Objective-C/Cocoa
* ;)
*/
/* Debug macros */
#define FBO 1 /* Disable this to see how the output is without the FBO in the middle of the processing chain. */
#if 0
# define CR_RENDER_FORCE_PRESENT_MAIN_THREAD /* force present schedule to main thread */
# define SHOW_WINDOW_BACKGROUND 1 /* Define this to see the window background even if the window is clipped */
# define DEBUG_VERBOSE /* Define this to get some debug info about the messages flow. */
#endif
#ifdef DEBUG_misha
# define DEBUG_MSG(text) \
printf text
# define DEBUG_WARN(text) do { \
crWarning text ; \
Assert(0); \
} while (0)
#else
# define DEBUG_MSG(text) \
do {} while (0)
# define DEBUG_WARN(text) do { \
crWarning text ; \
} while (0)
#endif
#ifdef DEBUG_VERBOSE
# define DEBUG_MSG_1(text) \
DEBUG_MSG(text)
#else
# define DEBUG_MSG_1(text) \
do {} while (0)
#endif
#ifdef DEBUG_poetzsch
# define CHECK_GL_ERROR()\
do \
{ \
checkGLError(__FILE__, __LINE__); \
}while (0);
static void checkGLError(char *file, int line)
{
GLenum g = glGetError();
if (g != GL_NO_ERROR)
{
char *errStr;
switch (g)
{
case GL_INVALID_ENUM: errStr = RTStrDup("GL_INVALID_ENUM"); break;
case GL_INVALID_VALUE: errStr = RTStrDup("GL_INVALID_VALUE"); break;
case GL_INVALID_OPERATION: errStr = RTStrDup("GL_INVALID_OPERATION"); break;
case GL_STACK_OVERFLOW: errStr = RTStrDup("GL_STACK_OVERFLOW"); break;
case GL_STACK_UNDERFLOW: errStr = RTStrDup("GL_STACK_UNDERFLOW"); break;
case GL_OUT_OF_MEMORY: errStr = RTStrDup("GL_OUT_OF_MEMORY"); break;
case GL_TABLE_TOO_LARGE: errStr = RTStrDup("GL_TABLE_TOO_LARGE"); break;
default: errStr = RTStrDup("UNKNOWN"); break;
}
DEBUG_MSG(("%s:%d: glError %d (%s)\n", file, line, g, errStr));
RTMemFree(errStr);
}
}
#else
# define CHECK_GL_ERROR()\
do {} while (0)
#endif
#define GL_SAVE_STATE \
do \
{ \
glPushAttrib(GL_ALL_ATTRIB_BITS); \
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS); \
glMatrixMode(GL_PROJECTION); \
glPushMatrix(); \
glMatrixMode(GL_TEXTURE); \
glPushMatrix(); \
glMatrixMode(GL_COLOR); \
glPushMatrix(); \
glMatrixMode(GL_MODELVIEW); \
glPushMatrix(); \
} \
while(0);
#define GL_RESTORE_STATE \
do \
{ \
glMatrixMode(GL_MODELVIEW); \
glPopMatrix(); \
glMatrixMode(GL_COLOR); \
glPopMatrix(); \
glMatrixMode(GL_TEXTURE); \
glPopMatrix(); \
glMatrixMode(GL_PROJECTION); \
glPopMatrix(); \
glPopClientAttrib(); \
glPopAttrib(); \
} \
while(0);
static NSOpenGLContext * vboxCtxGetCurrent()
{
GET_CONTEXT(pCtxInfo);
if (pCtxInfo)
{
Assert(pCtxInfo->context);
return pCtxInfo->context;
}
return nil;
}
static bool vboxCtxSyncCurrentInfo()
{
GET_CONTEXT(pCtxInfo);
NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
NSView *pView = pCtx ? [pCtx view] : nil;
bool fAdjusted = false;
if (pCtxInfo)
{
WindowInfo *pWinInfo = pCtxInfo->currentWindow;
Assert(pWinInfo);
if (pCtxInfo->context != pCtx
|| pWinInfo->window != pView)
{
renderspu_SystemMakeCurrent(pWinInfo, 0, pCtxInfo);
fAdjusted = true;
}
}
else
{
if (pCtx)
{
[NSOpenGLContext clearCurrentContext];
fAdjusted = true;
}
}
return fAdjusted;
}
typedef struct VBOX_CR_RENDER_CTX_INFO
{
bool fIsValid;
NSOpenGLContext *pCtx;
NSView *pView;
} VBOX_CR_RENDER_CTX_INFO, *PVBOX_CR_RENDER_CTX_INFO;
static void vboxCtxEnter(NSOpenGLContext*pCtx, PVBOX_CR_RENDER_CTX_INFO pCtxInfo)
{
NSOpenGLContext *pOldCtx = vboxCtxGetCurrent();
NSView *pOldView = (pOldCtx ? [pOldCtx view] : nil);
NSView *pView = [pCtx view];
bool fNeedCtxSwitch = (pOldCtx != pCtx || pOldView != pView);
Assert(pCtx);
// Assert(pOldCtx == m_pGLCtx);
// Assert(pOldView == self);
// Assert(fNeedCtxSwitch);
if (fNeedCtxSwitch)
{
if(pOldCtx != nil)
glFlush();
[pCtx makeCurrentContext];
pCtxInfo->fIsValid = true;
pCtxInfo->pCtx = pOldCtx;
pCtxInfo->pView = pView;
}
else
{
pCtxInfo->fIsValid = false;
}
}
static void vboxCtxLeave(PVBOX_CR_RENDER_CTX_INFO pCtxInfo)
{
if (pCtxInfo->fIsValid)
{
NSOpenGLContext *pOldCtx = pCtxInfo->pCtx;
NSView *pOldView = pCtxInfo->pView;
glFlush();
if (pOldCtx != nil)
{
if ([pOldCtx view] != pOldView)
{
[pOldCtx setView: pOldView];
}
[pOldCtx makeCurrentContext];
#ifdef DEBUG
{
NSOpenGLContext *pTstOldCtx = [NSOpenGLContext currentContext];
NSView *pTstOldView = (pTstOldCtx ? [pTstOldCtx view] : nil);
Assert(pTstOldCtx == pOldCtx);
Assert(pTstOldView == pOldView);
}
#endif
}
else
{
[NSOpenGLContext clearCurrentContext];
}
}
}
/** Custom OpenGL context class.
*
* This implementation doesn't allow to set a view to the
* context, but save the view for later use. Also it saves a copy of the
* pixel format used to create that context for later use. */
@interface OverlayOpenGLContext: NSOpenGLContext
{
@private
NSOpenGLPixelFormat *m_pPixelFormat;
NSView *m_pView;
}
- (NSOpenGLPixelFormat*)openGLPixelFormat;
@end
@class DockOverlayView;
/** The custom view class.
* This is the main class of the cocoa OpenGL implementation. It
* manages an frame buffer object for the rendering of the guest
* applications. The guest applications render in this frame buffer which
* is bind to an OpenGL texture. To display the guest content, an secondary
* shared OpenGL context of the main OpenGL context is created. The secondary
* context is marked as non opaque & the texture is displayed on an object
* which is composed out of the several visible region rectangles. */
@interface OverlayView: NSView
{
@private
NSView *m_pParentView;
NSWindow *m_pOverlayWin;
NSOpenGLContext *m_pGLCtx;
NSOpenGLContext *m_pSharedGLCtx;
RTTHREAD mThread;
GLuint m_FBOId;
/** The corresponding dock tile view of this OpenGL view & all helper
* members. */
DockOverlayView *m_DockTileView;
GLfloat m_FBOThumbScaleX;
GLfloat m_FBOThumbScaleY;
uint64_t m_uiDockUpdateTime;
/* For clipping */
GLint m_cClipRects;
GLint *m_paClipRects;
/* Position/Size tracking */
NSPoint m_Pos;
NSSize m_Size;
/** This is necessary for clipping on the root window */
NSRect m_RootRect;
float m_yInvRootOffset;
CR_BLITTER *m_pBlitter;
WindowInfo *m_pWinInfo;
bool m_fNeedViewportUpdate;
bool m_fNeedCtxUpdate;
bool m_fDataVisible;
bool m_fEverSized;
}
- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView winInfo:(WindowInfo*)pWinInfo;
- (void)setGLCtx:(NSOpenGLContext*)pCtx;
- (NSOpenGLContext*)glCtx;
- (void)setParentView: (NSView*)view;
- (NSView*)parentView;
- (void)setOverlayWin: (NSWindow*)win;
- (NSWindow*)overlayWin;
- (void)setPos:(NSPoint)pos;
- (NSPoint)pos;
- (bool)isEverSized;
- (void)setSize:(NSSize)size;
- (NSSize)size;
- (void)updateViewportCS;
- (void)vboxReshapePerform;
- (void)vboxReshapeOnResizePerform;
- (void)vboxReshapeOnReparentPerform;
- (void)createDockTile;
- (void)deleteDockTile;
- (void)makeCurrentFBO;
- (void)swapFBO;
- (void)vboxTryDraw;
- (void)vboxTryDrawUI;
- (void)vboxPresent:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
- (void)vboxPresentCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
- (void)vboxPresentToDockTileCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
- (void)vboxPresentToViewCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor;
- (void)presentComposition:(const VBOXVR_SCR_COMPOSITOR_ENTRY*)pChangedEntry;
- (void)vboxBlitterSyncWindow;
- (void)clearVisibleRegions;
- (void)setVisibleRegions:(GLint)cRects paRects:(const GLint*)paRects;
- (NSView*)dockTileScreen;
- (void)reshapeDockTile;
- (void)cleanupData;
@end
/** Helper view.
*
* This view is added as a sub view of the parent view to track
* main window changes. Whenever the main window is changed
* (which happens on fullscreen/seamless entry/exit) the overlay
* window is informed & can add them self as a child window
* again. */
@class OverlayWindow;
@interface OverlayHelperView: NSView
{
@private
OverlayWindow *m_pOverlayWindow;
}
-(id)initWithOverlayWindow:(OverlayWindow*)pOverlayWindow;
@end
/** Custom window class.
*
* This is the overlay window which contains our custom NSView.
* Its a direct child of the Qt Main window. It marks its background
* transparent & non opaque to make clipping possible. It also disable mouse
* events and handle frame change events of the parent view. */
@interface OverlayWindow: NSWindow
{
@private
NSView *m_pParentView;
OverlayView *m_pOverlayView;
OverlayHelperView *m_pOverlayHelperView;
NSThread *m_Thread;
}
- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView;
- (void)parentWindowFrameChanged:(NSNotification *)note;
- (void)parentWindowChanged:(NSWindow*)pWindow;
@end
@interface DockOverlayView: NSView
{
NSBitmapImageRep *m_ThumbBitmap;
NSImage *m_ThumbImage;
NSLock *m_Lock;
}
- (void)dealloc;
- (void)cleanup;
- (void)lock;
- (void)unlock;
- (void)setFrame:(NSRect)frame;
- (void)drawRect:(NSRect)aRect;
- (NSBitmapImageRep*)thumbBitmap;
- (NSImage*)thumbImage;
@end
@implementation DockOverlayView
- (id)init
{
self = [super init];
if (self)
{
/* We need a lock cause the thumb image could be accessed from the main
* thread when someone is calling display on the dock tile & from the
* OpenGL thread when the thumbnail is updated. */
m_Lock = [[NSLock alloc] init];
}
return self;
}
- (void)dealloc
{
[self cleanup];
[m_Lock release];
[super dealloc];
}
- (void)cleanup
{
if (m_ThumbImage != nil)
{
[m_ThumbImage release];
m_ThumbImage = nil;
}
if (m_ThumbBitmap != nil)
{
[m_ThumbBitmap release];
m_ThumbBitmap = nil;
}
}
- (void)lock
{
[m_Lock lock];
}
- (void)unlock
{
[m_Lock unlock];
}
- (void)setFrame:(NSRect)frame
{
[super setFrame:frame];
[self lock];
[self cleanup];
if ( frame.size.width > 0
&& frame.size.height > 0)
{
/* Create a buffer for our thumbnail image. Its in the size of this view. */
m_ThumbBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:frame.size.width
pixelsHigh:frame.size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:frame.size.width * 4
bitsPerPixel:8 * 4];
m_ThumbImage = [[NSImage alloc] initWithSize:[m_ThumbBitmap size]];
[m_ThumbImage addRepresentation:m_ThumbBitmap];
}
[self unlock];
}
- (BOOL)isFlipped
{
return YES;
}
- (void)drawRect:(NSRect)aRect
{
NSRect frame;
[self lock];
#ifdef SHOW_WINDOW_BACKGROUND
[[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7] set];
frame = [self frame];
[NSBezierPath fillRect:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
#endif /* SHOW_WINDOW_BACKGROUND */
if (m_ThumbImage != nil)
[m_ThumbImage drawAtPoint:NSMakePoint(0, 0) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[self unlock];
}
- (NSBitmapImageRep*)thumbBitmap
{
return m_ThumbBitmap;
}
- (NSImage*)thumbImage
{
return m_ThumbImage;
}
@end
/********************************************************************************
*
* OverlayOpenGLContext class implementation
*
********************************************************************************/
@implementation OverlayOpenGLContext
-(id)initWithFormat:(NSOpenGLPixelFormat*)format shareContext:(NSOpenGLContext*)share
{
m_pPixelFormat = NULL;
m_pView = NULL;
self = [super initWithFormat:format shareContext:share];
if (self)
m_pPixelFormat = format;
DEBUG_MSG(("OCTX(%p): init OverlayOpenGLContext\n", (void*)self));
return self;
}
- (void)dealloc
{
DEBUG_MSG(("OCTX(%p): dealloc OverlayOpenGLContext\n", (void*)self));
[m_pPixelFormat release];
[super dealloc];
}
-(bool)isDoubleBuffer
{
GLint val;
[m_pPixelFormat getValues:&val forAttribute:NSOpenGLPFADoubleBuffer forVirtualScreen:0];
return val == GL_TRUE ? YES : NO;
}
-(void)setView:(NSView*)view
{
DEBUG_MSG(("OCTX(%p): setView: new view: %p\n", (void*)self, (void*)view));
#if 1 /* def FBO */
m_pView = view;;
#else
[super setView: view];
#endif
}
-(NSView*)view
{
#if 1 /* def FBO */
return m_pView;
#else
return [super view];
#endif
}
-(void)clearDrawable
{
DEBUG_MSG(("OCTX(%p): clearDrawable\n", (void*)self));
m_pView = NULL;;
[super clearDrawable];
}
-(NSOpenGLPixelFormat*)openGLPixelFormat
{
return m_pPixelFormat;
}
@end
/********************************************************************************
*
* OverlayHelperView class implementation
*
********************************************************************************/
@implementation OverlayHelperView
-(id)initWithOverlayWindow:(OverlayWindow*)pOverlayWindow
{
self = [super initWithFrame:NSZeroRect];
m_pOverlayWindow = pOverlayWindow;
DEBUG_MSG(("OHVW(%p): init OverlayHelperView\n", (void*)self));
return self;
}
-(void)viewDidMoveToWindow
{
DEBUG_MSG(("OHVW(%p): viewDidMoveToWindow: new win: %p\n", (void*)self, (void*)[self window]));
[m_pOverlayWindow parentWindowChanged:[self window]];
}
@end
/********************************************************************************
*
* OverlayWindow class implementation
*
********************************************************************************/
@implementation OverlayWindow
- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView
{
NSWindow *pParentWin = nil;
if((self = [super initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]))
{
m_pParentView = pParentView;
m_pOverlayView = pOverlayView;
m_Thread = [NSThread currentThread];
[m_pOverlayView setOverlayWin: self];
m_pOverlayHelperView = [[OverlayHelperView alloc] initWithOverlayWindow:self];
/* Add the helper view as a child of the parent view to get notifications */
[pParentView addSubview:m_pOverlayHelperView];
/* Make sure this window is transparent */
#ifdef SHOW_WINDOW_BACKGROUND
/* For debugging */
[self setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7]];
#else
[self setBackgroundColor:[NSColor clearColor]];
#endif
[self setOpaque:NO];
[self setAlphaValue:.999];
/* Disable mouse events for this window */
[self setIgnoresMouseEvents:YES];
pParentWin = [m_pParentView window];
/* Initial set the position to the parents view top/left (Compiz fix). */
[self setFrameOrigin:
[pParentWin convertBaseToScreen:
[m_pParentView convertPoint:NSZeroPoint toView:nil]]];
/* Set the overlay view as our content view */
[self setContentView:m_pOverlayView];
/* Add ourself as a child to the parent views window. Note: this has to
* be done last so that everything else is setup in
* parentWindowChanged. */
[pParentWin addChildWindow:self ordered:NSWindowAbove];
}
DEBUG_MSG(("OWIN(%p): init OverlayWindow\n", (void*)self));
return self;
}
- (void)dealloc
{
DEBUG_MSG(("OWIN(%p): dealloc OverlayWindow\n", (void*)self));
[[NSNotificationCenter defaultCenter] removeObserver:self];
[m_pOverlayHelperView removeFromSuperview];
[m_pOverlayHelperView release];
[super dealloc];
}
- (void)parentWindowFrameChanged:(NSNotification*)pNote
{
DEBUG_MSG(("OWIN(%p): parentWindowFrameChanged\n", (void*)self));
/* Reposition this window with the help of the OverlayView. Perform the
* call in the OpenGL thread. */
/*
[m_pOverlayView performSelector:@selector(vboxReshapePerform) onThread:m_Thread withObject:nil waitUntilDone:YES];
*/
if ([m_pOverlayView isEverSized])
{
if([NSThread isMainThread])
[m_pOverlayView vboxReshapePerform];
else
[self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
}
}
- (void)parentWindowChanged:(NSWindow*)pWindow
{
DEBUG_MSG(("OWIN(%p): parentWindowChanged\n", (void*)self));
[[NSNotificationCenter defaultCenter] removeObserver:self];
if(pWindow != nil)
{
/* Ask to get notifications when our parent window frame changes. */
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(parentWindowFrameChanged:)
name:NSWindowDidResizeNotification
object:pWindow];
/* Add us self as child window */
[pWindow addChildWindow:self ordered:NSWindowAbove];
/* Reshape the overlay view after a short waiting time to let the main
* window resize itself properly. */
/*
[m_pOverlayView performSelector:@selector(vboxReshapePerform) withObject:nil afterDelay:0.2];
[NSTimer scheduledTimerWithTimeInterval:0.2 target:m_pOverlayView selector:@selector(vboxReshapePerform) userInfo:nil repeats:NO];
*/
if ([m_pOverlayView isEverSized])
{
if([NSThread isMainThread])
[m_pOverlayView vboxReshapePerform];
else
[self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
}
}
}
@end
/********************************************************************************
*
* OverlayView class implementation
*
********************************************************************************/
@implementation OverlayView
- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView winInfo:(WindowInfo*)pWinInfo
{
m_pParentView = pParentView;
/* Make some reasonable defaults */
m_pGLCtx = nil;
m_pSharedGLCtx = nil;
mThread = aThread;
m_FBOId = 0;
m_cClipRects = 0;
m_paClipRects = NULL;
m_Pos = NSZeroPoint;
m_Size = NSMakeSize(1, 1);
m_RootRect = NSMakeRect(0, 0, m_Size.width, m_Size.height);
m_yInvRootOffset = 0;
m_pBlitter = nil;
m_pWinInfo = pWinInfo;
m_fNeedViewportUpdate = true;
m_fNeedCtxUpdate = true;
m_fDataVisible = false;
m_fEverSized = false;
self = [super initWithFrame:frame];
DEBUG_MSG(("OVIW(%p): init OverlayView\n", (void*)self));
return self;
}
- (void)cleanupData
{
[self deleteDockTile];
[self setGLCtx:nil];
if (m_pSharedGLCtx)
{
if ([m_pSharedGLCtx view] == self)
[m_pSharedGLCtx clearDrawable];
[m_pSharedGLCtx release];
m_pSharedGLCtx = nil;
CrBltTerm(m_pBlitter);
RTMemFree(m_pBlitter);
m_pBlitter = nil;
}
[self clearVisibleRegions];
}
- (void)dealloc
{
DEBUG_MSG(("OVIW(%p): dealloc OverlayView\n", (void*)self));
[self cleanupData];
[super dealloc];
}
- (void)drawRect:(NSRect)aRect
{
[self vboxTryDrawUI];
}
- (void)setGLCtx:(NSOpenGLContext*)pCtx
{
DEBUG_MSG(("OVIW(%p): setGLCtx: new ctx: %p\n", (void*)self, (void*)pCtx));
if (m_pGLCtx == pCtx)
return;
/* ensure the context drawable is cleared to avoid holding a reference to inexistent view */
if (m_pGLCtx)
{
[m_pGLCtx clearDrawable];
[m_pGLCtx release];
/*[m_pGLCtx performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];*/
}
m_pGLCtx = pCtx;
if (pCtx)
[pCtx retain];
}
- (NSOpenGLContext*)glCtx
{
return m_pGLCtx;
}
- (NSView*)parentView
{
return m_pParentView;
}
- (void)setParentView:(NSView*)pView
{
DEBUG_MSG(("OVIW(%p): setParentView: new view: %p\n", (void*)self, (void*)pView));
m_pParentView = pView;
}
- (void)setOverlayWin:(NSWindow*)pWin
{
DEBUG_MSG(("OVIW(%p): setOverlayWin: new win: %p\n", (void*)self, (void*)pWin));
m_pOverlayWin = pWin;
}
- (NSWindow*)overlayWin
{
return m_pOverlayWin;
}
- (void)setPos:(NSPoint)pos
{
DEBUG_MSG(("OVIW(%p): setPos: new pos: %d, %d\n", (void*)self, (int)pos.x, (int)pos.y));
m_Pos = pos;
if (m_fEverSized)
[self performSelectorOnMainThread:@selector(vboxReshapePerform) withObject:nil waitUntilDone:NO];
/* we need to redwar on regions change, however the compositor now is cleared
* because all compositor&window data-related modifications are performed with compositor cleared
* the renderspu client will re-set the compositor after modifications are complete
* this way we indicate renderspu generic code not to ignore the empty compositor */
/* generally this should not be needed for setPos because compositor should not be zeroed with it,
* in any way setting this flag here should not hurt as it will be re-set on next present */
m_pWinInfo->fCompositorPresentEmpty = GL_TRUE;
}
- (NSPoint)pos
{
return m_Pos;
}
- (bool)isEverSized
{
return m_fEverSized;
}
- (void)setSize:(NSSize)size
{
NSOpenGLContext *pCurCtx;
NSView *pCurView;
m_Size = size;
m_fEverSized = true;
DEBUG_MSG(("OVIW(%p): setSize: new size: %dx%d\n", (void*)self, (int)size.width, (int)size.height));
[self performSelectorOnMainThread:@selector(vboxReshapeOnResizePerform) withObject:nil waitUntilDone:NO];
/* we need to redwar on regions change, however the compositor now is cleared
* because all compositor&window data-related modifications are performed with compositor cleared
* the renderspu client will re-set the compositor after modifications are complete
* this way we indicate renderspu generic code not to ignore the empty compositor */
/* generally this should not be needed for setSize because compositor should not be zeroed with it,
* in any way setting this flag here should not hurt as it will be re-set on next present */
m_pWinInfo->fCompositorPresentEmpty = GL_TRUE;
}
- (NSSize)size
{
return m_Size;
}
- (void)updateViewportCS
{
DEBUG_MSG(("OVIW(%p): updateViewport\n", (void*)self));
/* Update the viewport for our OpenGL view */
[m_pSharedGLCtx update];
[self vboxBlitterSyncWindow];
/* Clear background to transparent */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
- (void)vboxReshapeOnResizePerform
{
[self vboxReshapePerform];
[self createDockTile];
/* have to rebind GL_TEXTURE_RECTANGLE_ARB as m_FBOTexId could be changed in updateFBO call */
m_fNeedViewportUpdate = true;
#if 0
pCurCtx = [NSOpenGLContext currentContext];
if (pCurCtx && pCurCtx == m_pGLCtx && (pCurView = [pCurCtx view]) == self)
{
[m_pGLCtx update];
m_fNeedCtxUpdate = false;
}
else
{
/* do it in a lazy way */
m_fNeedCtxUpdate = true;
}
#endif
}
- (void)vboxReshapeOnReparentPerform
{
[self createDockTile];
}
- (void)vboxReshapePerform
{
NSRect parentFrame = NSZeroRect;
NSPoint parentPos = NSZeroPoint;
NSPoint childPos = NSZeroPoint;
NSRect childFrame = NSZeroRect;
NSRect newFrame = NSZeroRect;
DEBUG_MSG(("OVIW(%p): vboxReshapePerform\n", (void*)self));
parentFrame = [m_pParentView frame];
DEBUG_MSG(("FIXED parentFrame [%f:%f], [%f:%f]\n", parentFrame.origin.x, parentFrame.origin.y, parentFrame.size.width, parentFrame.size.height));
parentPos = parentFrame.origin;
parentPos.y += parentFrame.size.height;
DEBUG_MSG(("FIXED(view) parentPos [%f:%f]\n", parentPos.x, parentPos.y));
parentPos = [m_pParentView convertPoint:parentPos toView:nil];
DEBUG_MSG(("FIXED parentPos(win) [%f:%f]\n", parentPos.x, parentPos.y));
parentPos = [[m_pParentView window] convertBaseToScreen:parentPos];
DEBUG_MSG(("FIXED parentPos(screen) [%f:%f]\n", parentPos.x, parentPos.y));
parentFrame.origin = parentPos;
childPos = NSMakePoint(m_Pos.x, m_Pos.y + m_Size.height);
DEBUG_MSG(("FIXED(view) childPos [%f:%f]\n", childPos.x, childPos.y));
childPos = [m_pParentView convertPoint:childPos toView:nil];
DEBUG_MSG(("FIXED(win) childPos [%f:%f]\n", childPos.x, childPos.y));
childPos = [[m_pParentView window] convertBaseToScreen:childPos];
DEBUG_MSG(("FIXED childPos(screen) [%f:%f]\n", childPos.x, childPos.y));
childFrame = NSMakeRect(childPos.x, childPos.y, m_Size.width, m_Size.height);
DEBUG_MSG(("FIXED childFrame [%f:%f], [%f:%f]\n", childFrame.origin.x, childFrame.origin.y, childFrame.size.width, childFrame.size.height));
/* We have to make sure that the overlay window will not be displayed out
* of the parent window. So intersect both frames & use the result as the new
* frame for the window. */
newFrame = NSIntersectionRect(parentFrame, childFrame);
DEBUG_MSG(("[%#p]: parentFrame pos[%f : %f] size[%f : %f]\n",
(void*)self,
parentFrame.origin.x, parentFrame.origin.y,
parentFrame.size.width, parentFrame.size.height));
DEBUG_MSG(("[%#p]: childFrame pos[%f : %f] size[%f : %f]\n",
(void*)self,
childFrame.origin.x, childFrame.origin.y,
childFrame.size.width, childFrame.size.height));
DEBUG_MSG(("[%#p]: newFrame pos[%f : %f] size[%f : %f]\n",
(void*)self,
newFrame.origin.x, newFrame.origin.y,
newFrame.size.width, newFrame.size.height));
/* Later we have to correct the texture position in the case the window is
* out of the parents window frame. So save the shift values for later use. */
m_RootRect.origin.x = newFrame.origin.x - childFrame.origin.x;
m_RootRect.origin.y = childFrame.size.height + childFrame.origin.y - (newFrame.size.height + newFrame.origin.y);
m_RootRect.size = newFrame.size;
m_yInvRootOffset = newFrame.origin.y - childFrame.origin.y;
DEBUG_MSG(("[%#p]: m_RootRect pos[%f : %f] size[%f : %f]\n",
(void*)self,
m_RootRect.origin.x, m_RootRect.origin.y,
m_RootRect.size.width, m_RootRect.size.height));
/*
NSScrollView *pScrollView = [[[m_pParentView window] contentView] enclosingScrollView];
if (pScrollView)
{
NSRect scrollRect = [pScrollView documentVisibleRect];
NSRect scrollRect = [m_pParentView visibleRect];
printf ("sc rect: %d %d %d %d\n", (int) scrollRect.origin.x,(int) scrollRect.origin.y,(int) scrollRect.size.width,(int) scrollRect.size.height);
NSRect b = [[m_pParentView superview] bounds];
printf ("bound rect: %d %d %d %d\n", (int) b.origin.x,(int) b.origin.y,(int) b.size.width,(int) b.size.height);
newFrame.origin.x += scrollRect.origin.x;
newFrame.origin.y += scrollRect.origin.y;
}
*/
/* Set the new frame. */
[[self window] setFrame:newFrame display:YES];
/* Inform the dock tile view as well */
[self reshapeDockTile];
/* Make sure the context is updated according */
/* [self updateViewport]; */
if (m_pSharedGLCtx)
{
VBOX_CR_RENDER_CTX_INFO CtxInfo;
vboxCtxEnter(m_pSharedGLCtx, &CtxInfo);
[self updateViewportCS];
vboxCtxLeave(&CtxInfo);
}
}
- (void)createDockTile
{
NSView *pDockScreen = nil;
[self deleteDockTile];
/* Is there a dock tile preview enabled in the GUI? If so setup a
* additional thumbnail view for the dock tile. */
pDockScreen = [self dockTileScreen];
if (pDockScreen)
{
m_DockTileView = [[DockOverlayView alloc] init];
[self reshapeDockTile];
[pDockScreen addSubview:m_DockTileView];
}
}
- (void)deleteDockTile
{
if (m_DockTileView != nil)
{
[m_DockTileView removeFromSuperview];
[m_DockTileView release];
m_DockTileView = nil;
}
}
- (void)makeCurrentFBO
{
DEBUG_MSG(("OVIW(%p): makeCurrentFBO\n", (void*)self));
if (m_pGLCtx)
{
if ([m_pGLCtx view] != self)
{
/* We change the active view, so flush first */
if([NSOpenGLContext currentContext] != 0)
glFlush();
[m_pGLCtx setView: self];
CHECK_GL_ERROR();
}
/*
if ([NSOpenGLContext currentContext] != m_pGLCtx)
*/
{
[m_pGLCtx makeCurrentContext];
CHECK_GL_ERROR();
if (m_fNeedCtxUpdate == true)
{
[m_pGLCtx update];
m_fNeedCtxUpdate = false;
}
}
if (!m_FBOId)
{
glGenFramebuffersEXT(1, &m_FBOId);
Assert(m_FBOId);
}
}
}
- (bool)vboxSharedCtxCreate
{
if (m_pSharedGLCtx)
return true;
Assert(!m_pBlitter);
m_pBlitter = RTMemAlloc(sizeof (*m_pBlitter));
if (!m_pBlitter)
{
DEBUG_WARN(("m_pBlitter allocation failed"));
return false;
}
int rc = CrBltInit(m_pBlitter, NULL, false, false, &render_spu.GlobalShaders, &render_spu.blitterDispatch);
if (RT_SUCCESS(rc))
{
DEBUG_MSG(("blitter created successfully for view 0x%p\n", (void*)self));
}
else
{
DEBUG_WARN(("CrBltInit failed, rc %d", rc));
RTMemFree(m_pBlitter);
m_pBlitter = NULL;
return false;
}
GLint opaque = 0;
/* Create a shared context out of the main context. Use the same pixel format. */
NSOpenGLContext *pSharedGLCtx = [[NSOpenGLContext alloc] initWithFormat:[(OverlayOpenGLContext*)m_pGLCtx openGLPixelFormat] shareContext:m_pGLCtx];
/* Set the new context as non opaque */
[pSharedGLCtx setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
/* Set this view as the drawable for the new context */
[pSharedGLCtx setView: self];
m_fNeedViewportUpdate = true;
m_pSharedGLCtx = pSharedGLCtx;
return true;
}
- (void)vboxTryDraw
{
glFlush();
/* issue to the gui thread */
[self setNeedsDisplay:YES];
}
- (void)vboxTryDrawUI
{
const VBOXVR_SCR_COMPOSITOR *pCompositor = renderspuVBoxCompositorAcquire(m_pWinInfo);
if (!m_fDataVisible && !pCompositor)
return;
VBOXVR_SCR_COMPOSITOR TmpCompositor;
if (pCompositor)
{
if (!m_pSharedGLCtx)
{
Assert(!m_fDataVisible);
renderspuVBoxCompositorRelease(m_pWinInfo);
if (![self vboxSharedCtxCreate])
{
DEBUG_WARN(("vboxSharedCtxCreate failed\n"));
return;
}
Assert(m_pSharedGLCtx);
pCompositor = renderspuVBoxCompositorAcquire(m_pWinInfo);
Assert(!m_fDataVisible);
if (!pCompositor)
return;
}
}
else
{
CrVrScrCompositorInit(&TmpCompositor, NULL);
pCompositor = &TmpCompositor;
}
if ([self lockFocusIfCanDraw])
{
[self vboxPresent:pCompositor];
if (pCompositor != &TmpCompositor)
renderspuVBoxCompositorRelease(m_pWinInfo);
[self unlockFocus];
}
else
{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(vboxTryDrawUI) userInfo:nil repeats:NO];
}
}
- (void)swapFBO
{
[m_pGLCtx flushBuffer];
}
- (void)vboxPresent:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
{
VBOX_CR_RENDER_CTX_INFO CtxInfo;
DEBUG_MSG(("OVIW(%p): renderFBOToView\n", (void*)self));
Assert(pCompositor);
vboxCtxEnter(m_pSharedGLCtx, &CtxInfo);
[self vboxPresentCS:pCompositor];
vboxCtxLeave(&CtxInfo);
}
- (void)vboxPresentCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
{
{
if ([m_pSharedGLCtx view] != self)
{
DEBUG_MSG(("OVIW(%p): not current view of shared ctx! Switching ...\n", (void*)self));
[m_pSharedGLCtx setView: self];
m_fNeedViewportUpdate = true;
}
if (m_fNeedViewportUpdate)
{
[self updateViewportCS];
m_fNeedViewportUpdate = false;
}
/* Render FBO content to the dock tile when necessary. */
[self vboxPresentToDockTileCS:pCompositor];
/* change to #if 0 to see thumbnail image */
#if 1
[self vboxPresentToViewCS:pCompositor];
#else
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
[m_pSharedGLCtx flushBuffer];
#endif
}
}
DECLINLINE(void) vboxNSRectToRect(const NSRect *pR, RTRECT *pRect)
{
pRect->xLeft = (int)pR->origin.x;
pRect->yTop = (int)pR->origin.y;
pRect->xRight = (int)(pR->origin.x + pR->size.width);
pRect->yBottom = (int)(pR->origin.y + pR->size.height);
}
DECLINLINE(void) vboxNSRectToRectUnstretched(const NSRect *pR, RTRECT *pRect, float xStretch, float yStretch)
{
pRect->xLeft = (int)(pR->origin.x / xStretch);
pRect->yTop = (int)(pR->origin.y / yStretch);
pRect->xRight = (int)((pR->origin.x + pR->size.width) / xStretch);
pRect->yBottom = (int)((pR->origin.y + pR->size.height) / yStretch);
}
DECLINLINE(void) vboxNSRectToRectStretched(const NSRect *pR, RTRECT *pRect, float xStretch, float yStretch)
{
pRect->xLeft = (int)(pR->origin.x * xStretch);
pRect->yTop = (int)(pR->origin.y * yStretch);
pRect->xRight = (int)((pR->origin.x + pR->size.width) * xStretch);
pRect->yBottom = (int)((pR->origin.y + pR->size.height) * yStretch);
}
- (void)vboxPresentToViewCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
{
NSRect r = [self frame];
float xStretch, yStretch;
DEBUG_MSG(("OVIW(%p): rF2V frame: [%i, %i, %i, %i]\n", (void*)self, (int)r.origin.x, (int)r.origin.y, (int)r.size.width, (int)r.size.height));
#if 1 /* Set to 0 to see the docktile instead of the real output */
VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR CIter;
const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
CrVrScrCompositorConstIterInit(pCompositor, &CIter);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_BACK);
/* Clear background to transparent */
glClear(GL_COLOR_BUFFER_BIT);
m_fDataVisible = false;
CrVrScrCompositorGetStretching(pCompositor, &xStretch, &yStretch);
while ((pEntry = CrVrScrCompositorConstIterNext(&CIter)) != NULL)
{
uint32_t cRegions;
const RTRECT *paSrcRegions, *paDstRegions;
int rc = CrVrScrCompositorEntryRegionsGet(pCompositor, pEntry, &cRegions, &paSrcRegions, &paDstRegions, NULL);
uint32_t fFlags = CrVrScrCompositorEntryFlagsCombinedGet(pCompositor, pEntry);
if (RT_SUCCESS(rc))
{
uint32_t i;
int rc = CrBltEnter(m_pBlitter);
if (RT_SUCCESS(rc))
{
for (i = 0; i < cRegions; ++i)
{
const RTRECT * pSrcRect = &paSrcRegions[i];
const RTRECT * pDstRect = &paDstRegions[i];
RTRECT DstRect, RestrictDstRect;
RTRECT SrcRect, RestrictSrcRect;
vboxNSRectToRect(&m_RootRect, &RestrictDstRect);
VBoxRectIntersected(&RestrictDstRect, pDstRect, &DstRect);
if (VBoxRectIsZero(&DstRect))
continue;
VBoxRectTranslate(&DstRect, -RestrictDstRect.xLeft, -RestrictDstRect.yTop);
vboxNSRectToRectUnstretched(&m_RootRect, &RestrictSrcRect, xStretch, yStretch);
VBoxRectTranslate(&RestrictSrcRect, -CrVrScrCompositorEntryRectGet(pEntry)->xLeft, -CrVrScrCompositorEntryRectGet(pEntry)->yTop);
VBoxRectIntersected(&RestrictSrcRect, pSrcRect, &SrcRect);
if (VBoxRectIsZero(&SrcRect))
continue;
pSrcRect = &SrcRect;
pDstRect = &DstRect;
const CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
CrBltBlitTexMural(m_pBlitter, true, CrTdTexGet(pTexData), pSrcRect, pDstRect, 1, fFlags | CRBLT_F_NOALPHA);
m_fDataVisible = true;
}
CrBltLeave(m_pBlitter);
}
else
{
DEBUG_WARN(("CrBltEnter failed rc %d", rc));
}
}
else
{
Assert(0);
DEBUG_MSG_1(("BlitStretched: CrVrScrCompositorEntryRegionsGet failed rc %d\n", rc));
}
}
#endif
/*
glFinish();
*/
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
[m_pSharedGLCtx flushBuffer];
}
- (void)presentComposition:(const VBOXVR_SCR_COMPOSITOR_ENTRY*)pChangedEntry
{
[self vboxTryDraw];
}
- (void)vboxBlitterSyncWindow
{
CR_BLITTER_WINDOW WinInfo;
NSRect r;
if (!m_pBlitter)
return;
memset(&WinInfo, 0, sizeof (WinInfo));
r = [self frame];
WinInfo.width = r.size.width;
WinInfo.height = r.size.height;
Assert(WinInfo.width == m_RootRect.size.width);
Assert(WinInfo.height == m_RootRect.size.height);
/*CrBltMuralSetCurrentInfo(m_pBlitter, NULL);*/
CrBltMuralSetCurrentInfo(m_pBlitter, &WinInfo);
CrBltCheckUpdateViewport(m_pBlitter);
}
#ifdef VBOX_WITH_CRDUMPER_THUMBNAIL
static int g_cVBoxTgaCtr = 0;
#endif
- (void)vboxPresentToDockTileCS:(const VBOXVR_SCR_COMPOSITOR*)pCompositor
{
NSRect r = [self frame];
NSRect rr = NSZeroRect;
GLint i = 0;
NSDockTile *pDT = nil;
float xStretch, yStretch;
if ([m_DockTileView thumbBitmap] != nil)
{
/* Only update after at least 200 ms, cause glReadPixels is
* heavy performance wise. */
uint64_t uiNewTime = RTTimeMilliTS();
VBOXVR_SCR_COMPOSITOR_CONST_ITERATOR CIter;
const VBOXVR_SCR_COMPOSITOR_ENTRY *pEntry;
if (uiNewTime - m_uiDockUpdateTime > 200)
{
m_uiDockUpdateTime = uiNewTime;
#if 0
/* todo: check this for optimization */
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myTextureName);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_STORAGE_HINT_APPLE,
GL_STORAGE_SHARED_APPLE);
glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
sizex, sizey, 0, GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV, myImagePtr);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,
0, 0, 0, 0, 0, image_width, image_height);
glFlush();
/* Do other work processing here, using a double or triple buffer */
glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
#endif
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_BACK);
/* Clear background to transparent */
glClear(GL_COLOR_BUFFER_BIT);
rr = [m_DockTileView frame];
CrVrScrCompositorGetStretching(pCompositor, &xStretch, &yStretch);
CrVrScrCompositorConstIterInit(pCompositor, &CIter);
while ((pEntry = CrVrScrCompositorConstIterNext(&CIter)) != NULL)
{
uint32_t cRegions;
const RTRECT *paSrcRegions, *paDstRegions;
int rc = CrVrScrCompositorEntryRegionsGet(pCompositor, pEntry, &cRegions, &paSrcRegions, &paDstRegions, NULL);
uint32_t fFlags = CrVrScrCompositorEntryFlagsCombinedGet(pCompositor, pEntry);
if (RT_SUCCESS(rc))
{
uint32_t i;
int rc = CrBltEnter(m_pBlitter);
if (RT_SUCCESS(rc))
{
for (i = 0; i < cRegions; ++i)
{
const RTRECT * pSrcRect = &paSrcRegions[i];
const RTRECT * pDstRect = &paDstRegions[i];
RTRECT SrcRect, DstRect, RestrictSrcRect, RestrictDstRect;
vboxNSRectToRect(&m_RootRect, &RestrictDstRect);
VBoxRectIntersected(&RestrictDstRect, pDstRect, &DstRect);
VBoxRectTranslate(&DstRect, -RestrictDstRect.xLeft, -RestrictDstRect.yTop);
VBoxRectScale(&DstRect, m_FBOThumbScaleX, m_FBOThumbScaleY);
if (VBoxRectIsZero(&DstRect))
continue;
vboxNSRectToRectUnstretched(&m_RootRect, &RestrictSrcRect, xStretch, yStretch);
VBoxRectTranslate(&RestrictSrcRect, -CrVrScrCompositorEntryRectGet(pEntry)->xLeft, -CrVrScrCompositorEntryRectGet(pEntry)->yTop);
VBoxRectIntersected(&RestrictSrcRect, pSrcRect, &SrcRect);
if (VBoxRectIsZero(&SrcRect))
continue;
pSrcRect = &SrcRect;
pDstRect = &DstRect;
const CR_TEXDATA *pTexData = CrVrScrCompositorEntryTexGet(pEntry);
CrBltBlitTexMural(m_pBlitter, true, CrTdTexGet(pTexData), pSrcRect, pDstRect, 1, fFlags);
}
CrBltLeave(m_pBlitter);
}
else
{
DEBUG_WARN(("CrBltEnter failed rc %d", rc));
}
}
else
{
Assert(0);
DEBUG_MSG_1(("BlitStretched: CrVrScrCompositorEntryRegionsGet failed rc %d\n", rc));
}
}
glFinish();
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
glReadBuffer(GL_BACK);
/* Here the magic of reading the FBO content in our own buffer
* happens. We have to lock this access, in the case the dock
* is updated currently. */
[m_DockTileView lock];
glReadPixels(0, m_RootRect.size.height - rr.size.height, rr.size.width, rr.size.height,
GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8,
[[m_DockTileView thumbBitmap] bitmapData]);
[m_DockTileView unlock];
#ifdef VBOX_WITH_CRDUMPER_THUMBNAIL
++g_cVBoxTgaCtr;
crDumpNamedTGAF((GLint)rr.size.width, (GLint)rr.size.height,
[[m_DockTileView thumbBitmap] bitmapData], "/Users/leo/vboxdumps/dump%d.tga", g_cVBoxTgaCtr);
#endif
pDT = [[NSApplication sharedApplication] dockTile];
/* Send a display message to the dock tile in the main thread */
[[[NSApplication sharedApplication] dockTile] performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:NO];
}
}
}
- (void)clearVisibleRegions
{
if(m_paClipRects)
{
RTMemFree(m_paClipRects);
m_paClipRects = NULL;
}
m_cClipRects = 0;
}
- (void)setVisibleRegions:(GLint)cRects paRects:(const GLint*)paRects
{
GLint cOldRects = m_cClipRects;
DEBUG_MSG_1(("OVIW(%p): setVisibleRegions: cRects=%d\n", (void*)self, cRects));
[self clearVisibleRegions];
if (cRects > 0)
{
#ifdef DEBUG_poetzsch
int i =0;
for (i = 0; i < cRects; ++i)
DEBUG_MSG_1(("OVIW(%p): setVisibleRegions: %d - %d %d %d %d\n", (void*)self, i, paRects[i * 4], paRects[i * 4 + 1], paRects[i * 4 + 2], paRects[i * 4 + 3]));
#endif
m_paClipRects = (GLint*)RTMemAlloc(sizeof(GLint) * 4 * cRects);
m_cClipRects = cRects;
memcpy(m_paClipRects, paRects, sizeof(GLint) * 4 * cRects);
}
/* we need to redwar on regions change, however the compositor now is cleared
* because all compositor&window data-related modifications are performed with compositor cleared
* the renderspu client will re-set the compositor after modifications are complete
* this way we indicate renderspu generic code not to ignore the empty compositor */
m_pWinInfo->fCompositorPresentEmpty = GL_TRUE;
}
- (NSView*)dockTileScreen
{
NSView *contentView = [[[NSApplication sharedApplication] dockTile] contentView];
NSView *screenContent = nil;
/* First try the new variant which checks if this window is within the
screen which is previewed in the dock. */
if ([contentView respondsToSelector:@selector(screenContentWithParentView:)])
screenContent = [contentView performSelector:@selector(screenContentWithParentView:) withObject:(id)m_pParentView];
/* If it fails, fall back to the old variant (VBox...) */
else if ([contentView respondsToSelector:@selector(screenContent)])
screenContent = [contentView performSelector:@selector(screenContent)];
return screenContent;
}
- (void)reshapeDockTile
{
NSRect newFrame = NSZeroRect;
NSView *pView = [self dockTileScreen];
if (pView != nil)
{
NSRect dockFrame = [pView frame];
/* todo: this is not correct, we should use framebuffer size here, while parent view frame size may differ in case of scrolling */
NSRect parentFrame = [m_pParentView frame];
m_FBOThumbScaleX = (float)dockFrame.size.width / parentFrame.size.width;
m_FBOThumbScaleY = (float)dockFrame.size.height / parentFrame.size.height;
newFrame = NSMakeRect((int)(m_Pos.x * m_FBOThumbScaleX), (int)(dockFrame.size.height - (m_Pos.y + m_Size.height - m_yInvRootOffset) * m_FBOThumbScaleY), (int)(m_Size.width * m_FBOThumbScaleX), (int)(m_Size.height * m_FBOThumbScaleY));
/*
NSRect newFrame = NSMakeRect ((int)roundf(m_Pos.x * m_FBOThumbScaleX), (int)roundf(dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (int)roundf(m_Size.width * m_FBOThumbScaleX), (int)roundf(m_Size.height * m_FBOThumbScaleY));
NSRect newFrame = NSMakeRect ((m_Pos.x * m_FBOThumbScaleX), (dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (m_Size.width * m_FBOThumbScaleX), (m_Size.height * m_FBOThumbScaleY));
printf ("%f %f %f %f - %f %f\n", newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height, m_Size.height, m_FBOThumbScaleY);
*/
[m_DockTileView setFrame: newFrame];
}
}
@end
/********************************************************************************
*
* OpenGL context management
*
********************************************************************************/
void cocoaGLCtxCreate(NativeNSOpenGLContextRef *ppCtx, GLbitfield fVisParams, NativeNSOpenGLContextRef pSharedCtx)
{
NSOpenGLPixelFormat *pFmt = nil;
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
NSOpenGLPixelFormatAttribute attribs[24] =
{
NSOpenGLPFAWindow,
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24
};
int i = 4;
if (fVisParams & CR_ALPHA_BIT)
{
DEBUG_MSG(("CR_ALPHA_BIT requested\n"));
attribs[i++] = NSOpenGLPFAAlphaSize;
attribs[i++] = 8;
}
if (fVisParams & CR_DEPTH_BIT)
{
DEBUG_MSG(("CR_DEPTH_BIT requested\n"));
attribs[i++] = NSOpenGLPFADepthSize;
attribs[i++] = 24;
}
if (fVisParams & CR_STENCIL_BIT)
{
DEBUG_MSG(("CR_STENCIL_BIT requested\n"));
attribs[i++] = NSOpenGLPFAStencilSize;
attribs[i++] = 8;
}
if (fVisParams & CR_ACCUM_BIT)
{
DEBUG_MSG(("CR_ACCUM_BIT requested\n"));
attribs[i++] = NSOpenGLPFAAccumSize;
if (fVisParams & CR_ALPHA_BIT)
attribs[i++] = 32;
else
attribs[i++] = 24;
}
if (fVisParams & CR_MULTISAMPLE_BIT)
{
DEBUG_MSG(("CR_MULTISAMPLE_BIT requested\n"));
attribs[i++] = NSOpenGLPFASampleBuffers;
attribs[i++] = 1;
attribs[i++] = NSOpenGLPFASamples;
attribs[i++] = 4;
}
if (fVisParams & CR_DOUBLE_BIT)
{
DEBUG_MSG(("CR_DOUBLE_BIT requested\n"));
attribs[i++] = NSOpenGLPFADoubleBuffer;
}
if (fVisParams & CR_STEREO_BIT)
{
/* We don't support that.
DEBUG_MSG(("CR_STEREO_BIT requested\n"));
attribs[i++] = NSOpenGLPFAStereo;
*/
}
/* Mark the end */
attribs[i++] = 0;
/* Choose a pixel format */
pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
if (pFmt)
{
*ppCtx = [[OverlayOpenGLContext alloc] initWithFormat:pFmt shareContext:pSharedCtx];
/* Enable multi threaded OpenGL engine */
/*
CGLContextObj cglCtx = [*ppCtx CGLContextObj];
CGLError err = CGLEnable(cglCtx, kCGLCEMPEngine);
if (err != kCGLNoError)
printf ("Couldn't enable MT OpenGL engine!\n");
*/
DEBUG_MSG(("New context %X\n", (uint)*ppCtx));
}
[pPool release];
}
void cocoaGLCtxDestroy(NativeNSOpenGLContextRef pCtx)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[pCtx release];
/*[pCtx performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];*/
[pPool release];
}
/********************************************************************************
*
* View management
*
********************************************************************************/
void cocoaViewCreate(NativeNSViewRef *ppView, WindowInfo *pWinInfo, NativeNSViewRef pParentView, GLbitfield fVisParams)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
/* Create our worker view */
OverlayView* pView = [[OverlayView alloc] initWithFrame:NSZeroRect thread:RTThreadSelf() parentView:pParentView winInfo:pWinInfo];
if (pView)
{
/* We need a real window as container for the view */
[[OverlayWindow alloc] initWithParentView:pParentView overlayView:pView];
/* Return the freshly created overlay view */
*ppView = pView;
}
[pPool release];
}
void cocoaViewReparent(NativeNSViewRef pView, NativeNSViewRef pParentView)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
OverlayView* pOView = (OverlayView*)pView;
if (pOView)
{
/* Make sure the window is removed from any previous parent window. */
if ([[pOView overlayWin] parentWindow] != nil)
{
[[[pOView overlayWin] parentWindow] removeChildWindow:[pOView overlayWin]];
}
/* Set the new parent view */
[pOView setParentView: pParentView];
/* Add the overlay window as a child to the new parent window */
if (pParentView != nil)
{
[[pParentView window] addChildWindow:[pOView overlayWin] ordered:NSWindowAbove];
if ([pOView isEverSized])
[pOView performSelectorOnMainThread:@selector(vboxReshapeOnReparentPerform) withObject:nil waitUntilDone:NO];
}
}
[pPool release];
}
void cocoaViewDestroy(NativeNSViewRef pView)
{
NSWindow *pWin = nil;
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
/* Hide the view early */
[pView setHidden: YES];
pWin = [pView window];
[[NSNotificationCenter defaultCenter] removeObserver:pWin];
[pWin setContentView: nil];
[[pWin parentWindow] removeChildWindow: pWin];
/*
a = [pWin retainCount];
for (; a > 1; --a)
[pWin performSelector:@selector(release)]
*/
/* We can NOT run synchronously with the main thread since this may lead to a deadlock,
caused by main thread waiting xpcom thread, xpcom thread waiting to main hgcm thread,
and main hgcm thread waiting for us, this is why use waitUntilDone:NO,
which should cause no harm */
[pWin performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
/*
[pWin release];
*/
/* We can NOT run synchronously with the main thread since this may lead to a deadlock,
caused by main thread waiting xpcom thread, xpcom thread waiting to main hgcm thread,
and main hgcm thread waiting for us, this is why use waitUntilDone:NO.
We need to avoid concurrency though, so we cleanup some data right away via a cleanupData call */
[(OverlayView*)pView cleanupData];
/* There seems to be a bug in the performSelector method which is called in
* parentWindowChanged above. The object is retained but not released. This
* results in an unbalanced reference count, which is here manually
* decremented. */
/*
a = [pView retainCount];
for (; a > 1; --a)
*/
[pView performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
/*
[pView release];
*/
[pPool release];
}
void cocoaViewShow(NativeNSViewRef pView, GLboolean fShowIt)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[pView setHidden: fShowIt==GL_TRUE?NO:YES];
[pPool release];
}
void cocoaViewDisplay(NativeNSViewRef pView)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
DEBUG_MSG_1(("cocoaViewDisplay %p\n", (void*)pView));
[(OverlayView*)pView swapFBO];
[pPool release];
}
void cocoaViewSetPosition(NativeNSViewRef pView, NativeNSViewRef pParentView, int x, int y)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[(OverlayView*)pView setPos:NSMakePoint(x, y)];
[pPool release];
}
void cocoaViewSetSize(NativeNSViewRef pView, int w, int h)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[(OverlayView*)pView setSize:NSMakeSize(w, h)];
[pPool release];
}
void cocoaViewGetGeometry(NativeNSViewRef pView, int *pX, int *pY, int *pW, int *pH)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
NSRect frame = [[pView window] frame];
*pX = frame.origin.x;
*pY = frame.origin.y;
*pW = frame.size.width;
*pH = frame.size.height;
[pPool release];
}
void cocoaViewPresentComposition(NativeNSViewRef pView, const struct VBOXVR_SCR_COMPOSITOR_ENTRY *pChangedEntry)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
NSOpenGLContext *pCtx;
/* view should not necesserily have a context set */
pCtx = [(OverlayView*)pView glCtx];
if (!pCtx)
{
ContextInfo * pCtxInfo = renderspuDefaultSharedContextAcquire();
if (!pCtxInfo)
{
DEBUG_WARN(("renderspuDefaultSharedContextAcquire returned NULL"));
[pPool release];
return;
}
pCtx = pCtxInfo->context;
[(OverlayView*)pView setGLCtx:pCtx];
}
[(OverlayView*)pView presentComposition:pChangedEntry];
[pPool release];
}
void cocoaViewMakeCurrentContext(NativeNSViewRef pView, NativeNSOpenGLContextRef pCtx)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
DEBUG_MSG(("cocoaViewMakeCurrentContext(%p, %p)\n", (void*)pView, (void*)pCtx));
if (pView)
{
[(OverlayView*)pView setGLCtx:pCtx];
[(OverlayView*)pView makeCurrentFBO];
}
else
{
[NSOpenGLContext clearCurrentContext];
}
[pPool release];
}
void cocoaViewSetVisibleRegion(NativeNSViewRef pView, GLint cRects, const GLint* paRects)
{
NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
[(OverlayView*)pView setVisibleRegions:cRects paRects:paRects];
[pPool release];
}
|