blob: 240dde3b4f3e8b8e44ed2d821268f5a35945568e [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
Po-Hsien Wang00777b22019-04-24 16:37:09 -07005#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 Wang42e116c2020-06-09 16:10:27 -070015#include "utils.h"
16
Po-Hsien Wanga956e652020-11-11 16:29:04 -080017extern FilePath g_spirv_dir;
18
Po-Hsien Wang00777b22019-04-24 16:37:09 -070019const std::string kTemperatureScript =
20 "/usr/local/autotest/bin/temperature.py --maximum";
21
22void 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 Wanga956e652020-11-11 16:29:04 -080031std::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 Wanga0e1c312020-09-24 22:06:52 +080034 if (!file.is_open()) {
Po-Hsien Wanga956e652020-11-11 16:29:04 -080035 throw std::runtime_error("Failed to open " + file_path.value());
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080036 }
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.
47vk::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 Wanga956e652020-11-11 16:29:04 -080055// SavePPM saves data into file_path in PPM format. It assumes data is in
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080056// R8G8B8A8_UNORM format.
Po-Hsien Wanga956e652020-11-11 16:29:04 -080057void SavePPM(FilePath file_path,
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080058 const char* data,
59 uint32_t width,
60 uint32_t height,
61 vk::DeviceSize row_pitch) {
Po-Hsien Wanga956e652020-11-11 16:29:04 -080062 FilePath directory = file_path.DirName();
63 CreateDirectory(directory);
64
65 std::ofstream file(file_path.value(), std::ios::binary | std::ios::out);
Po-Hsien Wanga0e1c312020-09-24 22:06:52 +080066 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 Wang00777b22019-04-24 16:37:09 -070079/* 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. */
83bool 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
99std::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 Wang00777b22019-04-24 16:37:09 -0700111bool 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 Wanga956e652020-11-11 16:29:04 -0800118
119bool 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
127bool 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}