gstvulkan: Expose gst_vulkan_result_to_string
[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 /**
67  * gst_vulkan_result_to_string: (skip)
68  * @result: a VkResult
69  *
70  * Returns: a message that corresponds to @result
71  *
72  * Since: 1.22
73  */
74 const char *
75 gst_vulkan_result_to_string (VkResult result)
76 {
77   int i;
78
79   if (result >= 0)
80     return NULL;
81
82   for (i = 0; i < G_N_ELEMENTS (vk_result_string_map); i++) {
83     if (result == vk_result_string_map[i].result)
84       return vk_result_string_map[i].str;
85   }
86
87   return "Unknown Error";
88 }
89
90 /**
91  * gst_vulkan_error_to_g_error: (skip)
92  * @result: a VkResult
93  * @error: (inout) (optional): a #GError to fill
94  * @format: the printf-like format to write into the #GError
95  * @...: arguments for @format
96  *
97  * if @result indicates an error condition, fills out #GError with details of
98  * the error
99  *
100  * Returns: @result for easy chaining
101  *
102  * Since: 1.18
103  */
104 VkResult
105 gst_vulkan_error_to_g_error (VkResult result, GError ** error,
106     const char *format, ...)
107 {
108   const char *result_str;
109   gchar *string;
110   va_list args;
111
112   if (error == NULL)
113     /* we don't have an error to set */
114     return result;
115
116   result_str = gst_vulkan_result_to_string (result);
117   if (result_str == NULL) {
118     if (result < 0) {
119       result_str = "Unknown";
120     } else {
121       return result;
122     }
123   }
124
125   va_start (args, format);
126   g_vasprintf (&string, format, args);
127   va_end (args);
128
129   g_set_error (error, GST_VULKAN_ERROR, result, "%s (0x%x, %i): %s", result_str,
130       result, result, string);
131
132   return result;
133 }