| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Application that displays graphics using OpenGL [ES] with the intent | 5 // Application that displays graphics using OpenGL [ES] with the intent |
| 6 // of being used in functional tests. | 6 // of being used in functional tests. |
| 7 | 7 |
| 8 #include <gflags/gflags.h> | 8 #include <gflags/gflags.h> |
| 9 #include <stdio.h> | 9 #include <stdio.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 dist2 = 1.f; | 44 dist2 = 1.f; |
| 45 *pixel = (1.f-dist2) * 255.f; | 45 *pixel = (1.f-dist2) * 255.f; |
| 46 pixel++; | 46 pixel++; |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 return bitmap; | 49 return bitmap; |
| 50 } | 50 } |
| 51 | 51 |
| 52 | 52 |
| 53 const char kVertexShader[] = | 53 const char kVertexShader[] = |
| 54 "attribute vec4 c;" | 54 "attribute vec4 vertices;" |
| 55 "void main() {" | 55 "void main() {" |
| 56 " gl_Position = c;" | 56 " gl_Position = vertices;" |
| 57 " gl_TexCoord[0] = vec4(c.y, c.x, 0.0, 0.0);" | 57 " gl_TexCoord[0] = vec4(vertices.y, vertices.x, 0.0, 0.0);" |
| 58 "}"; | 58 "}"; |
| 59 | 59 |
| 60 const char kFragmentShader[] = | 60 const char kFragmentShader[] = |
| 61 "uniform sampler2D tex;" | 61 "uniform sampler2D tex;" |
| 62 "uniform vec4 color;" |
| 62 "void main() {" | 63 "void main() {" |
| 63 " gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);" | 64 " gl_FragColor = color * texture2D(tex, gl_TexCoord[0].xy);" |
| 64 "}"; | 65 "}"; |
| 65 | 66 |
| 66 | 67 // Command line flags |
| 67 // If refresh is set to zero, we enable vsync. Otherwise we redraw that many | 68 DEFINE_double(screenshot1_sec, 2.f, "seconds delay before screenshot1_cmd"); |
| 68 // times a second. | 69 DEFINE_double(screenshot2_sec, 1.f, "seconds delay before screenshot2_cmd"); |
| 69 DEFINE_int32(seconds_to_run, 10, "seconds to run application for"); | 70 DEFINE_string(screenshot1_cmd, "", "system command to take a screen shot 1"); |
| 71 DEFINE_string(screenshot2_cmd, "", "system command to take a screen shot 2"); |
| 72 DEFINE_double(cooldown_sec, 1.f, "seconds delay after all screenshots"); |
| 70 | 73 |
| 71 int main(int argc, char* argv[]) { | 74 int main(int argc, char* argv[]) { |
| 75 // Configure full screen |
| 72 g_width = -1; | 76 g_width = -1; |
| 73 g_height = -1; | 77 g_height = -1; |
| 78 |
| 74 google::ParseCommandLineFlags(&argc, &argv, true); | 79 google::ParseCommandLineFlags(&argc, &argv, true); |
| 75 | 80 |
| 76 if (!Init()) { | 81 if (!Init()) { |
| 77 printf("# Failed to initialize.\n"); | 82 printf("# Failed to initialize.\n"); |
| 78 return 1; | 83 return 1; |
| 79 } | 84 } |
| 80 | 85 |
| 81 InitContext(); | 86 InitContext(); |
| 82 glViewport(-g_width, -g_height, g_width*2, g_height*2); | 87 glViewport(-g_width, -g_height, g_width*2, g_height*2); |
| 83 | 88 |
| 84 unsigned char* bitmap = CreateBitmap(g_height, g_width); | 89 unsigned char* bitmap = CreateBitmap(g_height, g_width); |
| 85 GLuint texture = GenerateAndBindTexture(); | 90 GLuint texture = GenerateAndBindTexture(); |
| 86 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, g_height, g_width, 0, | 91 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, g_height, g_width, 0, |
| 87 GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap); | 92 GL_LUMINANCE, GL_UNSIGNED_BYTE, bitmap); |
| 88 | 93 |
| 89 GLfloat vertices[8] = { | 94 GLfloat vertices[8] = { |
| 90 0.f, 0.f, | 95 0.f, 0.f, |
| 91 1.f, 0.f, | 96 1.f, 0.f, |
| 92 0.f, 1.f, | 97 0.f, 1.f, |
| 93 1.f, 1.f, | 98 1.f, 1.f, |
| 94 }; | 99 }; |
| 95 | 100 |
| 96 GLuint program = InitShaderProgram(kVertexShader, kFragmentShader); | 101 GLuint program = InitShaderProgram(kVertexShader, kFragmentShader); |
| 97 int attribute_index = glGetAttribLocation(program, "c"); | 102 int attribute_index = glGetAttribLocation(program, "vertices"); |
| 98 glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, vertices); | 103 glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, vertices); |
| 99 glEnableVertexAttribArray(attribute_index); | 104 glEnableVertexAttribArray(attribute_index); |
| 100 | 105 |
| 101 int texture_sampler = glGetUniformLocation(program, "tex"); | 106 int texture_sampler = glGetUniformLocation(program, "tex"); |
| 102 glUniform1f(texture_sampler, 0); | 107 glUniform1i(texture_sampler, 0); |
| 103 | 108 |
| 104 uint64_t wait_until_done = GetUTime() + 1000000ULL * FLAGS_seconds_to_run; | 109 int display_color = glGetUniformLocation(program, "color"); |
| 110 float white[4] = {1.0f, 1.0f, 1.0f, 1.0f}; |
| 111 float blue[4] = {0.5f, 0.5f, 1.0f, 1.0f}; |
| 112 |
| 113 uint64_t last_event_time = GetUTime(); |
| 114 enum State { |
| 115 kStateScreenShot1, |
| 116 kStateScreenShot2, |
| 117 kStateCooldown, |
| 118 kStateExit |
| 119 } state = kStateScreenShot1; |
| 120 float seconds_delay_for_next_state[] = { |
| 121 FLAGS_screenshot1_sec, |
| 122 FLAGS_screenshot2_sec, |
| 123 FLAGS_cooldown_sec, |
| 124 0 |
| 125 }; |
| 126 |
| 105 do { | 127 do { |
| 128 // Draw |
| 106 glClear(GL_COLOR_BUFFER_BIT); | 129 glClear(GL_COLOR_BUFFER_BIT); |
| 130 if (state == kStateScreenShot1) |
| 131 glUniform4fv(display_color, 1, white); |
| 132 else |
| 133 glUniform4fv(display_color, 1, blue); |
| 107 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | 134 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 108 SwapBuffers(); | 135 SwapBuffers(); |
| 109 } while (GetUTime() < wait_until_done); | 136 |
| 137 // Loop until next event |
| 138 float seconds_since_last_event = |
| 139 static_cast<float>(GetUTime() - last_event_time) / 1000000ULL; |
| 140 if (seconds_since_last_event < seconds_delay_for_next_state[state]) |
| 141 continue; |
| 142 |
| 143 // State change. Perform action. |
| 144 switch(state) { |
| 145 case kStateScreenShot1: |
| 146 system(FLAGS_screenshot1_cmd.c_str()); |
| 147 break; |
| 148 case kStateScreenShot2: |
| 149 system(FLAGS_screenshot2_cmd.c_str()); |
| 150 break; |
| 151 default: |
| 152 break; |
| 153 } |
| 154 |
| 155 // Advance to next state |
| 156 last_event_time = GetUTime(); |
| 157 state = static_cast<State>(state + 1); |
| 158 |
| 159 } while (state != kStateExit); |
| 110 | 160 |
| 111 glDeleteTextures(1, &texture); | 161 glDeleteTextures(1, &texture); |
| 112 DestroyContext(); | 162 DestroyContext(); |
| 113 return 0; | 163 return 0; |
| 114 } | 164 } |
| OLD | NEW |