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