Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 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 | // Application that displays graphics using OpenGL [ES] with the intent |
| 6 | // of being used in functional tests. |
| 7 | |
| 8 | #include <gflags/gflags.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <time.h> |
| 13 | |
| 14 | #include <cmath> |
| 15 | |
Simon Que | 7f840e7 | 2012-12-19 12:05:42 -0800 | [diff] [blame] | 16 | #include "glinterface.h" |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 17 | #include "main.h" |
| 18 | #include "utils.h" |
| 19 | |
| 20 | |
| 21 | GLuint GenerateAndBindTexture() { |
| 22 | GLuint name = ~0; |
| 23 | glGenTextures(1, &name); |
| 24 | glBindTexture(GL_TEXTURE_2D, name); |
| 25 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 26 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Kenneth Waters | ca03c73 | 2010-12-20 12:26:44 -0800 | [diff] [blame] | 27 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 28 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 29 | return name; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | unsigned char* CreateBitmap(int w, int h) { |
| 34 | unsigned char* bitmap = new unsigned char[w * h]; |
| 35 | unsigned char* pixel = bitmap; |
| 36 | float w2 = w/2.0f; |
| 37 | float h2 = h/2.0f; |
| 38 | for (int y = 0; y < h; y++) { |
| 39 | for (int x = 0; x < w; x++) { |
| 40 | // Fill with soft ellipse |
| 41 | float dx = fabs((x - w2) / w2); |
| 42 | float dy = fabs((y - h2) / h2); |
| 43 | float dist2 = dx*dx + dy*dy; |
| 44 | if (dist2 > 1.f) |
| 45 | dist2 = 1.f; |
| 46 | *pixel = (1.f-dist2) * 255.f; |
| 47 | pixel++; |
| 48 | } |
| 49 | } |
| 50 | return bitmap; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | const char kVertexShader[] = |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 55 | "attribute vec4 vertices;" |
Kenneth Waters | ca03c73 | 2010-12-20 12:26:44 -0800 | [diff] [blame] | 56 | "varying vec4 v1;" |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 57 | "void main() {" |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 58 | " gl_Position = vertices;" |
Stuart Abercrombie | b8f4e7a | 2011-09-26 18:07:55 -0700 | [diff] [blame] | 59 | " v1 = vec4(vertices.yx, 0.0, 0.0);" |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 60 | "}"; |
| 61 | |
| 62 | const char kFragmentShader[] = |
| 63 | "uniform sampler2D tex;" |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 64 | "uniform vec4 color;" |
Kenneth Waters | ca03c73 | 2010-12-20 12:26:44 -0800 | [diff] [blame] | 65 | "varying vec4 v1;" |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 66 | "void main() {" |
Stuart Abercrombie | b8f4e7a | 2011-09-26 18:07:55 -0700 | [diff] [blame] | 67 | " gl_FragColor = color * texture2D(tex, v1.xy);" |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 68 | "}"; |
| 69 | |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 70 | // Command line flags |
| 71 | DEFINE_double(screenshot1_sec, 2.f, "seconds delay before screenshot1_cmd"); |
| 72 | DEFINE_double(screenshot2_sec, 1.f, "seconds delay before screenshot2_cmd"); |
| 73 | DEFINE_string(screenshot1_cmd, "", "system command to take a screen shot 1"); |
| 74 | DEFINE_string(screenshot2_cmd, "", "system command to take a screen shot 2"); |
| 75 | DEFINE_double(cooldown_sec, 1.f, "seconds delay after all screenshots"); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 76 | |
| 77 | int main(int argc, char* argv[]) { |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 78 | // Configure full screen |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 79 | g_width = -1; |
| 80 | g_height = -1; |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 81 | |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 82 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 83 | |
Simon Que | 7f840e7 | 2012-12-19 12:05:42 -0800 | [diff] [blame] | 84 | g_main_gl_interface.reset(GLInterface::Create()); |
| 85 | if (!g_main_gl_interface->Init()) { |
Ilja H. Friedel | 8faad30 | 2011-04-26 14:40:49 -0700 | [diff] [blame] | 86 | printf("# Error: Failed to initialize %s.\n", argv[0]); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 90 | glViewport(-g_width, -g_height, g_width*2, g_height*2); |
| 91 | |
| 92 | unsigned char* bitmap = CreateBitmap(g_height, g_width); |
| 93 | GLuint texture = GenerateAndBindTexture(); |
| 94 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, g_height, g_width, 0, |
| 95 | GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap); |
| 96 | |
| 97 | GLfloat vertices[8] = { |
| 98 | 0.f, 0.f, |
| 99 | 1.f, 0.f, |
| 100 | 0.f, 1.f, |
| 101 | 1.f, 1.f, |
| 102 | }; |
| 103 | |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 104 | GLuint program = glbench::InitShaderProgram(kVertexShader, kFragmentShader); |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 105 | int attribute_index = glGetAttribLocation(program, "vertices"); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 106 | glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, vertices); |
| 107 | glEnableVertexAttribArray(attribute_index); |
| 108 | |
| 109 | int texture_sampler = glGetUniformLocation(program, "tex"); |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 110 | glUniform1i(texture_sampler, 0); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 111 | |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 112 | int display_color = glGetUniformLocation(program, "color"); |
| 113 | float white[4] = {1.0f, 1.0f, 1.0f, 1.0f}; |
| 114 | float blue[4] = {0.5f, 0.5f, 1.0f, 1.0f}; |
| 115 | |
| 116 | uint64_t last_event_time = GetUTime(); |
| 117 | enum State { |
| 118 | kStateScreenShot1, |
| 119 | kStateScreenShot2, |
| 120 | kStateCooldown, |
| 121 | kStateExit |
| 122 | } state = kStateScreenShot1; |
| 123 | float seconds_delay_for_next_state[] = { |
Han Shen | 387edd7 | 2012-06-19 16:47:14 -0700 | [diff] [blame] | 124 | static_cast<float>(FLAGS_screenshot1_sec), |
| 125 | static_cast<float>(FLAGS_screenshot2_sec), |
| 126 | static_cast<float>(FLAGS_cooldown_sec), |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 127 | 0 |
| 128 | }; |
| 129 | |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 130 | do { |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 131 | // Draw |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 132 | glClear(GL_COLOR_BUFFER_BIT); |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 133 | if (state == kStateScreenShot1) |
| 134 | glUniform4fv(display_color, 1, white); |
| 135 | else |
| 136 | glUniform4fv(display_color, 1, blue); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 137 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
Simon Que | 7f840e7 | 2012-12-19 12:05:42 -0800 | [diff] [blame] | 138 | g_main_gl_interface->SwapBuffers(); |
Vincent Scheib | df7d51e | 2010-05-06 12:34:36 -0700 | [diff] [blame] | 139 | |
| 140 | // Loop until next event |
| 141 | float seconds_since_last_event = |
| 142 | static_cast<float>(GetUTime() - last_event_time) / 1000000ULL; |
| 143 | if (seconds_since_last_event < seconds_delay_for_next_state[state]) |
| 144 | continue; |
| 145 | |
| 146 | // State change. Perform action. |
| 147 | switch(state) { |
| 148 | case kStateScreenShot1: |
| 149 | system(FLAGS_screenshot1_cmd.c_str()); |
| 150 | break; |
| 151 | case kStateScreenShot2: |
| 152 | system(FLAGS_screenshot2_cmd.c_str()); |
| 153 | break; |
| 154 | default: |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | // Advance to next state |
| 159 | last_event_time = GetUTime(); |
| 160 | state = static_cast<State>(state + 1); |
| 161 | |
| 162 | } while (state != kStateExit); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 163 | |
| 164 | glDeleteTextures(1, &texture); |
Simon Que | 0686082 | 2012-12-18 13:17:56 -0800 | [diff] [blame^] | 165 | g_main_gl_interface->Cleanup(); |
Vincent Scheib | c5114c1 | 2010-04-08 08:49:50 -0700 | [diff] [blame] | 166 | return 0; |
| 167 | } |