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
|
/*
* Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// module header
#include "loader.h"
// standard C headers
#include <dirent.h>
#include <dlfcn.h>
#include <inttypes.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
// standard C++ headers
#include <algorithm>
#include <mutex>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
// platform/library headers
#include <cutils/properties.h>
#include <hardware/hwvulkan.h>
#include <log/log.h>
#include <vulkan/vulkan_loader_data.h>
#include <vulkan/vk_layer_interface.h>
// #define ENABLE_ALLOC_CALLSTACKS 1
#if ENABLE_ALLOC_CALLSTACKS
#include <utils/CallStack.h>
#define ALOGD_CALLSTACK(...) \
do { \
ALOGD(__VA_ARGS__); \
android::CallStack callstack; \
callstack.update(); \
callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
} while (false)
#else
#define ALOGD_CALLSTACK(...) \
do { \
} while (false)
#endif
using namespace vulkan;
static const uint32_t kMaxPhysicalDevices = 4;
namespace {
// ----------------------------------------------------------------------------
// Standard-library allocator that delegates to VkAllocationCallbacks.
//
// TODO(jessehall): This class currently always uses
// VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE. The scope to use could be a template
// parameter or a constructor parameter. The former would help catch bugs
// where we use the wrong scope, e.g. adding a command-scope string to an
// instance-scope vector. But that might also be pretty annoying to deal with.
template <class T>
class CallbackAllocator {
public:
typedef T value_type;
CallbackAllocator(const VkAllocationCallbacks* alloc_input)
: alloc(alloc_input) {}
template <class T2>
CallbackAllocator(const CallbackAllocator<T2>& other)
: alloc(other.alloc) {}
T* allocate(std::size_t n) {
void* mem =
alloc->pfnAllocation(alloc->pUserData, n * sizeof(T), alignof(T),
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (!mem)
throw std::bad_alloc();
return static_cast<T*>(mem);
}
void deallocate(T* array, std::size_t /*n*/) noexcept {
alloc->pfnFree(alloc->pUserData, array);
}
const VkAllocationCallbacks* alloc;
};
// These are needed in order to move Strings
template <class T>
bool operator==(const CallbackAllocator<T>& alloc1,
const CallbackAllocator<T>& alloc2) {
return alloc1.alloc == alloc2.alloc;
}
template <class T>
bool operator!=(const CallbackAllocator<T>& alloc1,
const CallbackAllocator<T>& alloc2) {
return !(alloc1 == alloc2);
}
template <class T>
using Vector = std::vector<T, CallbackAllocator<T>>;
typedef std::basic_string<char, std::char_traits<char>, CallbackAllocator<char>>
String;
// ----------------------------------------------------------------------------
VKAPI_ATTR void* DefaultAllocate(void*,
size_t size,
size_t alignment,
VkSystemAllocationScope) {
void* ptr = nullptr;
// Vulkan requires 'alignment' to be a power of two, but posix_memalign
// additionally requires that it be at least sizeof(void*).
int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
ret, ptr);
return ret == 0 ? ptr : nullptr;
}
VKAPI_ATTR void* DefaultReallocate(void*,
void* ptr,
size_t size,
size_t alignment,
VkSystemAllocationScope) {
if (size == 0) {
free(ptr);
return nullptr;
}
// TODO(jessehall): Right now we never shrink allocations; if the new
// request is smaller than the existing chunk, we just continue using it.
// Right now the loader never reallocs, so this doesn't matter. If that
// changes, or if this code is copied into some other project, this should
// probably have a heuristic to allocate-copy-free when doing so will save
// "enough" space.
size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
if (size <= old_size)
return ptr;
void* new_ptr = nullptr;
if (posix_memalign(&new_ptr, alignment, size) != 0)
return nullptr;
if (ptr) {
memcpy(new_ptr, ptr, std::min(old_size, size));
free(ptr);
}
return new_ptr;
}
VKAPI_ATTR void DefaultFree(void*, void* ptr) {
ALOGD_CALLSTACK("Free: %p", ptr);
free(ptr);
}
const VkAllocationCallbacks kDefaultAllocCallbacks = {
.pUserData = nullptr,
.pfnAllocation = DefaultAllocate,
.pfnReallocation = DefaultReallocate,
.pfnFree = DefaultFree,
};
// ----------------------------------------------------------------------------
// Global Data and Initialization
hwvulkan_device_t* g_hwdevice = nullptr;
InstanceExtensionSet g_driver_instance_extensions;
void LoadVulkanHAL() {
static const hwvulkan_module_t* module;
int result =
hw_get_module("vulkan", reinterpret_cast<const hw_module_t**>(&module));
if (result != 0) {
ALOGE("failed to load vulkan hal: %s (%d)", strerror(-result), result);
return;
}
result = module->common.methods->open(
&module->common, HWVULKAN_DEVICE_0,
reinterpret_cast<hw_device_t**>(&g_hwdevice));
if (result != 0) {
ALOGE("failed to open vulkan driver: %s (%d)", strerror(-result),
result);
module = nullptr;
return;
}
VkResult vkresult;
uint32_t count;
if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
nullptr, &count, nullptr)) != VK_SUCCESS) {
ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
vkresult);
g_hwdevice->common.close(&g_hwdevice->common);
g_hwdevice = nullptr;
module = nullptr;
return;
}
VkExtensionProperties* extensions = static_cast<VkExtensionProperties*>(
alloca(count * sizeof(VkExtensionProperties)));
if ((vkresult = g_hwdevice->EnumerateInstanceExtensionProperties(
nullptr, &count, extensions)) != VK_SUCCESS) {
ALOGE("driver EnumerateInstanceExtensionProperties failed: %d",
vkresult);
g_hwdevice->common.close(&g_hwdevice->common);
g_hwdevice = nullptr;
module = nullptr;
return;
}
ALOGV_IF(count > 0, "Driver-supported instance extensions:");
for (uint32_t i = 0; i < count; i++) {
ALOGV(" %s (v%u)", extensions[i].extensionName,
extensions[i].specVersion);
InstanceExtension id =
InstanceExtensionFromName(extensions[i].extensionName);
if (id != kInstanceExtensionCount)
g_driver_instance_extensions.set(id);
}
// Ignore driver attempts to support loader extensions
g_driver_instance_extensions.reset(kKHR_surface);
g_driver_instance_extensions.reset(kKHR_android_surface);
}
bool EnsureInitialized() {
static std::once_flag once_flag;
std::call_once(once_flag, []() {
LoadVulkanHAL();
DiscoverLayers();
});
return g_hwdevice != nullptr;
}
// -----------------------------------------------------------------------------
struct Instance {
Instance(const VkAllocationCallbacks* alloc_callbacks)
: dispatch_ptr(&dispatch),
handle(reinterpret_cast<VkInstance>(&dispatch_ptr)),
alloc(alloc_callbacks),
num_physical_devices(0),
active_layers(CallbackAllocator<LayerRef>(alloc)),
message(VK_NULL_HANDLE) {
memset(&dispatch, 0, sizeof(dispatch));
memset(physical_devices, 0, sizeof(physical_devices));
enabled_extensions.reset();
drv.instance = VK_NULL_HANDLE;
memset(&drv.dispatch, 0, sizeof(drv.dispatch));
drv.num_physical_devices = 0;
}
~Instance() {}
const InstanceDispatchTable* dispatch_ptr;
const VkInstance handle;
InstanceDispatchTable dispatch;
const VkAllocationCallbacks* alloc;
uint32_t num_physical_devices;
VkPhysicalDevice physical_devices_top[kMaxPhysicalDevices];
VkPhysicalDevice physical_devices[kMaxPhysicalDevices];
DeviceExtensionSet physical_device_driver_extensions[kMaxPhysicalDevices];
Vector<LayerRef> active_layers;
VkDebugReportCallbackEXT message;
DebugReportCallbackList debug_report_callbacks;
InstanceExtensionSet enabled_extensions;
struct {
VkInstance instance;
DriverDispatchTable dispatch;
uint32_t num_physical_devices;
} drv; // may eventually be an array
};
struct Device {
Device(Instance* instance_)
: instance(instance_),
active_layers(CallbackAllocator<LayerRef>(instance->alloc)) {
memset(&dispatch, 0, sizeof(dispatch));
enabled_extensions.reset();
}
DeviceDispatchTable dispatch;
Instance* instance;
PFN_vkGetDeviceProcAddr get_device_proc_addr;
Vector<LayerRef> active_layers;
DeviceExtensionSet enabled_extensions;
};
template <typename THandle>
struct HandleTraits {};
template <>
struct HandleTraits<VkInstance> {
typedef Instance LoaderObjectType;
};
template <>
struct HandleTraits<VkPhysicalDevice> {
typedef Instance LoaderObjectType;
};
template <>
struct HandleTraits<VkDevice> {
typedef Device LoaderObjectType;
};
template <>
struct HandleTraits<VkQueue> {
typedef Device LoaderObjectType;
};
template <>
struct HandleTraits<VkCommandBuffer> {
typedef Device LoaderObjectType;
};
template <typename THandle>
typename HandleTraits<THandle>::LoaderObjectType& GetDispatchParent(
THandle handle) {
// TODO(jessehall): Make Instance and Device POD types (by removing the
// non-default constructors), so that offsetof is actually legal to use.
// The specific case we're using here is safe in gcc/clang (and probably
// most other C++ compilers), but isn't guaranteed by C++.
typedef typename HandleTraits<THandle>::LoaderObjectType ObjectType;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winvalid-offsetof"
const size_t kDispatchOffset = offsetof(ObjectType, dispatch);
#pragma clang diagnostic pop
const auto& dispatch = GetDispatchTable(handle);
uintptr_t dispatch_addr = reinterpret_cast<uintptr_t>(&dispatch);
uintptr_t object_addr = dispatch_addr - kDispatchOffset;
return *reinterpret_cast<ObjectType*>(object_addr);
}
// -----------------------------------------------------------------------------
void DestroyDevice(Device* device) {
const VkAllocationCallbacks* alloc = device->instance->alloc;
device->~Device();
alloc->pfnFree(alloc->pUserData, device);
}
template <class TObject>
LayerRef GetLayerRef(const char* name);
template <>
LayerRef GetLayerRef<Instance>(const char* name) {
return GetInstanceLayerRef(name);
}
template <>
LayerRef GetLayerRef<Device>(const char* name) {
return GetDeviceLayerRef(name);
}
template <class TObject>
bool ActivateLayer(TObject* object, const char* name) {
LayerRef layer(GetLayerRef<TObject>(name));
if (!layer)
return false;
if (std::find(object->active_layers.begin(), object->active_layers.end(),
layer) == object->active_layers.end()) {
try {
object->active_layers.push_back(std::move(layer));
} catch (std::bad_alloc&) {
// TODO(jessehall): We should fail with VK_ERROR_OUT_OF_MEMORY
// if we can't enable a requested layer. Callers currently ignore
// ActivateLayer's return value.
ALOGW("failed to activate layer '%s': out of memory", name);
return false;
}
}
ALOGV("activated layer '%s'", name);
return true;
}
struct InstanceNamesPair {
Instance* instance;
Vector<String>* layer_names;
};
void SetLayerNamesFromProperty(const char* name,
const char* value,
void* data) {
try {
const char prefix[] = "debug.vulkan.layer.";
const size_t prefixlen = sizeof(prefix) - 1;
if (value[0] == '\0' || strncmp(name, prefix, prefixlen) != 0)
return;
const char* number_str = name + prefixlen;
long layer_number = strtol(number_str, nullptr, 10);
if (layer_number <= 0 || layer_number == LONG_MAX) {
ALOGW("Cannot use a layer at number %ld from string %s",
layer_number, number_str);
return;
}
auto instance_names_pair = static_cast<InstanceNamesPair*>(data);
Vector<String>* layer_names = instance_names_pair->layer_names;
Instance* instance = instance_names_pair->instance;
size_t layer_size = static_cast<size_t>(layer_number);
if (layer_size > layer_names->size()) {
layer_names->resize(
layer_size, String(CallbackAllocator<char>(instance->alloc)));
}
(*layer_names)[layer_size - 1] = value;
} catch (std::bad_alloc&) {
ALOGW("failed to handle property '%s'='%s': out of memory", name,
value);
return;
}
}
template <class TInfo, class TObject>
VkResult ActivateAllLayers(TInfo create_info,
Instance* instance,
TObject* object) {
ALOG_ASSERT(create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO ||
create_info->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
"Cannot activate layers for unknown object %p", object);
CallbackAllocator<char> string_allocator(instance->alloc);
// Load system layers
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
char layer_prop[PROPERTY_VALUE_MAX];
property_get("debug.vulkan.layers", layer_prop, "");
char* strtok_state;
char* layer_name = nullptr;
while ((layer_name = strtok_r(layer_name ? nullptr : layer_prop, ":",
&strtok_state))) {
ActivateLayer(object, layer_name);
}
Vector<String> layer_names(CallbackAllocator<String>(instance->alloc));
InstanceNamesPair instance_names_pair = {.instance = instance,
.layer_names = &layer_names};
property_list(SetLayerNamesFromProperty,
static_cast<void*>(&instance_names_pair));
for (auto layer_name_element : layer_names) {
ActivateLayer(object, layer_name_element.c_str());
}
}
// Load app layers
for (uint32_t i = 0; i < create_info->enabledLayerCount; ++i) {
if (!ActivateLayer(object, create_info->ppEnabledLayerNames[i])) {
ALOGE("requested %s layer '%s' not present",
create_info->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO
? "instance"
: "device",
create_info->ppEnabledLayerNames[i]);
return VK_ERROR_LAYER_NOT_PRESENT;
}
}
return VK_SUCCESS;
}
template <class TCreateInfo, class TObject>
bool AddLayersToCreateInfo(TCreateInfo& local_create_info,
const TObject& object,
const VkAllocationCallbacks* alloc,
bool& allocatedMemory) {
// This should never happen and means there is a likely a bug in layer
// tracking
if (object->active_layers.size() < local_create_info.enabledLayerCount) {
ALOGE("Total number of layers is less than those enabled by the app!");
return false;
}
// Check if the total number of layers enabled is greater than those
// enabled by the application. If it is then we have system enabled
// layers which need to be added to the list of layers passed in through
// create.
if (object->active_layers.size() > local_create_info.enabledLayerCount) {
void* mem = alloc->pfnAllocation(
alloc->pUserData, object->active_layers.size() * sizeof(char*),
alignof(char*), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
if (mem) {
local_create_info.enabledLayerCount = 0;
const char** names = static_cast<const char**>(mem);
for (const auto& layer : object->active_layers) {
const char* name = layer.GetName();
names[local_create_info.enabledLayerCount++] = name;
}
local_create_info.ppEnabledLayerNames = names;
} else {
ALOGE("System layers cannot be enabled: memory allocation failed");
return false;
}
allocatedMemory = true;
} else {
allocatedMemory = false;
}
return true;
}
template <class T>
void FreeAllocatedLayerCreateInfo(T& local_create_info,
const VkAllocationCallbacks* alloc) {
alloc->pfnFree(alloc->pUserData,
const_cast<char**>(local_create_info.ppEnabledLayerNames));
}
template <class TCreateInfo>
bool AddExtensionToCreateInfo(TCreateInfo& local_create_info,
const char* extension_name,
const VkAllocationCallbacks* alloc) {
uint32_t extension_count = local_create_info.enabledExtensionCount;
local_create_info.enabledExtensionCount++;
void* mem = alloc->pfnAllocation(
alloc->pUserData,
local_create_info.enabledExtensionCount * sizeof(char*), alignof(char*),
VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
if (mem) {
const char** enabled_extensions = static_cast<const char**>(mem);
for (uint32_t i = 0; i < extension_count; ++i) {
enabled_extensions[i] =
local_create_info.ppEnabledExtensionNames[i];
}
enabled_extensions[extension_count] = extension_name;
local_create_info.ppEnabledExtensionNames = enabled_extensions;
} else {
ALOGE("%s extension cannot be enabled: memory allocation failed",
extension_name);
return false;
}
return true;
}
template <class T>
void FreeAllocatedExtensionCreateInfo(T& local_create_info,
const VkAllocationCallbacks* alloc) {
alloc->pfnFree(
alloc->pUserData,
const_cast<char**>(local_create_info.ppEnabledExtensionNames));
}
VKAPI_ATTR
VkBool32 LogDebugMessageCallback(VkDebugReportFlagsEXT flags,
VkDebugReportObjectTypeEXT /*objectType*/,
uint64_t /*object*/,
size_t /*location*/,
int32_t message_code,
const char* layer_prefix,
const char* message,
void* /*user_data*/) {
if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
ALOGE("[%s] Code %d : %s", layer_prefix, message_code, message);
} else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
ALOGW("[%s] Code %d : %s", layer_prefix, message_code, message);
}
return false;
}
VkResult Noop() {
return VK_SUCCESS;
}
/*
* This function will return the pNext pointer of any
* CreateInfo extensions that are not loader extensions.
* This is used to skip past the loader extensions prepended
* to the list during CreateInstance and CreateDevice.
*/
void* StripCreateExtensions(const void* pNext) {
VkLayerInstanceCreateInfo* create_info =
const_cast<VkLayerInstanceCreateInfo*>(
static_cast<const VkLayerInstanceCreateInfo*>(pNext));
while (
create_info &&
(create_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
create_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)) {
create_info = const_cast<VkLayerInstanceCreateInfo*>(
static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
}
return create_info;
}
// Clean up and deallocate an Instance; called from both the failure paths in
// CreateInstance_Top as well as from DestroyInstance_Top. This function does
// not call down the dispatch chain; that should be done before calling this
// function, iff the lower vkCreateInstance call has been made and returned
// successfully.
void DestroyInstance(Instance* instance,
const VkAllocationCallbacks* allocator) {
if (instance->message) {
PFN_vkDestroyDebugReportCallbackEXT destroy_debug_report_callback;
destroy_debug_report_callback =
reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
GetInstanceProcAddr_Top(instance->handle,
"vkDestroyDebugReportCallbackEXT"));
destroy_debug_report_callback(instance->handle, instance->message,
allocator);
}
instance->~Instance();
allocator->pfnFree(allocator->pUserData, instance);
}
} // anonymous namespace
namespace vulkan {
// -----------------------------------------------------------------------------
// "Bottom" functions. These are called at the end of the instance dispatch
// chain.
VkResult CreateInstance_Bottom(const VkInstanceCreateInfo* create_info,
const VkAllocationCallbacks* allocator,
VkInstance* vkinstance) {
VkResult result;
VkLayerInstanceCreateInfo* chain_info =
const_cast<VkLayerInstanceCreateInfo*>(
static_cast<const VkLayerInstanceCreateInfo*>(create_info->pNext));
while (
chain_info &&
!(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO &&
chain_info->function == VK_LAYER_FUNCTION_INSTANCE)) {
chain_info = const_cast<VkLayerInstanceCreateInfo*>(
static_cast<const VkLayerInstanceCreateInfo*>(chain_info->pNext));
}
ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!");
Instance& instance = GetDispatchParent(
static_cast<VkInstance>(chain_info->u.instanceInfo.instance_info));
// Check that all enabled extensions are supported
uint32_t num_driver_extensions = 0;
for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
const char* name = create_info->ppEnabledExtensionNames[i];
InstanceExtension id = InstanceExtensionFromName(name);
if (id != kInstanceExtensionCount) {
if (g_driver_instance_extensions[id]) {
num_driver_extensions++;
instance.enabled_extensions.set(id);
continue;
}
if (id == kKHR_surface || id == kKHR_android_surface) {
instance.enabled_extensions.set(id);
continue;
}
// The loader natively supports debug report.
if (id == kEXT_debug_report) {
continue;
}
}
bool supported = false;
for (const auto& layer : instance.active_layers) {
if (layer.SupportsExtension(name))
supported = true;
}
if (!supported) {
ALOGE(
"requested instance extension '%s' not supported by "
"loader, driver, or any active layers",
name);
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
}
VkInstanceCreateInfo driver_create_info = *create_info;
driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
driver_create_info.enabledLayerCount = 0;
driver_create_info.ppEnabledLayerNames = nullptr;
driver_create_info.enabledExtensionCount = 0;
driver_create_info.ppEnabledExtensionNames = nullptr;
if (num_driver_extensions > 0) {
const char** names = static_cast<const char**>(
alloca(num_driver_extensions * sizeof(char*)));
for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
const char* name = create_info->ppEnabledExtensionNames[i];
InstanceExtension id = InstanceExtensionFromName(name);
if (id != kInstanceExtensionCount) {
if (g_driver_instance_extensions[id]) {
names[driver_create_info.enabledExtensionCount++] = name;
continue;
}
}
}
driver_create_info.ppEnabledExtensionNames = names;
ALOG_ASSERT(
driver_create_info.enabledExtensionCount == num_driver_extensions,
"counted enabled driver instance extensions twice and got "
"different answers!");
}
result = g_hwdevice->CreateInstance(&driver_create_info, instance.alloc,
&instance.drv.instance);
if (result != VK_SUCCESS) {
DestroyInstance_Bottom(instance.handle, allocator);
return result;
}
hwvulkan_dispatch_t* drv_dispatch =
reinterpret_cast<hwvulkan_dispatch_t*>(instance.drv.instance);
if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
ALOGE("invalid VkInstance dispatch magic: 0x%" PRIxPTR,
drv_dispatch->magic);
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
// Skip setting drv_dispatch->vtbl, since we never call through it;
// we go through instance.drv.dispatch instead.
if (!LoadDriverDispatchTable(
instance.drv.instance, g_hwdevice->GetInstanceProcAddr,
instance.enabled_extensions, instance.drv.dispatch)) {
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
uint32_t num_physical_devices = 0;
result = instance.drv.dispatch.EnumeratePhysicalDevices(
instance.drv.instance, &num_physical_devices, nullptr);
if (result != VK_SUCCESS) {
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
result = instance.drv.dispatch.EnumeratePhysicalDevices(
instance.drv.instance, &num_physical_devices,
instance.physical_devices);
if (result != VK_SUCCESS) {
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
Vector<VkExtensionProperties> extensions(
Vector<VkExtensionProperties>::allocator_type(instance.alloc));
for (uint32_t i = 0; i < num_physical_devices; i++) {
hwvulkan_dispatch_t* pdev_dispatch =
reinterpret_cast<hwvulkan_dispatch_t*>(
instance.physical_devices[i]);
if (pdev_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
ALOGE("invalid VkPhysicalDevice dispatch magic: 0x%" PRIxPTR,
pdev_dispatch->magic);
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
pdev_dispatch->vtbl = instance.dispatch_ptr;
uint32_t count;
if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
instance.physical_devices[i], nullptr, &count, nullptr)) !=
VK_SUCCESS) {
ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
result);
continue;
}
try {
extensions.resize(count);
} catch (std::bad_alloc&) {
ALOGE("instance creation failed: out of memory");
DestroyInstance_Bottom(instance.handle, allocator);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
if ((result = instance.drv.dispatch.EnumerateDeviceExtensionProperties(
instance.physical_devices[i], nullptr, &count,
extensions.data())) != VK_SUCCESS) {
ALOGW("driver EnumerateDeviceExtensionProperties(%u) failed: %d", i,
result);
continue;
}
ALOGV_IF(count > 0, "driver gpu[%u] supports extensions:", i);
for (const auto& extension : extensions) {
ALOGV(" %s (v%u)", extension.extensionName, extension.specVersion);
DeviceExtension id =
DeviceExtensionFromName(extension.extensionName);
if (id == kDeviceExtensionCount) {
ALOGW("driver gpu[%u] extension '%s' unknown to loader", i,
extension.extensionName);
} else {
instance.physical_device_driver_extensions[i].set(id);
}
}
// Ignore driver attempts to support loader extensions
instance.physical_device_driver_extensions[i].reset(kKHR_swapchain);
}
instance.drv.num_physical_devices = num_physical_devices;
instance.num_physical_devices = instance.drv.num_physical_devices;
*vkinstance = instance.handle;
return VK_SUCCESS;
}
VkResult CreateAndroidSurfaceKHR_Disabled(VkInstance,
const VkAndroidSurfaceCreateInfoKHR*,
const VkAllocationCallbacks*,
VkSurfaceKHR*) {
ALOGE(
"VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not "
"executed.");
return VK_SUCCESS;
}
void DestroySurfaceKHR_Disabled(VkInstance,
VkSurfaceKHR,
const VkAllocationCallbacks*) {
ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed.");
}
VkResult GetPhysicalDeviceSurfaceSupportKHR_Disabled(VkPhysicalDevice,
uint32_t,
VkSurfaceKHR,
VkBool32*) {
ALOGE(
"VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not "
"executed.");
return VK_SUCCESS;
}
VkResult GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled(
VkPhysicalDevice,
VkSurfaceKHR,
VkSurfaceCapabilitiesKHR*) {
ALOGE(
"VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceapabilitiesKHR "
"not executed.");
return VK_SUCCESS;
}
VkResult GetPhysicalDeviceSurfaceFormatsKHR_Disabled(VkPhysicalDevice,
VkSurfaceKHR,
uint32_t*,
VkSurfaceFormatKHR*) {
ALOGE(
"VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not "
"executed.");
return VK_SUCCESS;
}
VkResult GetPhysicalDeviceSurfacePresentModesKHR_Disabled(VkPhysicalDevice,
VkSurfaceKHR,
uint32_t*,
VkPresentModeKHR*) {
ALOGE(
"VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR "
"not executed.");
return VK_SUCCESS;
}
PFN_vkVoidFunction GetInstanceProcAddr_Bottom(VkInstance vkinstance,
const char* name) {
PFN_vkVoidFunction pfn;
if (vkinstance) {
Instance& instance = GetDispatchParent(vkinstance);
if (!instance.enabled_extensions[kKHR_android_surface]) {
// KHR_android_surface is not enabled, use error stubs instead
if (strcmp(name, "vkCreateAndroidSurfaceKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
CreateAndroidSurfaceKHR_Disabled);
}
}
if (!instance.enabled_extensions[kKHR_surface]) {
// KHR_surface is not enabled, use error stubs instead
if (strcmp(name, "vkDestroySurfaceKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
DestroySurfaceKHR_Disabled);
}
if (strcmp(name, "vkGetPhysicalDeviceSurfaceSupportKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
GetPhysicalDeviceSurfaceSupportKHR_Disabled);
}
if (strcmp(name, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR") ==
0) {
return reinterpret_cast<PFN_vkVoidFunction>(
GetPhysicalDeviceSurfaceCapabilitiesKHR_Disabled);
}
if (strcmp(name, "vkGetPhysicalDeviceSurfaceFormatsKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
GetPhysicalDeviceSurfaceFormatsKHR_Disabled);
}
if (strcmp(name, "vkGetPhysicalDeviceSurfacePresentModesKHR") ==
0) {
return reinterpret_cast<PFN_vkVoidFunction>(
GetPhysicalDeviceSurfacePresentModesKHR_Disabled);
}
}
}
if ((pfn = GetLoaderBottomProcAddr(name)))
return pfn;
return nullptr;
}
VkResult EnumeratePhysicalDevices_Bottom(VkInstance vkinstance,
uint32_t* pdev_count,
VkPhysicalDevice* pdevs) {
Instance& instance = GetDispatchParent(vkinstance);
uint32_t count = instance.num_physical_devices;
if (pdevs) {
count = std::min(count, *pdev_count);
std::copy(instance.physical_devices, instance.physical_devices + count,
pdevs);
}
*pdev_count = count;
return VK_SUCCESS;
}
void GetPhysicalDeviceProperties_Bottom(
VkPhysicalDevice pdev,
VkPhysicalDeviceProperties* properties) {
GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceProperties(
pdev, properties);
}
void GetPhysicalDeviceFeatures_Bottom(VkPhysicalDevice pdev,
VkPhysicalDeviceFeatures* features) {
GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFeatures(pdev,
features);
}
void GetPhysicalDeviceMemoryProperties_Bottom(
VkPhysicalDevice pdev,
VkPhysicalDeviceMemoryProperties* properties) {
GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceMemoryProperties(
pdev, properties);
}
void GetPhysicalDeviceQueueFamilyProperties_Bottom(
VkPhysicalDevice pdev,
uint32_t* pCount,
VkQueueFamilyProperties* properties) {
GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceQueueFamilyProperties(
pdev, pCount, properties);
}
void GetPhysicalDeviceFormatProperties_Bottom(VkPhysicalDevice pdev,
VkFormat format,
VkFormatProperties* properties) {
GetDispatchParent(pdev).drv.dispatch.GetPhysicalDeviceFormatProperties(
pdev, format, properties);
}
VkResult GetPhysicalDeviceImageFormatProperties_Bottom(
VkPhysicalDevice pdev,
VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkImageCreateFlags flags,
VkImageFormatProperties* properties) {
return GetDispatchParent(pdev)
.drv.dispatch.GetPhysicalDeviceImageFormatProperties(
pdev, format, type, tiling, usage, flags, properties);
}
void GetPhysicalDeviceSparseImageFormatProperties_Bottom(
VkPhysicalDevice pdev,
VkFormat format,
VkImageType type,
VkSampleCountFlagBits samples,
VkImageUsageFlags usage,
VkImageTiling tiling,
uint32_t* properties_count,
VkSparseImageFormatProperties* properties) {
GetDispatchParent(pdev)
.drv.dispatch.GetPhysicalDeviceSparseImageFormatProperties(
pdev, format, type, samples, usage, tiling, properties_count,
properties);
}
// This is a no-op, the Top function returns the aggregate layer property
// data. This is to keep the dispatch generator happy.
VKAPI_ATTR
VkResult EnumerateDeviceExtensionProperties_Bottom(
VkPhysicalDevice /*pdev*/,
const char* /*layer_name*/,
uint32_t* /*properties_count*/,
VkExtensionProperties* /*properties*/) {
return VK_SUCCESS;
}
// This is a no-op, the Top function returns the aggregate layer property
// data. This is to keep the dispatch generator happy.
VKAPI_ATTR
VkResult EnumerateDeviceLayerProperties_Bottom(
VkPhysicalDevice /*pdev*/,
uint32_t* /*properties_count*/,
VkLayerProperties* /*properties*/) {
return VK_SUCCESS;
}
VKAPI_ATTR
VkResult CreateDevice_Bottom(VkPhysicalDevice gpu,
const VkDeviceCreateInfo* create_info,
const VkAllocationCallbacks* allocator,
VkDevice* device_out) {
VkLayerDeviceCreateInfo* chain_info = const_cast<VkLayerDeviceCreateInfo*>(
static_cast<const VkLayerDeviceCreateInfo*>(create_info->pNext));
while (chain_info &&
!(chain_info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO &&
chain_info->function == VK_LAYER_FUNCTION_DEVICE)) {
chain_info = const_cast<VkLayerDeviceCreateInfo*>(
static_cast<const VkLayerDeviceCreateInfo*>(chain_info->pNext));
}
ALOG_ASSERT(chain_info != nullptr, "Missing initialization chain info!");
Instance& instance = GetDispatchParent(gpu);
size_t gpu_idx = 0;
while (instance.physical_devices[gpu_idx] != gpu)
gpu_idx++;
Device* device = static_cast<Device*>(chain_info->u.deviceInfo.device_info);
PFN_vkGetInstanceProcAddr get_instance_proc_addr =
chain_info->u.deviceInfo.pfnNextGetInstanceProcAddr;
VkDeviceCreateInfo driver_create_info = *create_info;
driver_create_info.pNext = StripCreateExtensions(create_info->pNext);
driver_create_info.enabledLayerCount = 0;
driver_create_info.ppEnabledLayerNames = nullptr;
uint32_t num_driver_extensions = 0;
const char** driver_extensions = static_cast<const char**>(
alloca(create_info->enabledExtensionCount * sizeof(const char*)));
for (uint32_t i = 0; i < create_info->enabledExtensionCount; i++) {
const char* name = create_info->ppEnabledExtensionNames[i];
DeviceExtension id = DeviceExtensionFromName(name);
if (id != kDeviceExtensionCount) {
if (instance.physical_device_driver_extensions[gpu_idx][id]) {
driver_extensions[num_driver_extensions++] = name;
device->enabled_extensions.set(id);
continue;
}
// Add the VK_ANDROID_native_buffer extension to the list iff
// the VK_KHR_swapchain extension was requested
if (id == kKHR_swapchain &&
instance.physical_device_driver_extensions
[gpu_idx][kANDROID_native_buffer]) {
driver_extensions[num_driver_extensions++] =
VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
device->enabled_extensions.set(id);
continue;
}
}
bool supported = false;
for (const auto& layer : device->active_layers) {
if (layer.SupportsExtension(name))
supported = true;
}
if (!supported) {
ALOGE(
"requested device extension '%s' not supported by loader, "
"driver, or any active layers",
name);
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
}
driver_create_info.enabledExtensionCount = num_driver_extensions;
driver_create_info.ppEnabledExtensionNames = driver_extensions;
VkDevice drv_device;
VkResult result = instance.drv.dispatch.CreateDevice(
gpu, &driver_create_info, allocator, &drv_device);
if (result != VK_SUCCESS) {
return VK_ERROR_INITIALIZATION_FAILED;
}
hwvulkan_dispatch_t* drv_dispatch =
reinterpret_cast<hwvulkan_dispatch_t*>(drv_device);
if (drv_dispatch->magic != HWVULKAN_DISPATCH_MAGIC) {
ALOGE("invalid VkDevice dispatch magic: 0x%" PRIxPTR,
drv_dispatch->magic);
PFN_vkDestroyDevice destroy_device =
reinterpret_cast<PFN_vkDestroyDevice>(
instance.drv.dispatch.GetDeviceProcAddr(drv_device,
"vkDestroyDevice"));
destroy_device(drv_device, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
// Set dispatch table for newly created Device
// CreateDevice_Top will fill in the details
drv_dispatch->vtbl = &device->dispatch;
device->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
instance.drv.dispatch.GetDeviceProcAddr(drv_device,
"vkGetDeviceProcAddr"));
*device_out = drv_device;
return VK_SUCCESS;
}
void DestroyInstance_Bottom(VkInstance vkinstance,
const VkAllocationCallbacks* allocator) {
Instance& instance = GetDispatchParent(vkinstance);
// These checks allow us to call DestroyInstance_Bottom from any error
// path in CreateInstance_Bottom, before the driver instance is fully
// initialized.
if (instance.drv.instance != VK_NULL_HANDLE &&
instance.drv.dispatch.DestroyInstance) {
instance.drv.dispatch.DestroyInstance(instance.drv.instance, allocator);
instance.drv.instance = VK_NULL_HANDLE;
}
}
VkResult CreateSwapchainKHR_Disabled(VkDevice,
const VkSwapchainCreateInfoKHR*,
const VkAllocationCallbacks*,
VkSwapchainKHR*) {
ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
return VK_SUCCESS;
}
void DestroySwapchainKHR_Disabled(VkDevice,
VkSwapchainKHR,
const VkAllocationCallbacks*) {
ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
}
VkResult GetSwapchainImagesKHR_Disabled(VkDevice,
VkSwapchainKHR,
uint32_t*,
VkImage*) {
ALOGE(
"VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
return VK_SUCCESS;
}
VkResult AcquireNextImageKHR_Disabled(VkDevice,
VkSwapchainKHR,
uint64_t,
VkSemaphore,
VkFence,
uint32_t*) {
ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
return VK_SUCCESS;
}
VkResult QueuePresentKHR_Disabled(VkQueue, const VkPresentInfoKHR*) {
ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
return VK_SUCCESS;
}
PFN_vkVoidFunction GetDeviceProcAddr_Bottom(VkDevice vkdevice,
const char* name) {
if (strcmp(name, "vkCreateDevice") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(CreateDevice_Bottom);
}
Device& device = GetDispatchParent(vkdevice);
if (!device.enabled_extensions[kKHR_swapchain]) {
if (strcmp(name, "vkCreateSwapchainKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
CreateSwapchainKHR_Disabled);
}
if (strcmp(name, "vkDestroySwapchainKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
DestroySwapchainKHR_Disabled);
}
if (strcmp(name, "vkGetSwapchainImagesKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
GetSwapchainImagesKHR_Disabled);
}
if (strcmp(name, "vkAcquireNextSwapchainImageKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
AcquireNextImageKHR_Disabled);
}
if (strcmp(name, "vkQueuePresentKHR") == 0) {
return reinterpret_cast<PFN_vkVoidFunction>(
QueuePresentKHR_Disabled);
}
}
// VK_ANDROID_native_buffer should be hidden from applications and layers.
// TODO(jessehall): Generate this as part of GetLoaderBottomProcAddr.
PFN_vkVoidFunction pfn;
if (strcmp(name, "vkGetSwapchainGrallocUsageANDROID") == 0 ||
strcmp(name, "vkAcquireImageANDROID") == 0 ||
strcmp(name, "vkQueueSignalReleaseImageANDROID") == 0) {
return nullptr;
}
if ((pfn = GetLoaderBottomProcAddr(name)))
return pfn;
return GetDispatchParent(vkdevice).get_device_proc_addr(vkdevice, name);
}
// -----------------------------------------------------------------------------
// Loader top functions. These are called directly from the loader entry
// points or from the application (via vkGetInstanceProcAddr) without going
// through a dispatch table.
VkResult EnumerateInstanceExtensionProperties_Top(
const char* layer_name,
uint32_t* properties_count,
VkExtensionProperties* properties) {
if (!EnsureInitialized())
return VK_ERROR_INITIALIZATION_FAILED;
const VkExtensionProperties* extensions = nullptr;
uint32_t num_extensions = 0;
if (layer_name) {
GetInstanceLayerExtensions(layer_name, &extensions, &num_extensions);
} else {
VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
alloca(kInstanceExtensionCount * sizeof(VkExtensionProperties)));
available[num_extensions++] = VkExtensionProperties{
VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION};
available[num_extensions++] =
VkExtensionProperties{VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
VK_KHR_ANDROID_SURFACE_SPEC_VERSION};
if (g_driver_instance_extensions[kEXT_debug_report]) {
available[num_extensions++] =
VkExtensionProperties{VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
VK_EXT_DEBUG_REPORT_SPEC_VERSION};
}
// TODO(jessehall): We need to also enumerate extensions supported by
// implicitly-enabled layers. Currently we don't have that list of
// layers until instance creation.
extensions = available;
}
if (!properties || *properties_count > num_extensions)
*properties_count = num_extensions;
if (properties)
std::copy(extensions, extensions + *properties_count, properties);
return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
}
VkResult EnumerateInstanceLayerProperties_Top(uint32_t* properties_count,
VkLayerProperties* properties) {
if (!EnsureInitialized())
return VK_ERROR_INITIALIZATION_FAILED;
uint32_t layer_count =
EnumerateInstanceLayers(properties ? *properties_count : 0, properties);
if (!properties || *properties_count > layer_count)
*properties_count = layer_count;
return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
}
VKAPI_ATTR
VkResult EnumerateDeviceExtensionProperties_Top(
VkPhysicalDevice gpu,
const char* layer_name,
uint32_t* properties_count,
VkExtensionProperties* properties) {
const VkExtensionProperties* extensions = nullptr;
uint32_t num_extensions = 0;
ALOGV("EnumerateDeviceExtensionProperties_Top:");
if (layer_name) {
ALOGV(" layer %s", layer_name);
GetDeviceLayerExtensions(layer_name, &extensions, &num_extensions);
} else {
ALOGV(" no layer");
Instance& instance = GetDispatchParent(gpu);
size_t gpu_idx = 0;
while (instance.physical_devices_top[gpu_idx] != gpu)
gpu_idx++;
const DeviceExtensionSet driver_extensions =
instance.physical_device_driver_extensions[gpu_idx];
// We only support VK_KHR_swapchain if the GPU supports
// VK_ANDROID_native_buffer
VkExtensionProperties* available = static_cast<VkExtensionProperties*>(
alloca(kDeviceExtensionCount * sizeof(VkExtensionProperties)));
if (driver_extensions[kANDROID_native_buffer]) {
available[num_extensions++] = VkExtensionProperties{
VK_KHR_SWAPCHAIN_EXTENSION_NAME, VK_KHR_SWAPCHAIN_SPEC_VERSION};
}
// TODO(jessehall): We need to also enumerate extensions supported by
// implicitly-enabled layers. Currently we don't have that list of
// layers until instance creation.
extensions = available;
}
ALOGV(" num: %d, extensions: %p", num_extensions, extensions);
if (!properties || *properties_count > num_extensions)
*properties_count = num_extensions;
if (properties)
std::copy(extensions, extensions + *properties_count, properties);
return *properties_count < num_extensions ? VK_INCOMPLETE : VK_SUCCESS;
}
VkResult CreateInstance_Top(const VkInstanceCreateInfo* create_info,
const VkAllocationCallbacks* allocator,
VkInstance* instance_out) {
VkResult result;
if (!EnsureInitialized())
return VK_ERROR_INITIALIZATION_FAILED;
if (!allocator)
allocator = &kDefaultAllocCallbacks;
VkInstanceCreateInfo local_create_info = *create_info;
create_info = &local_create_info;
void* instance_mem = allocator->pfnAllocation(
allocator->pUserData, sizeof(Instance), alignof(Instance),
VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
if (!instance_mem)
return VK_ERROR_OUT_OF_HOST_MEMORY;
Instance* instance = new (instance_mem) Instance(allocator);
result = ActivateAllLayers(create_info, instance, instance);
if (result != VK_SUCCESS) {
DestroyInstance(instance, allocator);
return result;
}
uint32_t activated_layers = 0;
VkLayerInstanceCreateInfo chain_info;
VkLayerInstanceLink* layer_instance_link_info = nullptr;
PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom;
VkInstance local_instance = nullptr;
if (instance->active_layers.size() > 0) {
chain_info.u.pLayerInfo = nullptr;
chain_info.pNext = create_info->pNext;
chain_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
chain_info.function = VK_LAYER_FUNCTION_LINK;
local_create_info.pNext = &chain_info;
layer_instance_link_info = static_cast<VkLayerInstanceLink*>(alloca(
sizeof(VkLayerInstanceLink) * instance->active_layers.size()));
if (!layer_instance_link_info) {
ALOGE("Failed to alloc Instance objects for layers");
DestroyInstance(instance, allocator);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
/* Create instance chain of enabled layers */
for (auto rit = instance->active_layers.rbegin();
rit != instance->active_layers.rend(); ++rit) {
LayerRef& layer = *rit;
layer_instance_link_info[activated_layers].pNext =
chain_info.u.pLayerInfo;
layer_instance_link_info[activated_layers]
.pfnNextGetInstanceProcAddr = next_gipa;
chain_info.u.pLayerInfo =
&layer_instance_link_info[activated_layers];
next_gipa = layer.GetGetInstanceProcAddr();
ALOGV("Insert instance layer %s (v%u)", layer.GetName(),
layer.GetSpecVersion());
activated_layers++;
}
}
PFN_vkCreateInstance create_instance =
reinterpret_cast<PFN_vkCreateInstance>(
next_gipa(VK_NULL_HANDLE, "vkCreateInstance"));
if (!create_instance) {
DestroyInstance(instance, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
VkLayerInstanceCreateInfo instance_create_info;
instance_create_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
instance_create_info.function = VK_LAYER_FUNCTION_INSTANCE;
instance_create_info.u.instanceInfo.instance_info = instance;
instance_create_info.u.instanceInfo.pfnNextGetInstanceProcAddr = next_gipa;
instance_create_info.pNext = local_create_info.pNext;
local_create_info.pNext = &instance_create_info;
// Force enable callback extension if required
bool enable_callback = false;
if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
enable_callback =
property_get_bool("debug.vulkan.enable_callback", false);
if (enable_callback) {
if (!AddExtensionToCreateInfo(local_create_info,
"VK_EXT_debug_report", allocator)) {
DestroyInstance(instance, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
}
}
bool allocatedLayerMem;
if (!AddLayersToCreateInfo(local_create_info, instance, allocator,
allocatedLayerMem)) {
if (enable_callback) {
FreeAllocatedExtensionCreateInfo(local_create_info, allocator);
}
DestroyInstance(instance, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
result = create_instance(&local_create_info, allocator, &local_instance);
if (allocatedLayerMem) {
FreeAllocatedLayerCreateInfo(local_create_info, allocator);
}
if (enable_callback) {
FreeAllocatedExtensionCreateInfo(local_create_info, allocator);
}
if (result != VK_SUCCESS) {
DestroyInstance(instance, allocator);
return result;
}
const InstanceDispatchTable& instance_dispatch =
GetDispatchTable(local_instance);
if (!LoadInstanceDispatchTable(
local_instance, next_gipa,
const_cast<InstanceDispatchTable&>(instance_dispatch))) {
ALOGV("Failed to initialize instance dispatch table");
PFN_vkDestroyInstance destroy_instance =
reinterpret_cast<PFN_vkDestroyInstance>(
next_gipa(local_instance, "vkDestroyInstance"));
if (!destroy_instance) {
ALOGD("Loader unable to find DestroyInstance");
return VK_ERROR_INITIALIZATION_FAILED;
}
destroy_instance(local_instance, allocator);
DestroyInstance(instance, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
// Capture the physical devices from the top of the
// chain in case it has been wrapped by a layer.
uint32_t num_physical_devices = 0;
result = instance_dispatch.EnumeratePhysicalDevices(
local_instance, &num_physical_devices, nullptr);
if (result != VK_SUCCESS) {
DestroyInstance(instance, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
num_physical_devices = std::min(num_physical_devices, kMaxPhysicalDevices);
result = instance_dispatch.EnumeratePhysicalDevices(
local_instance, &num_physical_devices,
instance->physical_devices_top);
if (result != VK_SUCCESS) {
DestroyInstance(instance, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
*instance_out = local_instance;
if (enable_callback) {
const VkDebugReportCallbackCreateInfoEXT callback_create_info = {
.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT,
.flags =
VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT,
.pfnCallback = LogDebugMessageCallback,
};
PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
GetInstanceProcAddr_Top(instance->handle,
"vkCreateDebugReportCallbackEXT"));
create_debug_report_callback(instance->handle, &callback_create_info,
allocator, &instance->message);
}
return result;
}
PFN_vkVoidFunction GetInstanceProcAddr_Top(VkInstance vkinstance,
const char* name) {
// vkGetInstanceProcAddr(NULL_HANDLE, ..) only works for global commands
if (!vkinstance)
return GetLoaderGlobalProcAddr(name);
const InstanceDispatchTable& dispatch = GetDispatchTable(vkinstance);
PFN_vkVoidFunction pfn;
// Always go through the loader-top function if there is one.
if ((pfn = GetLoaderTopProcAddr(name)))
return pfn;
// Otherwise, look up the handler in the instance dispatch table
if ((pfn = GetDispatchProcAddr(dispatch, name)))
return pfn;
// Anything not handled already must be a device-dispatched function
// without a loader-top. We must return a function that will dispatch based
// on the dispatchable object parameter -- which is exactly what the
// exported functions do. So just return them here.
return GetLoaderExportProcAddr(name);
}
void DestroyInstance_Top(VkInstance vkinstance,
const VkAllocationCallbacks* allocator) {
if (!vkinstance)
return;
if (!allocator)
allocator = &kDefaultAllocCallbacks;
GetDispatchTable(vkinstance).DestroyInstance(vkinstance, allocator);
DestroyInstance(&(GetDispatchParent(vkinstance)), allocator);
}
VKAPI_ATTR
VkResult EnumerateDeviceLayerProperties_Top(VkPhysicalDevice /*pdev*/,
uint32_t* properties_count,
VkLayerProperties* properties) {
uint32_t layer_count =
EnumerateDeviceLayers(properties ? *properties_count : 0, properties);
if (!properties || *properties_count > layer_count)
*properties_count = layer_count;
return *properties_count < layer_count ? VK_INCOMPLETE : VK_SUCCESS;
}
VKAPI_ATTR
VkResult CreateDevice_Top(VkPhysicalDevice gpu,
const VkDeviceCreateInfo* create_info,
const VkAllocationCallbacks* allocator,
VkDevice* device_out) {
Instance& instance = GetDispatchParent(gpu);
VkResult result;
// FIXME(jessehall): We don't have good conventions or infrastructure yet to
// do better than just using the instance allocator and scope for
// everything. See b/26732122.
if (true /*!allocator*/)
allocator = instance.alloc;
void* mem = allocator->pfnAllocation(allocator->pUserData, sizeof(Device),
alignof(Device),
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
if (!mem)
return VK_ERROR_OUT_OF_HOST_MEMORY;
Device* device = new (mem) Device(&instance);
result = ActivateAllLayers(create_info, &instance, device);
if (result != VK_SUCCESS) {
DestroyDevice(device);
return result;
}
uint32_t activated_layers = 0;
VkLayerDeviceCreateInfo chain_info;
VkLayerDeviceLink* layer_device_link_info = nullptr;
PFN_vkGetInstanceProcAddr next_gipa = GetInstanceProcAddr_Bottom;
PFN_vkGetDeviceProcAddr next_gdpa = GetDeviceProcAddr_Bottom;
VkDeviceCreateInfo local_create_info = *create_info;
VkDevice local_device = nullptr;
if (device->active_layers.size() > 0) {
chain_info.u.pLayerInfo = nullptr;
chain_info.pNext = local_create_info.pNext;
chain_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
chain_info.function = VK_LAYER_FUNCTION_LINK;
local_create_info.pNext = &chain_info;
layer_device_link_info = static_cast<VkLayerDeviceLink*>(
alloca(sizeof(VkLayerDeviceLink) * device->active_layers.size()));
if (!layer_device_link_info) {
ALOGE("Failed to alloc Device objects for layers");
DestroyDevice(device);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
/* Create device chain of enabled layers */
for (auto rit = device->active_layers.rbegin();
rit != device->active_layers.rend(); ++rit) {
LayerRef& layer = *rit;
layer_device_link_info[activated_layers].pNext =
chain_info.u.pLayerInfo;
layer_device_link_info[activated_layers].pfnNextGetDeviceProcAddr =
next_gdpa;
layer_device_link_info[activated_layers]
.pfnNextGetInstanceProcAddr = next_gipa;
chain_info.u.pLayerInfo = &layer_device_link_info[activated_layers];
next_gipa = layer.GetGetInstanceProcAddr();
next_gdpa = layer.GetGetDeviceProcAddr();
ALOGV("Insert device layer %s (v%u)", layer.GetName(),
layer.GetSpecVersion());
activated_layers++;
}
}
PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
next_gipa(instance.handle, "vkCreateDevice"));
if (!create_device) {
ALOGE("Unable to find vkCreateDevice for driver");
DestroyDevice(device);
return VK_ERROR_INITIALIZATION_FAILED;
}
VkLayerDeviceCreateInfo device_create_info;
device_create_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
device_create_info.function = VK_LAYER_FUNCTION_DEVICE;
device_create_info.u.deviceInfo.device_info = device;
device_create_info.u.deviceInfo.pfnNextGetInstanceProcAddr = next_gipa;
device_create_info.pNext = local_create_info.pNext;
local_create_info.pNext = &device_create_info;
bool allocatedLayerMem;
if (!AddLayersToCreateInfo(local_create_info, device, allocator,
allocatedLayerMem)) {
DestroyDevice(device);
return VK_ERROR_INITIALIZATION_FAILED;
}
result = create_device(gpu, &local_create_info, allocator, &local_device);
if (allocatedLayerMem) {
FreeAllocatedLayerCreateInfo(local_create_info, allocator);
}
if (result != VK_SUCCESS) {
DestroyDevice(device);
return result;
}
// Set dispatch table for newly created Device
hwvulkan_dispatch_t* vulkan_dispatch =
reinterpret_cast<hwvulkan_dispatch_t*>(local_device);
vulkan_dispatch->vtbl = &device->dispatch;
const DeviceDispatchTable& device_dispatch = GetDispatchTable(local_device);
if (!LoadDeviceDispatchTable(
local_device, next_gdpa,
const_cast<DeviceDispatchTable&>(device_dispatch))) {
ALOGV("Failed to initialize device dispatch table");
PFN_vkDestroyDevice destroy_device =
reinterpret_cast<PFN_vkDestroyDevice>(
next_gipa(instance.handle, "vkDestroyDevice"));
ALOG_ASSERT(destroy_device != nullptr,
"Loader unable to find DestroyDevice");
destroy_device(local_device, allocator);
return VK_ERROR_INITIALIZATION_FAILED;
}
*device_out = local_device;
return VK_SUCCESS;
}
PFN_vkVoidFunction GetDeviceProcAddr_Top(VkDevice device, const char* name) {
PFN_vkVoidFunction pfn;
if (!device)
return nullptr;
if ((pfn = GetLoaderTopProcAddr(name)))
return pfn;
return GetDispatchProcAddr(GetDispatchTable(device), name);
}
void GetDeviceQueue_Top(VkDevice vkdevice,
uint32_t family,
uint32_t index,
VkQueue* queue_out) {
const auto& table = GetDispatchTable(vkdevice);
table.GetDeviceQueue(vkdevice, family, index, queue_out);
hwvulkan_dispatch_t* queue_dispatch =
reinterpret_cast<hwvulkan_dispatch_t*>(*queue_out);
if (queue_dispatch->magic != HWVULKAN_DISPATCH_MAGIC &&
queue_dispatch->vtbl != &table)
ALOGE("invalid VkQueue dispatch magic: 0x%" PRIxPTR,
queue_dispatch->magic);
queue_dispatch->vtbl = &table;
}
VkResult AllocateCommandBuffers_Top(
VkDevice vkdevice,
const VkCommandBufferAllocateInfo* alloc_info,
VkCommandBuffer* cmdbufs) {
const auto& table = GetDispatchTable(vkdevice);
VkResult result =
table.AllocateCommandBuffers(vkdevice, alloc_info, cmdbufs);
if (result != VK_SUCCESS)
return result;
for (uint32_t i = 0; i < alloc_info->commandBufferCount; i++) {
hwvulkan_dispatch_t* cmdbuf_dispatch =
reinterpret_cast<hwvulkan_dispatch_t*>(cmdbufs[i]);
ALOGE_IF(cmdbuf_dispatch->magic != HWVULKAN_DISPATCH_MAGIC,
"invalid VkCommandBuffer dispatch magic: 0x%" PRIxPTR,
cmdbuf_dispatch->magic);
cmdbuf_dispatch->vtbl = &table;
}
return VK_SUCCESS;
}
void DestroyDevice_Top(VkDevice vkdevice,
const VkAllocationCallbacks* /*allocator*/) {
if (!vkdevice)
return;
Device& device = GetDispatchParent(vkdevice);
device.dispatch.DestroyDevice(vkdevice, device.instance->alloc);
DestroyDevice(&device);
}
// -----------------------------------------------------------------------------
const VkAllocationCallbacks* GetAllocator(VkInstance vkinstance) {
return GetDispatchParent(vkinstance).alloc;
}
const VkAllocationCallbacks* GetAllocator(VkDevice vkdevice) {
return GetDispatchParent(vkdevice).instance->alloc;
}
VkInstance GetDriverInstance(VkInstance instance) {
return GetDispatchParent(instance).drv.instance;
}
const DriverDispatchTable& GetDriverDispatch(VkInstance instance) {
return GetDispatchParent(instance).drv.dispatch;
}
const DriverDispatchTable& GetDriverDispatch(VkDevice device) {
return GetDispatchParent(device).instance->drv.dispatch;
}
const DriverDispatchTable& GetDriverDispatch(VkQueue queue) {
return GetDispatchParent(queue).instance->drv.dispatch;
}
DebugReportCallbackList& GetDebugReportCallbacks(VkInstance instance) {
return GetDispatchParent(instance).debug_report_callbacks;
}
} // namespace vulkan
|