blob: 72453fefa09c75ee0a2b8742dc82d23f76a3b5e4 [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 Wanga956e652020-11-11 16:29:04 -080010#include <sys/stat.h>
Po-Hsien Wang00777b22019-04-24 16:37:09 -070011#include <sys/time.h>
Po-Hsien Wang42e116c2020-06-09 16:10:27 -070012#include <functional>
Po-Hsien Wang00777b22019-04-24 16:37:09 -070013#include <string>
14#include <vector>
15#include <vulkan/vulkan.hpp>
16
Po-Hsien Wanga956e652020-11-11 16:29:04 -080017#include "filepath.h"
18
Po-Hsien Wang00777b22019-04-24 16:37:09 -070019extern int g_verbose;
20void PrintDateTime();
21std::vector<std::string> SplitString(const std::string& kInput, char delimiter);
Po-Hsien Wanga956e652020-11-11 16:29:04 -080022std::string readShaderFile(const std::string& filename);
23void SavePPM(FilePath, const char*, uint32_t, uint32_t, vk::DeviceSize);
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080024vk::ShaderModule CreateShaderModule(const vk::Device& device, std::string code);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070025
26inline 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
32bool IsItemInVector(const std::vector<std::string>& list,
33 const char* value,
34 bool empty_value);
Po-Hsien Wanga956e652020-11-11 16:29:04 -080035bool check_file_existence(const char* file_path, struct stat* buffer = NULL);
36bool check_dir_existence(const char* file_path);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070037
38inline 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 Wanga0e1c312020-09-24 22:06:52 +080057inline void VkCheck(const vk::Result& result, const char* func_str) {
58 if (result != vk::Result::eSuccess) {
59 char msg[1024];
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070060 snprintf(msg, 1024, "%s failed: %s", func_str,
61 vk::to_string(result).c_str());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080062 throw std::runtime_error(msg);
63 }
64}
65
Po-Hsien Wang00777b22019-04-24 16:37:09 -070066#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 Wang42e116c2020-06-09 16:10:27 -070096class 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 Wang00777b22019-04-24 16:37:09 -0700123#endif