blob: e4f2373b8b78f29b046cfd4eb235f1ed6a5afac2 [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 Wang53230a22020-11-12 18:03:18 -080019namespace vkbench {
20class 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 Wang00777b22019-04-24 16:37:09 -070044extern int g_verbose;
45void PrintDateTime();
46std::vector<std::string> SplitString(const std::string& kInput, char delimiter);
Po-Hsien Wanga956e652020-11-11 16:29:04 -080047std::string readShaderFile(const std::string& filename);
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080048vk::ShaderModule CreateShaderModule(const vk::Device& device, std::string code);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070049
50inline 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
56bool IsItemInVector(const std::vector<std::string>& list,
57 const char* value,
58 bool empty_value);
Po-Hsien Wanga956e652020-11-11 16:29:04 -080059bool check_file_existence(const char* file_path, struct stat* buffer = NULL);
60bool check_dir_existence(const char* file_path);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070061
Po-Hsien Wang53230a22020-11-12 18:03:18 -080062inline void DbgPrintf(bool should_abort,
63 const char* filename,
Po-Hsien Wang00777b22019-04-24 16:37:09 -070064 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 Wang53230a22020-11-12 18:03:18 -080080 if (should_abort)
81 throw std::runtime_error(string_format);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070082}
83
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080084inline void VkCheck(const vk::Result& result, const char* func_str) {
85 if (result != vk::Result::eSuccess) {
86 char msg[1024];
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070087 snprintf(msg, 1024, "%s failed: %s", func_str,
88 vk::to_string(result).c_str());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080089 throw std::runtime_error(msg);
90 }
91}
92
Po-Hsien Wang00777b22019-04-24 16:37:09 -070093#define DEBUG(fmt, ...) \
94 { \
95 if (g_verbose) { \
96 LOG(fmt, ##__VA_ARGS__); \
97 } \
98 }
99#define LOG(fmt, ...) \
Po-Hsien Wang53230a22020-11-12 18:03:18 -0800100 { DbgPrintf(false, __FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700101#define ERROR(fmt, ...) \
Po-Hsien Wang53230a22020-11-12 18:03:18 -0800102 { DbgPrintf(false, __FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); }
103#define RUNTIME_ERROR(fmt, ...) \
104 { DbgPrintf(true, __FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700105
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 Wang42e116c2020-06-09 16:10:27 -0700120class 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 Wang00777b22019-04-24 16:37:09 -0700147#endif