blob: fed138e780d79f099358a0d29a5d2a6ec350e484 [file] [log] [blame]
Chia-I Wu15d86772015-02-21 14:00:17 +08001/*
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002 * Vulkan
Chia-I Wu15d86772015-02-21 14:00:17 +08003 *
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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060034static void * VKAPI default_alloc(void *user_data, size_t size,
Chia-I Wu15d86772015-02-21 14:00:17 +080035 size_t alignment,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060036 VK_SYSTEM_ALLOC_TYPE allocType)
Chia-I Wu15d86772015-02-21 14:00:17 +080037{
38 if (alignment <= 1) {
39 return malloc(size);
Ian Elliott323a4872015-03-06 13:49:44 -070040 } else if (u_is_pow2((unsigned int) alignment)) {
Chia-I Wu15d86772015-02-21 14:00:17 +080041 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 Elliott65c65a52015-03-06 14:34:47 -070048#if defined(_WIN32)
49 return _aligned_malloc(alignment, size);
50#else
Chia-I Wu15d86772015-02-21 14:00:17 +080051 return aligned_alloc(alignment, size);
Ian Elliott65c65a52015-03-06 14:34:47 -070052#endif
Chia-I Wu15d86772015-02-21 14:00:17 +080053 }
54 else {
55 return NULL;
56 }
57}
58
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060059static void VKAPI default_free(void *user_data, void *ptr)
Chia-I Wu15d86772015-02-21 14:00:17 +080060{
61 free(ptr);
62}
63
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060064struct icd_instance *icd_instance_create(const VK_APPLICATION_INFO *app_info,
65 const VK_ALLOC_CALLBACKS *alloc_cb)
Chia-I Wu15d86772015-02-21 14:00:17 +080066{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060067 static const VK_ALLOC_CALLBACKS default_alloc_cb = {
Chia-I Wu15d86772015-02-21 14:00:17 +080068 .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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060079 VK_SYSTEM_ALLOC_API_OBJECT);
Chia-I Wu15d86772015-02-21 14:00:17 +080080 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060088 VK_SYSTEM_ALLOC_INTERNAL);
Chia-I Wu15d86772015-02-21 14:00:17 +080089 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
102void icd_instance_destroy(struct icd_instance *instance)
103{
104 struct icd_instance_logger *logger;
105
Tony Barbourf9530212015-04-08 09:13:39 -0600106 for (logger = instance->loggers; logger; ) {
Chia-I Wu15d86772015-02-21 14:00:17 +0800107 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600117VK_RESULT icd_instance_set_bool(struct icd_instance *instance,
118 VK_DBG_GLOBAL_OPTION option, bool yes)
Chia-I Wu15d86772015-02-21 14:00:17 +0800119{
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600120 VK_RESULT res = VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800121
122 switch (option) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600123 case VK_DBG_OPTION_DEBUG_ECHO_ENABLE:
Chia-I Wu15d86772015-02-21 14:00:17 +0800124 instance->debug_echo_enable = yes;
125 break;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600126 case VK_DBG_OPTION_BREAK_ON_ERROR:
Chia-I Wu15d86772015-02-21 14:00:17 +0800127 instance->break_on_error = yes;
128 break;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600129 case VK_DBG_OPTION_BREAK_ON_WARNING:
Chia-I Wu15d86772015-02-21 14:00:17 +0800130 instance->break_on_warning = yes;
131 break;
132 default:
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600133 res = VK_ERROR_INVALID_VALUE;
Chia-I Wu15d86772015-02-21 14:00:17 +0800134 break;
135 }
136
137 return res;
138}
139
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600140VK_RESULT icd_instance_add_logger(struct icd_instance *instance,
141 VK_DBG_MSG_CALLBACK_FUNCTION func,
Chia-I Wu15d86772015-02-21 14:00:17 +0800142 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600153 VK_SYSTEM_ALLOC_DEBUG);
Chia-I Wu15d86772015-02-21 14:00:17 +0800154 if (!logger)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600155 return VK_ERROR_OUT_OF_MEMORY;
Chia-I Wu15d86772015-02-21 14:00:17 +0800156
157 logger->func = func;
158 logger->next = instance->loggers;
159 instance->loggers = logger;
160 }
161
162 logger->user_data = user_data;
163
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600164 return VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800165}
166
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600167VK_RESULT icd_instance_remove_logger(struct icd_instance *instance,
168 VK_DBG_MSG_CALLBACK_FUNCTION func)
Chia-I Wu15d86772015-02-21 14:00:17 +0800169{
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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600179 return VK_ERROR_INVALID_POINTER;
Chia-I Wu15d86772015-02-21 14:00:17 +0800180
181 if (prev)
182 prev->next = logger->next;
183 else
184 instance->loggers = logger->next;
185
186 icd_instance_free(instance, logger);
187
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600188 return VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800189}
190
191void icd_instance_log(const struct icd_instance *instance,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600192 VK_DBG_MSG_TYPE msg_type,
193 VK_VALIDATION_LEVEL validation_level,
194 VK_BASE_OBJECT src_object,
Chia-I Wu15d86772015-02-21 14:00:17 +0800195 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 Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600211 case VK_DBG_MSG_ERROR:
Chia-I Wu15d86772015-02-21 14:00:17 +0800212 if (instance->break_on_error)
213 abort();
214 /* fall through */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600215 case VK_DBG_MSG_WARNING:
Chia-I Wu15d86772015-02-21 14:00:17 +0800216 if (instance->break_on_warning)
217 abort();
218 break;
219 default:
220 break;
221 }
222}