blob: 4cf1c2a68887bc66c51eb91fa607507baf126ad6 [file] [log] [blame]
Jesse Hall80523e22016-01-06 16:47:54 -08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Michael Lentined0b7cb52016-01-19 13:57:26 -060017// #define LOG_NDEBUG 0
18
Chia-I Wuc96880f2016-03-26 06:56:45 +080019#include "layers_extensions.h"
Jesse Hall80523e22016-01-06 16:47:54 -080020#include <alloca.h>
21#include <dirent.h>
22#include <dlfcn.h>
23#include <mutex>
24#include <sys/prctl.h>
25#include <string>
26#include <string.h>
27#include <vector>
28#include <log/log.h>
29#include <vulkan/vulkan_loader_data.h>
30
Jesse Hallb1471272016-01-17 21:36:58 -080031// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
32// not a good long-term solution. Having a hard-coded enum of extensions is
33// bad, of course. Representing sets of extensions (requested, supported, etc.)
34// as a bitset isn't necessarily bad, if the mapping from extension to bit were
35// dynamic. Need to rethink this completely when there's a little more time.
36
Jesse Hallaa410942016-01-17 13:07:10 -080037// TODO(jessehall): This file currently builds up global data structures as it
38// loads, and never cleans them up. This means we're doing heap allocations
39// without going through an app-provided allocator, but worse, we'll leak those
40// allocations if the loader is unloaded.
41//
42// We should allocate "enough" BSS space, and suballocate from there. Will
43// probably want to intern strings, etc., and will need some custom/manual data
44// structures.
45
Jesse Hall80523e22016-01-06 16:47:54 -080046namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080047namespace api {
48
Jesse Hall80523e22016-01-06 16:47:54 -080049struct Layer {
50 VkLayerProperties properties;
51 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080052
53 bool is_global;
54
55 std::vector<VkExtensionProperties> instance_extensions;
56 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080057};
Jesse Hall80523e22016-01-06 16:47:54 -080058
59namespace {
60
Chia-I Wu6693f5c2016-04-18 12:20:02 +080061class LayerLibrary {
62 public:
63 LayerLibrary(const std::string& path)
64 : path_(path), dlhandle_(nullptr), refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080065
Chia-I Wu50174ee2016-04-18 16:33:20 +080066 LayerLibrary(LayerLibrary&& other)
Chia-I Wu6693f5c2016-04-18 12:20:02 +080067 : path_(std::move(other.path_)),
68 dlhandle_(other.dlhandle_),
69 refcount_(other.refcount_) {
70 other.dlhandle_ = nullptr;
71 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080072 }
73
74 LayerLibrary(const LayerLibrary&) = delete;
75 LayerLibrary& operator=(const LayerLibrary&) = delete;
76
Chia-I Wua6229742016-04-26 07:37:44 +080077 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080078 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080079 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080080
Chia-I Wu50174ee2016-04-18 16:33:20 +080081 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080082 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080083
Chia-I Wuba113272016-04-18 16:38:39 +080084 void* GetGPA(const Layer& layer,
85 const char* gpa_name,
86 size_t gpa_name_len) const;
87
Chia-I Wu6693f5c2016-04-18 12:20:02 +080088 private:
89 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +080090
91 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +080092 void* dlhandle_;
93 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -080094};
Chia-I Wu74349592016-04-18 12:08:39 +080095
Chia-I Wufd0389f2016-04-18 12:11:00 +080096bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +080097 std::lock_guard<std::mutex> lock(mutex_);
98
Chia-I Wu6693f5c2016-04-18 12:20:02 +080099 if (refcount_++ == 0) {
100 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
101 ALOGV("Opening library %s", path_.c_str());
102 if (!dlhandle_) {
103 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
Chia-I Wufd0389f2016-04-18 12:11:00 +0800104 dlerror());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800105 refcount_ = 0;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800106 return false;
107 }
108 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800109 ALOGV("Refcount on activate is %zu", refcount_);
Chia-I Wufd0389f2016-04-18 12:11:00 +0800110 return true;
111}
112
Chia-I Wud91c74f2016-04-18 12:12:36 +0800113void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800114 std::lock_guard<std::mutex> lock(mutex_);
115
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800116 if (--refcount_ == 0) {
117 ALOGV("Closing library %s", path_.c_str());
118 dlclose(dlhandle_);
119 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800120 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800121 ALOGV("Refcount on destruction is %zu", refcount_);
Chia-I Wud91c74f2016-04-18 12:12:36 +0800122}
123
Chia-I Wu50174ee2016-04-18 16:33:20 +0800124bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800125 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800126 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Jesse Hall80523e22016-01-06 16:47:54 -0800127 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800128 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800129 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Jesse Hall80523e22016-01-06 16:47:54 -0800130 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800131 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties"));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800132 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
133 ALOGV("layer library '%s' misses some instance enumeraion functions",
134 path_.c_str());
135 return false;
136 }
137
138 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800139 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
140 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800141 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800142 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
143 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800144 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties"));
Jesse Hall80523e22016-01-06 16:47:54 -0800145
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800146 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800147 uint32_t num_instance_layers = 0;
148 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800149 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
150 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800151 if (result != VK_SUCCESS) {
152 ALOGW(
153 "vkEnumerateInstanceLayerProperties failed for library '%s': "
154 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800155 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800156 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800157 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800158 }
Jesse Hallaa410942016-01-17 13:07:10 -0800159 if (enumerate_device_layers) {
160 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
161 nullptr);
162 if (result != VK_SUCCESS) {
163 ALOGW(
164 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800165 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800166 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800167 }
168 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800169
170 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800171 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
172 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800173 result = enumerate_instance_layers(&num_instance_layers, properties);
174 if (result != VK_SUCCESS) {
175 ALOGW("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
176 path_.c_str(), result);
177 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800178 }
179 if (num_device_layers > 0) {
180 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
181 properties + num_instance_layers);
182 if (result != VK_SUCCESS) {
183 ALOGW(
184 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800185 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800186 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800187 }
Jesse Hall80523e22016-01-06 16:47:54 -0800188 }
189
Chia-I Wubea09db2016-04-22 09:42:41 +0800190 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800191 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800192 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800193 for (size_t i = 0; i < num_instance_layers; i++) {
194 const VkLayerProperties& props = properties[i];
195
Jesse Hall80523e22016-01-06 16:47:54 -0800196 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800197 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800198 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800199 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800200
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800201 uint32_t count = 0;
202 result =
203 enumerate_instance_extensions(props.layerName, &count, nullptr);
204 if (result != VK_SUCCESS) {
205 ALOGW(
206 "vkEnumerateInstanceExtensionProperties(%s) failed for library "
207 "'%s': %d",
208 props.layerName, path_.c_str(), result);
209 instance_layers.resize(prev_num_instance_layers);
210 return false;
211 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800212 layer.instance_extensions.resize(count);
213 result = enumerate_instance_extensions(
214 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800215 if (result != VK_SUCCESS) {
216 ALOGW(
217 "vkEnumerateInstanceExtensionProperties(%s) failed for library "
218 "'%s': %d",
219 props.layerName, path_.c_str(), result);
220 instance_layers.resize(prev_num_instance_layers);
221 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800222 }
223
Chia-I Wu279ccc02016-04-18 16:45:15 +0800224 for (size_t j = 0; j < num_device_layers; j++) {
225 const auto& dev_props = properties[num_instance_layers + j];
226 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800227 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800228 break;
229 }
230 }
Jesse Hallaa410942016-01-17 13:07:10 -0800231
Chia-I Wubea09db2016-04-22 09:42:41 +0800232 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800233 result = enumerate_device_extensions(
234 VK_NULL_HANDLE, props.layerName, &count, nullptr);
235 if (result != VK_SUCCESS) {
236 ALOGW(
237 "vkEnumerateDeviceExtensionProperties(%s) failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800238 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800239 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800240 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800242 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800243 layer.device_extensions.resize(count);
244 result = enumerate_device_extensions(
245 VK_NULL_HANDLE, props.layerName, &count,
246 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800247 if (result != VK_SUCCESS) {
248 ALOGW(
249 "vkEnumerateDeviceExtensionProperties(%s) failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800250 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800251 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800252 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800253 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800254 }
255 }
256
Chia-I Wubea09db2016-04-22 09:42:41 +0800257 instance_layers.push_back(layer);
258 ALOGV(" added %s layer '%s'",
259 (layer.is_global) ? "global" : "instance", props.layerName);
Jesse Hall80523e22016-01-06 16:47:54 -0800260 }
261
Chia-I Wu50174ee2016-04-18 16:33:20 +0800262 return true;
263}
Jesse Hall80523e22016-01-06 16:47:54 -0800264
Chia-I Wuba113272016-04-18 16:38:39 +0800265void* LayerLibrary::GetGPA(const Layer& layer,
266 const char* gpa_name,
267 size_t gpa_name_len) const {
268 void* gpa;
269 size_t layer_name_len =
270 std::max(size_t{2}, strlen(layer.properties.layerName));
271 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
272 strcpy(name, layer.properties.layerName);
273 strcpy(name + layer_name_len, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800274 if (!(gpa = dlsym(dlhandle_, name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800275 strcpy(name, "vk");
276 strcpy(name + 2, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800277 gpa = dlsym(dlhandle_, name);
Chia-I Wuba113272016-04-18 16:38:39 +0800278 }
279 return gpa;
280}
281
Chia-I Wu50174ee2016-04-18 16:33:20 +0800282std::vector<LayerLibrary> g_layer_libraries;
283std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800284
285void AddLayerLibrary(const std::string& path) {
286 ALOGV("examining layer library '%s'", path.c_str());
287
288 LayerLibrary library(path);
289 if (!library.Open())
290 return;
291
Chia-I Wubea09db2016-04-22 09:42:41 +0800292 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800293 library.Close();
294 return;
295 }
296
297 library.Close();
298
299 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800300}
301
302void DiscoverLayersInDirectory(const std::string& dir_path) {
303 ALOGV("looking for layers in '%s'", dir_path.c_str());
304
305 DIR* directory = opendir(dir_path.c_str());
306 if (!directory) {
307 int err = errno;
308 ALOGV_IF(err != ENOENT, "failed to open layer directory '%s': %s (%d)",
309 dir_path.c_str(), strerror(err), err);
310 return;
311 }
312
313 std::string path;
314 path.reserve(dir_path.size() + 20);
315 path.append(dir_path);
316 path.append("/");
317
318 struct dirent* entry;
319 while ((entry = readdir(directory))) {
320 size_t libname_len = strlen(entry->d_name);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800321 if (strncmp(entry->d_name, "libVkLayer", 10) != 0 ||
Jesse Hall80523e22016-01-06 16:47:54 -0800322 strncmp(entry->d_name + libname_len - 3, ".so", 3) != 0)
323 continue;
324 path.append(entry->d_name);
325 AddLayerLibrary(path);
326 path.resize(dir_path.size() + 1);
327 }
328
329 closedir(directory);
330}
331
Chia-I Wubea09db2016-04-22 09:42:41 +0800332const Layer* FindInstanceLayer(const char* name) {
Chia-I Wu50db9032016-04-22 10:10:04 +0800333 auto layer =
Chia-I Wubea09db2016-04-22 09:42:41 +0800334 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
335 [=](const Layer& entry) {
336 return strcmp(entry.properties.layerName, name) == 0;
337 });
338 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
339}
340
341const Layer* FindDeviceLayer(const char* name) {
342 const Layer* layer = FindInstanceLayer(name);
343 return (layer && layer->is_global) ? layer : nullptr;
Chia-I Wu50db9032016-04-22 10:10:04 +0800344}
345
Jesse Hall80523e22016-01-06 16:47:54 -0800346void* GetLayerGetProcAddr(const Layer& layer,
347 const char* gpa_name,
348 size_t gpa_name_len) {
349 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800350 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800351}
352
Jesse Hallaa410942016-01-17 13:07:10 -0800353} // anonymous namespace
354
Jesse Hallaa410942016-01-17 13:07:10 -0800355void DiscoverLayers() {
356 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0))
357 DiscoverLayersInDirectory("/data/local/debug/vulkan");
358 if (!LoaderData::GetInstance().layer_path.empty())
359 DiscoverLayersInDirectory(LoaderData::GetInstance().layer_path.c_str());
360}
361
362uint32_t EnumerateInstanceLayers(uint32_t count,
363 VkLayerProperties* properties) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800364 uint32_t n =
365 std::min(count, static_cast<uint32_t>(g_instance_layers.size()));
366 for (uint32_t i = 0; i < n; i++)
367 properties[i] = g_instance_layers[i].properties;
368
369 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800370}
371
372uint32_t EnumerateDeviceLayers(uint32_t count, VkLayerProperties* properties) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800373 uint32_t n = 0;
374 for (const auto& layer : g_instance_layers) {
375 // ignore non-global layers
376 if (!layer.is_global)
377 continue;
378
379 if (n < count)
380 properties[n] = layer.properties;
381 n++;
382 }
383
384 return n;
Jesse Hallaa410942016-01-17 13:07:10 -0800385}
386
387void GetInstanceLayerExtensions(const char* name,
388 const VkExtensionProperties** properties,
389 uint32_t* count) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800390 const Layer* layer = FindInstanceLayer(name);
391 if (layer) {
392 *properties = layer->instance_extensions.data();
393 *count = static_cast<uint32_t>(layer->instance_extensions.size());
394 } else {
395 *properties = nullptr;
396 *count = 0;
397 }
Jesse Hallaa410942016-01-17 13:07:10 -0800398}
399
400void GetDeviceLayerExtensions(const char* name,
401 const VkExtensionProperties** properties,
402 uint32_t* count) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800403 const Layer* layer = FindDeviceLayer(name);
404 if (layer) {
405 *properties = layer->device_extensions.data();
406 *count = static_cast<uint32_t>(layer->device_extensions.size());
407 } else {
408 *properties = nullptr;
409 *count = 0;
410 }
Jesse Hallaa410942016-01-17 13:07:10 -0800411}
412
413LayerRef GetInstanceLayerRef(const char* name) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800414 const Layer* layer = FindInstanceLayer(name);
415 if (layer) {
416 LayerLibrary& library = g_layer_libraries[layer->library_idx];
417 if (!library.Open())
418 layer = nullptr;
419 }
420
421 return LayerRef(layer, true);
Jesse Hallaa410942016-01-17 13:07:10 -0800422}
423
424LayerRef GetDeviceLayerRef(const char* name) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800425 const Layer* layer = FindDeviceLayer(name);
426 if (layer) {
427 LayerLibrary& library = g_layer_libraries[layer->library_idx];
428 if (!library.Open())
429 layer = nullptr;
430 }
431
432 return LayerRef(layer, false);
Jesse Hallaa410942016-01-17 13:07:10 -0800433}
434
Chia-I Wubea09db2016-04-22 09:42:41 +0800435LayerRef::LayerRef(const Layer* layer, bool is_instance)
436 : layer_(layer), is_instance_(is_instance) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800437
438LayerRef::~LayerRef() {
439 if (layer_) {
440 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800441 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800442 }
443}
444
Michael Lentine57036832016-03-04 11:03:35 -0600445const char* LayerRef::GetName() const {
Courtney Goeltzenleuchtereff63112016-02-08 20:12:59 -0700446 return layer_->properties.layerName;
447}
448
Chia-I Wu83506d82016-04-19 11:45:57 +0800449uint32_t LayerRef::GetSpecVersion() const {
Courtney Goeltzenleuchtereff63112016-02-08 20:12:59 -0700450 return layer_->properties.specVersion;
451}
452
Chia-I Wubea09db2016-04-22 09:42:41 +0800453LayerRef::LayerRef(LayerRef&& other)
454 : layer_(other.layer_), is_instance_(other.is_instance_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600455 other.layer_ = nullptr;
456}
Jesse Hall80523e22016-01-06 16:47:54 -0800457
458PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
459 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
460 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
461 : nullptr;
462}
463
464PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
465 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
466 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
467 : nullptr;
468}
469
Jesse Hallb1471272016-01-17 21:36:58 -0800470bool LayerRef::SupportsExtension(const char* name) const {
Chia-I Wubea09db2016-04-22 09:42:41 +0800471 const auto& extensions = (is_instance_) ? layer_->instance_extensions
472 : layer_->device_extensions;
473 return std::find_if(extensions.cbegin(), extensions.cend(),
Jesse Hallb1471272016-01-17 21:36:58 -0800474 [=](const VkExtensionProperties& ext) {
475 return strcmp(ext.extensionName, name) == 0;
Chia-I Wubea09db2016-04-22 09:42:41 +0800476 }) != extensions.cend();
Jesse Hallb1471272016-01-17 21:36:58 -0800477}
478
Chia-I Wuc96880f2016-03-26 06:56:45 +0800479} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800480} // namespace vulkan