| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd |
| 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 | * Authored by: Thomas Voß <thomas.voss@canonical.com> |
| 17 | * Ricardo Salveti de Araujo <ricardo.salveti@canonical.com> |
| 18 | */ |
| 19 | |
| 20 | #include <hybris/surface_flinger/surface_flinger_compatibility_layer.h> |
| 21 | |
| 22 | #include <cstdio> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <GLES2/gl2.h> |
| 26 | #include <GLES2/gl2ext.h> |
| 27 | |
| 28 | /* animation state variables */ |
| 29 | GLfloat rotation_angle = 0.0f; |
| 30 | |
| 31 | static GLuint gProgram; |
| 32 | static GLuint gvPositionHandle, gvColorHandle; |
| 33 | static GLuint rotation_uniform; |
| 34 | static GLint num_vertex = 3; |
| 35 | static const GLfloat triangle[] = { |
| 36 | -0.125f, -0.125f, 0.0f, 0.5f, |
| 37 | 0.0f, 0.125f, 0.0f, 0.5f, |
| 38 | 0.125f, -0.125f, 0.0f, 0.5f |
| 39 | }; |
| 40 | static const GLfloat color_triangle[] = { |
| 41 | 0.0f, 0.0f, 1.0f, 1.0f, |
| 42 | 0.0f, 1.0f, 0.0f, 1.0f, |
| 43 | 1.0f, 0.0f, 0.0f, 0.0f |
| 44 | }; |
| 45 | const GLfloat * vertex_data; |
| 46 | const GLfloat * color_data; |
| 47 | static const char gVertexShader[] = |
| 48 | "attribute vec4 vPosition;\n" |
| 49 | "attribute vec4 vColor;\n" |
| 50 | "uniform float angle;\n" |
| 51 | "varying vec4 colorinfo;\n" |
| 52 | "void main() {\n" |
| 53 | " mat3 rot_z = mat3( vec3( cos(angle), sin(angle), 0.0),\n" |
| 54 | " vec3(-sin(angle), cos(angle), 0.0),\n" |
| 55 | " vec3( 0.0, 0.0, 1.0));\n" |
| 56 | " gl_Position = vec4(rot_z * vPosition.xyz, 1.0);\n" |
| 57 | " colorinfo = vColor;\n" |
| 58 | "}\n"; |
| 59 | |
| 60 | static const char gFragmentShader[] = "precision mediump float;\n" |
| 61 | "varying vec4 colorinfo;\n" |
| 62 | "void main() {\n" |
| 63 | " gl_FragColor = colorinfo;\n" |
| 64 | "}\n"; |
| 65 | |
| 66 | /* util functions */ |
| 67 | GLuint loadShader(GLenum shaderType, const char* pSource) |
| 68 | { |
| 69 | GLuint shader = glCreateShader(shaderType); |
| 70 | if (shader) { |
| 71 | glShaderSource(shader, 1, &pSource, NULL); |
| 72 | glCompileShader(shader); |
| 73 | GLint compiled = 0; |
| 74 | glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); |
| 75 | |
| 76 | if (!compiled) { |
| 77 | GLint infoLen = 0; |
| 78 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); |
| 79 | if (infoLen) { |
| 80 | char* buf = (char*) malloc(infoLen); |
| 81 | if (buf) { |
| 82 | glGetShaderInfoLog(shader, infoLen, NULL, buf); |
| 83 | fprintf(stderr, "Could not compile shader %d:\n%s\n", |
| 84 | shaderType, buf); |
| 85 | free(buf); |
| 86 | } |
| 87 | glDeleteShader(shader); |
| 88 | shader = 0; |
| 89 | } |
| 90 | } |
| 91 | } else { |
| 92 | printf("Error, during shader creation: %i\n", glGetError()); |
| 93 | } |
| 94 | |
| 95 | return shader; |
| 96 | } |
| 97 | |
| 98 | GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) |
| 99 | { |
| 100 | GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); |
| 101 | if (!vertexShader) { |
| 102 | printf("vertex shader not compiled\n"); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); |
| 107 | if (!pixelShader) { |
| 108 | printf("frag shader not compiled\n"); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | GLuint program = glCreateProgram(); |
| 113 | if (program) { |
| 114 | glAttachShader(program, vertexShader); |
| 115 | glAttachShader(program, pixelShader); |
| 116 | glLinkProgram(program); |
| 117 | GLint linkStatus = GL_FALSE; |
| 118 | glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| 119 | |
| 120 | if (linkStatus != GL_TRUE) { |
| 121 | GLint bufLength = 0; |
| 122 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); |
| 123 | if (bufLength) { |
| 124 | char* buf = (char*) malloc(bufLength); |
| 125 | if (buf) { |
| 126 | glGetProgramInfoLog(program, bufLength, NULL, buf); |
| 127 | fprintf(stderr, "Could not link program:\n%s\n", buf); |
| 128 | free(buf); |
| 129 | } |
| 130 | } |
| 131 | glDeleteProgram(program); |
| 132 | program = 0; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return program; |
| 137 | } |
| 138 | |
| 139 | bool setupGraphics() |
| 140 | { |
| 141 | vertex_data = triangle; |
| 142 | color_data = color_triangle; |
| 143 | |
| 144 | gProgram = createProgram(gVertexShader, gFragmentShader); |
| 145 | if (!gProgram) { |
| 146 | printf("error making program\n"); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | gvPositionHandle = glGetAttribLocation(gProgram, "vPosition"); |
| 151 | gvColorHandle = glGetAttribLocation(gProgram, "vColor"); |
| 152 | |
| 153 | rotation_uniform = glGetUniformLocation(gProgram, "angle"); |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | void hw_render(EGLDisplay displ, EGLSurface surface) |
| 159 | { |
| 160 | glUseProgram(gProgram); |
| 161 | |
| 162 | glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 163 | |
| 164 | glUniform1fv(rotation_uniform,1, &rotation_angle); |
| 165 | |
| 166 | glVertexAttribPointer(gvColorHandle, num_vertex, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*4, color_data); |
| 167 | glVertexAttribPointer(gvPositionHandle, num_vertex, GL_FLOAT, GL_FALSE, 0, vertex_data); |
| 168 | glEnableVertexAttribArray(gvPositionHandle); |
| 169 | glEnableVertexAttribArray(gvColorHandle); |
| 170 | |
| 171 | glDrawArrays(GL_TRIANGLE_STRIP, 0, num_vertex); |
| 172 | glDisableVertexAttribArray(gvPositionHandle); |
| 173 | glDisableVertexAttribArray(gvColorHandle); |
| 174 | |
| 175 | eglSwapBuffers(displ, surface); |
| 176 | } |
| 177 | |
| 178 | void hw_step() |
| 179 | { |
| 180 | rotation_angle += 0.01; |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | int main(int argc, char** argv) |
| 185 | { |
| 186 | SfClient* sf_client = sf_client_create(); |
| 187 | |
| 188 | if (!sf_client) { |
| 189 | printf("Problem creating client ... aborting now."); |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | SfSurfaceCreationParameters params = { |
| 194 | 200, |
| 195 | 200, |
| 196 | 500, |
| 197 | 500, |
| 198 | -1, //PIXEL_FORMAT_RGBA_8888, |
| 199 | INT_MAX, |
| 200 | 0.5f, |
| 201 | true, // Associate surface with egl |
| 202 | "A test surface" |
| 203 | }; |
| 204 | |
| 205 | SfSurface* sf_surface = sf_surface_create(sf_client, ¶ms); |
| 206 | |
| 207 | if (!sf_surface) { |
| 208 | printf("Problem creating surface ... aborting now."); |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | sf_surface_make_current(sf_surface); |
| 213 | |
| 214 | EGLDisplay disp = sf_client_get_egl_display(sf_client); |
| 215 | EGLSurface surface = sf_surface_get_egl_surface(sf_surface); |
| 216 | |
| 217 | setupGraphics(); |
| 218 | |
| 219 | printf("Turning off screen\n"); |
| 220 | sf_blank(0); |
| 221 | |
| 222 | sleep(1); |
| 223 | |
| 224 | printf("Turning on screen\n"); |
| 225 | sf_unblank(0); |
| 226 | |
| 227 | for(;;) { |
| 228 | hw_render(disp, surface); |
| 229 | hw_step(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |