blob: 4363dd8fa81f3450763e0608e1e68b04d3539d59 [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
Chia-I Wu25700b42016-04-28 06:36:09 +080053 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080054 bool is_global;
55
56 std::vector<VkExtensionProperties> instance_extensions;
57 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080058};
Jesse Hall80523e22016-01-06 16:47:54 -080059
60namespace {
61
Chia-I Wu6693f5c2016-04-18 12:20:02 +080062class LayerLibrary {
63 public:
64 LayerLibrary(const std::string& path)
65 : path_(path), dlhandle_(nullptr), refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080066
Chia-I Wu50174ee2016-04-18 16:33:20 +080067 LayerLibrary(LayerLibrary&& other)
Chia-I Wu6693f5c2016-04-18 12:20:02 +080068 : path_(std::move(other.path_)),
69 dlhandle_(other.dlhandle_),
70 refcount_(other.refcount_) {
71 other.dlhandle_ = nullptr;
72 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080073 }
74
75 LayerLibrary(const LayerLibrary&) = delete;
76 LayerLibrary& operator=(const LayerLibrary&) = delete;
77
Chia-I Wua6229742016-04-26 07:37:44 +080078 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080079 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080080 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080081
Chia-I Wu50174ee2016-04-18 16:33:20 +080082 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080083 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080084
Chia-I Wuba113272016-04-18 16:38:39 +080085 void* GetGPA(const Layer& layer,
86 const char* gpa_name,
87 size_t gpa_name_len) const;
88
Chia-I Wu6693f5c2016-04-18 12:20:02 +080089 private:
90 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +080091
92 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +080093 void* dlhandle_;
94 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -080095};
Chia-I Wu74349592016-04-18 12:08:39 +080096
Chia-I Wufd0389f2016-04-18 12:11:00 +080097bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +080098 std::lock_guard<std::mutex> lock(mutex_);
99
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800100 if (refcount_++ == 0) {
101 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
102 ALOGV("Opening library %s", path_.c_str());
103 if (!dlhandle_) {
104 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
Chia-I Wufd0389f2016-04-18 12:11:00 +0800105 dlerror());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800106 refcount_ = 0;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800107 return false;
108 }
109 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800110 ALOGV("Refcount on activate is %zu", refcount_);
Chia-I Wufd0389f2016-04-18 12:11:00 +0800111 return true;
112}
113
Chia-I Wud91c74f2016-04-18 12:12:36 +0800114void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800115 std::lock_guard<std::mutex> lock(mutex_);
116
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800117 if (--refcount_ == 0) {
118 ALOGV("Closing library %s", path_.c_str());
119 dlclose(dlhandle_);
120 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800121 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800122 ALOGV("Refcount on destruction is %zu", refcount_);
Chia-I Wud91c74f2016-04-18 12:12:36 +0800123}
124
Chia-I Wu50174ee2016-04-18 16:33:20 +0800125bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800126 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800127 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Jesse Hall80523e22016-01-06 16:47:54 -0800128 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800129 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800130 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Jesse Hall80523e22016-01-06 16:47:54 -0800131 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800132 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties"));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800133 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
134 ALOGV("layer library '%s' misses some instance enumeraion functions",
135 path_.c_str());
136 return false;
137 }
138
139 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800140 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
141 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800142 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800143 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
144 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800145 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties"));
Jesse Hall80523e22016-01-06 16:47:54 -0800146
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800147 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800148 uint32_t num_instance_layers = 0;
149 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800150 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
151 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800152 if (result != VK_SUCCESS) {
153 ALOGW(
154 "vkEnumerateInstanceLayerProperties failed for library '%s': "
155 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800156 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800157 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800158 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800159 }
Jesse Hallaa410942016-01-17 13:07:10 -0800160 if (enumerate_device_layers) {
161 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
162 nullptr);
163 if (result != VK_SUCCESS) {
164 ALOGW(
165 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800166 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800167 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800168 }
169 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800170
171 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800172 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
173 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800174 result = enumerate_instance_layers(&num_instance_layers, properties);
175 if (result != VK_SUCCESS) {
176 ALOGW("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
177 path_.c_str(), result);
178 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800179 }
180 if (num_device_layers > 0) {
181 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
182 properties + num_instance_layers);
183 if (result != VK_SUCCESS) {
184 ALOGW(
185 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800186 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800187 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800188 }
Jesse Hall80523e22016-01-06 16:47:54 -0800189 }
190
Chia-I Wubea09db2016-04-22 09:42:41 +0800191 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800192 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800193 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800194 for (size_t i = 0; i < num_instance_layers; i++) {
195 const VkLayerProperties& props = properties[i];
196
Jesse Hall80523e22016-01-06 16:47:54 -0800197 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800198 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800199 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800200 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800201
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800202 uint32_t count = 0;
203 result =
204 enumerate_instance_extensions(props.layerName, &count, nullptr);
205 if (result != VK_SUCCESS) {
206 ALOGW(
207 "vkEnumerateInstanceExtensionProperties(%s) failed for library "
208 "'%s': %d",
209 props.layerName, path_.c_str(), result);
210 instance_layers.resize(prev_num_instance_layers);
211 return false;
212 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800213 layer.instance_extensions.resize(count);
214 result = enumerate_instance_extensions(
215 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800216 if (result != VK_SUCCESS) {
217 ALOGW(
218 "vkEnumerateInstanceExtensionProperties(%s) failed for library "
219 "'%s': %d",
220 props.layerName, path_.c_str(), result);
221 instance_layers.resize(prev_num_instance_layers);
222 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800223 }
224
Chia-I Wu279ccc02016-04-18 16:45:15 +0800225 for (size_t j = 0; j < num_device_layers; j++) {
226 const auto& dev_props = properties[num_instance_layers + j];
227 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800228 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800229 break;
230 }
231 }
Jesse Hallaa410942016-01-17 13:07:10 -0800232
Chia-I Wubea09db2016-04-22 09:42:41 +0800233 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800234 result = enumerate_device_extensions(
235 VK_NULL_HANDLE, props.layerName, &count, nullptr);
236 if (result != VK_SUCCESS) {
237 ALOGW(
238 "vkEnumerateDeviceExtensionProperties(%s) failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800239 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800240 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800242 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800243 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800244 layer.device_extensions.resize(count);
245 result = enumerate_device_extensions(
246 VK_NULL_HANDLE, props.layerName, &count,
247 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800248 if (result != VK_SUCCESS) {
249 ALOGW(
250 "vkEnumerateDeviceExtensionProperties(%s) failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800251 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800252 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800253 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800254 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800255 }
256 }
257
Chia-I Wubea09db2016-04-22 09:42:41 +0800258 instance_layers.push_back(layer);
259 ALOGV(" added %s layer '%s'",
260 (layer.is_global) ? "global" : "instance", props.layerName);
Jesse Hall80523e22016-01-06 16:47:54 -0800261 }
262
Chia-I Wu50174ee2016-04-18 16:33:20 +0800263 return true;
264}
Jesse Hall80523e22016-01-06 16:47:54 -0800265
Chia-I Wuba113272016-04-18 16:38:39 +0800266void* LayerLibrary::GetGPA(const Layer& layer,
267 const char* gpa_name,
268 size_t gpa_name_len) const {
269 void* gpa;
270 size_t layer_name_len =
271 std::max(size_t{2}, strlen(layer.properties.layerName));
272 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
273 strcpy(name, layer.properties.layerName);
274 strcpy(name + layer_name_len, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800275 if (!(gpa = dlsym(dlhandle_, name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800276 strcpy(name, "vk");
277 strcpy(name + 2, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800278 gpa = dlsym(dlhandle_, name);
Chia-I Wuba113272016-04-18 16:38:39 +0800279 }
280 return gpa;
281}
282
Chia-I Wu50174ee2016-04-18 16:33:20 +0800283std::vector<LayerLibrary> g_layer_libraries;
284std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800285
286void AddLayerLibrary(const std::string& path) {
287 ALOGV("examining layer library '%s'", path.c_str());
288
289 LayerLibrary library(path);
290 if (!library.Open())
291 return;
292
Chia-I Wubea09db2016-04-22 09:42:41 +0800293 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800294 library.Close();
295 return;
296 }
297
298 library.Close();
299
300 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800301}
302
303void DiscoverLayersInDirectory(const std::string& dir_path) {
304 ALOGV("looking for layers in '%s'", dir_path.c_str());
305
306 DIR* directory = opendir(dir_path.c_str());
307 if (!directory) {
308 int err = errno;
309 ALOGV_IF(err != ENOENT, "failed to open layer directory '%s': %s (%d)",
310 dir_path.c_str(), strerror(err), err);
311 return;
312 }
313
314 std::string path;
315 path.reserve(dir_path.size() + 20);
316 path.append(dir_path);
317 path.append("/");
318
319 struct dirent* entry;
320 while ((entry = readdir(directory))) {
321 size_t libname_len = strlen(entry->d_name);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800322 if (strncmp(entry->d_name, "libVkLayer", 10) != 0 ||
Jesse Hall80523e22016-01-06 16:47:54 -0800323 strncmp(entry->d_name + libname_len - 3, ".so", 3) != 0)
324 continue;
325 path.append(entry->d_name);
326 AddLayerLibrary(path);
327 path.resize(dir_path.size() + 1);
328 }
329
330 closedir(directory);
331}
332
Chia-I Wubea09db2016-04-22 09:42:41 +0800333const Layer* FindInstanceLayer(const char* name) {
Chia-I Wu50db9032016-04-22 10:10:04 +0800334 auto layer =
Chia-I Wubea09db2016-04-22 09:42:41 +0800335 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
336 [=](const Layer& entry) {
337 return strcmp(entry.properties.layerName, name) == 0;
338 });
339 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
340}
341
342const Layer* FindDeviceLayer(const char* name) {
343 const Layer* layer = FindInstanceLayer(name);
344 return (layer && layer->is_global) ? layer : nullptr;
Chia-I Wu50db9032016-04-22 10:10:04 +0800345}
346
Jesse Hall80523e22016-01-06 16:47:54 -0800347void* GetLayerGetProcAddr(const Layer& layer,
348 const char* gpa_name,
349 size_t gpa_name_len) {
350 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800351 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800352}
353
Jesse Hallaa410942016-01-17 13:07:10 -0800354} // anonymous namespace
355
Jesse Hallaa410942016-01-17 13:07:10 -0800356void DiscoverLayers() {
357 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0))
358 DiscoverLayersInDirectory("/data/local/debug/vulkan");
359 if (!LoaderData::GetInstance().layer_path.empty())
360 DiscoverLayersInDirectory(LoaderData::GetInstance().layer_path.c_str());
361}
362
Chia-I Wu25700b42016-04-28 06:36:09 +0800363uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800364 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800365}
366
Chia-I Wu25700b42016-04-28 06:36:09 +0800367const Layer& GetLayer(uint32_t index) {
368 return g_instance_layers[index];
369}
Chia-I Wubea09db2016-04-22 09:42:41 +0800370
Chia-I Wu25700b42016-04-28 06:36:09 +0800371const VkLayerProperties& GetLayerProperties(const Layer& layer) {
372 return layer.properties;
373}
Chia-I Wubea09db2016-04-22 09:42:41 +0800374
Chia-I Wu25700b42016-04-28 06:36:09 +0800375bool IsLayerGlobal(const Layer& layer) {
376 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800377}
378
379void GetInstanceLayerExtensions(const char* name,
380 const VkExtensionProperties** properties,
381 uint32_t* count) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800382 const Layer* layer = FindInstanceLayer(name);
383 if (layer) {
384 *properties = layer->instance_extensions.data();
385 *count = static_cast<uint32_t>(layer->instance_extensions.size());
386 } else {
387 *properties = nullptr;
388 *count = 0;
389 }
Jesse Hallaa410942016-01-17 13:07:10 -0800390}
391
392void GetDeviceLayerExtensions(const char* name,
393 const VkExtensionProperties** properties,
394 uint32_t* count) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800395 const Layer* layer = FindDeviceLayer(name);
396 if (layer) {
397 *properties = layer->device_extensions.data();
398 *count = static_cast<uint32_t>(layer->device_extensions.size());
399 } else {
400 *properties = nullptr;
401 *count = 0;
402 }
Jesse Hallaa410942016-01-17 13:07:10 -0800403}
404
405LayerRef GetInstanceLayerRef(const char* name) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800406 const Layer* layer = FindInstanceLayer(name);
407 if (layer) {
408 LayerLibrary& library = g_layer_libraries[layer->library_idx];
409 if (!library.Open())
410 layer = nullptr;
411 }
412
413 return LayerRef(layer, true);
Jesse Hallaa410942016-01-17 13:07:10 -0800414}
415
416LayerRef GetDeviceLayerRef(const char* name) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800417 const Layer* layer = FindDeviceLayer(name);
418 if (layer) {
419 LayerLibrary& library = g_layer_libraries[layer->library_idx];
420 if (!library.Open())
421 layer = nullptr;
422 }
423
424 return LayerRef(layer, false);
Jesse Hallaa410942016-01-17 13:07:10 -0800425}
426
Chia-I Wubea09db2016-04-22 09:42:41 +0800427LayerRef::LayerRef(const Layer* layer, bool is_instance)
428 : layer_(layer), is_instance_(is_instance) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800429
430LayerRef::~LayerRef() {
431 if (layer_) {
432 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800433 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800434 }
435}
436
Michael Lentine57036832016-03-04 11:03:35 -0600437const char* LayerRef::GetName() const {
Courtney Goeltzenleuchtereff63112016-02-08 20:12:59 -0700438 return layer_->properties.layerName;
439}
440
Chia-I Wu83506d82016-04-19 11:45:57 +0800441uint32_t LayerRef::GetSpecVersion() const {
Courtney Goeltzenleuchtereff63112016-02-08 20:12:59 -0700442 return layer_->properties.specVersion;
443}
444
Chia-I Wubea09db2016-04-22 09:42:41 +0800445LayerRef::LayerRef(LayerRef&& other)
446 : layer_(other.layer_), is_instance_(other.is_instance_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600447 other.layer_ = nullptr;
448}
Jesse Hall80523e22016-01-06 16:47:54 -0800449
450PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
451 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
452 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
453 : nullptr;
454}
455
456PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
457 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
458 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
459 : nullptr;
460}
461
Jesse Hallb1471272016-01-17 21:36:58 -0800462bool LayerRef::SupportsExtension(const char* name) const {
Chia-I Wubea09db2016-04-22 09:42:41 +0800463 const auto& extensions = (is_instance_) ? layer_->instance_extensions
464 : layer_->device_extensions;
465 return std::find_if(extensions.cbegin(), extensions.cend(),
Jesse Hallb1471272016-01-17 21:36:58 -0800466 [=](const VkExtensionProperties& ext) {
467 return strcmp(ext.extensionName, name) == 0;
Chia-I Wubea09db2016-04-22 09:42:41 +0800468 }) != extensions.cend();
Jesse Hallb1471272016-01-17 21:36:58 -0800469}
470
Chia-I Wuc96880f2016-03-26 06:56:45 +0800471} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800472} // namespace vulkan