blob: 6db87b1ff826f3a90bf37c7936ec00a0bfb513a2 [file] [log] [blame]
Vincent Scheibc5114c12010-04-08 08:49:50 -07001// 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
20GLuint 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 Watersca03c732010-12-20 12:26:44 -080026 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 Scheibc5114c12010-04-08 08:49:50 -070028 return name;
29}
30
31
32unsigned 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
53const char kVertexShader[] =
Vincent Scheibdf7d51e2010-05-06 12:34:36 -070054 "attribute vec4 vertices;"
Kenneth Watersca03c732010-12-20 12:26:44 -080055 "varying vec4 v1;"
Vincent Scheibc5114c12010-04-08 08:49:50 -070056 "void main() {"
Vincent Scheibdf7d51e2010-05-06 12:34:36 -070057 " gl_Position = vertices;"
Stuart Abercrombieb8f4e7a2011-09-26 18:07:55 -070058 " v1 = vec4(vertices.yx, 0.0, 0.0);"
Vincent Scheibc5114c12010-04-08 08:49:50 -070059 "}";
60
61const char kFragmentShader[] =
62 "uniform sampler2D tex;"
Vincent Scheibdf7d51e2010-05-06 12:34:36 -070063 "uniform vec4 color;"
Kenneth Watersca03c732010-12-20 12:26:44 -080064 "varying vec4 v1;"
Vincent Scheibc5114c12010-04-08 08:49:50 -070065 "void main() {"
Stuart Abercrombieb8f4e7a2011-09-26 18:07:55 -070066 " gl_FragColor = color * texture2D(tex, v1.xy);"
Vincent Scheibc5114c12010-04-08 08:49:50 -070067 "}";
68
Vincent Scheibdf7d51e2010-05-06 12:34:36 -070069// Command line flags
70DEFINE_double(screenshot1_sec, 2.f, "seconds delay before screenshot1_cmd");
71DEFINE_double(screenshot2_sec, 1.f, "seconds delay before screenshot2_cmd");
72DEFINE_string(screenshot1_cmd, "", "system command to take a screen shot 1");
73DEFINE_string(screenshot2_cmd, "", "system command to take a screen shot 2");
74DEFINE_double(cooldown_sec, 1.f, "seconds delay after all screenshots");
Vincent Scheibc5114c12010-04-08 08:49:50 -070075
76int main(int argc, char* argv[]) {
Vincent Scheibdf7d51e2010-05-06 12:34:36 -070077 // Configure full screen
Vincent Scheibc5114c12010-04-08 08:49:50 -070078 g_width = -1;
79 g_height = -1;
Vincent Scheibdf7d51e2010-05-06 12:34:36 -070080
Vincent Scheibc5114c12010-04-08 08:49:50 -070081 google::ParseCommandLineFlags(&argc, &argv, true);
82
83 if (!Init()) {
Ilja H. Friedel8faad302011-04-26 14:40:49 -070084 printf("# Error: Failed to initialize %s.\n", argv[0]);
Vincent Scheibc5114c12010-04-08 08:49:50 -070085 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 Marinichev9f9b8732010-05-20 19:33:44 -0700103 GLuint program = glbench::InitShaderProgram(kVertexShader, kFragmentShader);
Vincent Scheibdf7d51e2010-05-06 12:34:36 -0700104 int attribute_index = glGetAttribLocation(program, "vertices");
Vincent Scheibc5114c12010-04-08 08:49:50 -0700105 glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, vertices);
106 glEnableVertexAttribArray(attribute_index);
107
108 int texture_sampler = glGetUniformLocation(program, "tex");
Vincent Scheibdf7d51e2010-05-06 12:34:36 -0700109 glUniform1i(texture_sampler, 0);
Vincent Scheibc5114c12010-04-08 08:49:50 -0700110
Vincent Scheibdf7d51e2010-05-06 12:34:36 -0700111 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 Scheibc5114c12010-04-08 08:49:50 -0700129 do {
Vincent Scheibdf7d51e2010-05-06 12:34:36 -0700130 // Draw
Vincent Scheibc5114c12010-04-08 08:49:50 -0700131 glClear(GL_COLOR_BUFFER_BIT);
Vincent Scheibdf7d51e2010-05-06 12:34:36 -0700132 if (state == kStateScreenShot1)
133 glUniform4fv(display_color, 1, white);
134 else
135 glUniform4fv(display_color, 1, blue);
Vincent Scheibc5114c12010-04-08 08:49:50 -0700136 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
137 SwapBuffers();
Vincent Scheibdf7d51e2010-05-06 12:34:36 -0700138
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 Scheibc5114c12010-04-08 08:49:50 -0700162
163 glDeleteTextures(1, &texture);
164 DestroyContext();
165 return 0;
166}