Hash :
f3a38d97
Author :
Date :
2020-07-23T19:00:34
Add a custom trace tests loader. This custom loader will disambiguate the trace tests gl layer from the util GL/EGL loader. Bug: angleproject:4845 Change-Id: I5e8340eb50f736d931302f71f15f556fd9e52081 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2315627 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Manh Nguyen <nguyenmh@google.com>
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
#!/usr/bin/python2
#
# Copyright 2020 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# gen_restricted_traces.py:
# Generates integration code for the restricted trace tests.
import fnmatch
import json
import os
import sys
gni_template = """# GENERATED FILE - DO NOT EDIT.
# Generated by {script_name} using data from {data_source_name}
#
# Copyright 2020 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# A list of all restricted trace tests, paired with their context.
# Can be consumed by tests/BUILD.gn.
angle_restricted_traces = [
{test_list}
]
"""
header_template = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// {filename}: Types and enumerations for trace tests.
#ifndef ANGLE_RESTRICTED_TRACES_H_
#define ANGLE_RESTRICTED_TRACES_H_
#include <cstdint>
#include <vector>
#include <KHR/khrplatform.h>
// See util/util_export.h for details on import/export labels.
#if !defined(ANGLE_TRACE_EXPORT)
# if defined(_WIN32)
# if defined(ANGLE_TRACE_IMPLEMENTATION)
# define ANGLE_TRACE_EXPORT __declspec(dllexport)
# else
# define ANGLE_TRACE_EXPORT __declspec(dllimport)
# endif
# elif defined(__GNUC__)
# define ANGLE_TRACE_EXPORT __attribute__((visibility("default")))
# else
# define ANGLE_TRACE_EXPORT
# endif
#endif // !defined(ANGLE_TRACE_EXPORT)
namespace trace_angle
{{
using GenericProc = void (*)();
using LoadProc = GenericProc(KHRONOS_APIENTRY *)(const char *);
ANGLE_TRACE_EXPORT void LoadGLES(LoadProc loadProc);
}} // namespace trace_angle
namespace angle
{{
enum class RestrictedTraceID
{{
{trace_ids}, InvalidEnum, EnumCount = InvalidEnum
}};
using ReplayFunc = void (*)(uint32_t);
using ResetFunc = void (*)();
using SetupFunc = void (*)();
using DecompressFunc = uint8_t *(*)(const std::vector<uint8_t> &);
using SetBinaryDataDirFunc = void (*)(const char *);
static constexpr size_t kTraceInfoMaxNameLen = 32;
struct TraceInfo
{{
uint32_t startFrame;
uint32_t endFrame;
uint32_t drawSurfaceWidth;
uint32_t drawSurfaceHeight;
char name[kTraceInfoMaxNameLen];
}};
using DecompressCallback = uint8_t *(*)(const std::vector<uint8_t> &);
ANGLE_TRACE_EXPORT const TraceInfo &GetTraceInfo(RestrictedTraceID traceID);
ANGLE_TRACE_EXPORT void ReplayFrame(RestrictedTraceID traceID, uint32_t frameIndex);
ANGLE_TRACE_EXPORT void ResetReplay(RestrictedTraceID traceID);
ANGLE_TRACE_EXPORT void SetupReplay(RestrictedTraceID traceID);
ANGLE_TRACE_EXPORT void SetBinaryDataDir(RestrictedTraceID traceID, const char *dataDir);
ANGLE_TRACE_EXPORT void SetBinaryDataDecompressCallback(RestrictedTraceID traceID, DecompressCallback callback);
}} // namespace angle
#endif // ANGLE_RESTRICTED_TRACES_H_
"""
source_template = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// {filename}: Types and enumerations for trace tests.
#include "{filename}.h"
#include "common/PackedEnums.h"
{trace_includes}
namespace angle
{{
namespace
{{
constexpr angle::PackedEnumMap<RestrictedTraceID, TraceInfo> kTraceInfos = {{
{trace_infos}
}};
}}
const TraceInfo &GetTraceInfo(RestrictedTraceID traceID)
{{
return kTraceInfos[traceID];
}}
void ReplayFrame(RestrictedTraceID traceID, uint32_t frameIndex)
{{
switch (traceID)
{{
{replay_func_cases}
default:
fprintf(stderr, "Error in switch.\\n");
assert(0);
break;
}}
}}
void ResetReplay(RestrictedTraceID traceID)
{{
switch (traceID)
{{
{reset_func_cases}
default:
fprintf(stderr, "Error in switch.\\n");
assert(0);
break;
}}
}}
void SetupReplay(RestrictedTraceID traceID)
{{
switch (traceID)
{{
{setup_func_cases}
default:
fprintf(stderr, "Error in switch.\\n");
assert(0);
break;
}}
}}
void SetBinaryDataDir(RestrictedTraceID traceID, const char *dataDir)
{{
switch (traceID)
{{
{set_binary_data_dir_cases}
default:
fprintf(stderr, "Error in switch.\\n");
assert(0);
break;
}}
}}
void SetBinaryDataDecompressCallback(RestrictedTraceID traceID, DecompressCallback callback)
{{
switch (traceID)
{{
{decompress_callback_cases}
default:
fprintf(stderr, "Error in switch.\\n");
assert(0);
break;
}}
}}
}} // namespace angle
"""
def reject_duplicate_keys(pairs):
found_keys = {}
for key, value in pairs:
if key in found_keys:
raise ValueError("duplicate key: %r" % (key,))
else:
found_keys[key] = value
return found_keys
def gen_gni(traces, gni_file, format_args):
format_args["test_list"] = ",\n".join(
['"%s %s"' % (trace, get_context(trace)) for trace in traces])
gni_data = gni_template.format(**format_args)
with open(gni_file, "w") as out_file:
out_file.write(gni_data)
return True
def get_trace_info(trace):
info = [
"%s::kReplayFrameStart", "%s::kReplayFrameEnd", "%s::kReplayDrawSurfaceWidth",
"%s::kReplayDrawSurfaceHeight", "\"%s\""
]
return ", ".join([element % trace for element in info])
def get_context(trace):
"Returns the context number used by trace header file"
for file in os.listdir(trace):
# Load up the only header present for each trace
if fnmatch.fnmatch(file, '*.h'):
# Strip the extension to isolate the context
context = file[len(file) - 3]
assert context.isdigit() == True, "Failed to find trace context number"
assert file[len(file) - 4].isdigit() == False, "Context number is higher than 9"
return context
def get_cases(traces, function, args):
funcs = [
"case RestrictedTraceID::%s: %s::%s(%s); break;" % (trace, trace, function, args)
for trace in traces
]
return "\n".join(funcs)
def get_header_name(trace):
return "%s/%s_capture_context%s.h" % (trace, trace, get_context(trace))
def get_source_name(trace):
return "%s/%s_capture_context%s.cpp" % (trace, trace, get_context(trace))
def get_sha1_name(trace):
return "%s.tar.gz.sha1" % trace
def get_cases_with_context(traces, function_start, function_end, args):
funcs = [
"case RestrictedTraceID::%s: %s::%s%s%s(%s); break;" %
(trace, trace, function_start, get_context(trace), function_end, args) for trace in traces
]
return "\n".join(funcs)
def gen_header(header_file, format_args):
header_data = header_template.format(**format_args)
with open(header_file, "w") as out_file:
out_file.write(header_data)
return True
def gen_source(source_file, format_args):
source_data = source_template.format(**format_args)
with open(source_file, "w") as out_file:
out_file.write(source_data)
return True
def read_json(json_file):
with open(json_file) as map_file:
return json.loads(map_file.read(), object_pairs_hook=reject_duplicate_keys)
def main():
json_file = 'restricted_traces.json'
gni_file = 'restricted_traces_autogen.gni'
header_file = 'restricted_traces_autogen.h'
source_file = 'restricted_traces_autogen.cpp'
json_data = read_json(json_file)
if 'traces' not in json_data:
print('Trace data missing traces key.')
return 1
traces = json_data['traces']
# auto_script parameters.
if len(sys.argv) > 1:
inputs = [json_file] + [get_sha1_name(trace) for trace in traces]
outputs = [gni_file, header_file, source_file]
if sys.argv[1] == 'inputs':
print ','.join(inputs)
elif sys.argv[1] == 'outputs':
print ','.join(outputs)
else:
print('Invalid script parameters.')
return 1
return 0
format_args = {
"script_name": __file__,
"data_source_name": json_file,
}
if not gen_gni(traces, gni_file, format_args):
print('.gni file generation failed.')
return 1
includes = ["#include \"%s\"" % get_header_name(trace) for trace in traces]
trace_infos = [
"{RestrictedTraceID::%s, {%s}}" % (trace, get_trace_info(trace)) for trace in traces
]
format_args["filename"] = "restricted_traces_autogen"
format_args["trace_ids"] = ",\n".join(traces)
format_args["trace_includes"] = "\n".join(includes)
format_args["trace_infos"] = ",\n".join(trace_infos)
format_args["replay_func_cases"] = get_cases_with_context(traces, "ReplayContext", "Frame",
"frameIndex")
format_args["reset_func_cases"] = get_cases_with_context(traces, "ResetContext", "Replay", "")
format_args["setup_func_cases"] = get_cases_with_context(traces, "SetupContext", "Replay", "")
format_args["set_binary_data_dir_cases"] = get_cases(traces, "SetBinaryDataDir", "dataDir")
format_args["decompress_callback_cases"] = get_cases(traces, "SetBinaryDataDecompressCallback",
"callback")
if not gen_header(header_file, format_args):
print('.h file generation failed.')
return 1
if not gen_source(source_file, format_args):
print('.cpp file generation failed.')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())