1/*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.0 Module
3 * -------------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Prerequisite tests.
22 *//*--------------------------------------------------------------------*/
23
24#include "es3fPrerequisiteTests.hpp"
25#include "deRandom.h"
26#include "tcuRGBA.hpp"
27#include "tcuSurface.hpp"
28#include "tcuTextureUtil.hpp"
29#include "tcuTestLog.hpp"
30#include "tcuRenderTarget.hpp"
31#include "gluPixelTransfer.hpp"
32#include "gluStateReset.hpp"
33
34#include "glw.h"
35
36using tcu::RGBA;
37using tcu::Surface;
38using tcu::TestLog;
39
40namespace deqp
41{
42namespace gles3
43{
44namespace Functional
45{
46
47class StateResetCase : public TestCase
48{
49public:
50										StateResetCase	(Context& context);
51	virtual								~StateResetCase	(void);
52	virtual TestCase::IterateResult		iterate			(void);
53};
54
55StateResetCase::StateResetCase (Context& context)
56	: TestCase(context, "state_reset", "State Reset Test")
57{
58}
59
60StateResetCase::~StateResetCase (void)
61{
62}
63
64TestCase::IterateResult StateResetCase::iterate (void)
65{
66	try
67	{
68		glu::resetState(m_context.getRenderContext());
69		m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
70	}
71	catch (const tcu::TestError& e)
72	{
73		m_testCtx.getLog() << e;
74		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
75	}
76
77	return TestCase::STOP;
78}
79
80class ClearColorCase : public TestCase
81{
82public:
83										ClearColorCase		(Context& context);
84	virtual								~ClearColorCase		(void);
85	virtual TestCase::IterateResult		iterate				(void);
86
87private:
88	RGBA		m_clearColor;
89	int			m_numIters;
90	int			m_curIter;
91};
92
93ClearColorCase::ClearColorCase (Context& context)
94	: TestCase		(context, "clear_color", "glClearColor test")
95	, m_numIters	(10)
96	, m_curIter		(0)
97{
98}
99
100ClearColorCase::~ClearColorCase (void)
101{
102}
103
104TestCase::IterateResult ClearColorCase::iterate (void)
105{
106	int r = 0;
107	int g = 0;
108	int b = 0;
109	int a = 255;
110
111	switch (m_curIter)
112	{
113		case 0:
114			// Black, skip
115			break;
116		case 1:
117			r = 255;
118			g = 255;
119			b = 255;
120			break;
121		case 2:
122			r = 255;
123			break;
124		case 3:
125			g = 255;
126			break;
127		case 4:
128			b = 255;
129			break;
130		default:
131			deRandom rnd;
132			deRandom_init(&rnd, deInt32Hash(m_curIter));
133			r = (int)(deRandom_getUint32(&rnd) & 0xFF);
134			g = (int)(deRandom_getUint32(&rnd) & 0xFF);
135			b = (int)(deRandom_getUint32(&rnd) & 0xFF);
136			a = (int)(deRandom_getUint32(&rnd) & 0xFF);
137			break;
138
139	};
140
141	glClearColor(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
142	glClear(GL_COLOR_BUFFER_BIT);
143
144	GLU_CHECK_MSG("CLES2 ClearColor failed.");
145
146	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
147
148	return (++m_curIter < m_numIters) ? CONTINUE : STOP;
149}
150
151class ReadPixelsCase : public TestCase
152{
153public:
154										ReadPixelsCase		(Context& context);
155	virtual								~ReadPixelsCase		(void);
156	virtual TestCase::IterateResult		iterate				(void);
157
158private:
159	int m_numIters;
160	int m_curIter;
161};
162
163ReadPixelsCase::ReadPixelsCase (Context& context)
164	: TestCase(context, "read_pixels", "Read pixels test")
165	, m_numIters(20)
166	, m_curIter(0)
167{
168}
169
170ReadPixelsCase::~ReadPixelsCase (void)
171{
172}
173
174TestCase::IterateResult ReadPixelsCase::iterate (void)
175{
176	const tcu::RenderTarget&	renderTarget	= m_context.getRenderTarget();
177	tcu::PixelFormat			pixelFormat		= renderTarget.getPixelFormat();
178	int							targetWidth		= renderTarget.getWidth();
179	int							targetHeight	= renderTarget.getHeight();
180	int							x				= 0;
181	int							y				= 0;
182	int							imageWidth		= 0;
183	int							imageHeight		= 0;
184
185	deRandom rnd;
186	deRandom_init(&rnd, deInt32Hash(m_curIter));
187
188	switch (m_curIter)
189	{
190		case 0:
191			// Fullscreen
192			x = 0;
193			y = 0;
194			imageWidth  = targetWidth;
195			imageHeight = targetHeight;
196			break;
197		case 1:
198			// Upper left corner
199			x = 0;
200			y = 0;
201			imageWidth = targetWidth / 2;
202			imageHeight = targetHeight / 2;
203			break;
204		case 2:
205			// Lower right corner
206			x = targetWidth / 2;
207			y = targetHeight / 2;
208			imageWidth = targetWidth - x;
209			imageHeight = targetHeight - y;
210			break;
211		default:
212			x = deRandom_getUint32(&rnd) % (targetWidth - 1);
213			y = deRandom_getUint32(&rnd) % (targetHeight - 1);
214			imageWidth = 1 + (deRandom_getUint32(&rnd) % (targetWidth - x - 1));
215			imageHeight = 1 + (deRandom_getUint32(&rnd) % (targetHeight - y - 1));
216			break;
217	}
218
219	Surface	resImage(imageWidth, imageHeight);
220	Surface	refImage(imageWidth, imageHeight);
221	Surface	diffImage(imageWidth, imageHeight);
222
223	int r = (int)(deRandom_getUint32(&rnd) & 0xFF);
224	int g = (int)(deRandom_getUint32(&rnd) & 0xFF);
225	int b = (int)(deRandom_getUint32(&rnd) & 0xFF);
226
227	tcu::clear(refImage.getAccess(), tcu::IVec4(r, g, b, 255));
228	glClearColor(r/255.0f, g/255.0f, b/255.0f, 1.0f);
229	glClear(GL_COLOR_BUFFER_BIT);
230
231	glu::readPixels(m_context.getRenderContext(), x, y, resImage.getAccess());
232	GLU_CHECK_MSG("glReadPixels() failed.");
233
234	RGBA colorThreshold = pixelFormat.getColorThreshold();
235	RGBA matchColor(0, 255, 0, 255);
236	RGBA diffColor(255, 0, 0, 255);
237	bool isImageOk = true;
238
239	for (int j = 0; j < imageHeight; j++)
240	{
241		for (int i = 0; i < imageWidth; i++)
242		{
243			RGBA		resRGBA		= resImage.getPixel(i, j);
244			RGBA		refRGBA		= refImage.getPixel(i, j);
245			bool		isPixelOk	= compareThreshold(refRGBA, resRGBA, colorThreshold);
246			diffImage.setPixel(i, j, isPixelOk ? matchColor : diffColor);
247
248			isImageOk = isImageOk && isPixelOk;
249		}
250	}
251
252	if (isImageOk)
253		m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
254	else
255	{
256		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
257
258		m_testCtx.getLog() << TestLog::ImageSet("Result", "Resulting framebuffer")
259						   << TestLog::Image("Result",		"Resulting framebuffer",	resImage)
260						   << TestLog::Image("Reference",	"Reference image",			refImage)
261						   << TestLog::Image("DiffMask",	"Failing pixels",			diffImage)
262						   << TestLog::EndImageSet;
263	}
264
265	return (++m_curIter < m_numIters) ? CONTINUE : STOP;
266}
267
268PrerequisiteTests::PrerequisiteTests (Context& context)
269	: TestCaseGroup(context, "prerequisite", "Prerequisite Test Cases")
270{
271}
272
273PrerequisiteTests::~PrerequisiteTests (void)
274{
275}
276
277void PrerequisiteTests::init (void)
278{
279	addChild(new StateResetCase(m_context));
280	addChild(new ClearColorCase(m_context));
281	addChild(new ReadPixelsCase(m_context));
282}
283
284} // Functional
285} // gles3
286} // deqp
287