forked from apitrace/apitrace-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_driver.py
executable file
·462 lines (374 loc) · 14.4 KB
/
app_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python
##########################################################################
#
# Copyright 2011 Jose Fonseca
# All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
##########################################################################/
'''Application test driver.'''
import os.path
import platform
import re
import subprocess
import sys
import time
import json
import base64
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import tracematch
from base_driver import *
class SrcTraceParser(tracematch.SrcTraceParser):
def __init__(self, stream):
tracematch.SrcTraceParser.__init__(self, stream)
self.swapbuffers = 0
def handleCall(self, callNo, functionName, args, ret):
tracematch.SrcTraceParser.handleCall(self, callNo, functionName, args, ret)
if functionName.find('SwapBuffers') != -1 or \
repr(args).find('kCGLPFADoubleBuffer') != -1:
self.swapbuffers += 1
class AppDriver(Driver):
cmd = None
cwd = None
api = 'gl'
max_frames = None
trace_file = None
ref_dump = None
doubleBuffer = True
verbose = False
threshold_precision = 12.0
def __init__(self):
Driver.__init__(self)
self.stateCache = {}
def runApp(self):
'''Run the application standalone, skipping this test if it fails by
some reason.'''
if not self.cmd:
return
sys.stderr.write('Run application...\n')
env = os.environ.copy()
env['DRY_RUN'] = 'yes'
p = popen(self.cmd, cwd=self.cwd, env=env)
p.wait()
if p.returncode == 125:
skip('application returned code %i' % p.returncode)
if p.returncode != 0:
fail('application returned code %i' % p.returncode)
sys.stdout.flush()
sys.stderr.write('\n')
api_trace_map = {
'gl': 'gl',
'egl_gl': 'egl',
'egl_gles1': 'egl',
'egl_gles2': 'egl',
'd3d7': 'd3d7',
'd3d8': 'd3d8',
'd3d9': 'd3d9',
'd3d10': 'd3d10',
'd3d10_1': 'd3d10_1',
'd3d11': 'd3d11',
'd3d11_1': 'd3d11',
}
api_replay_map = {
'gl': 'glreplay',
'egl_gl': 'eglreplay',
'egl_gles1': 'eglreplay',
'egl_gles2': 'eglreplay',
'd3d8': 'd3dreplay',
'd3d9': 'd3dreplay',
'd3d10': 'd3dreplay',
'd3d10_1': 'd3dreplay',
'd3d11': 'd3dreplay',
'd3d11_1': 'd3dreplay',
}
def traceApp(self):
if not self.cmd:
return
sys.stderr.write('Capturing trace...\n')
if self.trace_file is None:
if self.ref_dump is not None:
name = self.ref_dump
else:
name = self.cmd[0]
name, ext = os.path.splitext(os.path.basename(name))
while ext:
name, ext = os.path.splitext(os.path.basename(name))
self.trace_file = os.path.abspath(os.path.join(self.results, name + '.trace'))
if os.path.exists(self.trace_file):
os.remove(self.trace_file)
else:
trace_dir = os.path.dirname(self.trace_file)
if not os.path.exists(trace_dir):
os.makedirs(trace_dir)
cmd = self.cmd
env = os.environ.copy()
cmd = [
options.apitrace, 'trace',
'-v',
'-a', self.api_trace_map[self.api],
'-o', self.trace_file,
'--'
] + cmd
if self.max_frames is not None:
env['TRACE_FRAMES'] = str(self.max_frames)
p = popen(cmd, env=env, cwd=self.cwd)
p.wait()
if p.returncode != 0:
if self.getNamePrefix() != 'exception':
fail('`apitrace trace` returned code %i' % p.returncode)
if not os.path.exists(self.trace_file):
fail('no trace file generated\n')
sys.stdout.flush()
sys.stderr.write('\n')
def checkTrace(self):
sys.stderr.write('Comparing trace %s against %s...\n' % (self.trace_file, self.ref_dump))
cmd = [options.apitrace, 'dump', '--verbose', '--color=never', self.trace_file]
p = popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
srcParser = SrcTraceParser(p.stdout)
srcTrace = srcParser.parse()
self.doubleBuffer = srcParser.swapbuffers > 0
images = []
states = []
if self.ref_dump:
refParser = tracematch.RefTraceParser(self.ref_dump)
refTrace = refParser.parse()
try:
mo = refTrace.match(srcTrace)
except tracematch.TraceMismatch, ex:
fail(str(ex))
dirName, baseName = os.path.split(os.path.abspath(self.ref_dump))
prefix, _ = os.path.splitext(baseName)
prefix += '.'
fileNames = os.listdir(dirName)
for fileName in fileNames:
if fileName.startswith(prefix) and fileName != self.ref_dump:
rest = fileName[len(prefix):]
paramName, ext = os.path.splitext(rest)
if ext in ('.json', '.png'):
if paramName.isdigit():
callNo = int(paramName)
else:
try:
callNo = mo.params[paramName]
except KeyError:
fail('could not find parameter %s for %s' % (paramName, fileName))
filePath = os.path.join(dirName, fileName)
if ext == '.png':
images.append((callNo, filePath))
if ext == '.json':
states.append((callNo, filePath))
p.wait()
if p.returncode != 0:
fail('`apitrace dump` returned code %i' % p.returncode)
sys.stdout.flush()
sys.stderr.write('\n')
if self.api not in self.api_replay_map:
return
for callNo, refImageFileName in images:
self.checkImage(callNo, refImageFileName)
for callNo, refStateFileName in states:
self.checkState(callNo, refStateFileName)
def checkImage(self, callNo, refImageFileName):
sys.stderr.write('Comparing snapshot from call %u against %s...\n' % (callNo, refImageFileName))
try:
from PIL import Image
except ImportError:
sys.stderr.write('warning: PIL not found, skipping image comparison\n');
return
srcImage = self.getImage(callNo)
refImage = Image.open(refImageFileName)
from snapdiff import Comparer
comparer = Comparer(refImage, srcImage)
precision = comparer.precision(filter=True)
sys.stdout.write('precision of %f bits against %s\n' % (precision, refImageFileName))
if precision < self.threshold_precision:
prefix = self.getNamePrefix()
srcImageFileName = '%s.src.%u.png' % (prefix, callNo)
diffImageFileName = '%s.diff.%u.png' % (prefix, callNo)
srcImage.save(srcImageFileName)
comparer.write_diff(diffImageFileName)
fail('snapshot from call %u does not match %s' % (callNo, refImageFileName))
sys.stdout.flush()
sys.stderr.write('\n')
def checkState(self, callNo, refStateFileName):
sys.stderr.write('Comparing state dump from call %u against %s...\n' % (callNo, refStateFileName))
srcState = self.getState(callNo)
refState = self.getRefState(refStateFileName)
from jsondiff import Comparer, Differ
comparer = Comparer(ignore_added = True)
match = comparer.visit(refState, srcState)
if not match:
prefix = self.getNamePrefix()
srcStateFileName = '%s.src.%u.json' % (prefix, callNo)
diffStateFileName = '%s.diff.%u.json' % (prefix, callNo)
self.saveState(srcState, srcStateFileName)
#diffStateFile = open(diffStateFileName, 'wt')
diffStateFile = sys.stdout
differ = Differ(diffStateFile, ignore_added = True)
differ.visit(refState, srcState)
fail('state from call %u does not match %s' % (callNo, refStateFileName))
sys.stdout.flush()
sys.stderr.write('\n')
def getRefState(self, refStateFileName):
stream = open(refStateFileName, 'rt')
from jsondiff import load
state = load(stream, strip_images=False)
self.adjustRefState(state)
return state
def getNamePrefix(self):
name = os.path.basename(self.ref_dump)
try:
index = name.index('.')
except ValueError:
pass
else:
name = name[:index]
return name
def saveState(self, state, filename):
s = json.dumps(state, sort_keys=True, indent=2)
open(filename, 'wt').write(s)
def replay(self):
if self.api not in self.api_replay_map:
return
sys.stderr.write('Retracing %s...\n' % (self.trace_file,))
p = self._replay()
p.wait()
if p.returncode != 0:
fail('replay failed with code %i' % (p.returncode))
sys.stdout.flush()
sys.stderr.write('\n')
def getImage(self, callNo):
from PIL import Image
state = self.getState(callNo)
if self.doubleBuffer:
attachments = ['GL_BACK', 'GL_BACK_LEFT', 'GL_BACK_RIGHT', 'GL_COLOR_ATTACHMENT0', 'RENDER_TARGET_0']
else:
attachments = ['GL_FRONT', 'GL_FRONT_LEFT', 'GL_FRONT_RIGHT', 'GL_COLOR_ATTACHMENT0', 'RENDER_TARGET_0']
imageObj = self.getFramebufferAttachment(state, attachments)
data = imageObj['__data__']
stream = StringIO(base64.b64decode(data))
im = Image.open(stream)
return im
def getFramebufferAttachment(self, state, attachments):
framebufferObj = state['framebuffer']
for attachment in attachments:
try:
attachmentObj = framebufferObj[attachment]
except KeyError:
pass
else:
return attachmentObj
raise Exception("no attachment found")
def getState(self, callNo):
try:
state = self.stateCache[callNo]
except KeyError:
pass
else:
return state
p = self._replay(['-D', str(callNo)])
state = json.load(p.stdout, strict=False)
p.wait()
if p.returncode != 0:
fail('replay returned code %i' % (p.returncode))
self.adjustSrcState(state)
self.stateCache[callNo] = state
return state
def adjustSrcState(self, state):
# Do some adjustments on the obtained state to eliminate failures from
# bugs/issues outside of apitrace
try:
parameters = state['parameters']
except KeyError:
return
# On NVIDIA drivers glGetIntegerv(GL_INDEX_WRITEMASK) returns 255
self.replaceState(parameters, 'GL_INDEX_WRITEMASK', 255, -1)
def adjustRefState(self, state):
# Do some adjustments on reference state to eliminate failures from
# bugs/issues outside of apitrace
try:
parameters = state['parameters']
except KeyError:
return
if platform.system() == 'Darwin':
# Mac OS X drivers fail on GL_COLOR_SUM
# XXX: investigate this
self.removeState(parameters, 'GL_COLOR_SUM')
def replaceState(self, obj, key, srcValue, dstValue):
try:
value = obj[key]
except KeyError:
pass
else:
if value == srcValue:
obj[key] = dstValue
def removeState(self, obj, key):
try:
del obj[key]
except KeyError:
pass
def _replay(self, args = None, stdout=subprocess.PIPE):
replay = self.api_replay_map[self.api]
#cmd = [get_build_program(replay)]
cmd = [options.apitrace, 'replay']
if self.doubleBuffer:
cmd += ['-db']
else:
cmd += ['-sb']
if args:
cmd += args
cmd += [self.trace_file]
return popen(cmd, stdout=stdout)
def createOptParser(self):
optparser = Driver.createOptParser(self)
optparser.add_option(
'-a', '--api', metavar='API',
type='string', dest='api', default='gl',
help='api to trace')
optparser.add_option(
'-R', '--results', metavar='PATH',
type='string', dest='results', default='.',
help='results directory [default=%default]')
optparser.add_option(
'--ref-dump', metavar='PATH',
type='string', dest='ref_dump', default=None,
help='reference dump')
return optparser
def run(self):
global options
(options, args) = self.parseOptions()
if not os.path.exists(options.results):
os.makedirs(options.results)
self.verbose = options.verbose
self.cmd = args
self.cwd = options.cwd
self.api = options.api
self.ref_dump = options.ref_dump
self.results = options.results
self.runApp()
self.traceApp()
self.checkTrace()
self.replay()
pass_()
if __name__ == '__main__':
AppDriver().run()