Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 1 | /* |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2 | * Vulkan |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2014-2015 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * Authors: |
| 25 | * Chia-I Wu <olv@lunarg.com> |
| 26 | */ |
| 27 | |
| 28 | #define _ISOC11_SOURCE /* for aligned_alloc() */ |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include "icd-instance.h" |
| 33 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 34 | static void * VKAPI default_alloc(void *user_data, size_t size, |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 35 | size_t alignment, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 36 | VkSystemAllocType allocType) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 37 | { |
| 38 | if (alignment <= 1) { |
| 39 | return malloc(size); |
Ian Elliott | 323a487 | 2015-03-06 13:49:44 -0700 | [diff] [blame] | 40 | } else if (u_is_pow2((unsigned int) alignment)) { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 41 | if (alignment < sizeof(void *)) { |
| 42 | assert(u_is_pow2(sizeof(void*))); |
| 43 | alignment = sizeof(void *); |
| 44 | } |
| 45 | |
| 46 | size = (size + alignment - 1) & ~(alignment - 1); |
| 47 | |
Ian Elliott | 65c65a5 | 2015-03-06 14:34:47 -0700 | [diff] [blame] | 48 | #if defined(_WIN32) |
| 49 | return _aligned_malloc(alignment, size); |
| 50 | #else |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 51 | return aligned_alloc(alignment, size); |
Ian Elliott | 65c65a5 | 2015-03-06 14:34:47 -0700 | [diff] [blame] | 52 | #endif |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 53 | } |
| 54 | else { |
| 55 | return NULL; |
| 56 | } |
| 57 | } |
| 58 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 59 | static void VKAPI default_free(void *user_data, void *ptr) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 60 | { |
| 61 | free(ptr); |
| 62 | } |
| 63 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 64 | struct icd_instance *icd_instance_create(const VkApplicationInfo *app_info, |
| 65 | const VkAllocCallbacks *alloc_cb) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 66 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 67 | static const VkAllocCallbacks default_alloc_cb = { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 68 | .pfnAlloc = default_alloc, |
| 69 | .pfnFree = default_free, |
| 70 | }; |
| 71 | struct icd_instance *instance; |
| 72 | const char *name; |
| 73 | size_t len; |
| 74 | |
| 75 | if (!alloc_cb) |
| 76 | alloc_cb = &default_alloc_cb; |
| 77 | |
| 78 | instance = alloc_cb->pfnAlloc(alloc_cb->pUserData, sizeof(*instance), 0, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 79 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 80 | if (!instance) |
| 81 | return NULL; |
| 82 | |
| 83 | memset(instance, 0, sizeof(*instance)); |
| 84 | |
| 85 | name = (app_info->pAppName) ? app_info->pAppName : "unnamed"; |
| 86 | len = strlen(name); |
| 87 | instance->name = alloc_cb->pfnAlloc(alloc_cb->pUserData, len + 1, 0, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 88 | VK_SYSTEM_ALLOC_TYPE_INTERNAL); |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 89 | if (!instance->name) { |
| 90 | alloc_cb->pfnFree(alloc_cb->pUserData, instance); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | memcpy(instance->name, name, len); |
| 95 | instance->name[len] = '\0'; |
| 96 | |
| 97 | instance->alloc_cb = *alloc_cb; |
| 98 | |
| 99 | return instance; |
| 100 | } |
| 101 | |
| 102 | void icd_instance_destroy(struct icd_instance *instance) |
| 103 | { |
| 104 | struct icd_instance_logger *logger; |
| 105 | |
Tony Barbour | f953021 | 2015-04-08 09:13:39 -0600 | [diff] [blame] | 106 | for (logger = instance->loggers; logger; ) { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 107 | struct icd_instance_logger *next = logger->next; |
| 108 | |
| 109 | icd_instance_free(instance, logger); |
| 110 | logger = next; |
| 111 | } |
| 112 | |
| 113 | icd_instance_free(instance, instance->name); |
| 114 | icd_instance_free(instance, instance); |
| 115 | } |
| 116 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 117 | VkResult icd_instance_create_logger( |
| 118 | struct icd_instance *instance, |
| 119 | VkFlags msg_flags, |
| 120 | PFN_vkDbgMsgCallback func, |
| 121 | void *user_data, |
| 122 | VkDbgMsgCallback *msg_obj) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 123 | { |
| 124 | struct icd_instance_logger *logger; |
| 125 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 126 | if (msg_obj == NULL) { |
| 127 | return VK_ERROR_INVALID_POINTER; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 130 | logger = icd_instance_alloc(instance, sizeof(*logger), 0, |
| 131 | VK_SYSTEM_ALLOC_TYPE_DEBUG); |
| 132 | if (!logger) |
| 133 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 134 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 135 | logger->func = func; |
| 136 | logger->flags = msg_flags; |
| 137 | logger->next = instance->loggers; |
| 138 | instance->loggers = logger; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 139 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 140 | logger->user_data = (void *) user_data; |
| 141 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 142 | *( struct icd_instance_logger **)msg_obj = logger; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 143 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 144 | return VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 147 | VkResult icd_instance_destroy_logger( |
| 148 | struct icd_instance *instance, |
| 149 | const VkDbgMsgCallback msg_obj) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 150 | { |
| 151 | struct icd_instance_logger *logger, *prev; |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 152 | VkDbgMsgCallback local_msg_obj = msg_obj; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 153 | |
| 154 | for (prev = NULL, logger = instance->loggers; logger; |
| 155 | prev = logger, logger = logger->next) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 156 | if (logger == *(struct icd_instance_logger **) &local_msg_obj) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
| 160 | if (!logger) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 161 | return VK_ERROR_INVALID_POINTER; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 162 | |
| 163 | if (prev) |
| 164 | prev->next = logger->next; |
| 165 | else |
| 166 | instance->loggers = logger->next; |
| 167 | |
| 168 | icd_instance_free(instance, logger); |
| 169 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 170 | return VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void icd_instance_log(const struct icd_instance *instance, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 174 | VkFlags msg_flags, |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 175 | VkDbgObjectType obj_type, |
| 176 | uint64_t src_object, |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 177 | size_t location, int32_t msg_code, |
| 178 | const char *msg) |
| 179 | { |
| 180 | const struct icd_instance_logger *logger; |
| 181 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 182 | if (!instance->loggers) { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 183 | fputs(msg, stderr); |
| 184 | fputc('\n', stderr); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 185 | return; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | for (logger = instance->loggers; logger; logger = logger->next) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 189 | if (msg_flags & logger->flags) { |
Tony Barbour | 2a199c1 | 2015-07-09 17:31:46 -0600 | [diff] [blame^] | 190 | logger->func(msg_flags, obj_type, (uint64_t)src_object, location, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 191 | msg_code, instance->name, msg, logger->user_data); |
| 192 | } |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 193 | } |
| 194 | } |