453fc8365f435630228f3680c5ba6c15b3f24136
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / vulkan / gstvkerror.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <glib/gprintf.h>
26
27 #include "gstvkerror.h"
28
29 /**
30  * SECTION:vkerror
31  * @title: GstVulkanError
32  * @short_description: Vulkan errors
33  * @see_also: #GstVulkanInstance, #GstVulkanDevice
34  */
35
36 /* *INDENT-OFF* */
37 static const struct
38 {
39   VkResult result;
40   const char *str;
41 } vk_result_string_map[] = {
42   {VK_ERROR_OUT_OF_HOST_MEMORY, "Out Of host memory"},
43   {VK_ERROR_OUT_OF_DEVICE_MEMORY, "Out of device memory"},
44   {VK_ERROR_INITIALIZATION_FAILED, "Initialization failed"},
45   {VK_ERROR_DEVICE_LOST, "Device lost"},
46   {VK_ERROR_MEMORY_MAP_FAILED, "Map failed"},
47   {VK_ERROR_LAYER_NOT_PRESENT, "Layer not present"},
48   {VK_ERROR_EXTENSION_NOT_PRESENT, "Extension not present"},
49   {VK_ERROR_FEATURE_NOT_PRESENT, "Feature not present"},
50   {VK_ERROR_INCOMPATIBLE_DRIVER, "Incompatible driver"},
51   {VK_ERROR_TOO_MANY_OBJECTS, "Too many objects"},
52   {VK_ERROR_FORMAT_NOT_SUPPORTED, "format not supported"},
53   {VK_ERROR_SURFACE_LOST_KHR, "Surface lost"},
54   {VK_ERROR_OUT_OF_DATE_KHR, "out of date"},
55   {VK_ERROR_INCOMPATIBLE_DISPLAY_KHR, "Incompatible display"},
56   {VK_ERROR_NATIVE_WINDOW_IN_USE_KHR, "Native window in use"},
57 };
58 /* *INDENT-ON* */
59
60 GQuark
61 gst_vulkan_error_quark (void)
62 {
63   return g_quark_from_static_string ("gst-vulkan-error");
64 }
65
66 static const char *
67 _vk_result_to_string (VkResult result)
68 {
69   int i;
70
71   if (result >= 0)
72     return NULL;
73
74   for (i = 0; i < G_N_ELEMENTS (vk_result_string_map); i++) {
75     if (result == vk_result_string_map[i].result)
76       return vk_result_string_map[i].str;
77   }
78
79   return "Unknown Error";
80 }
81
82 /**
83  * gst_vulkan_error_to_g_error: (skip)
84  * @result: a VkResult
85  * @error: (inout) (optional): a #GError to fill
86  * @format: the printf-like format to write into the #GError
87  * @...: arguments for @format
88  *
89  * if @result indicates an error condition, fills out #GError with details of
90  * the error
91  *
92  * Returns: @result for easy chaining
93  *
94  * Since: 1.18
95  */
96 VkResult
97 gst_vulkan_error_to_g_error (VkResult result, GError ** error,
98     const char *format, ...)
99 {
100   const char *result_str;
101   gchar *string;
102   va_list args;
103
104   if (error == NULL)
105     /* we don't have an error to set */
106     return result;
107
108   result_str = _vk_result_to_string (result);
109   if (result_str == NULL) {
110     if (result < 0) {
111       result_str = "Unknown";
112     } else {
113       return result;
114     }
115   }
116
117   va_start (args, format);
118   g_vasprintf (&string, format, args);
119   va_end (args);
120
121   g_set_error (error, GST_VULKAN_ERROR, result, "%s (0x%x, %i): %s", result_str,
122       result, result, string);
123
124   return result;
125 }