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 #include "Utility.h"
17 
18 #include <GLES3/gl3.h>
19 
20 #include "AndroidOut.h"
21 #include "JNIManager.h"
22 #include "Renderer.h"
23 
24 #define CHECK_ERROR(e)                        \
25     case e:                                   \
26         aout << "GL Error: " #e << std::endl; \
27         break;
28 
checkAndLogGlError(bool alwaysLog)29 bool Utility::checkAndLogGlError(bool alwaysLog) {
30     GLenum error = glGetError();
31     if (error == GL_NO_ERROR) {
32         if (alwaysLog) {
33             aout << "No GL error" << std::endl;
34         }
35         return true;
36     } else {
37         switch (error) {
38             CHECK_ERROR(GL_INVALID_ENUM);
39             CHECK_ERROR(GL_INVALID_VALUE);
40             CHECK_ERROR(GL_INVALID_OPERATION);
41             CHECK_ERROR(GL_INVALID_FRAMEBUFFER_OPERATION);
42             CHECK_ERROR(GL_OUT_OF_MEMORY);
43             default:
44                 aout << "Unknown GL error: " << error << std::endl;
45         }
46         return false;
47     }
48 }
49 
buildOrthographicMatrix(float * outMatrix,float halfHeight,float aspect,float near,float far)50 float *Utility::buildOrthographicMatrix(float *outMatrix, float halfHeight, float aspect,
51                                         float near, float far) {
52     float halfWidth = halfHeight * aspect;
53 
54     // column 1
55     outMatrix[0] = 1.f / halfWidth;
56     outMatrix[1] = 0.f;
57     outMatrix[2] = 0.f;
58     outMatrix[3] = 0.f;
59 
60     // column 2
61     outMatrix[4] = 0.f;
62     outMatrix[5] = 1.f / halfHeight;
63     outMatrix[6] = 0.f;
64     outMatrix[7] = 0.f;
65 
66     // column 3
67     outMatrix[8] = 0.f;
68     outMatrix[9] = 0.f;
69     outMatrix[10] = -2.f / (far - near);
70     outMatrix[11] = -(far + near) / (far - near);
71 
72     // column 4
73     outMatrix[12] = 0.f;
74     outMatrix[13] = 0.f;
75     outMatrix[14] = 0.f;
76     outMatrix[15] = 1.f;
77 
78     return outMatrix;
79 }
80 
buildIdentityMatrix(float * outMatrix)81 float *Utility::buildIdentityMatrix(float *outMatrix) {
82     // column 1
83     outMatrix[0] = 1.f;
84     outMatrix[1] = 0.f;
85     outMatrix[2] = 0.f;
86     outMatrix[3] = 0.f;
87 
88     // column 2
89     outMatrix[4] = 0.f;
90     outMatrix[5] = 1.f;
91     outMatrix[6] = 0.f;
92     outMatrix[7] = 0.f;
93 
94     // column 3
95     outMatrix[8] = 0.f;
96     outMatrix[9] = 0.f;
97     outMatrix[10] = 1.f;
98     outMatrix[11] = 0.f;
99 
100     // column 4
101     outMatrix[12] = 0.f;
102     outMatrix[13] = 0.f;
103     outMatrix[14] = 0.f;
104     outMatrix[15] = 1.f;
105 
106     return outMatrix;
107 }
108 
setFailure(std::string message,Renderer * renderer)109 void Utility::setFailure(std::string message, Renderer *renderer) {
110     aerr << message << std::endl;
111     if (renderer != nullptr) {
112         renderer->addResult("failure", message);
113         JNIManager::sendResultsToJava(renderer->getResults());
114         delete renderer;
115     } else {
116         JNIManager::sendResultsToJava({{"failure", message}});
117     }
118     exit(1);
119 }
120