blob: f9b1002efef21d2308f7fef74596403794a795e2 [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 Wudab25652016-04-28 07:15:51 +0800333const VkExtensionProperties* FindExtension(
334 const std::vector<VkExtensionProperties>& extensions,
335 const char* name) {
336 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
337 [=](const VkExtensionProperties& ext) {
338 return (strcmp(ext.extensionName, name) == 0);
339 });
340 return (it != extensions.cend()) ? &*it : nullptr;
341}
342
Jesse Hall80523e22016-01-06 16:47:54 -0800343void* GetLayerGetProcAddr(const Layer& layer,
344 const char* gpa_name,
345 size_t gpa_name_len) {
346 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800347 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800348}
349
Jesse Hallaa410942016-01-17 13:07:10 -0800350} // anonymous namespace
351
Jesse Hallaa410942016-01-17 13:07:10 -0800352void DiscoverLayers() {
353 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0))
354 DiscoverLayersInDirectory("/data/local/debug/vulkan");
355 if (!LoaderData::GetInstance().layer_path.empty())
356 DiscoverLayersInDirectory(LoaderData::GetInstance().layer_path.c_str());
357}
358
Chia-I Wu25700b42016-04-28 06:36:09 +0800359uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800360 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800361}
362
Chia-I Wu25700b42016-04-28 06:36:09 +0800363const Layer& GetLayer(uint32_t index) {
364 return g_instance_layers[index];
365}
Chia-I Wubea09db2016-04-22 09:42:41 +0800366
Chia-I Wu04c65512016-04-27 09:54:02 +0800367const Layer* FindLayer(const char* name) {
368 auto layer =
369 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
370 [=](const Layer& entry) {
371 return strcmp(entry.properties.layerName, name) == 0;
372 });
373 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
374}
375
Chia-I Wu25700b42016-04-28 06:36:09 +0800376const VkLayerProperties& GetLayerProperties(const Layer& layer) {
377 return layer.properties;
378}
Chia-I Wubea09db2016-04-22 09:42:41 +0800379
Chia-I Wu25700b42016-04-28 06:36:09 +0800380bool IsLayerGlobal(const Layer& layer) {
381 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800382}
383
Chia-I Wu04c65512016-04-27 09:54:02 +0800384const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
385 uint32_t& count) {
386 count = static_cast<uint32_t>(layer.instance_extensions.size());
387 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800388}
389
Chia-I Wu04c65512016-04-27 09:54:02 +0800390const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
391 uint32_t& count) {
392 count = static_cast<uint32_t>(layer.device_extensions.size());
393 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800394}
395
Chia-I Wudab25652016-04-28 07:15:51 +0800396const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
397 const char* name) {
398 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800399}
400
Chia-I Wudab25652016-04-28 07:15:51 +0800401const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
402 const char* name) {
403 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800404}
405
Chia-I Wudab25652016-04-28 07:15:51 +0800406LayerRef GetLayerRef(const Layer& layer) {
407 LayerLibrary& library = g_layer_libraries[layer.library_idx];
408 return LayerRef((library.Open()) ? &layer : nullptr);
409}
410
411LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800412
413LayerRef::~LayerRef() {
414 if (layer_) {
415 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800416 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800417 }
418}
419
Chia-I Wudab25652016-04-28 07:15:51 +0800420LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600421 other.layer_ = nullptr;
422}
Jesse Hall80523e22016-01-06 16:47:54 -0800423
424PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
425 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
426 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
427 : nullptr;
428}
429
430PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
431 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
432 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
433 : nullptr;
434}
435
Chia-I Wuc96880f2016-03-26 06:56:45 +0800436} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800437} // namespace vulkan