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 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <time.h> |
| 8 | #include <unistd.h> |
| 9 | #include <chrono> |
| 10 | #include <fstream> |
| 11 | #include <sstream> |
| 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Po-Hsien Wang | 42e116c | 2020-06-09 16:10:27 -0700 | [diff] [blame] | 15 | #include "utils.h" |
| 16 | |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 17 | extern FilePath g_spirv_dir; |
| 18 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 19 | const std::string kTemperatureScript = |
| 20 | "/usr/local/autotest/bin/temperature.py --maximum"; |
| 21 | |
| 22 | void PrintDateTime() { |
| 23 | time_t timer; |
| 24 | char buffer[26]; |
| 25 | time(&timer); |
| 26 | struct tm* tm_info = localtime(&timer); |
| 27 | strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); |
| 28 | LOG("# DateTime: %s", buffer) |
| 29 | } |
| 30 | |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 31 | std::string readShaderFile(const std::string& filename) { |
| 32 | FilePath file_path = g_spirv_dir.Append(FilePath(filename)); |
| 33 | std::ifstream file(file_path.value(), std::ios::ate | std::ios::binary); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 34 | if (!file.is_open()) { |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 35 | throw std::runtime_error("Failed to open " + file_path.value()); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 36 | } |
| 37 | DEFER(file.close()); |
| 38 | |
| 39 | size_t fileSize = (size_t)file.tellg(); |
| 40 | std::vector<char> buffer(fileSize); |
| 41 | file.seekg(0); |
| 42 | file.read(buffer.data(), fileSize); |
| 43 | return std::string(buffer.begin(), buffer.end()); |
| 44 | } |
| 45 | |
| 46 | // CreateShaderModule creates a shader module from a loaded SPIR-V code. |
| 47 | vk::ShaderModule CreateShaderModule(const vk::Device& device, |
| 48 | std::string code) { |
| 49 | vk::ShaderModuleCreateInfo create_info; |
| 50 | create_info.setCodeSize(code.length()); |
| 51 | create_info.setPCode(reinterpret_cast<const uint32_t*>(code.c_str())); |
| 52 | return device.createShaderModule(create_info); |
| 53 | } |
| 54 | |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 55 | // SavePPM saves data into file_path in PPM format. It assumes data is in |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 56 | // R8G8B8A8_UNORM format. |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 57 | void SavePPM(FilePath file_path, |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 58 | const char* data, |
| 59 | uint32_t width, |
| 60 | uint32_t height, |
| 61 | vk::DeviceSize row_pitch) { |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 62 | FilePath directory = file_path.DirName(); |
| 63 | CreateDirectory(directory); |
| 64 | |
| 65 | std::ofstream file(file_path.value(), std::ios::binary | std::ios::out); |
Po-Hsien Wang | a0e1c31 | 2020-09-24 22:06:52 +0800 | [diff] [blame] | 66 | DEFER(file.close()); |
| 67 | |
| 68 | file << "P6\n" << width << "\n" << height << "\n" << 255 << std::endl; |
| 69 | for (uint32_t y = 0; y < height; y++) { |
| 70 | unsigned int* row = (unsigned int*)data; |
| 71 | for (uint32_t x = 0; x < width; x++) { |
| 72 | file.write((char*)row, 3); |
| 73 | row++; |
| 74 | } |
| 75 | data += row_pitch; |
| 76 | } |
| 77 | } |
| 78 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 79 | /* Execute a shell command and return its file descriptor for reading output. */ |
| 80 | /* @param command: command to be run. */ |
| 81 | /* @param result: the stdout of the command output. */ |
| 82 | /* @return true if the command is executed successfully. */ |
| 83 | bool ExecuteCommand(const std::string& kCommand, |
| 84 | std::string* result = nullptr) { |
| 85 | FILE* fd = popen(kCommand.c_str(), "r"); |
| 86 | if (!fd) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if (result) { |
| 91 | fseek(fd, 0, SEEK_END); |
| 92 | long size = ftell(fd); |
| 93 | fseek(fd, 0, SEEK_SET); |
| 94 | fread(&result[0], sizeof(char), size, fd); |
| 95 | } |
| 96 | return pclose(fd) == 0; |
| 97 | } |
| 98 | |
| 99 | std::vector<std::string> SplitString(const std::string& kInput, |
| 100 | char delimiter) { |
| 101 | std::vector<std::string> result; |
| 102 | std::stringstream srcStream(kInput); |
| 103 | |
| 104 | std::string token; |
| 105 | while (getline(srcStream, token, delimiter)) { |
| 106 | result.push_back(token); |
| 107 | } |
| 108 | return result; |
| 109 | } |
| 110 | |
Po-Hsien Wang | 00777b2 | 2019-04-24 16:37:09 -0700 | [diff] [blame] | 111 | bool IsItemInVector(const std::vector<std::string>& list, |
| 112 | const char* value, |
| 113 | bool empty_value = false) { |
| 114 | if (list.empty()) |
| 115 | return empty_value; |
| 116 | return !(find(list.begin(), list.end(), std::string(value)) == list.end()); |
| 117 | } |
Po-Hsien Wang | a956e65 | 2020-11-11 16:29:04 -0800 | [diff] [blame] | 118 | |
| 119 | bool check_file_existence(const char* file_path, struct stat* buffer) { |
| 120 | struct stat local_buf; |
| 121 | bool exist = stat(file_path, &local_buf) == 0; |
| 122 | if (buffer && exist) |
| 123 | memcpy(buffer, &local_buf, sizeof(local_buf)); |
| 124 | return exist; |
| 125 | } |
| 126 | |
| 127 | bool check_dir_existence(const char* file_path) { |
| 128 | struct stat buffer; |
| 129 | bool exist = check_file_existence(file_path, &buffer); |
| 130 | if (!exist) |
| 131 | return false; |
| 132 | return S_ISDIR(buffer.st_mode); |
| 133 | } |