blob: a6337d2cf58be17f2ac3bc7e9322a4b5817b02be [file] [log] [blame]
Po-Hsien Wang00777b22019-04-24 16:37:09 -07001// 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 Wang42e116c2020-06-09 16:10:27 -07009#include <stdio.h>
Po-Hsien Wang00777b22019-04-24 16:37:09 -070010#include <sys/time.h>
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070011#include <functional>
Po-Hsien Wang00777b22019-04-24 16:37:09 -070012#include <string>
13#include <vector>
14#include <vulkan/vulkan.hpp>
15
16extern int g_verbose;
17void PrintDateTime();
18std::vector<std::string> SplitString(const std::string& kInput, char delimiter);
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080019std::string readFile(const std::string& filename);
20void SavePPM(std::string, const char*, uint32_t, uint32_t, vk::DeviceSize);
21vk::ShaderModule CreateShaderModule(const vk::Device& device, std::string code);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070022
23inline 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
29bool IsItemInVector(const std::vector<std::string>& list,
30 const char* value,
31 bool empty_value);
32
33inline 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 Wanga0e1c312020-09-24 22:06:52 +080052inline void VkCheck(const vk::Result& result, const char* func_str) {
53 if (result != vk::Result::eSuccess) {
54 char msg[1024];
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070055 snprintf(msg, 1024, "%s failed: %s", func_str,
56 vk::to_string(result).c_str());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080057 throw std::runtime_error(msg);
58 }
59}
60
Po-Hsien Wang00777b22019-04-24 16:37:09 -070061#define DEBUG(fmt, ...) \
62 { \
63 if (g_verbose) { \
64 LOG(fmt, ##__VA_ARGS__); \
65 } \
66 }
67#define LOG(fmt, ...) \
68 { DbgPrintf(__FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); }
69#define ERROR(fmt, ...) \
70 { DbgPrintf(__FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); }
71#define ABORT(code, fmt, ...) \
72 { \
73 ERROR(fmt, ##__VA_ARGS__); \
74 exit(code); \
75 }
76
77// Put this in the declarations for a class to be uncopyable.
78#define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
79
80// Put this in the declarations for a class to be unassignable.
81#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
82
83// Put this in the declarations for a class to be uncopyable and unassignable.
84#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
85 DISALLOW_COPY(TypeName); \
86 DISALLOW_ASSIGN(TypeName)
87
88// Ignore warning for linter for unused variable.
89#define UNUSED(x) (void)(x)
90
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070091class ScopeGuard {
92 public:
93 template <class Callable>
94 ScopeGuard(Callable&& fn) : fn_(std::forward<Callable>(fn)) {}
95
96 ScopeGuard(ScopeGuard&& other) : fn_(std::move(other.fn_)) {
97 other.fn_ = nullptr;
98 }
99
100 ~ScopeGuard() {
101 // must not throw
102 if (fn_)
103 fn_();
104 }
105
106 ScopeGuard(const ScopeGuard&) = delete;
107 void operator=(const ScopeGuard&) = delete;
108
109 private:
110 std::function<void()> fn_;
111};
112
113// Used to defer a function call for cleanup
114#define CONCAT_(a, b) a##b
115#define CONCAT(a, b) CONCAT_(a, b)
116#define DEFER(fn) ScopeGuard CONCAT(__defer__, __LINE__) = [&]() { fn; }
117
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700118#endif