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 | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 36 | VK_SYSTEM_ALLOC_TYPE 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 | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 64 | struct icd_instance *icd_instance_create(const VK_APPLICATION_INFO *app_info, |
| 65 | const VK_ALLOC_CALLBACKS *alloc_cb) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 66 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 67 | static const VK_ALLOC_CALLBACKS 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, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 79 | VK_SYSTEM_ALLOC_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, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 88 | VK_SYSTEM_ALLOC_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 | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 117 | VK_RESULT icd_instance_set_bool(struct icd_instance *instance, |
| 118 | VK_DBG_GLOBAL_OPTION option, bool yes) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 119 | { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 120 | VK_RESULT res = VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 121 | |
| 122 | switch (option) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 123 | case VK_DBG_OPTION_DEBUG_ECHO_ENABLE: |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 124 | instance->debug_echo_enable = yes; |
| 125 | break; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 126 | case VK_DBG_OPTION_BREAK_ON_ERROR: |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 127 | instance->break_on_error = yes; |
| 128 | break; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 129 | case VK_DBG_OPTION_BREAK_ON_WARNING: |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 130 | instance->break_on_warning = yes; |
| 131 | break; |
| 132 | default: |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 133 | res = VK_ERROR_INVALID_VALUE; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | |
| 137 | return res; |
| 138 | } |
| 139 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 140 | VK_RESULT icd_instance_add_logger(struct icd_instance *instance, |
| 141 | VK_DBG_MSG_CALLBACK_FUNCTION func, |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 142 | void *user_data) |
| 143 | { |
| 144 | struct icd_instance_logger *logger; |
| 145 | |
| 146 | for (logger = instance->loggers; logger; logger = logger->next) { |
| 147 | if (logger->func == func) |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | if (!logger) { |
| 152 | logger = icd_instance_alloc(instance, sizeof(*logger), 0, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 153 | VK_SYSTEM_ALLOC_DEBUG); |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 154 | if (!logger) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 155 | return VK_ERROR_OUT_OF_MEMORY; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 156 | |
| 157 | logger->func = func; |
| 158 | logger->next = instance->loggers; |
| 159 | instance->loggers = logger; |
| 160 | } |
| 161 | |
| 162 | logger->user_data = user_data; |
| 163 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 164 | return VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 167 | VK_RESULT icd_instance_remove_logger(struct icd_instance *instance, |
| 168 | VK_DBG_MSG_CALLBACK_FUNCTION func) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 169 | { |
| 170 | struct icd_instance_logger *logger, *prev; |
| 171 | |
| 172 | for (prev = NULL, logger = instance->loggers; logger; |
| 173 | prev = logger, logger = logger->next) { |
| 174 | if (logger->func == func) |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | if (!logger) |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 179 | return VK_ERROR_INVALID_POINTER; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 180 | |
| 181 | if (prev) |
| 182 | prev->next = logger->next; |
| 183 | else |
| 184 | instance->loggers = logger->next; |
| 185 | |
| 186 | icd_instance_free(instance, logger); |
| 187 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 188 | return VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void icd_instance_log(const struct icd_instance *instance, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 192 | VK_DBG_MSG_TYPE msg_type, |
| 193 | VK_VALIDATION_LEVEL validation_level, |
| 194 | VK_BASE_OBJECT src_object, |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 195 | size_t location, int32_t msg_code, |
| 196 | const char *msg) |
| 197 | { |
| 198 | const struct icd_instance_logger *logger; |
| 199 | |
| 200 | if (instance->debug_echo_enable || !instance->loggers) { |
| 201 | fputs(msg, stderr); |
| 202 | fputc('\n', stderr); |
| 203 | } |
| 204 | |
| 205 | for (logger = instance->loggers; logger; logger = logger->next) { |
| 206 | logger->func(msg_type, validation_level, src_object, location, |
| 207 | msg_code, msg, logger->user_data); |
| 208 | } |
| 209 | |
| 210 | switch (msg_type) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 211 | case VK_DBG_MSG_ERROR: |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 212 | if (instance->break_on_error) |
| 213 | abort(); |
| 214 | /* fall through */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame^] | 215 | case VK_DBG_MSG_WARNING: |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 216 | if (instance->break_on_warning) |
| 217 | abort(); |
| 218 | break; |
| 219 | default: |
| 220 | break; |
| 221 | } |
| 222 | } |