1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include <cassert> 19 #include <chrono> 20 #include <string> 21 22 #include "Renderer.h" 23 24 class Utility { 25 public: 26 static bool checkAndLogGlError(bool alwaysLog = false); 27 assertGlError()28 static inline void assertGlError() { assert(checkAndLogGlError()); } 29 30 /** 31 * Generates an orthographic projection matrix given the half height, aspect ratio, near, and 32 * far planes 33 * 34 * @param outMatrix the matrix to write into 35 * @param halfHeight half of the height of the screen 36 * @param aspect the width of the screen divided by the height 37 * @param near the distance of the near plane 38 * @param far the distance of the far plane 39 * @return the generated matrix, this will be the same as @a outMatrix so you can chain calls 40 * together if needed 41 */ 42 static float *buildOrthographicMatrix(float *outMatrix, float halfHeight, float aspect, 43 float near, float far); 44 45 static float *buildIdentityMatrix(float *outMatrix); 46 47 static void setFailure(std::string message, Renderer *renderer = nullptr); 48 now()49 static int64_t now() { 50 return duration_cast<std::chrono::nanoseconds>( 51 std::chrono::steady_clock::now().time_since_epoch()) 52 .count(); 53 } 54 55 // Converts lists of numbers into strings, so they can be 56 // passed up to the Java code the results map. 57 template <typename T> serializeValues(const std::vector<T> & values)58 static std::string serializeValues(const std::vector<T> &values) { 59 std::stringstream stream; 60 for (auto &&value : values) { 61 stream << value; 62 stream << ","; 63 } 64 std::string out = stream.str(); 65 out.pop_back(); // remove the last comma 66 return out; 67 } 68 }; 69