1 /* Copyright (c) 2015-2016 The Khronos Group Inc.
2  * Copyright (c) 2015-2016 Valve Corporation
3  * Copyright (c) 2015-2016 LunarG, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author: Mark Lobodzinski <mark@lunarg.com>
18  * Author: Dave Houlton <daveh@lunarg.com>
19  *
20  */
21 
22 #include <string.h>
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include "vulkan/vulkan.h"
27 #include "vk_layer_config.h"
28 #include "vk_layer_utils.h"
29 
30 static const uint8_t UTF8_ONE_BYTE_CODE = 0xC0;
31 static const uint8_t UTF8_ONE_BYTE_MASK = 0xE0;
32 static const uint8_t UTF8_TWO_BYTE_CODE = 0xE0;
33 static const uint8_t UTF8_TWO_BYTE_MASK = 0xF0;
34 static const uint8_t UTF8_THREE_BYTE_CODE = 0xF0;
35 static const uint8_t UTF8_THREE_BYTE_MASK = 0xF8;
36 static const uint8_t UTF8_DATA_BYTE_CODE = 0x80;
37 static const uint8_t UTF8_DATA_BYTE_MASK = 0xC0;
38 
39 VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) {
40     VkStringErrorFlags result = VK_STRING_ERROR_NONE;
41     int num_char_bytes = 0;
42     int i, j;
43 
44     for (i = 0; i <= max_length; i++) {
45         if (utf8[i] == 0) {
46             break;
47         } else if (i == max_length) {
48             result = VK_STRING_ERROR_LENGTH;
49             break;
50         } else if ((utf8[i] >= 0xa) && (utf8[i] < 0x7f)) {
51             num_char_bytes = 0;
52         } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) {
53             num_char_bytes = 1;
54         } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) {
55             num_char_bytes = 2;
56         } else if ((utf8[i] & UTF8_THREE_BYTE_MASK) == UTF8_THREE_BYTE_CODE) {
57             num_char_bytes = 3;
58         } else {
59             result = VK_STRING_ERROR_BAD_DATA;
60         }
61 
62         // Validate the following num_char_bytes of data
63         for (j = 0; (j < num_char_bytes) && (i < max_length); j++) {
64             if (++i == max_length) {
65                 result |= VK_STRING_ERROR_LENGTH;
66                 break;
67             }
68             if ((utf8[i] & UTF8_DATA_BYTE_MASK) != UTF8_DATA_BYTE_CODE) {
69                 result |= VK_STRING_ERROR_BAD_DATA;
70             }
71         }
72     }
73     return result;
74 }
75 
76 // Utility function for finding a text string in another string
77 VK_LAYER_EXPORT bool white_list(const char *item, const char *list) {
78     std::string candidate(item);
79     std::string white_list(list);
80     return (white_list.find(candidate) != std::string::npos);
81 }
82 
83 // Debug callbacks get created in three ways:
84 //   o  Application-defined debug callbacks
85 //   o  Through settings in a vk_layer_settings.txt file
86 //   o  By default, if neither an app-defined debug callback nor a vk_layer_settings.txt file is present
87 //
88 // At layer initialization time, default logging callbacks are created to output layer error messages.
89 // If a vk_layer_settings.txt file is present its settings will override any default settings.
90 //
91 // If a vk_layer_settings.txt file is present and an application defines a debug callback, both callbacks
92 // will be active.  If no vk_layer_settings.txt file is present, creating an application-defined debug
93 // callback will cause the default callbacks to be unregisterd and removed.
94 VK_LAYER_EXPORT void layer_debug_actions(debug_report_data *report_data, std::vector<VkDebugReportCallbackEXT> &logging_callback,
95                                          const VkAllocationCallbacks *pAllocator, const char *layer_identifier) {
96     VkDebugReportCallbackEXT callback = VK_NULL_HANDLE;
97 
98     std::string report_flags_key = layer_identifier;
99     std::string debug_action_key = layer_identifier;
100     std::string log_filename_key = layer_identifier;
101     report_flags_key.append(".report_flags");
102     debug_action_key.append(".debug_action");
103     log_filename_key.append(".log_filename");
104 
105     // Initialize layer options
106     VkDebugReportFlagsEXT report_flags = GetLayerOptionFlags(report_flags_key, report_flags_option_definitions, 0);
107     VkLayerDbgActionFlags debug_action = GetLayerOptionFlags(debug_action_key, debug_actions_option_definitions, 0);
108     // Flag as default if these settings are not from a vk_layer_settings.txt file
109     bool default_layer_callback = (debug_action & VK_DBG_LAYER_ACTION_DEFAULT) ? true : false;
110 
111     if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) {
112         const char *log_filename = getLayerOption(log_filename_key.c_str());
113         FILE *log_output = getLayerLogOutput(log_filename, layer_identifier);
114         VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
115         memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
116         dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
117         dbgCreateInfo.flags = report_flags;
118         dbgCreateInfo.pfnCallback = log_callback;
119         dbgCreateInfo.pUserData = (void *)log_output;
120         layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
121         logging_callback.push_back(callback);
122     }
123 
124     callback = VK_NULL_HANDLE;
125 
126     if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
127         VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
128         memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
129         dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
130         dbgCreateInfo.flags = report_flags;
131         dbgCreateInfo.pfnCallback = win32_debug_output_msg;
132         dbgCreateInfo.pUserData = NULL;
133         layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
134         logging_callback.push_back(callback);
135     }
136 
137     callback = VK_NULL_HANDLE;
138 
139     if (debug_action & VK_DBG_LAYER_ACTION_BREAK) {
140         VkDebugReportCallbackCreateInfoEXT dbgCreateInfo;
141         memset(&dbgCreateInfo, 0, sizeof(dbgCreateInfo));
142         dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
143         dbgCreateInfo.flags = report_flags;
144         dbgCreateInfo.pfnCallback = DebugBreakCallback;
145         dbgCreateInfo.pUserData = NULL;
146         layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback);
147         logging_callback.push_back(callback);
148     }
149 }
150