• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2010 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 BENCH_GL_UTILS_H_
6 #define BENCH_GL_UTILS_H_
7 
8 #if defined(USE_OPENGLES)
9 #include "GLES2/gl2.h"
10 #elif defined(USE_OPENGL)
11 #include "GL/gl.h"
12 #endif
13 
14 #include <signal.h>
15 #include <algorithm>
16 #include <functional>
17 #include <limits>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 extern double g_initial_temperature;
23 
24 void SetBasePathFromArgv0(const char* argv0, const char* relative);
25 void* MmapFile(const char* name, size_t* length);
26 
27 // Returns temperature of system before testing started. It is used as a
28 // reference for keeping the machine cool.
29 const double GetInitialMachineTemperature();
30 // For thermal monitoring of system.
31 double GetMachineTemperature();
32 // Wait for machine to cool with temperature in Celsius and timeout in seconds.
33 // Returns the time spent waiting and sets the last observed temperature.
34 double WaitForCoolMachine(double cold_temperature,
35                           double timeout,
36                           double* temperature);
37 bool check_dir_existence(const char* file_path);
38 bool check_file_existence(const char* file_path, struct stat* buffer);
39 // SplitString by delimiter.
40 std::vector<std::string> SplitString(std::string& input,
41                                      std::string delimiter,
42                                      bool trim_space);
43 template <typename INT>
44 std::string IntToString(INT value) {
45   // log10(2) ~= 0.3 bytes needed per bit or per byte log10(2**8) ~= 2.4.
46   // So round up to allocate 3 output characters per byte, plus 1 for '-'.
47   const size_t kOutputBufSize =
48       3 * sizeof(INT) + std::numeric_limits<INT>::is_signed;
49 
50   // Create the string in a temporary buffer, write it back to front, and
51   // then return the substr of what we ended up using.
52   using CHR = typename std::string::value_type;
53   CHR outbuf[kOutputBufSize];
54 
55   // The ValueOrDie call below can never fail, because UnsignedAbs is valid
56   // for all valid inputs.
57   typename std::make_unsigned<INT>::type res = value < 0 ? -value : value;
58 
59   CHR* end = outbuf + kOutputBufSize;
60   CHR* i = end;
61   do {
62     --i;
63     // DCHECK(i != outbuf);
64     *i = static_cast<CHR>((res % 10) + '0');
65     res /= 10;
66   } while (res != 0);
67   if (value < 0) {
68     --i;
69     // DCHECK(i != outbuf);
70     *i = static_cast<CHR>('-');
71   }
72   return std::string(i, end);
73 }
74 
75 namespace glbench {
76 
77 GLuint SetupTexture(GLsizei size_log2);
78 GLuint SetupVBO(GLenum target, GLsizeiptr size, const GLvoid* data);
79 void CreateLattice(GLfloat** vertices,
80                    GLsizeiptr* size,
81                    GLfloat size_x,
82                    GLfloat size_y,
83                    int width,
84                    int height);
85 int CreateMesh(GLushort** indices,
86                GLsizeiptr* size,
87                int width,
88                int height,
89                int culled_ratio);
90 GLuint InitShaderProgram(const char* vertex_src, const char* fragment_src);
91 GLuint InitShaderProgramWithHeader(const char* header,
92                                    const char* vertex_src,
93                                    const char* fragment_src);
94 GLuint InitShaderProgramWithHeaders(const char** headers,
95                                     int count,
96                                     const char* vertex_src,
97                                     const char* fragment_src);
98 void ClearBuffers();
99 
100 }  // namespace glbench
101 
102 class Callback {
103  public:
104   void Reset() { bind_state_ = NULL; }
105   bool is_null() const { return !bind_state_; }
106   void Run() { bind_state_(); }
107   void Set(std::function<void()> func) { bind_state_ = func; }
108 
109  private:
110   std::function<void()> bind_state_;
111 };
112 
113 #define IMMEDIATE_CRASH() raise(SIGTRAP)
114 
115 // Macro for hinting that an expression is likely to be false.
116 #if !defined(UNLIKELY)
117 #if defined(COMPILER_GCC) || defined(__clang__)
118 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
119 #else
120 #define UNLIKELY(x) (x)
121 #endif  // defined(COMPILER_GCC)
122 #endif  // !defined(UNLIKELY)
123 
124 #define CHECK(condition) UNLIKELY(!(condition)) ? IMMEDIATE_CRASH() : (0)
125 
126 // trim from start (in place)
127 static inline void ltrim(std::string& s) {
128   s.erase(s.begin(), std::find_if(s.begin(), s.end(),
129                                   [](int ch) { return !std::isspace(ch); }));
130 }
131 
132 // trim from end (in place)
133 static inline void rtrim(std::string& s) {
134   s.erase(std::find_if(s.rbegin(), s.rend(),
135                        [](int ch) { return !std::isspace(ch); })
136               .base(),
137           s.end());
138 }
139 
140 // trim from both ends (in place)
141 static inline void trim(std::string& s) {
142   ltrim(s);
143   rtrim(s);
144 }
145 
146 // Put this in the declarations for a class to be uncopyable.
147 #define DISALLOW_COPY(TypeName) TypeName(const TypeName&) = delete
148 
149 // Put this in the declarations for a class to be unassignable.
150 #define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
151 
152 // Put this in the declarations for a class to be uncopyable and unassignable.
153 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
154   DISALLOW_COPY(TypeName);                 \
155   DISALLOW_ASSIGN(TypeName)
156 
157 #endif  // BENCH_GL_UTILS_H_
158