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 | { |
Mike Stroyan | 5d1b3b3 | 2015-10-06 15:32:56 -0600 | [diff] [blame^] | 61 | #if defined(_WIN32) |
| 62 | _aligned_free(ptr); |
| 63 | #else |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 64 | free(ptr); |
Mike Stroyan | 5d1b3b3 | 2015-10-06 15:32:56 -0600 | [diff] [blame^] | 65 | #endif |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 68 | struct icd_instance *icd_instance_create(const VkApplicationInfo *app_info, |
| 69 | const VkAllocCallbacks *alloc_cb) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 70 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 71 | static const VkAllocCallbacks default_alloc_cb = { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 72 | .pfnAlloc = default_alloc, |
| 73 | .pfnFree = default_free, |
| 74 | }; |
| 75 | struct icd_instance *instance; |
| 76 | const char *name; |
| 77 | size_t len; |
| 78 | |
| 79 | if (!alloc_cb) |
| 80 | alloc_cb = &default_alloc_cb; |
| 81 | |
| 82 | instance = alloc_cb->pfnAlloc(alloc_cb->pUserData, sizeof(*instance), 0, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 83 | VK_SYSTEM_ALLOC_TYPE_API_OBJECT); |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 84 | if (!instance) |
| 85 | return NULL; |
| 86 | |
| 87 | memset(instance, 0, sizeof(*instance)); |
| 88 | |
| 89 | name = (app_info->pAppName) ? app_info->pAppName : "unnamed"; |
| 90 | len = strlen(name); |
| 91 | instance->name = alloc_cb->pfnAlloc(alloc_cb->pUserData, len + 1, 0, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 92 | VK_SYSTEM_ALLOC_TYPE_INTERNAL); |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 93 | if (!instance->name) { |
| 94 | alloc_cb->pfnFree(alloc_cb->pUserData, instance); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | memcpy(instance->name, name, len); |
| 99 | instance->name[len] = '\0'; |
| 100 | |
| 101 | instance->alloc_cb = *alloc_cb; |
| 102 | |
| 103 | return instance; |
| 104 | } |
| 105 | |
| 106 | void icd_instance_destroy(struct icd_instance *instance) |
| 107 | { |
| 108 | struct icd_instance_logger *logger; |
| 109 | |
Tony Barbour | f953021 | 2015-04-08 09:13:39 -0600 | [diff] [blame] | 110 | for (logger = instance->loggers; logger; ) { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 111 | struct icd_instance_logger *next = logger->next; |
| 112 | |
| 113 | icd_instance_free(instance, logger); |
| 114 | logger = next; |
| 115 | } |
| 116 | |
| 117 | icd_instance_free(instance, instance->name); |
| 118 | icd_instance_free(instance, instance); |
| 119 | } |
| 120 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 121 | VkResult icd_instance_create_logger( |
| 122 | struct icd_instance *instance, |
| 123 | VkFlags msg_flags, |
| 124 | PFN_vkDbgMsgCallback func, |
| 125 | void *user_data, |
| 126 | VkDbgMsgCallback *msg_obj) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 127 | { |
| 128 | struct icd_instance_logger *logger; |
| 129 | |
Courtney Goeltzenleuchter | a54b76a | 2015-09-04 13:39:59 -0600 | [diff] [blame] | 130 | /* TODOVV: Move this test to a validation layer */ |
| 131 | // if (msg_obj == NULL) { |
| 132 | // return VK_ERROR_INVALID_POINTER; |
| 133 | // } |
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 = icd_instance_alloc(instance, sizeof(*logger), 0, |
| 136 | VK_SYSTEM_ALLOC_TYPE_DEBUG); |
| 137 | if (!logger) |
| 138 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
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->func = func; |
| 141 | logger->flags = msg_flags; |
| 142 | logger->next = instance->loggers; |
| 143 | instance->loggers = logger; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 144 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 145 | logger->user_data = (void *) user_data; |
| 146 | |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 147 | *( struct icd_instance_logger **)msg_obj = logger; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 148 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 149 | return VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 150 | } |
| 151 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 152 | VkResult icd_instance_destroy_logger( |
| 153 | struct icd_instance *instance, |
| 154 | const VkDbgMsgCallback msg_obj) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 155 | { |
| 156 | struct icd_instance_logger *logger, *prev; |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 157 | VkDbgMsgCallback local_msg_obj = msg_obj; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 158 | |
| 159 | for (prev = NULL, logger = instance->loggers; logger; |
| 160 | prev = logger, logger = logger->next) { |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 161 | if (logger == *(struct icd_instance_logger **) &local_msg_obj) |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 162 | break; |
| 163 | } |
| 164 | |
Courtney Goeltzenleuchter | a54b76a | 2015-09-04 13:39:59 -0600 | [diff] [blame] | 165 | /* TODOVV: Move this to validation layer */ |
| 166 | // if (!logger) |
| 167 | // return VK_ERROR_INVALID_POINTER; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 168 | |
| 169 | if (prev) |
| 170 | prev->next = logger->next; |
| 171 | else |
| 172 | instance->loggers = logger->next; |
| 173 | |
| 174 | icd_instance_free(instance, logger); |
| 175 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 176 | return VK_SUCCESS; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void icd_instance_log(const struct icd_instance *instance, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 180 | VkFlags msg_flags, |
Tony Barbour | de4124d | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 181 | VkDbgObjectType obj_type, |
| 182 | uint64_t src_object, |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 183 | size_t location, int32_t msg_code, |
| 184 | const char *msg) |
| 185 | { |
| 186 | const struct icd_instance_logger *logger; |
| 187 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 188 | if (!instance->loggers) { |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 189 | fputs(msg, stderr); |
| 190 | fputc('\n', stderr); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 191 | return; |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | for (logger = instance->loggers; logger; logger = logger->next) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 195 | if (msg_flags & logger->flags) { |
Tony Barbour | 2a199c1 | 2015-07-09 17:31:46 -0600 | [diff] [blame] | 196 | logger->func(msg_flags, obj_type, (uint64_t)src_object, location, |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 197 | msg_code, instance->name, msg, logger->user_data); |
| 198 | } |
Chia-I Wu | 15d8677 | 2015-02-21 14:00:17 +0800 | [diff] [blame] | 199 | } |
| 200 | } |