• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program EGL Module
3  * ---------------------------------------
4  *
5  * Copyright 2017 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 Test the EGL_ANDROID_get_frame_timestamps extension.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "teglGetFrameTimestampsTests.hpp"
25 
26 #include "teglSimpleConfigCase.hpp"
27 
28 #include "egluNativeWindow.hpp"
29 #include "egluUtil.hpp"
30 #include "egluUnique.hpp"
31 #include "eglwLibrary.hpp"
32 #include "eglwEnums.hpp"
33 
34 #include "gluDefs.hpp"
35 #include "glwEnums.hpp"
36 #include "glwFunctions.hpp"
37 
38 #include "tcuResultCollector.hpp"
39 #include "tcuTestLog.hpp"
40 #include "tcuSurface.hpp"
41 #include "tcuTexture.hpp"
42 #include "tcuTextureUtil.hpp"
43 #include "tcuImageCompare.hpp"
44 #include "tcuVector.hpp"
45 #include "tcuVectorUtil.hpp"
46 
47 #include "deClock.h"
48 #include "deMath.h"
49 #include "deUniquePtr.hpp"
50 #include "deStringUtil.hpp"
51 #include "deThread.hpp"
52 
53 #include <algorithm>
54 #include <string>
55 #include <vector>
56 #include <sstream>
57 
58 // Tentative EGL header definitions for EGL_ANDROID_get_Frame_timestamps.
59 // \todo [2017-01-25 brianderson] Remove once defined in the official headers.
60 #define EGL_TIMESTAMPS_ANDROID 0x3430
61 #define EGL_COMPOSITE_DEADLINE_ANDROID 0x3431
62 #define EGL_COMPOSITE_INTERVAL_ANDROID 0x3432
63 #define EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID 0x3433
64 #define EGL_REQUESTED_PRESENT_TIME_ANDROID 0x3434
65 #define EGL_RENDERING_COMPLETE_TIME_ANDROID 0x3435
66 #define EGL_COMPOSITION_LATCH_TIME_ANDROID 0x3436
67 #define EGL_FIRST_COMPOSITION_START_TIME_ANDROID 0x3437
68 #define EGL_LAST_COMPOSITION_START_TIME_ANDROID 0x3438
69 #define EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID 0x3439
70 #define EGL_DISPLAY_PRESENT_TIME_ANDROID 0x343A
71 #define EGL_DEQUEUE_READY_TIME_ANDROID 0x343B
72 #define EGL_READS_DONE_TIME_ANDROID 0x343C
73 typedef deInt64 EGLnsecsANDROID;
74 typedef deUint64 EGLuint64KHR;
75 #define EGL_TIMESTAMP_PENDING_ANDROID (-2)
76 #define EGL_TIMESTAMP_INVALID_ANDROID (-1)
77 typedef EGLW_APICALL eglw::EGLBoolean (EGLW_APIENTRY* eglGetNextFrameIdANDROIDFunc) (eglw::EGLDisplay dpy, eglw::EGLSurface surface, EGLuint64KHR *frameId);
78 typedef EGLW_APICALL eglw::EGLBoolean (EGLW_APIENTRY* eglGetCompositorTimingANDROIDFunc) (eglw::EGLDisplay dpy, eglw::EGLSurface surface, eglw::EGLint numTimestamps, const eglw::EGLint *names, EGLnsecsANDROID *values);
79 typedef EGLW_APICALL eglw::EGLBoolean (EGLW_APIENTRY* eglGetCompositorTimingSupportedANDROIDFunc) (eglw::EGLDisplay dpy, eglw::EGLSurface surface, eglw::EGLint name);
80 typedef EGLW_APICALL eglw::EGLBoolean (EGLW_APIENTRY* eglGetFrameTimestampsANDROIDFunc) (eglw::EGLDisplay dpy, eglw::EGLSurface surface, EGLuint64KHR frameId, eglw::EGLint numTimestamps, const eglw::EGLint *timestamps, EGLnsecsANDROID *values);
81 typedef EGLW_APICALL eglw::EGLBoolean (EGLW_APIENTRY* eglGetFrameTimestampSupportedANDROIDFunc) (eglw::EGLDisplay dpy, eglw::EGLSurface surface, eglw::EGLint timestamp);
82 
83 #define CHECK_NAKED_EGL_CALL(EGLW, CALL)	do { CALL; eglu::checkError((EGLW).getError(), #CALL, __FILE__, __LINE__); } while (deGetFalse())
84 
85 namespace deqp
86 {
87 namespace egl
88 {
89 
90 using tcu::TestLog;
91 using std::string;
92 using std::vector;
93 using namespace eglw;
94 
95 namespace
96 {
97 
98 // Careful: This has microsecond precision, which can cause timestamps to
99 // appear non monotonic when compared to the nanosecond precision timestamps
100 // we get from the eglGetFrameTimestamps extension.
101 // Current test expectations only make sure microsecond precision timestamps
102 // are less than the nanosecond precision timestamps, so this is okay.
getNanoseconds(void)103 EGLnsecsANDROID getNanoseconds (void)
104 {
105 	return deGetMicroseconds() * 1000;
106 }
107 
108 struct FrameTimes
109 {
FrameTimesdeqp::egl::__anon542a4c200111::FrameTimes110 	FrameTimes (void)
111 		: frameId						(-1)
112 		, swapBufferBeginNs				(-1)
113 		, compositeDeadline				(-1)
114 		, compositeInterval				(-1)
115 		, compositeToPresentLatency		(-1)
116 		, requestedPresent				(-1)
117 		, latch							(-1)
118 		, firstCompositionStart			(-1)
119 		, lastCompositionStart			(-1)
120 		, dequeueReady					(-1)
121 		, renderingComplete				(-1)
122 		, firstCompositionGpuFinished	(-1)
123 		, displayPresent				(-1)
124 		, readsDone						(-1)
125 	{
126 	}
127 
128 	EGLuint64KHR	frameId;
129 
130 	// Timestamps sampled by the test.
131 	EGLnsecsANDROID	swapBufferBeginNs;
132 
133 	// Compositor info.
134 	EGLnsecsANDROID	compositeDeadline;
135 	EGLnsecsANDROID	compositeInterval;
136 	EGLnsecsANDROID	compositeToPresentLatency;
137 
138 	// CPU Timeline.
139 	EGLnsecsANDROID	requestedPresent;
140 	EGLnsecsANDROID	latch;
141 	EGLnsecsANDROID	firstCompositionStart;
142 	EGLnsecsANDROID	lastCompositionStart;
143 	EGLnsecsANDROID	dequeueReady;
144 
145 	// GPU Timeline.
146 	EGLnsecsANDROID	renderingComplete;
147 	EGLnsecsANDROID	firstCompositionGpuFinished;
148 	EGLnsecsANDROID	displayPresent;
149 	EGLnsecsANDROID	readsDone;
150 };
151 
152 
153 struct TimestampInfo
154 {
TimestampInfodeqp::egl::__anon542a4c200111::TimestampInfo155 	TimestampInfo()
156 		: required(false)
157 		, supported(false)
158 		, supportedIndex(0)
159 	{
160 	}
161 
TimestampInfodeqp::egl::__anon542a4c200111::TimestampInfo162 	TimestampInfo(bool required_, bool supported_, size_t supportedIndex_)
163 		: required(required_)
164 		, supported(supported_)
165 		, supportedIndex(supportedIndex_)
166 	{
167 	}
168 
169 	bool	required;
170 	bool	supported;
171 	size_t	supportedIndex;
172 };
173 
174 typedef std::map<eglw::EGLint, TimestampInfo> TimestampInfoMap;
175 
getTimestamp(eglw::EGLint name,TimestampInfoMap & map,const std::vector<EGLnsecsANDROID> & supportedValues)176 EGLnsecsANDROID getTimestamp(eglw::EGLint name, TimestampInfoMap& map, const std::vector<EGLnsecsANDROID>& supportedValues)
177 {
178 	TimestampInfo& info = map[name];
179 	return info.supported ? supportedValues[info.supportedIndex] : EGL_TIMESTAMP_INVALID_ANDROID;
180 }
181 
populateFrameTimes(FrameTimes * frameTimes,TimestampInfoMap & map,const std::vector<EGLnsecsANDROID> & supportedValues)182 void populateFrameTimes(FrameTimes* frameTimes, TimestampInfoMap& map, const std::vector<EGLnsecsANDROID>& supportedValues)
183 {
184 	frameTimes->requestedPresent			=	getTimestamp(EGL_REQUESTED_PRESENT_TIME_ANDROID, map, supportedValues);
185 	frameTimes->renderingComplete			=	getTimestamp(EGL_RENDERING_COMPLETE_TIME_ANDROID, map, supportedValues);
186 	frameTimes->latch						=	getTimestamp(EGL_COMPOSITION_LATCH_TIME_ANDROID, map, supportedValues);
187 	frameTimes->firstCompositionStart		=	getTimestamp(EGL_FIRST_COMPOSITION_START_TIME_ANDROID, map, supportedValues);
188 	frameTimes->lastCompositionStart		=	getTimestamp(EGL_LAST_COMPOSITION_START_TIME_ANDROID, map, supportedValues);
189 	frameTimes->firstCompositionGpuFinished	=	getTimestamp(EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID, map, supportedValues);
190 	frameTimes->displayPresent				=	getTimestamp(EGL_DISPLAY_PRESENT_TIME_ANDROID, map, supportedValues);
191 	frameTimes->dequeueReady				=	getTimestamp(EGL_DEQUEUE_READY_TIME_ANDROID, map, supportedValues);
192 	frameTimes->readsDone					=	getTimestamp(EGL_READS_DONE_TIME_ANDROID, map, supportedValues);
193 }
194 
timestampValid(EGLnsecsANDROID timestamp)195 bool timestampValid (EGLnsecsANDROID timestamp)
196 {
197 	// \todo [2017-10-19 brianderson] Don't consider 0 invalid once kernel fix is in.
198 	return (timestamp > 0) || (timestamp == EGL_TIMESTAMP_PENDING_ANDROID);
199 }
200 
timestampPending(EGLnsecsANDROID timestamp)201 bool timestampPending (EGLnsecsANDROID timestamp)
202 {
203 	return timestamp == EGL_TIMESTAMP_PENDING_ANDROID;
204 }
205 
206 template<typename T>
check_lt(tcu::ResultCollector & result,const T & a,const T & b,const std::string & msg)207 void check_lt(tcu::ResultCollector& result, const T& a, const T& b, const std::string& msg) {
208 	if (a < b)
209 		return;
210 	std::string m = msg + "!(" + de::toString(a) + " < " + de::toString(b) + ")";
211 	result.fail(m);
212 }
213 
214 template<typename T>
check_le(tcu::ResultCollector & result,const T & a,const T & b,const std::string & msg)215 void check_le(tcu::ResultCollector& result, const T& a, const T& b, const std::string& msg) {
216 	if (a <= b)
217 		return;
218 	std::string m = msg + "!(" + de::toString(a) + " <= " + de::toString(b) + ")";
219 	result.fail(m);
220 }
221 
verifySingleFrame(const FrameTimes & frameTimes,tcu::ResultCollector & result,bool verifyReadsDone)222 void verifySingleFrame (const FrameTimes& frameTimes, tcu::ResultCollector& result, bool verifyReadsDone)
223 {
224 	// Verify CPU timeline is monotonic.
225 	check_lt(result, frameTimes.swapBufferBeginNs, frameTimes.latch, "Buffer latched before it was swapped.");
226 	check_lt(result, frameTimes.latch, frameTimes.firstCompositionStart, "Buffer composited before it was latched.");
227 	check_le(result, frameTimes.firstCompositionStart, frameTimes.lastCompositionStart, "First composition start after last composition start.");
228 	check_lt(result, frameTimes.lastCompositionStart, frameTimes.dequeueReady, "Buffer composited after it was ready to be dequeued.");
229 
230 	// Verify GPU timeline is monotonic.
231 	if (timestampValid(frameTimes.firstCompositionGpuFinished))
232 		check_lt(result, frameTimes.renderingComplete, frameTimes.firstCompositionGpuFinished, "Buffer rendering completed after compositor GPU work finished.");
233 
234 	if (timestampValid(frameTimes.displayPresent))
235 		check_lt(result, frameTimes.renderingComplete, frameTimes.displayPresent, "Buffer displayed before rendering completed.");
236 
237 	if (timestampValid(frameTimes.firstCompositionGpuFinished) && timestampValid(frameTimes.displayPresent))
238 		check_lt(result, frameTimes.firstCompositionGpuFinished, frameTimes.displayPresent, "Buffer displayed before compositor GPU work completed");
239 
240 	// Drivers may maintain shadow copies of the buffer, so the readsDone time
241 	// of the real buffer may be earlier than apparent dependencies. We can only
242 	// be sure that the readsDone time must be after the renderingComplete time.
243     // It may also be equal to the renderingComplete time if no reads were
244     // peformed.
245 	if (verifyReadsDone && timestampValid(frameTimes.readsDone))
246 		check_le(result, frameTimes.renderingComplete, frameTimes.readsDone, "Buffer rendering completed after reads completed.");
247 
248 	// Verify CPU/GPU dependencies
249 	if (timestampValid(frameTimes.firstCompositionGpuFinished))
250 		check_lt(result, frameTimes.firstCompositionStart, frameTimes.firstCompositionGpuFinished, "Composition CPU work started after GPU work finished.");
251 
252 	if (timestampValid(frameTimes.displayPresent))
253 		check_lt(result, frameTimes.firstCompositionStart, frameTimes.displayPresent, "Buffer displayed before it was composited.");
254 }
255 
verifyNeighboringFrames(const FrameTimes & frame1,const FrameTimes & frame2,tcu::ResultCollector & result)256 void verifyNeighboringFrames (const FrameTimes& frame1, const FrameTimes& frame2, tcu::ResultCollector& result)
257 {
258 	// CPU timeline.
259 	check_lt(result, frame1.swapBufferBeginNs, frame2.swapBufferBeginNs, "Swap begin times not monotonic.");
260 	check_lt(result, frame1.latch, frame2.latch, "Latch times not monotonic.");
261 	check_lt(result, frame1.lastCompositionStart, frame2.latch, "Old buffer composited after new buffer latched.");
262 	check_lt(result, frame1.lastCompositionStart, frame2.firstCompositionStart, "Composition times overlap.");
263 	check_lt(result, frame1.dequeueReady, frame2.dequeueReady, "Dequeue ready times not monotonic.");
264 
265 	// GPU timeline.
266 	if (timestampValid(frame1.firstCompositionGpuFinished) && timestampValid(frame2.firstCompositionGpuFinished))
267 		check_lt(result, frame1.firstCompositionGpuFinished, frame2.firstCompositionGpuFinished, "Composition GPU work complete times not monotonic.");
268 
269 	if (timestampValid(frame1.displayPresent) && timestampValid(frame2.displayPresent))
270 		check_lt(result, frame1.displayPresent, frame2.displayPresent, "Display present times not monotonic.");
271 }
272 
createGLES2Context(const Library & egl,EGLDisplay display,EGLConfig config)273 EGLContext createGLES2Context (const Library& egl, EGLDisplay display, EGLConfig config)
274 {
275 	EGLContext		context = EGL_NO_CONTEXT;
276 	const EGLint	attribList[] =
277 	{
278 		EGL_CONTEXT_CLIENT_VERSION, 2,
279 		EGL_NONE
280 	};
281 
282 	EGLU_CHECK_CALL(egl, bindAPI(EGL_OPENGL_ES_API));
283 
284 	context = egl.createContext(display, config, EGL_NO_CONTEXT, attribList);
285 	EGLU_CHECK_MSG(egl, "eglCreateContext() failed");
286 	TCU_CHECK(context);
287 
288 	return context;
289 }
290 
291 class GetFrameTimestampTest : public SimpleConfigCase
292 {
293 public:
294 							GetFrameTimestampTest	(EglTestContext& eglTestCtx, const NamedFilterList& filters);
295 							~GetFrameTimestampTest	(void);
296 
297 private:
298 	void					executeForConfig		(EGLDisplay display, EGLConfig config);
299 	void					initializeExtension		(const Library& egl);
300 
301 	// Not allowed
302 							GetFrameTimestampTest	(const GetFrameTimestampTest&);
303 	GetFrameTimestampTest&	operator=				(const GetFrameTimestampTest&);
304 
305 	// TODO: Move these to eglw::Library.
306 	eglGetNextFrameIdANDROIDFunc				m_eglGetNextFrameIdANDROID;
307 	eglGetCompositorTimingANDROIDFunc			m_eglGetCompositorTimingANDROID;
308 	eglGetCompositorTimingSupportedANDROIDFunc	m_eglGetCompositorTimingSupportedANDROID;
309 	eglGetFrameTimestampsANDROIDFunc			m_eglGetFrameTimestampsANDROID;
310 	eglGetFrameTimestampSupportedANDROIDFunc	m_eglGetFrameTimestampSupportedANDROID;
311 
312 	tcu::ResultCollector						m_result;
313 };
314 
GetFrameTimestampTest(EglTestContext & eglTestCtx,const NamedFilterList & filters)315 GetFrameTimestampTest::GetFrameTimestampTest (EglTestContext& eglTestCtx, const NamedFilterList& filters)
316 	: SimpleConfigCase							(eglTestCtx, filters.getName(), filters.getDescription(), filters)
317 	, m_eglGetNextFrameIdANDROID				(DE_NULL)
318 	, m_eglGetCompositorTimingANDROID			(DE_NULL)
319 	, m_eglGetCompositorTimingSupportedANDROID	(DE_NULL)
320 	, m_eglGetFrameTimestampsANDROID			(DE_NULL)
321 	, m_eglGetFrameTimestampSupportedANDROID	(DE_NULL)
322 	, m_result									(m_testCtx.getLog())
323 {
324 }
325 
~GetFrameTimestampTest(void)326 GetFrameTimestampTest::~GetFrameTimestampTest (void)
327 {
328 }
329 
initializeExtension(const Library & egl)330 void GetFrameTimestampTest::initializeExtension (const Library& egl)
331 {
332 	m_eglGetNextFrameIdANDROID = reinterpret_cast<eglGetNextFrameIdANDROIDFunc>(egl.getProcAddress("eglGetNextFrameIdANDROID"));
333 	EGLU_CHECK_MSG(egl, "getProcAddress of eglGetNextFrameIdANDROID failed.");
334 	m_eglGetCompositorTimingANDROID = reinterpret_cast<eglGetCompositorTimingANDROIDFunc>(egl.getProcAddress("eglGetCompositorTimingANDROID"));
335 	EGLU_CHECK_MSG(egl, "getProcAddress of eglGetCompositorTimingANDROID failed.");
336 	m_eglGetCompositorTimingSupportedANDROID = reinterpret_cast<eglGetCompositorTimingSupportedANDROIDFunc>(egl.getProcAddress("eglGetCompositorTimingSupportedANDROID"));
337 	EGLU_CHECK_MSG(egl, "getProcAddress of eglGetCompositorTimingSupportedANDROID failed.");
338 	m_eglGetFrameTimestampsANDROID = reinterpret_cast<eglGetFrameTimestampsANDROIDFunc>(egl.getProcAddress("eglGetFrameTimestampsANDROID"));
339 	EGLU_CHECK_MSG(egl, "getProcAddress of eglGetFrameTimestampsANDROID failed.");
340 	m_eglGetFrameTimestampSupportedANDROID = reinterpret_cast<eglGetFrameTimestampSupportedANDROIDFunc>(egl.getProcAddress("eglGetFrameTimestampSupportedANDROID"));
341 	EGLU_CHECK_MSG(egl, "getProcAddress of eglGetFrameTimestampSupportedANDROID failed.");
342 }
343 
344 
getConfigIdString(const Library & egl,EGLDisplay display,EGLConfig config)345 string getConfigIdString (const Library& egl, EGLDisplay display, EGLConfig config)
346 {
347 	std::ostringstream	stream;
348 	EGLint				id;
349 
350 	EGLU_CHECK_CALL(egl, getConfigAttrib(display, config , EGL_CONFIG_ID, &id));
351 
352 	stream << id;
353 
354 	return stream.str();
355 }
356 
createGLES2Program(const glw::Functions & gl,TestLog & log)357 deUint32 createGLES2Program (const glw::Functions& gl, TestLog& log)
358 {
359 	const char* const vertexShaderSource =
360 	"attribute highp vec2 a_pos;\n"
361 	"void main (void)\n"
362 	"{\n"
363 	"\tgl_Position = vec4(a_pos, 0.0, 1.0);\n"
364 	"}";
365 
366 	const char* const fragmentShaderSource =
367 	"void main (void)\n"
368 	"{\n"
369 	"\tgl_FragColor = vec4(0.9, 0.1, 0.4, 1.0);\n"
370 	"}";
371 
372 	deUint32	program			= 0;
373 	deUint32	vertexShader	= 0;
374 	deUint32	fragmentShader	= 0;
375 
376 	deInt32		vertexCompileStatus;
377 	string		vertexInfoLog;
378 	deInt32		fragmentCompileStatus;
379 	string		fragmentInfoLog;
380 	deInt32		linkStatus;
381 	string		programInfoLog;
382 
383 	try
384 	{
385 		program			= gl.createProgram();
386 		vertexShader	= gl.createShader(GL_VERTEX_SHADER);
387 		fragmentShader	= gl.createShader(GL_FRAGMENT_SHADER);
388 
389 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to create shaders and program");
390 
391 		gl.shaderSource(vertexShader, 1, &vertexShaderSource, DE_NULL);
392 		gl.compileShader(vertexShader);
393 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to setup vertex shader");
394 
395 		gl.shaderSource(fragmentShader, 1, &fragmentShaderSource, DE_NULL);
396 		gl.compileShader(fragmentShader);
397 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to setup fragment shader");
398 
399 		{
400 			deInt32		infoLogLength = 0;
401 
402 			gl.getShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexCompileStatus);
403 			gl.getShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &infoLogLength);
404 
405 			vertexInfoLog.resize(infoLogLength, '\0');
406 
407 			gl.getShaderInfoLog(vertexShader, (glw::GLsizei)vertexInfoLog.length(), &infoLogLength, &(vertexInfoLog[0]));
408 			GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to get vertex shader compile info");
409 
410 			vertexInfoLog.resize(infoLogLength);
411 		}
412 
413 		{
414 			deInt32		infoLogLength = 0;
415 
416 			gl.getShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentCompileStatus);
417 			gl.getShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &infoLogLength);
418 
419 			fragmentInfoLog.resize(infoLogLength, '\0');
420 
421 			gl.getShaderInfoLog(fragmentShader, (glw::GLsizei)fragmentInfoLog.length(), &infoLogLength, &(fragmentInfoLog[0]));
422 			GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to get fragment shader compile info");
423 
424 			fragmentInfoLog.resize(infoLogLength);
425 		}
426 
427 		gl.attachShader(program, vertexShader);
428 		gl.attachShader(program, fragmentShader);
429 		gl.linkProgram(program);
430 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to setup program");
431 
432 		{
433 			deInt32		infoLogLength = 0;
434 
435 			gl.getProgramiv(program, GL_LINK_STATUS, &linkStatus);
436 			gl.getProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
437 
438 			programInfoLog.resize(infoLogLength, '\0');
439 
440 			gl.getProgramInfoLog(program, (glw::GLsizei)programInfoLog.length(), &infoLogLength, &(programInfoLog[0]));
441 			GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to get program link info");
442 
443 			programInfoLog.resize(infoLogLength);
444 		}
445 
446 		if (linkStatus == 0 || vertexCompileStatus == 0 || fragmentCompileStatus == 0)
447 		{
448 
449 			log.startShaderProgram(linkStatus != 0, programInfoLog.c_str());
450 
451 			log << TestLog::Shader(QP_SHADER_TYPE_VERTEX, vertexShaderSource, vertexCompileStatus != 0, vertexInfoLog);
452 			log << TestLog::Shader(QP_SHADER_TYPE_FRAGMENT, fragmentShaderSource, fragmentCompileStatus != 0, fragmentInfoLog);
453 
454 			log.endShaderProgram();
455 		}
456 
457 		gl.deleteShader(vertexShader);
458 		gl.deleteShader(fragmentShader);
459 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to delete shaders");
460 
461 		TCU_CHECK(linkStatus != 0 && vertexCompileStatus != 0 && fragmentCompileStatus != 0);
462 	}
463 	catch (...)
464 	{
465 		if (program)
466 			gl.deleteProgram(program);
467 
468 		if (vertexShader)
469 			gl.deleteShader(vertexShader);
470 
471 		if (fragmentShader)
472 			gl.deleteShader(fragmentShader);
473 
474 		throw;
475 	}
476 
477 	return program;
478 }
479 
executeForConfig(EGLDisplay display,EGLConfig config)480 void GetFrameTimestampTest::executeForConfig (EGLDisplay display, EGLConfig config)
481 {
482 	const Library&						egl			= m_eglTestCtx.getLibrary();
483 
484 	if (!eglu::hasExtension(egl, display, "EGL_ANDROID_get_frame_timestamps"))
485 		TCU_THROW(NotSupportedError, "EGL_ANDROID_get_frame_timestamps is not supported");
486 
487 	initializeExtension(egl);
488 
489 	const string						configIdStr	(getConfigIdString(egl, display, config));
490 	tcu::ScopedLogSection				logSection	(m_testCtx.getLog(), ("Config ID " + configIdStr).c_str(), ("Config ID " + configIdStr).c_str());
491 	const eglu::NativeWindowFactory&	factory		= eglu::selectNativeWindowFactory(m_eglTestCtx.getNativeDisplayFactory(), m_testCtx.getCommandLine());
492 
493 	{
494 		TestLog& log = m_testCtx.getLog();
495 
496 		log << TestLog::Message << "EGL_RED_SIZE: "		<< eglu::getConfigAttribInt(egl, display, config, EGL_RED_SIZE)		<< TestLog::EndMessage;
497 		log << TestLog::Message << "EGL_GREEN_SIZE: "	<< eglu::getConfigAttribInt(egl, display, config, EGL_GREEN_SIZE)	<< TestLog::EndMessage;
498 		log << TestLog::Message << "EGL_BLUE_SIZE: "	<< eglu::getConfigAttribInt(egl, display, config, EGL_BLUE_SIZE)	<< TestLog::EndMessage;
499 		log << TestLog::Message << "EGL_ALPHA_SIZE: "	<< eglu::getConfigAttribInt(egl, display, config, EGL_ALPHA_SIZE)	<< TestLog::EndMessage;
500 		log << TestLog::Message << "EGL_DEPTH_SIZE: "	<< eglu::getConfigAttribInt(egl, display, config, EGL_DEPTH_SIZE)	<< TestLog::EndMessage;
501 		log << TestLog::Message << "EGL_STENCIL_SIZE: "	<< eglu::getConfigAttribInt(egl, display, config, EGL_STENCIL_SIZE)	<< TestLog::EndMessage;
502 		log << TestLog::Message << "EGL_SAMPLES: "		<< eglu::getConfigAttribInt(egl, display, config, EGL_SAMPLES)		<< TestLog::EndMessage;
503 	}
504 
505 	de::UniquePtr<eglu::NativeWindow>	window	(factory.createWindow(&m_eglTestCtx.getNativeDisplay(), display, config, DE_NULL, eglu::WindowParams(128, 128, eglu::WindowParams::VISIBILITY_VISIBLE)));
506 
507 	eglu::UniqueSurface					surface	(egl, display, eglu::createWindowSurface(m_eglTestCtx.getNativeDisplay(), *window, display, config, DE_NULL));
508 	eglu::UniqueContext					context	(egl, display, createGLES2Context(egl, display, config));
509 	glw::Functions						gl;
510 	deUint32							program = 0;
511 
512 	EGLU_CHECK_CALL(egl, surfaceAttrib(display, *surface, EGL_TIMESTAMPS_ANDROID, EGL_TRUE));
513 
514 	m_eglTestCtx.initGLFunctions(&gl, glu::ApiType::es(2,0));
515 
516 	EGLU_CHECK_CALL(egl, makeCurrent(display, *surface, *surface, *context));
517 
518 	try
519 	{
520 		// EGL_DISPLAY_PRESENT_TIME_ANDROID support is currently optional
521 		// but should be required once HWC1 is no longer supported.
522 		// All HWC2 devices should support EGL_DISPLAY_PRESENT_TIME_ANDROID.
523 		TimestampInfoMap timestamps;
524 		timestamps[EGL_REQUESTED_PRESENT_TIME_ANDROID]				=	TimestampInfo(true,		false, 0);
525 		timestamps[EGL_RENDERING_COMPLETE_TIME_ANDROID]				=	TimestampInfo(true,		false, 0);
526 		timestamps[EGL_COMPOSITION_LATCH_TIME_ANDROID]				=	TimestampInfo(true,		false, 0);
527 		timestamps[EGL_FIRST_COMPOSITION_START_TIME_ANDROID]		=	TimestampInfo(true,		false, 0);
528 		timestamps[EGL_LAST_COMPOSITION_START_TIME_ANDROID]			=	TimestampInfo(true,		false, 0);
529 		timestamps[EGL_FIRST_COMPOSITION_GPU_FINISHED_TIME_ANDROID]	=	TimestampInfo(true,		false, 0);
530 		timestamps[EGL_DISPLAY_PRESENT_TIME_ANDROID]				=	TimestampInfo(false,	false, 0);
531 		timestamps[EGL_DEQUEUE_READY_TIME_ANDROID]					=	TimestampInfo(true,		false, 0);
532 		timestamps[EGL_READS_DONE_TIME_ANDROID]						=	TimestampInfo(true,		false, 0);
533 
534 		const eglw::EGLint invalidTimestampName = EGL_READS_DONE_TIME_ANDROID + 1;
535 
536 		// Verify required timestamps are supported and populate supportedNames.
537 		std::vector<eglw::EGLint> supportedNames;
538 		for (TimestampInfoMap::iterator i = timestamps.begin(); i != timestamps.end(); i++)
539 		{
540 			TimestampInfo& info = i->second;
541 			info.supported = m_eglGetFrameTimestampSupportedANDROID(display, *surface, i->first) != EGL_FALSE;
542 			EGLU_CHECK_MSG(egl, "eglGetFrameTimestampSupportedANDROID failed.");
543 
544 			if (info.supported)
545 			{
546 				info.supportedIndex = supportedNames.size();
547 				supportedNames.push_back(i->first);
548 			}
549 			else
550 				TCU_CHECK_MSG(!info.required, "Required timestamp not supported.");
551 		}
552 
553 		// Verify unsupported timestamps are reported properly.
554 		const bool invalidSupported = m_eglGetFrameTimestampSupportedANDROID(display, *surface, invalidTimestampName) != EGL_FALSE;
555 		EGLU_CHECK_MSG(egl, "eglGetFrameTimestampSupportedANDROID failed.");
556 		TCU_CHECK_MSG(!invalidSupported, "Non existant timestamp reports that it is supported.");
557 
558 		// Verify compositor timings are supported.
559 		const bool deadlineSupported = m_eglGetCompositorTimingSupportedANDROID(display, *surface, EGL_COMPOSITE_DEADLINE_ANDROID) != EGL_FALSE;
560 		EGLU_CHECK_MSG(egl, "eglGetCompositorTimingSupportedANDROID failed.");
561 		TCU_CHECK_MSG(deadlineSupported, "EGL_COMPOSITE_DEADLINE_ANDROID not supported.");
562 		const bool intervalSupported = m_eglGetCompositorTimingSupportedANDROID(display, *surface, EGL_COMPOSITE_INTERVAL_ANDROID) != EGL_FALSE;
563 		EGLU_CHECK_MSG(egl, "eglGetCompositorTimingSupportedANDROID failed.");
564 		TCU_CHECK_MSG(intervalSupported, "EGL_COMPOSITE_INTERVAL_ANDROID not supported.");
565 		const bool latencySupported = m_eglGetCompositorTimingSupportedANDROID(display, *surface, EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID) != EGL_FALSE;
566 		EGLU_CHECK_MSG(egl, "eglGetCompositorTimingSupportedANDROID failed.");
567 		TCU_CHECK_MSG(latencySupported, "EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID not supported.");
568 
569 		const float positions1[] =
570 		{
571 			 0.00f,  0.00f,
572 			 0.75f,  0.00f,
573 			 0.75f,  0.75f,
574 
575 			 0.75f,  0.75f,
576 			 0.00f,  0.75f,
577 			 0.00f,  0.00f
578 		};
579 
580 		const float positions2[] =
581 		{
582 			-0.75f, -0.75f,
583 			 0.00f, -0.75f,
584 			 0.00f,  0.00f,
585 
586 			 0.00f,  0.00f,
587 			-0.75f,  0.00f,
588 			-0.75f, -0.75f
589 		};
590 
591 		deUint32 posLocation;
592 
593 		program	= createGLES2Program(gl, m_testCtx.getLog());
594 
595 		gl.useProgram(program);
596 		posLocation	= gl.getAttribLocation(program, "a_pos");
597 		gl.enableVertexAttribArray(posLocation);
598 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to setup shader program for rendering");
599 
600 		const size_t frameCount = 120;
601 		std::vector<FrameTimes> frameTimes(frameCount);
602 		for (size_t i = 0; i < frameCount; i++)
603 		{
604 			FrameTimes& frame = frameTimes[i];
605 
606 			const eglw::EGLint compositorTimingNames[] =
607 			{
608 				EGL_COMPOSITE_DEADLINE_ANDROID,
609 				EGL_COMPOSITE_INTERVAL_ANDROID,
610 				EGL_COMPOSITE_TO_PRESENT_LATENCY_ANDROID,
611 			};
612 			const EGLint compositorTimingCount = DE_LENGTH_OF_ARRAY(compositorTimingNames);
613 			EGLnsecsANDROID compositorTimingValues[compositorTimingCount] = { -2 };
614 
615 			// Get the current time before making any API calls in case "now"
616 			// just happens to get sampled near one of the composite deadlines.
617 			EGLnsecsANDROID now = getNanoseconds();
618 
619 			// Get the frame id.
620 			EGLuint64KHR nextFrameId = 0;
621 			CHECK_NAKED_EGL_CALL(egl, m_eglGetNextFrameIdANDROID(display, *surface, &nextFrameId));
622 			frame.frameId				=	nextFrameId;
623 
624 			// Get the compositor timing.
625 			CHECK_NAKED_EGL_CALL(egl, m_eglGetCompositorTimingANDROID(
626 				display, *surface, compositorTimingCount,
627 				compositorTimingNames, compositorTimingValues));
628 			frame.compositeDeadline			=	compositorTimingValues[0];
629 			frame.compositeInterval			=	compositorTimingValues[1];
630 			frame.compositeToPresentLatency	=	compositorTimingValues[2];
631 
632 			// Verify compositor timing is sane.
633 			check_lt<EGLnsecsANDROID>(m_result, 1000000, frame.compositeInterval, "Reported refresh rate greater than 1kHz.");
634 			check_lt<EGLnsecsANDROID>(m_result, frame.compositeInterval, 1000000000, "Reported refresh rate less than 1Hz.");
635 			check_lt<EGLnsecsANDROID>(m_result, 0, frame.compositeToPresentLatency, "Composite to present latency must be greater than 0.");
636 			check_lt(m_result, frame.compositeToPresentLatency, frame.compositeInterval * 4, "Composite to present latency is more than 4 vsyncs.");
637 			const EGLnsecsANDROID minDeadline = now;
638 			check_lt(m_result, minDeadline, frame.compositeDeadline, "Next composite deadline is in the past.");
639 			const EGLnsecsANDROID maxDeadline = now + frame.compositeInterval * 2;
640 			check_lt(m_result, frame.compositeDeadline, maxDeadline, "Next composite deadline over two intervals away.");
641 
642 			const float colorAngle = (static_cast<float>(i) / static_cast<float>(frameCount)) * 6.28318f;
643 			gl.clearColor((1.0f + deFloatSin(colorAngle)) / 2.0f, 0.7f, (1.0f + deFloatCos(colorAngle)) / 2.0f, 1.0f);
644 			gl.clear(GL_COLOR_BUFFER_BIT);
645 			GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to clear surface");
646 
647 			const bool posSelect  = ((i % 2) == 0);
648 			gl.vertexAttribPointer(posLocation, 2, GL_FLOAT, GL_FALSE, 0, posSelect ? positions1 : positions2);
649 			gl.drawArrays(GL_TRIANGLES, 0, 6);
650 			GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to render");
651 
652 			frame.swapBufferBeginNs = getNanoseconds();
653 			EGLU_CHECK_CALL(egl, swapBuffers(display, *surface));
654 
655 			// All timestamps from 6 frames ago should definitely be available.
656 			const size_t frameDelay = 6;
657 			if (i >= frameDelay)
658 			{
659 				// \todo [2017-01-25 brianderson] Remove this work around once reads done is fixed.
660 				const bool verifyReadsDone	=	i > (frameDelay + 3);
661 				FrameTimes&		frame6ago	=	frameTimes[i-frameDelay];
662 				std::vector<EGLnsecsANDROID> supportedValues(supportedNames.size(), 0);
663 
664 				CHECK_NAKED_EGL_CALL(egl, m_eglGetFrameTimestampsANDROID(
665 					display, *surface, frame6ago.frameId, static_cast<eglw::EGLint>(supportedNames.size()),
666 					&supportedNames[0], &supportedValues[0]));
667 				populateFrameTimes(&frame6ago, timestamps, supportedValues);
668 
669 				verifySingleFrame(frame6ago, m_result, verifyReadsDone);
670 				if (i >= frameDelay + 1)
671 				{
672 					FrameTimes& frame7ago = frameTimes[i-frameDelay-1];
673 					verifyNeighboringFrames(frame7ago, frame6ago, m_result);
674 				}
675 			}
676 			deSleep(static_cast<deUint32>(frame.compositeInterval / 1000000));
677 		}
678 
679 		// All timestamps for the most recently swapped frame should
680 		// become available by only polling eglGetFrametimestamps.
681 		// No additional swaps should be necessary.
682 		FrameTimes&				lastFrame				=	frameTimes.back();
683 		const EGLnsecsANDROID	pollingDeadline			=	lastFrame.swapBufferBeginNs + 1000000000;
684 		bool					finalTimestampAvailable	=	false;
685 
686 		do
687 		{
688 			std::vector<EGLnsecsANDROID> supportedValues(supportedNames.size(), 0);
689 			CHECK_NAKED_EGL_CALL(egl, m_eglGetFrameTimestampsANDROID(
690 				display, *surface, lastFrame.frameId, static_cast<eglw::EGLint>(supportedNames.size()),
691 				&supportedNames[0], &supportedValues[0]));
692 			populateFrameTimes(&lastFrame, timestamps, supportedValues);
693 
694 			// Poll for present if it's supported.
695 			// Otherwise, poll for firstCompositionStart.
696 			if (timestamps[EGL_DISPLAY_PRESENT_TIME_ANDROID].supported)
697 				finalTimestampAvailable = !timestampPending(lastFrame.displayPresent);
698 			else
699 				finalTimestampAvailable = !timestampPending(lastFrame.firstCompositionStart);
700 		} while (!finalTimestampAvailable && (getNanoseconds() < pollingDeadline));
701 
702 		m_result.check(finalTimestampAvailable, "Timed out polling for timestamps of last swap.");
703 		m_result.check((lastFrame.requestedPresent >= 0), "Requested present of last swap not avaiable.");
704 		m_result.check((lastFrame.renderingComplete >= 0), "Rendering complete of last swap not avaiable.");
705 		m_result.check((lastFrame.latch >= 0), "Latch of last swap not avaiable.");
706 		m_result.check((lastFrame.firstCompositionStart >= 0), "First composite time of last swap not avaiable.");
707 		m_result.check((lastFrame.lastCompositionStart >= 0), "Last composite time of last swap not avaiable.");
708 
709 		window->processEvents();
710 		gl.disableVertexAttribArray(posLocation);
711 		gl.useProgram(0);
712 		GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to release program state");
713 
714 		gl.deleteProgram(program);
715 		program = 0;
716 		GLU_EXPECT_NO_ERROR(gl.getError(), "glDeleteProgram()");
717 
718 		m_result.setTestContextResult(m_testCtx);
719 	}
720 	catch (...)
721 	{
722 		if (program != 0)
723 			gl.deleteProgram(program);
724 
725 		EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
726 		throw;
727 	}
728 
729 	EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
730 }
731 
732 class GetFrameTimestampsTests : public TestCaseGroup
733 {
734 public:
735 								GetFrameTimestampsTests	(EglTestContext& eglTestCtx);
736 	void						init					(void);
737 
738 private:
739 								GetFrameTimestampsTests	(const GetFrameTimestampsTests&);
740 	GetFrameTimestampsTests&	operator=				(const GetFrameTimestampsTests&);
741 };
742 
743 
GetFrameTimestampsTests(EglTestContext & eglTestCtx)744 GetFrameTimestampsTests::GetFrameTimestampsTests (EglTestContext& eglTestCtx)
745 	: TestCaseGroup(eglTestCtx, "get_frame_timestamps", "Get frame timestamp tests")
746 {
747 }
748 
isWindow(const eglu::CandidateConfig & c)749 bool isWindow (const eglu::CandidateConfig& c)
750 {
751 	return (c.surfaceType() & EGL_WINDOW_BIT) != 0;
752 }
753 
init(void)754 void GetFrameTimestampsTests::init (void)
755 {
756 	eglu::FilterList baseFilters;
757 	baseFilters << isWindow;
758 
759 	vector<NamedFilterList> filterLists;
760 	getDefaultFilterLists(filterLists, baseFilters);
761 
762 	for (vector<NamedFilterList>::iterator i = filterLists.begin(); i != filterLists.end(); i++)
763 		addChild(new GetFrameTimestampTest(m_eglTestCtx, *i));
764 }
765 
766 } // anonymous
767 
createGetFrameTimestampsTests(EglTestContext & eglTestCtx)768 TestCaseGroup* createGetFrameTimestampsTests (EglTestContext& eglTestCtx)
769 {
770 	return new GetFrameTimestampsTests(eglTestCtx);
771 }
772 
773 } // egl
774 } // deqp
775