Home | History | Annotate | Download | only in qphelper
      1 /*-------------------------------------------------------------------------
      2  * drawElements TestLog Library
      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 Test case result logging
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "qpTestLog.h"
     25 #include "qpXmlWriter.h"
     26 #include "qpInfo.h"
     27 #include "qpDebugOut.h"
     28 
     29 #include "deMemory.h"
     30 #include "deInt32.h"
     31 #include "deString.h"
     32 
     33 #include "deMutex.h"
     34 
     35 #if defined(QP_SUPPORT_PNG)
     36 #	include <png.h>
     37 #endif
     38 
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <stdarg.h>
     42 
     43 #if (DE_OS == DE_OS_WIN32)
     44 #	include <windows.h>
     45 #	include <io.h>
     46 #endif
     47 
     48 #if defined(DE_DEBUG)
     49 
     50 /* Utils for verifying container (Section, ImageSet, EglConfigSet) usage in debug builds. */
     51 
     52 typedef enum ContainerType_e
     53 {
     54 	CONTAINERTYPE_SECTION = 0,
     55 	CONTAINERTYPE_IMAGESET,
     56 	CONTAINERTYPE_EGLCONFIGSET,
     57 	CONTAINERTYPE_SHADERPROGRAM,
     58 	CONTAINERTYPE_SAMPLELIST,
     59 	CONTAINERTYPE_SAMPLEINFO,
     60 	CONTAINERTYPE_SAMPLE,
     61 
     62 	CONTAINERTYPE_LAST
     63 } ContainerType;
     64 
     65 DE_INLINE deBool childContainersOk (ContainerType type)
     66 {
     67 	return type == CONTAINERTYPE_SECTION || type == CONTAINERTYPE_SAMPLELIST;
     68 }
     69 
     70 enum
     71 {
     72 	MAX_CONTAINER_STACK_DEPTH		= 32
     73 };
     74 
     75 typedef struct ContainerStack_s
     76 {
     77 	int				numElements;
     78 	ContainerType	elements[MAX_CONTAINER_STACK_DEPTH];
     79 } ContainerStack;
     80 
     81 DE_INLINE void ContainerStack_reset (ContainerStack* stack)
     82 {
     83 	deMemset(stack, 0, sizeof(ContainerStack));
     84 }
     85 
     86 DE_INLINE deBool ContainerStack_isEmpty (const ContainerStack* stack)
     87 {
     88 	return stack->numElements == 0;
     89 }
     90 
     91 DE_INLINE deBool ContainerStack_push (ContainerStack* stack, ContainerType type)
     92 {
     93 	if (stack->numElements == MAX_CONTAINER_STACK_DEPTH)
     94 		return DE_FALSE;
     95 
     96 	if (stack->numElements > 0 && !childContainersOk(stack->elements[stack->numElements-1]))
     97 		return DE_FALSE;
     98 
     99 	stack->elements[stack->numElements]  = type;
    100 	stack->numElements					+= 1;
    101 
    102 	return DE_TRUE;
    103 }
    104 
    105 DE_INLINE ContainerType ContainerStack_pop (ContainerStack* stack)
    106 {
    107 	DE_ASSERT(stack->numElements > 0);
    108 	stack->numElements -= 1;
    109 	return stack->elements[stack->numElements];
    110 }
    111 
    112 DE_INLINE ContainerType ContainerStack_getTop (const ContainerStack* stack)
    113 {
    114 	if (stack->numElements > 0)
    115 		return stack->elements[stack->numElements-1];
    116 	else
    117 		return CONTAINERTYPE_LAST;
    118 }
    119 
    120 #endif
    121 
    122 /* qpTestLog instance */
    123 struct qpTestLog_s
    124 {
    125 	deUint32				flags;				/*!< Logging flags.						*/
    126 
    127 	deMutex					lock;				/*!< Lock for mutable state below.		*/
    128 
    129 	/* State protected by lock. */
    130 	FILE*					outputFile;
    131 	qpXmlWriter*			writer;
    132 	deBool					isSessionOpen;
    133 	deBool					isCaseOpen;
    134 
    135 #if defined(DE_DEBUG)
    136 	ContainerStack			containerStack;		/*!< For container usage verification.	*/
    137 #endif
    138 };
    139 
    140 /* Maps integer to string. */
    141 typedef struct qpKeyStringMap_s
    142 {
    143 	int		key;
    144 	char*	string;
    145 } qpKeyStringMap;
    146 
    147 static const char* LOG_FORMAT_VERSION = "0.3.3";
    148 
    149 /* Mapping enum to above strings... */
    150 static const qpKeyStringMap s_qpTestTypeMap[] =
    151 {
    152 	{ QP_TEST_CASE_TYPE_SELF_VALIDATE,		"SelfValidate"	},
    153 	{ QP_TEST_CASE_TYPE_PERFORMANCE,		"Performance"	},
    154 	{ QP_TEST_CASE_TYPE_CAPABILITY,			"Capability"	},
    155 	{ QP_TEST_CASE_TYPE_ACCURACY,			"Accuracy"		},
    156 
    157 	{ QP_TEST_CASE_TYPE_LAST,				DE_NULL			}
    158 };
    159 
    160 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpTestTypeMap) == QP_TEST_CASE_TYPE_LAST + 1);
    161 
    162 static const qpKeyStringMap s_qpTestResultMap[] =
    163 {
    164 	{ QP_TEST_RESULT_PASS,						"Pass"					},
    165 	{ QP_TEST_RESULT_FAIL,						"Fail"					},
    166 	{ QP_TEST_RESULT_QUALITY_WARNING,			"QualityWarning"		},
    167 	{ QP_TEST_RESULT_COMPATIBILITY_WARNING,		"CompatibilityWarning"	},
    168 	{ QP_TEST_RESULT_PENDING,					"Pending"				},	/* should not be needed here */
    169 	{ QP_TEST_RESULT_NOT_SUPPORTED,				"NotSupported"			},
    170 	{ QP_TEST_RESULT_RESOURCE_ERROR,			"ResourceError"			},
    171 	{ QP_TEST_RESULT_INTERNAL_ERROR,			"InternalError"			},
    172 	{ QP_TEST_RESULT_CRASH,						"Crash"					},
    173 	{ QP_TEST_RESULT_TIMEOUT,					"Timeout"				},
    174 
    175 	/* Add new values here if needed, remember to update qpTestResult enumeration. */
    176 
    177 	{ QP_TEST_RESULT_LAST,						DE_NULL					}
    178 };
    179 
    180 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpTestResultMap) == QP_TEST_RESULT_LAST + 1);
    181 
    182 /* Key tag to string mapping. */
    183 
    184 static const qpKeyStringMap s_qpTagMap[] =
    185 {
    186 	{ QP_KEY_TAG_NONE,			DE_NULL			},
    187 	{ QP_KEY_TAG_PERFORMANCE,	"Performance"	},
    188 	{ QP_KEY_TAG_QUALITY,		"Quality"		},
    189 	{ QP_KEY_TAG_PRECISION,		"Precision"		},
    190 	{ QP_KEY_TAG_TIME,			"Time"			},
    191 
    192 	{ QP_KEY_TAG_LAST,			DE_NULL			}
    193 };
    194 
    195 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpTagMap) == QP_KEY_TAG_LAST + 1);
    196 
    197 /* Sample value tag to string mapping. */
    198 
    199 static const qpKeyStringMap s_qpSampleValueTagMap[] =
    200 {
    201 	{ QP_SAMPLE_VALUE_TAG_PREDICTOR,	"Predictor"	},
    202 	{ QP_SAMPLE_VALUE_TAG_RESPONSE,		"Response"	},
    203 
    204 	{ QP_SAMPLE_VALUE_TAG_LAST,			DE_NULL		}
    205 };
    206 
    207 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpSampleValueTagMap) == QP_SAMPLE_VALUE_TAG_LAST + 1);
    208 
    209 /* Image compression mode to string mapping. */
    210 
    211 static const qpKeyStringMap s_qpImageCompressionModeMap[] =
    212 {
    213 	{ QP_IMAGE_COMPRESSION_MODE_NONE,	"None"	},
    214 	{ QP_IMAGE_COMPRESSION_MODE_PNG,	"PNG"	},
    215 
    216 	{ QP_IMAGE_COMPRESSION_MODE_BEST,	DE_NULL	},	/* not allowed to be written! */
    217 
    218 	{ QP_IMAGE_COMPRESSION_MODE_LAST,	DE_NULL	}
    219 };
    220 
    221 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpImageCompressionModeMap) == QP_IMAGE_COMPRESSION_MODE_LAST + 1);
    222 
    223 /* Image format to string mapping. */
    224 
    225 static const qpKeyStringMap s_qpImageFormatMap[] =
    226 {
    227 	{ QP_IMAGE_FORMAT_RGB888,	"RGB888"	},
    228 	{ QP_IMAGE_FORMAT_RGBA8888,	"RGBA8888"	},
    229 
    230 	{ QP_IMAGE_FORMAT_LAST,		DE_NULL		}
    231 };
    232 
    233 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpImageFormatMap) == QP_IMAGE_FORMAT_LAST + 1);
    234 
    235 /* Shader type to string mapping. */
    236 
    237 static const qpKeyStringMap s_qpShaderTypeMap[] =
    238 {
    239 	{ QP_SHADER_TYPE_VERTEX,			"VertexShader"			},
    240 	{ QP_SHADER_TYPE_FRAGMENT,			"FragmentShader"		},
    241 	{ QP_SHADER_TYPE_GEOMETRY,			"GeometryShader"		},
    242 	{ QP_SHADER_TYPE_TESS_CONTROL,		"TessControlShader"		},
    243 	{ QP_SHADER_TYPE_TESS_EVALUATION,	"TessEvaluationShader"	},
    244 	{ QP_SHADER_TYPE_COMPUTE,			"ComputeShader"			},
    245 
    246 	{ QP_SHADER_TYPE_LAST,				DE_NULL					}
    247 };
    248 
    249 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_qpShaderTypeMap) == QP_SHADER_TYPE_LAST + 1);
    250 
    251 static void qpTestLog_flushFile (qpTestLog* log)
    252 {
    253 	DE_ASSERT(log && log->outputFile);
    254 	fflush(log->outputFile);
    255 #if (DE_OS == DE_OS_WIN32) && (DE_COMPILER == DE_COMPILER_MSC)
    256 	/* \todo [petri] Is this really necessary? */
    257 	FlushFileBuffers((HANDLE)_get_osfhandle(_fileno(log->outputFile)));
    258 #endif
    259 }
    260 
    261 #define QP_LOOKUP_STRING(KEYMAP, KEY)	qpLookupString(KEYMAP, DE_LENGTH_OF_ARRAY(KEYMAP), (int)(KEY))
    262 
    263 static const char* qpLookupString (const qpKeyStringMap* keyMap, int keyMapSize, int key)
    264 {
    265 	DE_ASSERT(keyMap);
    266 	DE_ASSERT(deInBounds32(key, 0, keyMapSize));
    267 	DE_ASSERT(keyMap[key].key == key);
    268 	DE_UNREF(keyMapSize); /* for asserting only */
    269 	return keyMap[key].string;
    270 }
    271 
    272 DE_INLINE void int32ToString (int val, char buf[32])
    273 {
    274 	deSprintf(&buf[0], 32, "%d", val);
    275 }
    276 
    277 DE_INLINE void int64ToString (deInt64 val, char buf[32])
    278 {
    279 	deSprintf(&buf[0], 32, "%lld", (long long int)val);
    280 }
    281 
    282 DE_INLINE void floatToString (float value, char* buf, int bufSize)
    283 {
    284 	deSprintf(buf, bufSize, "%f", value);
    285 }
    286 
    287 DE_INLINE void doubleToString (double value, char* buf, int bufSize)
    288 {
    289 	deSprintf(buf, bufSize, "%f", value);
    290 }
    291 
    292 static deBool beginSession (qpTestLog* log)
    293 {
    294 	DE_ASSERT(log && !log->isSessionOpen);
    295 
    296 	/* Write session info. */
    297 	fprintf(log->outputFile, "#sessionInfo releaseName %s\n", qpGetReleaseName());
    298 	fprintf(log->outputFile, "#sessionInfo releaseId 0x%08x\n", qpGetReleaseId());
    299 	fprintf(log->outputFile, "#sessionInfo targetName \"%s\"\n", qpGetTargetName());
    300 
    301     /* Write out #beginSession. */
    302 	fprintf(log->outputFile, "#beginSession\n");
    303 	qpTestLog_flushFile(log);
    304 
    305 	log->isSessionOpen = DE_TRUE;
    306 
    307 	return DE_TRUE;
    308 }
    309 
    310 static deBool endSession (qpTestLog* log)
    311 {
    312 	DE_ASSERT(log && log->isSessionOpen);
    313 
    314     /* Make sure xml is flushed. */
    315     qpXmlWriter_flush(log->writer);
    316 
    317     /* Write out #endSession. */
    318 	fprintf(log->outputFile, "\n#endSession\n");
    319 	qpTestLog_flushFile(log);
    320 
    321 	log->isSessionOpen = DE_FALSE;
    322 
    323 	return DE_TRUE;
    324 }
    325 
    326 /*--------------------------------------------------------------------*//*!
    327  * \brief Create a file based logger instance
    328  * \param fileName Name of the file where to put logs
    329  * \return qpTestLog instance, or DE_NULL if cannot create file
    330  *//*--------------------------------------------------------------------*/
    331 qpTestLog* qpTestLog_createFileLog (const char* fileName, deUint32 flags)
    332 {
    333 	qpTestLog* log = (qpTestLog*)deCalloc(sizeof(qpTestLog));
    334 	if (!log)
    335 		return DE_NULL;
    336 
    337 	DE_ASSERT(fileName && fileName[0]); /* must have filename. */
    338 
    339 #if defined(DE_DEBUG)
    340 	ContainerStack_reset(&log->containerStack);
    341 #endif
    342 
    343 	/* Create output file. */
    344 	log->outputFile = fopen(fileName, "wb");
    345 	if (!log->outputFile)
    346 	{
    347 		qpPrintf("ERROR: Unable to open test log output file '%s'.\n", fileName);
    348 		qpTestLog_destroy(log);
    349 		return DE_NULL;
    350 	}
    351 
    352 	log->flags			= flags;
    353 	log->writer			= qpXmlWriter_createFileWriter(log->outputFile, 0);
    354 	log->lock			= deMutex_create(DE_NULL);
    355 	log->isSessionOpen	= DE_FALSE;
    356 	log->isCaseOpen		= DE_FALSE;
    357 
    358 	if (!log->writer)
    359 	{
    360 		qpPrintf("ERROR: Unable to create output XML writer to file '%s'.\n", fileName);
    361 		qpTestLog_destroy(log);
    362 		return DE_NULL;
    363 	}
    364 
    365 	if (!log->lock)
    366 	{
    367 		qpPrintf("ERROR: Unable to create mutex.\n");
    368 		qpTestLog_destroy(log);
    369 		return DE_NULL;
    370 	}
    371 
    372 	beginSession(log);
    373 
    374 	return log;
    375 }
    376 
    377 /*--------------------------------------------------------------------*//*!
    378  * \brief Destroy a logger instance
    379  * \param a	qpTestLog instance
    380  *//*--------------------------------------------------------------------*/
    381 void qpTestLog_destroy (qpTestLog* log)
    382 {
    383 	DE_ASSERT(log);
    384 
    385 	if (log->isSessionOpen)
    386 		endSession(log);
    387 
    388 	if (log->writer)
    389 		qpXmlWriter_destroy(log->writer);
    390 
    391 	if (log->outputFile)
    392 		fclose(log->outputFile);
    393 
    394 	if (log->lock)
    395 		deMutex_destroy(log->lock);
    396 
    397 	deFree(log);
    398 }
    399 
    400 /*--------------------------------------------------------------------*//*!
    401  * \brief Log start of test case
    402  * \param log qpTestLog instance
    403  * \param testCasePath	Full test case path (as seen in Candy).
    404  * \param testCaseType	Test case type
    405  * \return true if ok, false otherwise
    406  *//*--------------------------------------------------------------------*/
    407 deBool qpTestLog_startCase (qpTestLog* log, const char* testCasePath, qpTestCaseType testCaseType)
    408 {
    409 	const char*		typeStr				= QP_LOOKUP_STRING(s_qpTestTypeMap, testCaseType);
    410 	int				numResultAttribs	= 0;
    411 	qpXmlAttribute	resultAttribs[8];
    412 
    413 	DE_ASSERT(log && testCasePath && (testCasePath[0] != 0));
    414 	deMutex_lock(log->lock);
    415 
    416 	DE_ASSERT(!log->isCaseOpen);
    417 	DE_ASSERT(ContainerStack_isEmpty(&log->containerStack));
    418 
    419 	/* Flush XML and write out #beginTestCaseResult. */
    420 	qpXmlWriter_flush(log->writer);
    421 	fprintf(log->outputFile, "\n#beginTestCaseResult %s\n", testCasePath);
    422 	qpTestLog_flushFile(log);
    423 
    424 	log->isCaseOpen = DE_TRUE;
    425 
    426 	/* Fill in attributes. */
    427 	resultAttribs[numResultAttribs++] = qpSetStringAttrib("Version", LOG_FORMAT_VERSION);
    428 	resultAttribs[numResultAttribs++] = qpSetStringAttrib("CasePath", testCasePath);
    429 	resultAttribs[numResultAttribs++] = qpSetStringAttrib("CaseType", typeStr);
    430 
    431 	if (!qpXmlWriter_startDocument(log->writer) ||
    432 		!qpXmlWriter_startElement(log->writer, "TestCaseResult", numResultAttribs, resultAttribs))
    433 	{
    434 		qpPrintf("qpTestLog_startCase(): Writing XML failed\n");
    435 		deMutex_unlock(log->lock);
    436 		return DE_FALSE;
    437 	}
    438 
    439 	deMutex_unlock(log->lock);
    440 	return DE_TRUE;
    441 }
    442 
    443 /*--------------------------------------------------------------------*//*!
    444  * \brief Log end of test case
    445  * \param log qpTestLog instance
    446  * \param result Test result
    447  * \param description Description of a problem in case of error
    448  * \return true if ok, false otherwise
    449  *//*--------------------------------------------------------------------*/
    450 deBool qpTestLog_endCase (qpTestLog* log, qpTestResult result, const char* resultDetails)
    451 {
    452 	const char*		statusStr		= QP_LOOKUP_STRING(s_qpTestResultMap, result);
    453 	qpXmlAttribute	statusAttrib	= qpSetStringAttrib("StatusCode", statusStr);
    454 
    455 	DE_ASSERT(log && log->isCaseOpen);
    456 	DE_ASSERT(ContainerStack_isEmpty(&log->containerStack));
    457 	deMutex_lock(log->lock);
    458 
    459 	/* <Result StatusCode="Pass">Result details</Result>
    460 	 * </TestCaseResult>
    461 	 */
    462 	if (!qpXmlWriter_startElement(log->writer, "Result", 1, &statusAttrib) ||
    463 		(resultDetails && !qpXmlWriter_writeString(log->writer, resultDetails)) ||
    464 		!qpXmlWriter_endElement(log->writer, "Result") ||
    465 		!qpXmlWriter_endElement(log->writer, "TestCaseResult") ||
    466 		!qpXmlWriter_endDocument(log->writer))		/* Close any XML elements still open */
    467 	{
    468 		qpPrintf("qpTestLog_endCase(): Writing XML failed\n");
    469 		deMutex_unlock(log->lock);
    470 		return DE_FALSE;
    471 	}
    472 
    473 	/* Flush XML and write #endTestCaseResult. */
    474 	qpXmlWriter_flush(log->writer);
    475 	fprintf(log->outputFile, "\n#endTestCaseResult\n");
    476 	qpTestLog_flushFile(log);
    477 
    478 	log->isCaseOpen = DE_FALSE;
    479 
    480 	deMutex_unlock(log->lock);
    481 	return DE_TRUE;
    482 }
    483 
    484 /*--------------------------------------------------------------------*//*!
    485  * \brief Abrupt termination of logging.
    486  * \param log		qpTestLog instance
    487  * \param result	Result code, only Crash and Timeout are allowed.
    488  * \return true if ok, false otherwise
    489  *//*--------------------------------------------------------------------*/
    490 deBool qpTestLog_terminateCase (qpTestLog* log, qpTestResult result)
    491 {
    492 	const char* resultStr = QP_LOOKUP_STRING(s_qpTestResultMap, result);
    493 
    494 	DE_ASSERT(log);
    495 	DE_ASSERT(result == QP_TEST_RESULT_CRASH || result == QP_TEST_RESULT_TIMEOUT);
    496 
    497 	deMutex_lock(log->lock);
    498 
    499 	if (!log->isCaseOpen)
    500 	{
    501 		deMutex_unlock(log->lock);
    502 		return DE_FALSE; /* Soft error. This is called from error handler. */
    503 	}
    504 
    505 	/* Flush XML and write #terminateTestCaseResult. */
    506 	qpXmlWriter_flush(log->writer);
    507 	fprintf(log->outputFile, "\n#terminateTestCaseResult %s\n", resultStr);
    508 	qpTestLog_flushFile(log);
    509 
    510 	log->isCaseOpen = DE_FALSE;
    511 
    512 #if defined(DE_DEBUG)
    513 	ContainerStack_reset(&log->containerStack);
    514 #endif
    515 
    516 	deMutex_unlock(log->lock);
    517 	return DE_TRUE;
    518 }
    519 
    520 static deBool qpTestLog_writeKeyValuePair (qpTestLog* log, const char* elementName, const char* name, const char* description, const char* unit, qpKeyValueTag tag, const char* text)
    521 {
    522 	const char*		tagString = QP_LOOKUP_STRING(s_qpTagMap, tag);
    523 	qpXmlAttribute	attribs[8];
    524 	int				numAttribs = 0;
    525 
    526 	DE_ASSERT(log && elementName && text);
    527 	deMutex_lock(log->lock);
    528 
    529 	/* Fill in attributes. */
    530 	if (name)			attribs[numAttribs++] = qpSetStringAttrib("Name", name);
    531 	if (description)	attribs[numAttribs++] = qpSetStringAttrib("Description", description);
    532 	if (tagString)		attribs[numAttribs++] = qpSetStringAttrib("Tag", tagString);
    533 	if (unit)			attribs[numAttribs++] = qpSetStringAttrib("Unit", unit);
    534 
    535 	if (!qpXmlWriter_startElement(log->writer, elementName, numAttribs, attribs) ||
    536 		!qpXmlWriter_writeString(log->writer, text) ||
    537 		!qpXmlWriter_endElement(log->writer, elementName))
    538 	{
    539 		qpPrintf("qpTestLog_writeKeyValuePair(): Writing XML failed\n");
    540 		deMutex_unlock(log->lock);
    541 		return DE_FALSE;
    542 	}
    543 
    544 	deMutex_unlock(log->lock);
    545 	return DE_TRUE;
    546 }
    547 
    548 /*--------------------------------------------------------------------*//*!
    549  * \brief Write a message to output log
    550  * \param log		qpTestLog instance
    551  * \param format	Format string of message
    552  * \param ...		Parameters for message
    553  * \return true if ok, false otherwise
    554  *//*--------------------------------------------------------------------*/
    555 deBool qpTestLog_writeMessage (qpTestLog* log, const char* format, ...)
    556 {
    557 	char	buffer[1024];
    558 	va_list	args;
    559 
    560 	/* \todo [petri] Handle buffer overflows! */
    561 
    562 	va_start(args, format);
    563 	buffer[DE_LENGTH_OF_ARRAY(buffer) - 1] = 0;
    564 	vsnprintf(buffer, sizeof(buffer), format, args);
    565 	va_end(args);
    566 
    567 	printf("%s\n", buffer);
    568 
    569 	/* <Text>text</Text> */
    570 	return qpTestLog_writeKeyValuePair(log, "Text", DE_NULL, DE_NULL, DE_NULL, QP_KEY_TAG_LAST, buffer);
    571 }
    572 
    573 /*--------------------------------------------------------------------*//*!
    574  * \brief Write key-value-pair into log
    575  * \param log			qpTestLog instance
    576  * \param name			Unique identifier for entry
    577  * \param description	Human readable description
    578  * \param tag			Optional tag
    579  * \param value			Value of the key-value-pair
    580  * \return true if ok, false otherwise
    581  *//*--------------------------------------------------------------------*/
    582 deBool qpTestLog_writeText (qpTestLog* log, const char* name, const char* description, qpKeyValueTag tag, const char* text)
    583 {
    584 	/* <Text Name="name" Description="description" Tag="tag">text</Text> */
    585 	return qpTestLog_writeKeyValuePair(log, "Text", name, description, DE_NULL, tag, text);
    586 }
    587 
    588 /*--------------------------------------------------------------------*//*!
    589  * \brief Write key-value-pair into log
    590  * \param log			qpTestLog instance
    591  * \param name			Unique identifier for entry
    592  * \param description	Human readable description
    593  * \param tag			Optional tag
    594  * \param value			Value of the key-value-pair
    595  * \return true if ok, false otherwise
    596  *//*--------------------------------------------------------------------*/
    597 deBool qpTestLog_writeInteger (qpTestLog* log, const char* name, const char* description, const char* unit, qpKeyValueTag tag, deInt64 value)
    598 {
    599 	char tmpString[64];
    600 	int64ToString(value, tmpString);
    601 
    602 	printf("%s = %lld %s\n", description, (signed long long)value, unit ? unit : "");
    603 
    604 	/* <Number Name="name" Description="description" Tag="Performance">15</Number> */
    605 	return qpTestLog_writeKeyValuePair(log, "Number", name, description, unit, tag, tmpString);
    606 }
    607 
    608 /*--------------------------------------------------------------------*//*!
    609  * \brief Write key-value-pair into log
    610  * \param log			qpTestLog instance
    611  * \param name			Unique identifier for entry
    612  * \param description	Human readable description
    613  * \param tag			Optional tag
    614  * \param value			Value of the key-value-pair
    615  * \return true if ok, false otherwise
    616  *//*--------------------------------------------------------------------*/
    617 deBool qpTestLog_writeFloat (qpTestLog* log, const char* name, const char* description, const char* unit, qpKeyValueTag tag, float value)
    618 {
    619 	char tmpString[64];
    620 	floatToString(value, tmpString, sizeof(tmpString));
    621 
    622 	printf("%s = %f %s\n", description, value, unit ? unit : "");
    623 
    624 	/* <Number Name="name" Description="description" Tag="Performance">15</Number> */
    625 	return qpTestLog_writeKeyValuePair(log, "Number", name, description, unit, tag, tmpString);
    626 }
    627 
    628 typedef struct Buffer_s
    629 {
    630 	int			capacity;
    631 	int			size;
    632 	deUint8*	data;
    633 } Buffer;
    634 
    635 void Buffer_init (Buffer* buffer)
    636 {
    637 	buffer->capacity	= 0;
    638 	buffer->size		= 0;
    639 	buffer->data		= DE_NULL;
    640 }
    641 
    642 void Buffer_deinit (Buffer* buffer)
    643 {
    644 	deFree(buffer->data);
    645 	Buffer_init(buffer);
    646 }
    647 
    648 deBool Buffer_resize (Buffer* buffer, int newSize)
    649 {
    650 	/* Grow buffer if necessary. */
    651 	if (newSize > buffer->capacity)
    652 	{
    653 		int			newCapacity	= deAlign32(deMax32(2*buffer->capacity, newSize), 512);
    654 		deUint8*	newData		= (deUint8*)deMalloc(newCapacity);
    655 		if (!newData)
    656 			return DE_FALSE;
    657 
    658 		memcpy(newData, buffer->data, buffer->size);
    659 		deFree(buffer->data);
    660 		buffer->data		= newData;
    661 		buffer->capacity	= newCapacity;
    662 	}
    663 
    664 	buffer->size = newSize;
    665 	return DE_TRUE;
    666 }
    667 
    668 deBool Buffer_append (Buffer* buffer, const deUint8* data, int numBytes)
    669 {
    670 	int offset = buffer->size;
    671 
    672 	if (!Buffer_resize(buffer, buffer->size + numBytes))
    673 		return DE_FALSE;
    674 
    675 	/* Append bytes. */
    676 	memcpy(&buffer->data[offset], data, numBytes);
    677 	return DE_TRUE;
    678 }
    679 
    680 #if defined(QP_SUPPORT_PNG)
    681 void pngWriteData (png_structp png, png_bytep dataPtr, png_size_t numBytes)
    682 {
    683 	Buffer* buffer = (Buffer*)png_get_io_ptr(png);
    684 	if (!Buffer_append(buffer, (const deUint8*)dataPtr, (int)numBytes))
    685 		png_error(png, "unable to resize PNG write buffer!");
    686 }
    687 
    688 void pngFlushData (png_structp png)
    689 {
    690 	DE_UNREF(png);
    691 	/* nada */
    692 }
    693 
    694 static deBool compressImagePNG (Buffer* buffer, qpImageFormat imageFormat, int width, int height, int rowStride, const void* data)
    695 {
    696 	deBool		compressOk		= DE_FALSE;
    697 	png_structp	png				= DE_NULL;
    698 	png_infop	info			= DE_NULL;
    699 	png_byte**	rowPointers		= DE_NULL;
    700 	deBool		hasAlpha		= imageFormat == QP_IMAGE_FORMAT_RGBA8888;
    701 	int			ndx;
    702 
    703 	/* Handle format. */
    704 	DE_ASSERT(imageFormat == QP_IMAGE_FORMAT_RGB888 || imageFormat == QP_IMAGE_FORMAT_RGBA8888);
    705 
    706 	/* Allocate & set row pointers. */
    707 	rowPointers = (png_byte**)deMalloc(height * sizeof(png_byte*));
    708 	if (!rowPointers)
    709 		return DE_FALSE;
    710 
    711 	for (ndx = 0; ndx < height; ndx++)
    712 		rowPointers[ndx] = (png_bytep)((const deUint8*)data + ndx*rowStride);
    713 
    714 	/* Initialize PNG compressor. */
    715 	png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    716 	info = png ? png_create_info_struct(png) : DE_NULL;
    717 	if (png && info)
    718 	{
    719 		if (setjmp(png_jmpbuf(png)) == 0)
    720 		{
    721 			/* Set our own write function. */
    722 			png_set_write_fn(png, buffer, pngWriteData, pngFlushData);
    723 
    724 			/* Write data. */
    725 			png_set_IHDR(png, info, width, height,
    726 				8,
    727 				hasAlpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
    728 				PNG_INTERLACE_NONE,
    729 				PNG_COMPRESSION_TYPE_BASE,
    730 				PNG_FILTER_TYPE_BASE);
    731 			png_write_info(png, info);
    732 			png_write_image(png, rowPointers);
    733 			png_write_end(png, NULL);
    734 
    735 			compressOk = DE_TRUE;
    736 		}
    737 	}
    738 
    739 	/* Cleanup & return. */
    740 	if (png && info)
    741 	{
    742 		png_destroy_info_struct(png, &info);
    743 		png_destroy_write_struct(&png, DE_NULL);
    744 	}
    745 	else if (png)
    746 		png_destroy_write_struct(&png, &info);
    747 
    748 	deFree(rowPointers);
    749 	return compressOk;
    750 }
    751 #endif /* QP_SUPPORT_PNG */
    752 
    753 /*--------------------------------------------------------------------*//*!
    754  * \brief Start image set
    755  * \param log			qpTestLog instance
    756  * \param name			Unique identifier for the set
    757  * \param description	Human readable description
    758  * \return true if ok, false otherwise
    759  *//*--------------------------------------------------------------------*/
    760 deBool qpTestLog_startImageSet (qpTestLog* log, const char* name, const char* description)
    761 {
    762 	qpXmlAttribute	attribs[4];
    763 	int				numAttribs = 0;
    764 
    765 	DE_ASSERT(log && name);
    766 	deMutex_lock(log->lock);
    767 
    768 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
    769 	if (description)
    770 		attribs[numAttribs++] = qpSetStringAttrib("Description", description);
    771 
    772 	/* <ImageSet Name="<name>"> */
    773 	if (!qpXmlWriter_startElement(log->writer, "ImageSet", numAttribs, attribs))
    774 	{
    775 		qpPrintf("qpTestLog_startImageSet(): Writing XML failed\n");
    776 		deMutex_unlock(log->lock);
    777 		return DE_FALSE;
    778 	}
    779 
    780 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_IMAGESET));
    781 
    782 	deMutex_unlock(log->lock);
    783 	return DE_TRUE;
    784 }
    785 
    786 /*--------------------------------------------------------------------*//*!
    787  * \brief End image set
    788  * \param log			qpTestLog instance
    789  * \return true if ok, false otherwise
    790  *//*--------------------------------------------------------------------*/
    791 deBool qpTestLog_endImageSet (qpTestLog* log)
    792 {
    793 	DE_ASSERT(log);
    794 	deMutex_lock(log->lock);
    795 
    796 	/* <ImageSet Name="<name>"> */
    797 	if (!qpXmlWriter_endElement(log->writer, "ImageSet"))
    798 	{
    799 		qpPrintf("qpTestLog_endImageSet(): Writing XML failed\n");
    800 		deMutex_unlock(log->lock);
    801 		return DE_FALSE;
    802 	}
    803 
    804 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_IMAGESET);
    805 
    806 	deMutex_unlock(log->lock);
    807 	return DE_TRUE;
    808 }
    809 
    810 /*--------------------------------------------------------------------*//*!
    811  * \brief Write base64 encoded raw image data into log
    812  * \param log				qpTestLog instance
    813  * \param name				Unique name (matching names can be compared across BatchResults).
    814  * \param description		Textual description (shown in Candy).
    815  * \param compressionMode	Compression mode
    816  * \param imageFormat		Color format
    817  * \param width				Width in pixels
    818  * \param height			Height in pixels
    819  * \param stride			Data stride (offset between rows)
    820  * \param data				Pointer to pixel data
    821  * \return 0 if OK, otherwise <0
    822  *//*--------------------------------------------------------------------*/
    823 deBool qpTestLog_writeImage	(
    824 	qpTestLog*				log,
    825 	const char*				name,
    826 	const char*				description,
    827 	qpImageCompressionMode	compressionMode,
    828 	qpImageFormat			imageFormat,
    829 	int						width,
    830 	int						height,
    831 	int						stride,
    832 	const void*				data)
    833 {
    834 	char			widthStr[32];
    835 	char			heightStr[32];
    836 	qpXmlAttribute	attribs[8];
    837 	int				numAttribs			= 0;
    838 	Buffer			compressedBuffer;
    839 	const void*		writeDataPtr		= DE_NULL;
    840 	int				writeDataBytes		= -1;
    841 
    842 	DE_ASSERT(log && name);
    843 	DE_ASSERT(deInRange32(width, 1, 16384));
    844 	DE_ASSERT(deInRange32(height, 1, 16384));
    845 	DE_ASSERT(data);
    846 
    847 	if (log->flags & QP_TEST_LOG_EXCLUDE_IMAGES)
    848 		return DE_TRUE; /* Image not logged. */
    849 
    850 	Buffer_init(&compressedBuffer);
    851 
    852 	/* BEST compression mode defaults to PNG. */
    853 	if (compressionMode == QP_IMAGE_COMPRESSION_MODE_BEST)
    854 	{
    855 #if defined(QP_SUPPORT_PNG)
    856 		compressionMode = QP_IMAGE_COMPRESSION_MODE_PNG;
    857 #else
    858 		compressionMode = QP_IMAGE_COMPRESSION_MODE_NONE;
    859 #endif
    860 	}
    861 
    862 #if defined(QP_SUPPORT_PNG)
    863 	/* Try storing with PNG compression. */
    864 	if (compressionMode == QP_IMAGE_COMPRESSION_MODE_PNG)
    865 	{
    866 		deBool compressOk = compressImagePNG(&compressedBuffer, imageFormat, width, height, stride, data);
    867 		if (compressOk)
    868 		{
    869 			writeDataPtr	= compressedBuffer.data;
    870 			writeDataBytes	= compressedBuffer.size;
    871 		}
    872 		else
    873 		{
    874 			/* Fall-back to default compression. */
    875 			qpPrintf("WARNING: PNG compression failed -- storing image uncompressed.\n");
    876 			compressionMode	= QP_IMAGE_COMPRESSION_MODE_NONE;
    877 		}
    878 	}
    879 #endif
    880 
    881 	/* Handle image compression. */
    882 	switch (compressionMode)
    883 	{
    884 		case QP_IMAGE_COMPRESSION_MODE_NONE:
    885 		{
    886 			int pixelSize		= imageFormat == QP_IMAGE_FORMAT_RGB888 ? 3 : 4;
    887 			int packedStride	= pixelSize*width;
    888 
    889 			if (packedStride == stride)
    890 				writeDataPtr = data;
    891 			else
    892 			{
    893 				/* Need to re-pack pixels. */
    894 				if (Buffer_resize(&compressedBuffer, packedStride*height))
    895 				{
    896 					int row;
    897 					for (row = 0; row < height; row++)
    898 						memcpy(&compressedBuffer.data[packedStride*row], &((const deUint8*)data)[row*stride], pixelSize*width);
    899 				}
    900 				else
    901 				{
    902 					qpPrintf("ERROR: Failed to pack pixels for writing.\n");
    903 					Buffer_deinit(&compressedBuffer);
    904 					return DE_FALSE;
    905 				}
    906 			}
    907 
    908 			writeDataBytes = packedStride*height;
    909 			break;
    910 		}
    911 
    912 #if defined(QP_SUPPORT_PNG)
    913 		case QP_IMAGE_COMPRESSION_MODE_PNG:
    914 			DE_ASSERT(writeDataPtr); /* Already handled. */
    915 			break;
    916 #endif
    917 
    918 		default:
    919 			qpPrintf("qpTestLog_writeImage(): Unknown compression mode: %s\n", QP_LOOKUP_STRING(s_qpImageCompressionModeMap, compressionMode));
    920 			Buffer_deinit(&compressedBuffer);
    921 			return DE_FALSE;
    922 	}
    923 
    924 	/* Fill in attributes. */
    925 	int32ToString(width, widthStr);
    926 	int32ToString(height, heightStr);
    927 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
    928 	attribs[numAttribs++] = qpSetStringAttrib("Width", widthStr);
    929 	attribs[numAttribs++] = qpSetStringAttrib("Height", heightStr);
    930 	attribs[numAttribs++] = qpSetStringAttrib("Format", QP_LOOKUP_STRING(s_qpImageFormatMap, imageFormat));
    931 	attribs[numAttribs++] = qpSetStringAttrib("CompressionMode", QP_LOOKUP_STRING(s_qpImageCompressionModeMap, compressionMode));
    932 	if (description) attribs[numAttribs++] = qpSetStringAttrib("Description", description);
    933 
    934 	/* \note Log lock is acquired after compression! */
    935 	deMutex_lock(log->lock);
    936 
    937 	/* <Image ID="result" Name="Foobar" Width="640" Height="480" Format="RGB888" CompressionMode="None">base64 data</Image> */
    938 	if (!qpXmlWriter_startElement(log->writer, "Image", numAttribs, attribs) ||
    939 		!qpXmlWriter_writeBase64(log->writer, (const deUint8*)writeDataPtr, writeDataBytes) ||
    940 		!qpXmlWriter_endElement(log->writer, "Image"))
    941 	{
    942 		qpPrintf("qpTestLog_writeImage(): Writing XML failed\n");
    943 		deMutex_unlock(log->lock);
    944 		Buffer_deinit(&compressedBuffer);
    945 		return DE_FALSE;
    946 	}
    947 
    948 	deMutex_unlock(log->lock);
    949 
    950 	/* Free compressed data if allocated. */
    951 	Buffer_deinit(&compressedBuffer);
    952 
    953 	return DE_TRUE;
    954 }
    955 
    956 /*--------------------------------------------------------------------*//*!
    957  * \brief Write a OpenGL ES shader program into the log.
    958  * \param linkOk			Shader program link result, false on failure
    959  * \param linkInfoLog		Implementation provided linkage log
    960  *//*--------------------------------------------------------------------*/
    961 deBool qpTestLog_startShaderProgram (qpTestLog* log, deBool linkOk, const char* linkInfoLog)
    962 {
    963 	qpXmlAttribute	programAttribs[4];
    964 	int				numProgramAttribs = 0;
    965 
    966 	DE_ASSERT(log);
    967 	deMutex_lock(log->lock);
    968 
    969 	programAttribs[numProgramAttribs++] = qpSetStringAttrib("LinkStatus", linkOk ? "OK" : "Fail");
    970 
    971 	if (!qpXmlWriter_startElement(log->writer, "ShaderProgram", numProgramAttribs, programAttribs) ||
    972 		!qpXmlWriter_writeStringElement(log->writer, "InfoLog", linkInfoLog))
    973 	{
    974 		qpPrintf("qpTestLog_startShaderProgram(): Writing XML failed\n");
    975 		deMutex_unlock(log->lock);
    976 		return DE_FALSE;
    977 	}
    978 
    979 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_SHADERPROGRAM));
    980 
    981 	deMutex_unlock(log->lock);
    982 	return DE_TRUE;
    983 }
    984 
    985 /*--------------------------------------------------------------------*//*!
    986  * \brief End shader program
    987  * \param log			qpTestLog instance
    988  * \return true if ok, false otherwise
    989  *//*--------------------------------------------------------------------*/
    990 deBool qpTestLog_endShaderProgram (qpTestLog* log)
    991 {
    992 	DE_ASSERT(log);
    993 	deMutex_lock(log->lock);
    994 
    995 	/* </ShaderProgram> */
    996 	if (!qpXmlWriter_endElement(log->writer, "ShaderProgram"))
    997 	{
    998 		qpPrintf("qpTestLog_endShaderProgram(): Writing XML failed\n");
    999 		deMutex_unlock(log->lock);
   1000 		return DE_FALSE;
   1001 	}
   1002 
   1003 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_SHADERPROGRAM);
   1004 
   1005 	deMutex_unlock(log->lock);
   1006 	return DE_TRUE;
   1007 }
   1008 
   1009 /*--------------------------------------------------------------------*//*!
   1010  * \brief Write a OpenGL ES shader into the log.
   1011  * \param type				Shader type
   1012  * \param source	 		Shader source
   1013  * \param compileOk			Shader compilation result, false on failure
   1014  * \param infoLog			Implementation provided shader compilation log
   1015  *//*--------------------------------------------------------------------*/
   1016 deBool qpTestLog_writeShader (qpTestLog* log, qpShaderType type, const char* source, deBool compileOk, const char* infoLog)
   1017 {
   1018 	const char*		tagName				= QP_LOOKUP_STRING(s_qpShaderTypeMap, type);
   1019 	int				numShaderAttribs	= 0;
   1020 	qpXmlAttribute	shaderAttribs[4];
   1021 
   1022 	DE_ASSERT(log && source);
   1023 	DE_ASSERT(ContainerStack_getTop(&log->containerStack) == CONTAINERTYPE_SHADERPROGRAM);
   1024 	deMutex_lock(log->lock);
   1025 
   1026 	shaderAttribs[numShaderAttribs++]	= qpSetStringAttrib("CompileStatus", compileOk ? "OK" : "Fail");
   1027 
   1028 	if (!qpXmlWriter_startElement(log->writer, tagName, numShaderAttribs, shaderAttribs) ||
   1029 		!qpXmlWriter_writeStringElement(log->writer, "ShaderSource", source) ||
   1030 		!qpXmlWriter_writeStringElement(log->writer, "InfoLog", infoLog) ||
   1031 		!qpXmlWriter_endElement(log->writer, tagName))
   1032 	{
   1033 		qpPrintf("qpTestLog_writeShader(): Writing XML failed\n");
   1034 		deMutex_unlock(log->lock);
   1035 		return DE_FALSE;
   1036 	}
   1037 
   1038 	deMutex_unlock(log->lock);
   1039 	return DE_TRUE;
   1040 }
   1041 
   1042 /*--------------------------------------------------------------------*//*!
   1043  * \brief Start writing a set of EGL configurations into the log.
   1044  *//*--------------------------------------------------------------------*/
   1045 deBool qpTestLog_startEglConfigSet (qpTestLog* log, const char* name, const char* description)
   1046 {
   1047 	qpXmlAttribute	attribs[4];
   1048 	int				numAttribs = 0;
   1049 
   1050 	DE_ASSERT(log && name);
   1051 	deMutex_lock(log->lock);
   1052 
   1053 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
   1054 	if (description)
   1055 		attribs[numAttribs++] = qpSetStringAttrib("Description", description);
   1056 
   1057 	/* <EglConfigSet Name="<name>"> */
   1058 	if (!qpXmlWriter_startElement(log->writer, "EglConfigSet", numAttribs, attribs))
   1059 	{
   1060 		qpPrintf("qpTestLog_startEglImageSet(): Writing XML failed\n");
   1061 		deMutex_unlock(log->lock);
   1062 		return DE_FALSE;
   1063 	}
   1064 
   1065 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_EGLCONFIGSET));
   1066 
   1067 	deMutex_unlock(log->lock);
   1068 	return DE_TRUE;
   1069 }
   1070 
   1071 /*--------------------------------------------------------------------*//*!
   1072  * \brief End an EGL config set
   1073  *//*--------------------------------------------------------------------*/
   1074 deBool qpTestLog_endEglConfigSet (qpTestLog* log)
   1075 {
   1076 	DE_ASSERT(log);
   1077 	deMutex_lock(log->lock);
   1078 
   1079 	/* <EglConfigSet Name="<name>"> */
   1080 	if (!qpXmlWriter_endElement(log->writer, "EglConfigSet"))
   1081 	{
   1082 		qpPrintf("qpTestLog_endEglImageSet(): Writing XML failed\n");
   1083 		deMutex_unlock(log->lock);
   1084 		return DE_FALSE;
   1085 	}
   1086 
   1087 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_EGLCONFIGSET);
   1088 
   1089 	deMutex_unlock(log->lock);
   1090 	return DE_TRUE;
   1091 }
   1092 
   1093 /*--------------------------------------------------------------------*//*!
   1094  * \brief Write an EGL config inside an EGL config set
   1095  * \see   qpElgConfigInfo for details
   1096  *//*--------------------------------------------------------------------*/
   1097 deBool qpTestLog_writeEglConfig (qpTestLog* log, const qpEglConfigInfo* config)
   1098 {
   1099 	qpXmlAttribute	attribs[64];
   1100 	int				numAttribs = 0;
   1101 
   1102 	DE_ASSERT(log && config);
   1103 	deMutex_lock(log->lock);
   1104 
   1105 	attribs[numAttribs++] = qpSetIntAttrib		("BufferSize", config->bufferSize);
   1106 	attribs[numAttribs++] = qpSetIntAttrib		("RedSize", config->redSize);
   1107 	attribs[numAttribs++] = qpSetIntAttrib		("GreenSize", config->greenSize);
   1108 	attribs[numAttribs++] = qpSetIntAttrib		("BlueSize", config->blueSize);
   1109 	attribs[numAttribs++] = qpSetIntAttrib		("LuminanceSize", config->luminanceSize);
   1110 	attribs[numAttribs++] = qpSetIntAttrib		("AlphaSize", config->alphaSize);
   1111 	attribs[numAttribs++] = qpSetIntAttrib		("AlphaMaskSize", config->alphaMaskSize);
   1112 	attribs[numAttribs++] = qpSetBoolAttrib		("BindToTextureRGB", config->bindToTextureRGB);
   1113 	attribs[numAttribs++] = qpSetBoolAttrib		("BindToTextureRGBA", config->bindToTextureRGBA);
   1114 	attribs[numAttribs++] = qpSetStringAttrib	("ColorBufferType", config->colorBufferType);
   1115 	attribs[numAttribs++] = qpSetStringAttrib	("ConfigCaveat", config->configCaveat);
   1116 	attribs[numAttribs++] = qpSetIntAttrib		("ConfigID", config->configID);
   1117 	attribs[numAttribs++] = qpSetStringAttrib	("Conformant", config->conformant);
   1118 	attribs[numAttribs++] = qpSetIntAttrib		("DepthSize", config->depthSize);
   1119 	attribs[numAttribs++] = qpSetIntAttrib		("Level", config->level);
   1120 	attribs[numAttribs++] = qpSetIntAttrib		("MaxPBufferWidth", config->maxPBufferWidth);
   1121 	attribs[numAttribs++] = qpSetIntAttrib		("MaxPBufferHeight", config->maxPBufferHeight);
   1122 	attribs[numAttribs++] = qpSetIntAttrib		("MaxPBufferPixels", config->maxPBufferPixels);
   1123 	attribs[numAttribs++] = qpSetIntAttrib		("MaxSwapInterval", config->maxSwapInterval);
   1124 	attribs[numAttribs++] = qpSetIntAttrib		("MinSwapInterval", config->minSwapInterval);
   1125 	attribs[numAttribs++] = qpSetBoolAttrib		("NativeRenderable", config->nativeRenderable);
   1126 	attribs[numAttribs++] = qpSetStringAttrib	("RenderableType", config->renderableType);
   1127 	attribs[numAttribs++] = qpSetIntAttrib		("SampleBuffers", config->sampleBuffers);
   1128 	attribs[numAttribs++] = qpSetIntAttrib		("Samples", config->samples);
   1129 	attribs[numAttribs++] = qpSetIntAttrib		("StencilSize", config->stencilSize);
   1130 	attribs[numAttribs++] = qpSetStringAttrib	("SurfaceTypes", config->surfaceTypes);
   1131 	attribs[numAttribs++] = qpSetStringAttrib	("TransparentType", config->transparentType);
   1132 	attribs[numAttribs++] = qpSetIntAttrib		("TransparentRedValue", config->transparentRedValue);
   1133 	attribs[numAttribs++] = qpSetIntAttrib		("TransparentGreenValue", config->transparentGreenValue);
   1134 	attribs[numAttribs++] = qpSetIntAttrib		("TransparentBlueValue", config->transparentBlueValue);
   1135 	DE_ASSERT(numAttribs <= DE_LENGTH_OF_ARRAY(attribs));
   1136 
   1137 	if (!qpXmlWriter_startElement(log->writer, "EglConfig", numAttribs, attribs) ||
   1138 		!qpXmlWriter_endElement(log->writer, "EglConfig"))
   1139 	{
   1140 		qpPrintf("qpTestLog_writeEglConfig(): Writing XML failed\n");
   1141 		deMutex_unlock(log->lock);
   1142 		return DE_FALSE;
   1143 	}
   1144 
   1145 	deMutex_unlock(log->lock);
   1146 	return DE_TRUE;
   1147 }
   1148 
   1149 /*--------------------------------------------------------------------*//*!
   1150  * \brief Start section in log.
   1151  * \param log			qpTestLog instance
   1152  * \param name			Section name
   1153  * \param description	Human readable description
   1154  * \return true if ok, false otherwise
   1155  *//*--------------------------------------------------------------------*/
   1156 deBool qpTestLog_startSection (qpTestLog* log, const char* name, const char* description)
   1157 {
   1158 	qpXmlAttribute	attribs[2];
   1159 	int				numAttribs = 0;
   1160 
   1161 	DE_ASSERT(log && name);
   1162 	deMutex_lock(log->lock);
   1163 
   1164 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
   1165 	if (description)
   1166 		attribs[numAttribs++] = qpSetStringAttrib("Description", description);
   1167 
   1168 	/* <Section Name="<name>" Description="<description>"> */
   1169 	if (!qpXmlWriter_startElement(log->writer, "Section", numAttribs, attribs))
   1170 	{
   1171 		qpPrintf("qpTestLog_startSection(): Writing XML failed\n");
   1172 		deMutex_unlock(log->lock);
   1173 		return DE_FALSE;
   1174 	}
   1175 
   1176 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_SECTION));
   1177 
   1178 	deMutex_unlock(log->lock);
   1179 	return DE_TRUE;
   1180 }
   1181 
   1182 /*--------------------------------------------------------------------*//*!
   1183  * \brief End section in log.
   1184  * \param log			qpTestLog instance
   1185  * \return true if ok, false otherwise
   1186  *//*--------------------------------------------------------------------*/
   1187 deBool qpTestLog_endSection (qpTestLog* log)
   1188 {
   1189 	DE_ASSERT(log);
   1190 	deMutex_lock(log->lock);
   1191 
   1192 	/* </Section> */
   1193 	if (!qpXmlWriter_endElement(log->writer, "Section"))
   1194 	{
   1195 		qpPrintf("qpTestLog_endSection(): Writing XML failed\n");
   1196 		deMutex_unlock(log->lock);
   1197 		return DE_FALSE;
   1198 	}
   1199 
   1200 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_SECTION);
   1201 
   1202 	deMutex_unlock(log->lock);
   1203 	return DE_TRUE;
   1204 }
   1205 
   1206 /*--------------------------------------------------------------------*//*!
   1207  * \brief Write OpenCL compute kernel source into the log.
   1208  *//*--------------------------------------------------------------------*/
   1209 deBool qpTestLog_writeKernelSource (qpTestLog* log, const char* source)
   1210 {
   1211 	DE_ASSERT(log);
   1212 	deMutex_lock(log->lock);
   1213 
   1214 	if (!qpXmlWriter_writeStringElement(log->writer, "KernelSource", source))
   1215 	{
   1216 		qpPrintf("qpTestLog_writeKernelSource(): Writing XML failed\n");
   1217 		deMutex_unlock(log->lock);
   1218 		return DE_FALSE;
   1219 	}
   1220 
   1221 	deMutex_unlock(log->lock);
   1222 	return DE_TRUE;
   1223 }
   1224 
   1225 /*--------------------------------------------------------------------*//*!
   1226  * \brief Write OpenCL kernel compilation results into the log
   1227  *//*--------------------------------------------------------------------*/
   1228 deBool qpTestLog_writeCompileInfo (qpTestLog* log, const char* name, const char* description, deBool compileOk, const char* infoLog)
   1229 {
   1230 	int				numAttribs = 0;
   1231 	qpXmlAttribute	attribs[3];
   1232 
   1233 	DE_ASSERT(log && name && description && infoLog);
   1234 	deMutex_lock(log->lock);
   1235 
   1236 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
   1237 	attribs[numAttribs++] = qpSetStringAttrib("Description", description);
   1238 	attribs[numAttribs++] = qpSetStringAttrib("CompileStatus", compileOk ? "OK" : "Fail");
   1239 
   1240 	if (!qpXmlWriter_startElement(log->writer, "CompileInfo", numAttribs, attribs) ||
   1241 		!qpXmlWriter_writeStringElement(log->writer, "InfoLog", infoLog) ||
   1242 		!qpXmlWriter_endElement(log->writer, "CompileInfo"))
   1243 	{
   1244 		qpPrintf("qpTestLog_writeCompileInfo(): Writing XML failed\n");
   1245 		deMutex_unlock(log->lock);
   1246 		return DE_FALSE;
   1247 	}
   1248 
   1249 	deMutex_unlock(log->lock);
   1250 	return DE_TRUE;
   1251 }
   1252 
   1253 deBool qpTestLog_startSampleList (qpTestLog* log, const char* name, const char* description)
   1254 {
   1255 	int				numAttribs = 0;
   1256 	qpXmlAttribute	attribs[2];
   1257 
   1258 	DE_ASSERT(log && name && description);
   1259 	deMutex_lock(log->lock);
   1260 
   1261 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
   1262 	attribs[numAttribs++] = qpSetStringAttrib("Description", description);
   1263 
   1264 	if (!qpXmlWriter_startElement(log->writer, "SampleList", numAttribs, attribs))
   1265 	{
   1266 		qpPrintf("qpTestLog_startSampleList(): Writing XML failed\n");
   1267 		deMutex_unlock(log->lock);
   1268 		return DE_FALSE;
   1269 	}
   1270 
   1271 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_SAMPLELIST));
   1272 
   1273 	deMutex_unlock(log->lock);
   1274 	return DE_TRUE;
   1275 }
   1276 
   1277 deBool qpTestLog_startSampleInfo (qpTestLog* log)
   1278 {
   1279 	DE_ASSERT(log);
   1280 	deMutex_lock(log->lock);
   1281 
   1282 	if (!qpXmlWriter_startElement(log->writer, "SampleInfo", 0, DE_NULL))
   1283 	{
   1284 		qpPrintf("qpTestLog_startSampleInfo(): Writing XML failed\n");
   1285 		deMutex_unlock(log->lock);
   1286 		return DE_FALSE;
   1287 	}
   1288 
   1289 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_SAMPLEINFO));
   1290 
   1291 	deMutex_unlock(log->lock);
   1292 	return DE_TRUE;
   1293 }
   1294 
   1295 deBool qpTestLog_writeValueInfo (qpTestLog* log, const char* name, const char* description, const char* unit, qpSampleValueTag tag)
   1296 {
   1297 	const char*		tagName		= QP_LOOKUP_STRING(s_qpSampleValueTagMap, tag);
   1298 	int				numAttribs	= 0;
   1299 	qpXmlAttribute	attribs[4];
   1300 
   1301 	DE_ASSERT(log && name && description && tagName);
   1302 	deMutex_lock(log->lock);
   1303 
   1304 	DE_ASSERT(ContainerStack_getTop(&log->containerStack) == CONTAINERTYPE_SAMPLEINFO);
   1305 
   1306 	attribs[numAttribs++] = qpSetStringAttrib("Name", name);
   1307 	attribs[numAttribs++] = qpSetStringAttrib("Description", description);
   1308 	attribs[numAttribs++] = qpSetStringAttrib("Tag", tagName);
   1309 
   1310 	if (unit)
   1311 		attribs[numAttribs++] = qpSetStringAttrib("Unit", unit);
   1312 
   1313 	if (!qpXmlWriter_startElement(log->writer, "ValueInfo", numAttribs, attribs) ||
   1314 		!qpXmlWriter_endElement(log->writer, "ValueInfo"))
   1315 	{
   1316 		qpPrintf("qpTestLog_writeValueInfo(): Writing XML failed\n");
   1317 		deMutex_unlock(log->lock);
   1318 		return DE_FALSE;
   1319 	}
   1320 
   1321 	deMutex_unlock(log->lock);
   1322 	return DE_TRUE;
   1323 }
   1324 
   1325 deBool qpTestLog_endSampleInfo (qpTestLog* log)
   1326 {
   1327 	DE_ASSERT(log);
   1328 	deMutex_lock(log->lock);
   1329 
   1330 	if (!qpXmlWriter_endElement(log->writer, "SampleInfo"))
   1331 	{
   1332 		qpPrintf("qpTestLog_endSampleInfo(): Writing XML failed\n");
   1333 		deMutex_unlock(log->lock);
   1334 		return DE_FALSE;
   1335 	}
   1336 
   1337 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_SAMPLEINFO);
   1338 
   1339 	deMutex_unlock(log->lock);
   1340 	return DE_TRUE;
   1341 }
   1342 
   1343 deBool qpTestLog_startSample (qpTestLog* log)
   1344 {
   1345 	DE_ASSERT(log);
   1346 	deMutex_lock(log->lock);
   1347 
   1348 	DE_ASSERT(ContainerStack_getTop(&log->containerStack) == CONTAINERTYPE_SAMPLELIST);
   1349 
   1350 	if (!qpXmlWriter_startElement(log->writer, "Sample", 0, DE_NULL))
   1351 	{
   1352 		qpPrintf("qpTestLog_startSample(): Writing XML failed\n");
   1353 		deMutex_unlock(log->lock);
   1354 		return DE_FALSE;
   1355 	}
   1356 
   1357 	DE_ASSERT(ContainerStack_push(&log->containerStack, CONTAINERTYPE_SAMPLE));
   1358 
   1359 	deMutex_unlock(log->lock);
   1360 	return DE_TRUE;
   1361 }
   1362 
   1363 deBool qpTestLog_writeValueFloat (qpTestLog* log, double value)
   1364 {
   1365 	char tmpString[512];
   1366 	doubleToString(value, tmpString, (int)sizeof(tmpString));
   1367 
   1368 	deMutex_lock(log->lock);
   1369 
   1370 	DE_ASSERT(ContainerStack_getTop(&log->containerStack) == CONTAINERTYPE_SAMPLE);
   1371 
   1372 	if (!qpXmlWriter_writeStringElement(log->writer, "Value", &tmpString[0]))
   1373 	{
   1374 		qpPrintf("qpTestLog_writeSampleValue(): Writing XML failed\n");
   1375 		deMutex_unlock(log->lock);
   1376 		return DE_FALSE;
   1377 	}
   1378 
   1379 	deMutex_unlock(log->lock);
   1380 	return DE_TRUE;
   1381 }
   1382 
   1383 deBool qpTestLog_writeValueInteger (qpTestLog* log, deInt64 value)
   1384 {
   1385 	char tmpString[64];
   1386 	int64ToString(value, tmpString);
   1387 
   1388 	deMutex_lock(log->lock);
   1389 
   1390 	DE_ASSERT(ContainerStack_getTop(&log->containerStack) == CONTAINERTYPE_SAMPLE);
   1391 
   1392 	if (!qpXmlWriter_writeStringElement(log->writer, "Value", &tmpString[0]))
   1393 	{
   1394 		qpPrintf("qpTestLog_writeSampleValue(): Writing XML failed\n");
   1395 		deMutex_unlock(log->lock);
   1396 		return DE_FALSE;
   1397 	}
   1398 
   1399 	deMutex_unlock(log->lock);
   1400 	return DE_TRUE;
   1401 }
   1402 
   1403 deBool qpTestLog_endSample (qpTestLog* log)
   1404 {
   1405 	DE_ASSERT(log);
   1406 	deMutex_lock(log->lock);
   1407 
   1408 	if (!qpXmlWriter_endElement(log->writer, "Sample"))
   1409 	{
   1410 		qpPrintf("qpTestLog_endSample(): Writing XML failed\n");
   1411 		deMutex_unlock(log->lock);
   1412 		return DE_FALSE;
   1413 	}
   1414 
   1415 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_SAMPLE);
   1416 
   1417 	deMutex_unlock(log->lock);
   1418 	return DE_TRUE;
   1419 }
   1420 
   1421 deBool qpTestLog_endSampleList (qpTestLog* log)
   1422 {
   1423 	DE_ASSERT(log);
   1424 	deMutex_lock(log->lock);
   1425 
   1426 	if (!qpXmlWriter_endElement(log->writer, "SampleList"))
   1427 	{
   1428 		qpPrintf("qpTestLog_endSampleList(): Writing XML failed\n");
   1429 		deMutex_unlock(log->lock);
   1430 		return DE_FALSE;
   1431 	}
   1432 
   1433 	DE_ASSERT(ContainerStack_pop(&log->containerStack) == CONTAINERTYPE_SAMPLELIST);
   1434 
   1435 	deMutex_unlock(log->lock);
   1436 	return DE_TRUE;
   1437 }
   1438 
   1439 const char* qpGetTestResultName (qpTestResult result)
   1440 {
   1441 	return QP_LOOKUP_STRING(s_qpTestResultMap, result);
   1442 }
   1443