1 /*
2  * Copyright © 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "piglit-util-gl.h"
25 #include "egl-util.h"
26 
27 static bool test_srgb;
28 
29 static enum piglit_result
draw(struct egl_state * state)30 draw(struct egl_state *state)
31 {
32 	enum piglit_result result = PIGLIT_PASS;
33 	float green[] = {0, 0.3, 0, 0};
34 	float expected_green[4];
35 	float expected_blend[4];
36 
37 	eglMakeCurrent(state->egl_dpy, state->surf, state->surf, state->ctx);
38 
39 	glViewport(0, 0, state->width, state->height);
40 	piglit_ortho_projection(state->width, state->height, GL_FALSE);
41 
42 	glClearColor(0.5, 0.5, 0.5, 1.0);
43 	glClear(GL_COLOR_BUFFER_BIT);
44 
45 	glColor4f(green[0], green[1], green[2], green[3]);
46 
47 	/* Draw first without blending. */
48 	piglit_draw_rect(0, 0, 20, 20);
49 
50 	/* Draw second with blending. */
51 	piglit_draw_rect(20, 0, 20, 20);
52 	glEnable(GL_BLEND);
53 	glBlendFunc(GL_ONE, GL_ONE);
54 	piglit_draw_rect(20, 0, 20, 20);
55 	glDisable(GL_BLEND);
56 
57 	/* Check first. */
58 	memcpy(expected_green, green, sizeof(float) * 4);
59 	if (test_srgb)
60 		expected_green[1] = piglit_linear_to_srgb(green[1]);
61 	if (!piglit_probe_rect_rgb(0, 0, 20, 20, expected_green))
62 		result = PIGLIT_FAIL;
63 
64 	/* Check second. */
65 	memcpy(expected_blend, green, sizeof(float) * 4);
66 	if (test_srgb)
67 		expected_blend[1] = piglit_linear_to_srgb(green[1] * 2.0);
68 	else
69 		expected_blend[1] *= 2;
70 	if (!piglit_probe_rect_rgb(20, 0, 20, 20, expected_blend))
71 		result = PIGLIT_FAIL;
72 
73 	eglSwapBuffers(state->egl_dpy, state->surf);
74 	return result;
75 }
76 
77 int
main(int argc,char * argv[])78 main(int argc, char *argv[])
79 {
80 	struct egl_test test;
81 	const char *extensions[] = {"EGL_KHR_gl_colorspace", NULL};
82 	const EGLint surface_linear[] = {
83 		EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_LINEAR_KHR,
84 		EGL_NONE
85 	};
86 	const EGLint surface_srgb[] = {
87 		EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR,
88 		EGL_NONE
89 	};
90 	const EGLint test_attribs[] = {
91 		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
92 		EGL_NONE
93 	};
94 
95 	piglit_strip_arg(&argc, argv, "-fbo");
96 	test_srgb = piglit_strip_arg(&argc, argv, "srgb");
97 
98 	egl_init_test(&test);
99 	test.draw = draw;
100 	test.stop_on_failure = true;
101 	test.config_attribs = test_attribs;
102 	test.surface_attribs = test_srgb ? surface_srgb : surface_linear;
103 	test.extensions = extensions;
104 
105 	return egl_util_run(&test, argc, argv) == PIGLIT_PASS ? EXIT_SUCCESS
106 							      : EXIT_FAILURE;
107 }
108