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