Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef __UTILS_H__ |
| 6 | #define __UTILS_H__ |
| 7 | |
| 8 | #include <stdarg.h> |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 10 | #include <sys/stat.h> |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 11 | #include <sys/time.h> |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame] | 12 | #include <functional> |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | #include <vulkan/vulkan.hpp> |
| 16 | |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 17 | #include "filepath.h" |
| 18 | |
Po-Hsien Wang | 53230a2 | 2020-11-12 18:03:18 -0800 | [diff] [blame] | 19 | namespace vkbench { |
| 20 | class Image { |
| 21 | public: |
| 22 | Image() : data_(nullptr) {} |
| 23 | Image(const unsigned char* data, |
| 24 | const vk::Extent2D size, |
| 25 | vk::SubresourceLayout resource_layout) |
| 26 | : size_(size), resource_layout_(resource_layout) { |
| 27 | data_ = new unsigned char[resource_layout_.size]; |
| 28 | memcpy(data_, data, resource_layout_.size); |
| 29 | } |
| 30 | ~Image() { delete data_; } |
| 31 | |
| 32 | void Save(FilePath); |
| 33 | |
| 34 | private: |
| 35 | void savePPM(FilePath); |
| 36 | void savePNG(FilePath); |
| 37 | |
| 38 | unsigned char* data_; |
| 39 | vk::Extent2D size_; |
| 40 | vk::SubresourceLayout resource_layout_; |
| 41 | }; |
| 42 | } // namespace vkbench |
| 43 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 44 | extern int g_verbose; |
| 45 | void PrintDateTime(); |
| 46 | std::vector<std::string> SplitString(const std::string& kInput, char delimiter); |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 47 | std::string readShaderFile(const std::string& filename); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 48 | vk::ShaderModule CreateShaderModule(const vk::Device& device, std::string code); |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 49 | |
| 50 | inline uint64_t GetUTime() { |
| 51 | struct timeval tv = {}; |
| 52 | gettimeofday(&tv, nullptr); |
| 53 | return tv.tv_usec + 1000000ULL * static_cast<uint64_t>(tv.tv_sec); |
| 54 | } |
| 55 | |
| 56 | bool IsItemInVector(const std::vector<std::string>& list, |
| 57 | const char* value, |
| 58 | bool empty_value); |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 59 | bool check_file_existence(const char* file_path, struct stat* buffer = NULL); |
| 60 | bool check_dir_existence(const char* file_path); |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 61 | |
Po-Hsien Wang | 53230a2 | 2020-11-12 18:03:18 -0800 | [diff] [blame] | 62 | inline void DbgPrintf(bool should_abort, |
| 63 | const char* filename, |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 64 | int line, |
| 65 | FILE* fileid, |
| 66 | const char* format, |
| 67 | ...) { |
| 68 | va_list args; |
| 69 | va_start(args, format); |
| 70 | char string_format[strlen(format) + 1024]; |
| 71 | char debug_header[1024] = "\0"; |
| 72 | if (g_verbose) { |
| 73 | sprintf(debug_header, "[%s:%d]", filename, line); |
| 74 | sprintf(string_format, "%30s %s\n", debug_header, format); |
| 75 | } else { |
| 76 | sprintf(string_format, "%s\n", format); |
| 77 | } |
| 78 | vfprintf(fileid, string_format, args); |
| 79 | va_end(args); |
Po-Hsien Wang | 53230a2 | 2020-11-12 18:03:18 -0800 | [diff] [blame] | 80 | if (should_abort) |
| 81 | throw std::runtime_error(string_format); |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 84 | inline void VkCheck(const vk::Result& result, const char* func_str) { |
| 85 | if (result != vk::Result::eSuccess) { |
| 86 | char msg[1024]; |
Po-Hsien Wang | 93cc689 | 2020-10-28 17:01:28 -0700 | [diff] [blame] | 87 | snprintf(msg, 1024, "%s failed: %s", func_str, |
| 88 | vk::to_string(result).c_str()); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 89 | throw std::runtime_error(msg); |
| 90 | } |
| 91 | } |
| 92 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 93 | #define DEBUG(fmt, ...) \ |
| 94 | { \ |
| 95 | if (g_verbose) { \ |
| 96 | LOG(fmt, ##__VA_ARGS__); \ |
| 97 | } \ |
| 98 | } |
| 99 | #define LOG(fmt, ...) \ |
Po-Hsien Wang | 53230a2 | 2020-11-12 18:03:18 -0800 | [diff] [blame] | 100 | { DbgPrintf(false, __FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); } |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 101 | #define ERROR(fmt, ...) \ |
Po-Hsien Wang | 53230a2 | 2020-11-12 18:03:18 -0800 | [diff] [blame] | 102 | { DbgPrintf(false, __FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); } |
| 103 | #define RUNTIME_ERROR(fmt, ...) \ |
| 104 | { DbgPrintf(true, __FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); } |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 105 | |
| 106 | // Put this in the declarations for a class to be uncopyable. |
| 107 | #define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete |
| 108 | |
| 109 | // Put this in the declarations for a class to be unassignable. |
| 110 | #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete |
| 111 | |
| 112 | // Put this in the declarations for a class to be uncopyable and unassignable. |
| 113 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 114 | DISALLOW_COPY(TypeName); \ |
| 115 | DISALLOW_ASSIGN(TypeName) |
| 116 | |
| 117 | // Ignore warning for linter for unused variable. |
| 118 | #define UNUSED(x) (void)(x) |
| 119 | |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame] | 120 | class ScopeGuard { |
| 121 | public: |
| 122 | template <class Callable> |
| 123 | ScopeGuard(Callable&& fn) : fn_(std::forward<Callable>(fn)) {} |
| 124 | |
| 125 | ScopeGuard(ScopeGuard&& other) : fn_(std::move(other.fn_)) { |
| 126 | other.fn_ = nullptr; |
| 127 | } |
| 128 | |
| 129 | ~ScopeGuard() { |
| 130 | // must not throw |
| 131 | if (fn_) |
| 132 | fn_(); |
| 133 | } |
| 134 | |
| 135 | ScopeGuard(const ScopeGuard&) = delete; |
| 136 | void operator=(const ScopeGuard&) = delete; |
| 137 | |
| 138 | private: |
| 139 | std::function<void()> fn_; |
| 140 | }; |
| 141 | |
| 142 | // Used to defer a function call for cleanup |
| 143 | #define CONCAT_(a, b) a##b |
| 144 | #define CONCAT(a, b) CONCAT_(a, b) |
| 145 | #define DEFER(fn) ScopeGuard CONCAT(__defer__, __LINE__) = [&]() { fn; } |
| 146 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 147 | #endif |