blob: 5fde169ea0a2f27f0e3f193840c245832dc02758 [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 }
Po-Hsien Wang7a396c82020-11-19 20:36:45 -080078 char result[strlen(string_format) + 1024];
79 vsprintf(result, string_format, args);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070080 va_end(args);
Po-Hsien Wang7a396c82020-11-19 20:36:45 -080081 fprintf(fileid, result);
Po-Hsien Wang53230a22020-11-12 18:03:18 -080082 if (should_abort)
Po-Hsien Wang7a396c82020-11-19 20:36:45 -080083 throw std::runtime_error(result);
Po-Hsien Wang00777b22019-04-24 16:37:09 -070084}
85
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080086inline void VkCheck(const vk::Result& result, const char* func_str) {
87 if (result != vk::Result::eSuccess) {
88 char msg[1024];
Po-Hsien Wang93cc6892020-10-28 17:01:28 -070089 snprintf(msg, 1024, "%s failed: %s", func_str,
90 vk::to_string(result).c_str());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080091 throw std::runtime_error(msg);
92 }
93}
94
Po-Hsien Wang00777b22019-04-24 16:37:09 -070095#define DEBUG(fmt, ...) \
96 { \
97 if (g_verbose) { \
98 LOG(fmt, ##__VA_ARGS__); \
99 } \
100 }
101#define LOG(fmt, ...) \
Po-Hsien Wang53230a22020-11-12 18:03:18 -0800102 { DbgPrintf(false, __FILE__, __LINE__, stdout, fmt, ##__VA_ARGS__); }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700103#define ERROR(fmt, ...) \
Po-Hsien Wang53230a22020-11-12 18:03:18 -0800104 { DbgPrintf(false, __FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); }
105#define RUNTIME_ERROR(fmt, ...) \
106 { DbgPrintf(true, __FILE__, __LINE__, stderr, fmt, ##__VA_ARGS__); }
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700107
108// Put this in the declarations for a class to be uncopyable.
109#define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
110
111// Put this in the declarations for a class to be unassignable.
112#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
113
114// Put this in the declarations for a class to be uncopyable and unassignable.
115#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
116 DISALLOW_COPY(TypeName); \
117 DISALLOW_ASSIGN(TypeName)
118
119// Ignore warning for linter for unused variable.
120#define UNUSED(x) (void)(x)
121
Po-Hsien Wang42e116c2020-06-09 16:10:27 -0700122class ScopeGuard {
123 public:
124 template <class Callable>
125 ScopeGuard(Callable&& fn) : fn_(std::forward<Callable>(fn)) {}
126
127 ScopeGuard(ScopeGuard&& other) : fn_(std::move(other.fn_)) {
128 other.fn_ = nullptr;
129 }
130
131 ~ScopeGuard() {
132 // must not throw
133 if (fn_)
134 fn_();
135 }
136
137 ScopeGuard(const ScopeGuard&) = delete;
138 void operator=(const ScopeGuard&) = delete;
139
140 private:
141 std::function<void()> fn_;
142};
143
144// Used to defer a function call for cleanup
145#define CONCAT_(a, b) a##b
146#define CONCAT(a, b) CONCAT_(a, b)
147#define DEFER(fn) ScopeGuard CONCAT(__defer__, __LINE__) = [&]() { fn; }
148
Po-Hsien Wang00777b22019-04-24 16:37:09 -0700149#endif