blob: 449ff98470538287e4a1a93bcb1d92389a893585 [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{
61 free(ptr);
62}
63
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060064struct icd_instance *icd_instance_create(const VkApplicationInfo *app_info,
65 const VkAllocCallbacks *alloc_cb)
Chia-I Wu15d86772015-02-21 14:00:17 +080066{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060067 static const VkAllocCallbacks 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,
Tony Barbour8205d902015-04-16 15:59:00 -060079 VK_SYSTEM_ALLOC_TYPE_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,
Tony Barbour8205d902015-04-16 15:59:00 -060088 VK_SYSTEM_ALLOC_TYPE_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 Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600117VkResult 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 Wu15d86772015-02-21 14:00:17 +0800123{
124 struct icd_instance_logger *logger;
125
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600126 if (msg_obj == NULL) {
127 return VK_ERROR_INVALID_POINTER;
Chia-I Wu15d86772015-02-21 14:00:17 +0800128 }
129
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600130 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 Wu15d86772015-02-21 14:00:17 +0800134
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600135 logger->func = func;
136 logger->flags = msg_flags;
137 logger->next = instance->loggers;
138 instance->loggers = logger;
Chia-I Wu15d86772015-02-21 14:00:17 +0800139
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600140 logger->user_data = (void *) user_data;
141
Tony Barbourde4124d2015-07-03 10:33:54 -0600142 *( struct icd_instance_logger **)msg_obj = logger;
Chia-I Wu15d86772015-02-21 14:00:17 +0800143
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600144 return VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800145}
146
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600147VkResult icd_instance_destroy_logger(
148 struct icd_instance *instance,
149 const VkDbgMsgCallback msg_obj)
Chia-I Wu15d86772015-02-21 14:00:17 +0800150{
151 struct icd_instance_logger *logger, *prev;
Tony Barbourde4124d2015-07-03 10:33:54 -0600152 VkDbgMsgCallback local_msg_obj = msg_obj;
Chia-I Wu15d86772015-02-21 14:00:17 +0800153
154 for (prev = NULL, logger = instance->loggers; logger;
155 prev = logger, logger = logger->next) {
Tony Barbourde4124d2015-07-03 10:33:54 -0600156 if (logger == *(struct icd_instance_logger **) &local_msg_obj)
Chia-I Wu15d86772015-02-21 14:00:17 +0800157 break;
158 }
159
160 if (!logger)
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600161 return VK_ERROR_INVALID_POINTER;
Chia-I Wu15d86772015-02-21 14:00:17 +0800162
163 if (prev)
164 prev->next = logger->next;
165 else
166 instance->loggers = logger->next;
167
168 icd_instance_free(instance, logger);
169
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600170 return VK_SUCCESS;
Chia-I Wu15d86772015-02-21 14:00:17 +0800171}
172
173void icd_instance_log(const struct icd_instance *instance,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600174 VkFlags msg_flags,
Tony Barbourde4124d2015-07-03 10:33:54 -0600175 VkDbgObjectType obj_type,
176 uint64_t src_object,
Chia-I Wu15d86772015-02-21 14:00:17 +0800177 size_t location, int32_t msg_code,
178 const char *msg)
179{
180 const struct icd_instance_logger *logger;
181
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600182 if (!instance->loggers) {
Chia-I Wu15d86772015-02-21 14:00:17 +0800183 fputs(msg, stderr);
184 fputc('\n', stderr);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600185 return;
Chia-I Wu15d86772015-02-21 14:00:17 +0800186 }
187
188 for (logger = instance->loggers; logger; logger = logger->next) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600189 if (msg_flags & logger->flags) {
Tony Barbour2a199c12015-07-09 17:31:46 -0600190 logger->func(msg_flags, obj_type, (uint64_t)src_object, location,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600191 msg_code, instance->name, msg, logger->user_data);
192 }
Chia-I Wu15d86772015-02-21 14:00:17 +0800193 }
194}