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 | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 19 | extern int g_verbose; |
| 20 | void PrintDateTime(); |
| 21 | std::vector<std::string> SplitString(const std::string& kInput, char delimiter); |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 22 | std::string readShaderFile(const std::string& filename); |
| 23 | void SavePPM(FilePath, const char*, uint32_t, uint32_t, vk::DeviceSize); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 24 | vk::ShaderModule CreateShaderModule(const vk::Device& device, std::string code); |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 25 | |
| 26 | inline uint64_t GetUTime() { |
| 27 | struct timeval tv = {}; |
| 28 | gettimeofday(&tv, nullptr); |
| 29 | return tv.tv_usec + 1000000ULL * static_cast<uint64_t>(tv.tv_sec); |
| 30 | } |
| 31 | |
| 32 | bool IsItemInVector(const std::vector<std::string>& list, |
| 33 | const char* value, |
| 34 | bool empty_value); |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 35 | bool check_file_existence(const char* file_path, struct stat* buffer = NULL); |
| 36 | bool check_dir_existence(const char* file_path); |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 37 | |
| 38 | inline void DbgPrintf(const char* filename, |
| 39 | int line, |
| 40 | FILE* fileid, |
| 41 | const char* format, |
| 42 | ...) { |
| 43 | va_list args; |
| 44 | va_start(args, format); |
| 45 | char string_format[strlen(format) + 1024]; |
| 46 | char debug_header[1024] = "\0"; |
| 47 | if (g_verbose) { |
| 48 | sprintf(debug_header, "[%s:%d]", filename, line); |
| 49 | sprintf(string_format, "%30s %s\n", debug_header, format); |
| 50 | } else { |
| 51 | sprintf(string_format, "%s\n", format); |
| 52 | } |
| 53 | vfprintf(fileid, string_format, args); |
| 54 | va_end(args); |
| 55 | } |
| 56 | |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 57 | inline void VkCheck(const vk::Result& result, const char* func_str) { |
| 58 | if (result != vk::Result::eSuccess) { |
| 59 | char msg[1024]; |
Po-Hsien Wang | 93cc689 | 2020-10-28 17:01:28 -0700 | [diff] [blame] | 60 | snprintf(msg, 1024, "%s failed: %s", func_str, |
| 61 | vk::to_string(result).c_str()); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 62 | throw std::runtime_error(msg); |
| 63 | } |
| 64 | } |
| 65 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 66 | #define DEBUG(fmt, ...) \ |
| 67 | { \ |
| 68 | if (g_verbose) { \ |
| 69 | LOG(fmt, ##__VA_ARGS__); \ |
| 70 | } \ |
| 71 | } |
| 72 | #define LOG(fmt, ...) \ |
| 73 | { DbgPrintf(__FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); } |
| 74 | #define ERROR(fmt, ...) \ |
| 75 | { DbgPrintf(__FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); } |
| 76 | #define ABORT(code, fmt, ...) \ |
| 77 | { \ |
| 78 | ERROR(fmt, ##__VA_ARGS__); \ |
| 79 | exit(code); \ |
| 80 | } |
| 81 | |
| 82 | // Put this in the declarations for a class to be uncopyable. |
| 83 | #define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete |
| 84 | |
| 85 | // Put this in the declarations for a class to be unassignable. |
| 86 | #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete |
| 87 | |
| 88 | // Put this in the declarations for a class to be uncopyable and unassignable. |
| 89 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 90 | DISALLOW_COPY(TypeName); \ |
| 91 | DISALLOW_ASSIGN(TypeName) |
| 92 | |
| 93 | // Ignore warning for linter for unused variable. |
| 94 | #define UNUSED(x) (void)(x) |
| 95 | |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame] | 96 | class ScopeGuard { |
| 97 | public: |
| 98 | template <class Callable> |
| 99 | ScopeGuard(Callable&& fn) : fn_(std::forward<Callable>(fn)) {} |
| 100 | |
| 101 | ScopeGuard(ScopeGuard&& other) : fn_(std::move(other.fn_)) { |
| 102 | other.fn_ = nullptr; |
| 103 | } |
| 104 | |
| 105 | ~ScopeGuard() { |
| 106 | // must not throw |
| 107 | if (fn_) |
| 108 | fn_(); |
| 109 | } |
| 110 | |
| 111 | ScopeGuard(const ScopeGuard&) = delete; |
| 112 | void operator=(const ScopeGuard&) = delete; |
| 113 | |
| 114 | private: |
| 115 | std::function<void()> fn_; |
| 116 | }; |
| 117 | |
| 118 | // Used to defer a function call for cleanup |
| 119 | #define CONCAT_(a, b) a##b |
| 120 | #define CONCAT(a, b) CONCAT_(a, b) |
| 121 | #define DEFER(fn) ScopeGuard CONCAT(__defer__, __LINE__) = [&]() { fn; } |
| 122 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 123 | #endif |