blob: b79204868c47d21d1040f704abdc8153d064a7f6 [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 Goeltzenleuchter382489d2015-04-10 08:34:15 -060036 VkSystemAllocType 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{
Mike Stroyan5d1b3b32015-10-06 15:32:56 -060061#if defined(_WIN32)
62 _aligned_free(ptr);
63#else
Chia-I Wu15d86772015-02-21 14:00:17 +080064 free(ptr);
Mike Stroyan5d1b3b32015-10-06 15:32:56 -060065#endif
Chia-I Wu15d86772015-02-21 14:00:17 +080066}
67
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060068struct icd_instance *icd_instance_create(const VkApplicationInfo *app_info,
69 const VkAllocCallbacks *alloc_cb)
Chia-I Wu15d86772015-02-21 14:00:17 +080070{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060071 static const VkAllocCallbacks default_alloc_cb = {
Chia-I Wu15d86772015-02-21 14:00:17 +080072 .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 Barbour8205d902015-04-16 15:59:00 -060083 VK_SYSTEM_ALLOC_TYPE_API_OBJECT);
Chia-I Wu15d86772015-02-21 14:00:17 +080084 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 Barbour8205d902015-04-16 15:59:00 -060092 VK_SYSTEM_ALLOC_TYPE_INTERNAL);
Chia-I Wu15d86772015-02-21 14:00:17 +080093 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
106void icd_instance_destroy(struct icd_instance *instance)
107{
108 struct icd_instance_logger *logger;
109
Tony Barbourf9530212015-04-08 09:13:39 -0600110 for (logger = instance->loggers; logger; ) {
Chia-I Wu15d86772015-02-21 14:00:17 +0800111 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 Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600121VkResult 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 Wu15d86772015-02-21 14:00:17 +0800127{
128 struct icd_instance_logger *logger;
129
Courtney Goeltzenleuchtera54b76a2015-09-04 13:39:59 -0600130 /* TODOVV: Move this test to a validation layer */
131// if (msg_obj == NULL) {
132// return VK_ERROR_INVALID_POINTER;
133// }
Chia-I Wu15d86772015-02-21 14:00:17 +0800134
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600135 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 Wu15d86772015-02-21 14:00:17 +0800139
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600140 logger->func = func;
141 logger->flags = msg_flags;
142 logger->next = instance->loggers;
143 instance->loggers = logger;
Chia-I Wu15d86772015-02-21 14:00:17 +0800144
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600145 logger->user_data = (void *) user_data;
146
Tony Barbourde4124d2015-07-03 10:33:54 -0600147 *( struct icd_instance_logger **)msg_obj = logger;
Chia-I Wu15d86772015-02-21 14:00:17 +0800148
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600149 return VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800150}
151
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600152VkResult icd_instance_destroy_logger(
153 struct icd_instance *instance,
154 const VkDbgMsgCallback msg_obj)
Chia-I Wu15d86772015-02-21 14:00:17 +0800155{
156 struct icd_instance_logger *logger, *prev;
Tony Barbourde4124d2015-07-03 10:33:54 -0600157 VkDbgMsgCallback local_msg_obj = msg_obj;
Chia-I Wu15d86772015-02-21 14:00:17 +0800158
159 for (prev = NULL, logger = instance->loggers; logger;
160 prev = logger, logger = logger->next) {
Tony Barbourde4124d2015-07-03 10:33:54 -0600161 if (logger == *(struct icd_instance_logger **) &local_msg_obj)
Chia-I Wu15d86772015-02-21 14:00:17 +0800162 break;
163 }
164
Courtney Goeltzenleuchtera54b76a2015-09-04 13:39:59 -0600165 /* TODOVV: Move this to validation layer */
166// if (!logger)
167// return VK_ERROR_INVALID_POINTER;
Chia-I Wu15d86772015-02-21 14:00:17 +0800168
169 if (prev)
170 prev->next = logger->next;
171 else
172 instance->loggers = logger->next;
173
174 icd_instance_free(instance, logger);
175
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600176 return VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800177}
178
179void icd_instance_log(const struct icd_instance *instance,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600180 VkFlags msg_flags,
Tony Barbourde4124d2015-07-03 10:33:54 -0600181 VkDbgObjectType obj_type,
182 uint64_t src_object,
Chia-I Wu15d86772015-02-21 14:00:17 +0800183 size_t location, int32_t msg_code,
184 const char *msg)
185{
186 const struct icd_instance_logger *logger;
187
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600188 if (!instance->loggers) {
Chia-I Wu15d86772015-02-21 14:00:17 +0800189 fputs(msg, stderr);
190 fputc('\n', stderr);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600191 return;
Chia-I Wu15d86772015-02-21 14:00:17 +0800192 }
193
194 for (logger = instance->loggers; logger; logger = logger->next) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600195 if (msg_flags & logger->flags) {
Tony Barbour2a199c12015-07-09 17:31:46 -0600196 logger->func(msg_flags, obj_type, (uint64_t)src_object, location,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600197 msg_code, instance->name, msg, logger->user_data);
198 }
Chia-I Wu15d86772015-02-21 14:00:17 +0800199 }
200}