Mark Lobodzinski | 6eda00a | 2016-02-02 15:55:36 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2016 Valve Corporation |
| 3 | * Copyright (c) 2015-2016 LunarG, Inc. |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 4 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 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 |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 8 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 10 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 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. |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 16 | * |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 17 | * Author: Mark Lobodzinski <mark@lunarg.com> |
Dave Houlton | 3c9fca7 | 2017-03-27 17:25:54 -0600 | [diff] [blame^] | 18 | * Author: Dave Houlton <daveh@lunarg.com> |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 19 | * |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <string.h> |
Mark Lobodzinski | 882655d | 2016-01-05 11:32:53 -0700 | [diff] [blame] | 23 | #include <string> |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 24 | #include <vector> |
Mark Lobodzinski | 9bfeb04 | 2016-08-26 11:31:25 -0600 | [diff] [blame] | 25 | #include <map> |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 26 | #include "vulkan/vulkan.h" |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 27 | #include "vk_layer_config.h" |
Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 28 | #include "vk_layer_utils.h" |
| 29 | |
Dustin Graves | 33b7cbe | 2016-04-06 11:06:19 -0600 | [diff] [blame] | 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; |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 38 | |
Mike Stroyan | a551bc0 | 2016-09-28 09:42:28 -0600 | [diff] [blame] | 39 | VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *utf8) { |
Mark Lobodzinski | 9a1c7b7 | 2016-01-12 11:52:05 -0700 | [diff] [blame] | 40 | VkStringErrorFlags result = VK_STRING_ERROR_NONE; |
Courtney Goeltzenleuchter | 8ed83c8 | 2016-03-22 16:31:34 -0600 | [diff] [blame] | 41 | int num_char_bytes = 0; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 42 | int i, j; |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 43 | |
Courtney Goeltzenleuchter | 7a3486d | 2016-12-21 16:24:34 -0700 | [diff] [blame] | 44 | for (i = 0; i <= max_length; i++) { |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 45 | if (utf8[i] == 0) { |
| 46 | break; |
Courtney Goeltzenleuchter | 7a3486d | 2016-12-21 16:24:34 -0700 | [diff] [blame] | 47 | } else if (i == max_length) { |
Mark Lobodzinski | 729a8d3 | 2017-01-26 12:16:30 -0700 | [diff] [blame] | 48 | result = VK_STRING_ERROR_LENGTH; |
| 49 | break; |
Tony Barbour | 4575af4 | 2016-03-31 16:54:49 -0600 | [diff] [blame] | 50 | } else if ((utf8[i] >= 0xa) && (utf8[i] < 0x7f)) { |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 51 | num_char_bytes = 0; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 52 | } else if ((utf8[i] & UTF8_ONE_BYTE_MASK) == UTF8_ONE_BYTE_CODE) { |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 53 | num_char_bytes = 1; |
Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 54 | } else if ((utf8[i] & UTF8_TWO_BYTE_MASK) == UTF8_TWO_BYTE_CODE) { |
Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 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 | } |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 75 | |
Mark Lobodzinski | aa96d32 | 2016-08-26 08:47:37 -0600 | [diff] [blame] | 76 | // Utility function for finding a text string in another string |
Mike Stroyan | a551bc0 | 2016-09-28 09:42:28 -0600 | [diff] [blame] | 77 | VK_LAYER_EXPORT bool white_list(const char *item, const char *list) { |
Mark Lobodzinski | aa96d32 | 2016-08-26 08:47:37 -0600 | [diff] [blame] | 78 | std::string candidate(item); |
| 79 | std::string white_list(list); |
| 80 | return (white_list.find(candidate) != std::string::npos); |
| 81 | } |
| 82 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 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. |
Mike Stroyan | a551bc0 | 2016-09-28 09:42:28 -0600 | [diff] [blame] | 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) { |
Mark Lobodzinski | f856847 | 2016-03-17 13:59:51 -0600 | [diff] [blame] | 96 | VkDebugReportCallbackEXT callback = VK_NULL_HANDLE; |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 97 | |
Dustin Graves | bce0a37 | 2016-03-16 18:43:34 -0600 | [diff] [blame] | 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"); |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 104 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 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; |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 110 | |
| 111 | if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) { |
Dustin Graves | bce0a37 | 2016-03-16 18:43:34 -0600 | [diff] [blame] | 112 | const char *log_filename = getLayerOption(log_filename_key.c_str()); |
| 113 | FILE *log_output = getLayerLogOutput(log_filename, layer_identifier); |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 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; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 120 | layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback); |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 121 | logging_callback.push_back(callback); |
| 122 | } |
| 123 | |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 124 | callback = VK_NULL_HANDLE; |
| 125 | |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 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; |
Mark Lobodzinski | 97c4d51 | 2016-05-19 15:27:18 -0600 | [diff] [blame] | 133 | layer_create_msg_callback(report_data, default_layer_callback, &dbgCreateInfo, pAllocator, &callback); |
Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 134 | logging_callback.push_back(callback); |
| 135 | } |
| 136 | } |