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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
content / test / gpu / gpu_tests / overlay_support.py [blame]
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Helper module for shared code related to video overlays."""
import collections
import enum
import functools
import json
import logging
from typing import Any, Dict, Iterable, List, Optional, Union
import dataclasses # Built-in, but pylint gives an ordering false positive
from gpu_tests import common_typing as ct
from gpu_tests import constants
from gpu_tests import gpu_helper
from telemetry.internal.platform import gpu_device
# These can be changed to enum.StrEnum once Python 3.11+ is used.
class OverlaySupport:
SOFTWARE = 'SOFTWARE'
DIRECT = 'DIRECT'
SCALING = 'SCALING'
NONE = 'NONE'
HARDWARE_OVERLAY_MODES = [DIRECT, SCALING]
class PixelFormat:
NV12 = 'NV12'
YUY2 = 'YUY2'
BGRA8 = 'BGRA'
ALL_PIXEL_FORMATS = [NV12, YUY2, BGRA8]
class VideoRotation(enum.IntEnum):
UNROTATED = 0
ROT90 = 90
ROT180 = 180
ROT270 = 270
class PresentationMode:
COMPOSED = 'COMPOSED'
OVERLAY = 'OVERLAY'
NONE = 'NONE'
COMPOSITION_FAILURE = 'COMPOSITION_FAILURE'
GET_STATISTICS_FAILED = 'GET_STATISTICS_FAILED'
class ZeroCopyCodec(enum.Enum):
UNSPECIFIED = 0
H264 = 1
VP9 = 2
# Trace events used by Chrome corresponding to PresentationMode values.
class PresentationModeEvent(enum.IntEnum):
# Defined by Chromium for internal testing use.
GET_STATISTICS_FAILED = -1
# These match DXGI_FRAME_PRESENTATION_MODE
COMPOSED = 0
OVERLAY = 1
NONE = 2
COMPOSITION_FAILURE = 3
def PresentationModeEventToStr(
presentation_mode_event: Union[int, PresentationModeEvent]) -> str:
try:
event = PresentationModeEvent(presentation_mode_event)
return getattr(PresentationMode, event.name)
except ValueError:
# Hit when not a valid PresentationModeEvent value.
return f'{presentation_mode_event} (unknown)'
DriverConditional = collections.namedtuple('DriverConditional',
['operation', 'version'])
@dataclasses.dataclass
class ZeroCopyConfig:
supports_scaled_video: bool = False
supported_codecs: List[ZeroCopyCodec] = ct.EmptyList()
class GpuOverlayConfig:
"""Contains all the video overlay information for a single GPU."""
def __init__(self):
self.direct_composition = False
self.supports_overlays = False
self._driver_version: Optional[str] = None
self._zero_copy_config = ZeroCopyConfig()
self._possible_overlay_support: Dict[str, str] = {}
self._driver_conditionals: Dict[str, Iterable[DriverConditional]] = {}
self._supported_rotations: Dict[str, List[VideoRotation]] = {}
self._force_composed_bgra8_driver_conditionals: Iterable[
DriverConditional] = []
for pixel_format in PixelFormat.ALL_PIXEL_FORMATS:
self._possible_overlay_support[pixel_format] = OverlaySupport.SOFTWARE
self._driver_conditionals[pixel_format] = []
self._supported_rotations[pixel_format] = [VideoRotation.UNROTATED]
def __eq__(self, other: Any):
if not isinstance(other, self.__class__):
return False
self_dict = self.__dict__
other_dict = other.__dict__
if set(self_dict.keys()) != set(other_dict.keys()):
return False
return all(self_dict[k] == other_dict[k] for k in self_dict)
def WithDirectComposition(self) -> 'GpuOverlayConfig':
"""Enables direct composition support via software."""
self.direct_composition = True
self.supports_overlays = True
for key in self._possible_overlay_support:
self._possible_overlay_support[key] = OverlaySupport.SOFTWARE
return self
def WithHardwareNV12Support(
self,
driver_conditionals: Optional[Iterable[DriverConditional]] = None,
supported_rotations: Optional[List[VideoRotation]] = None
) -> 'GpuOverlayConfig':
"""Enables NV12 hardware support.
Args:
driver_conditionals: An optional Iterable of DriverConditional instances
specifying when NV12 support is available. All conditionals must
evaluate to true for support to be available. If not set, all drivers
are assumed to be valid.
supported_rotations: An optional List of supported video rotations for
NV12 overlays. If not set, no video rotation support is assumed.
"""
self._WithHardwareSupport(PixelFormat.NV12, driver_conditionals,
supported_rotations)
return self
def WithHardwareYUY2Support(
self,
driver_conditionals: Optional[Iterable[DriverConditional]] = None,
supported_rotations: Optional[List[VideoRotation]] = None
) -> 'GpuOverlayConfig':
"""Enables YUY2 hardware support.
Args:
driver_conditionals: An optional Iterable of DriverConditional instances
specifying when YUY2 support is available. All conditionals must
evaluate to true for support to be available. If not set, all drivers
are assumed to be valid.
supported_rotations: An optional List of supported video rotations for
YUY2 overlays. If not set, no video rotation support is assumed.
"""
self._WithHardwareSupport(PixelFormat.YUY2, driver_conditionals,
supported_rotations)
return self
def WithHardwareBGRA8Support(
self,
driver_conditionals: Optional[Iterable[DriverConditional]] = None,
supported_rotations: Optional[List[VideoRotation]] = None
) -> 'GpuOverlayConfig':
"""Enables BGRA8 hardware support.
Args:
driver_conditionals: An optional Iterable of DriverConditional instances
specifying when BGRA8 support is available. All conditionals must
evaluate to true for support to be available. If not set, all drivers
are assumed to be valid.
supported_rotations: An optional List of supported video rotations for
BGRA8 overlays. If not set, no video rotation support is assumed.
"""
self._WithHardwareSupport(PixelFormat.BGRA8, driver_conditionals,
supported_rotations)
return self
def _WithHardwareSupport(
self,
pixel_format: str,
driver_conditionals: Optional[Iterable[DriverConditional]] = None,
supported_rotations: Optional[List[VideoRotation]] = None) -> None:
assert self.supports_overlays
assert (
self._possible_overlay_support[pixel_format] == OverlaySupport.SOFTWARE)
self._driver_conditionals[pixel_format] = driver_conditionals or []
self._supported_rotations[pixel_format].extend(supported_rotations or [])
self._possible_overlay_support[pixel_format] = OverlaySupport.SCALING
def WithForceComposedBGRA8(
self,
driver_conditionals: Iterable[DriverConditional]) -> 'GpuOverlayConfig':
"""Forces BGRA8 to use the COMPOSED presentation mode on certain drivers.
In some cases, Chrome can report that software BGRA8 support is available,
but BGRA8 presentation mode will still be COMPOSED instead of OVERLAY.
Args:
driver_conditionals: An Iterable of DriverConditional instances
specifying when BGRA8 should be forced to use the COMPOSED
presentation mode. Use of COMPOSED will be forced if any conditionals
evaluate to true.
"""
self._force_composed_bgra8_driver_conditionals = driver_conditionals
return self
def OnDriverVersion(self, driver_version: str) -> 'GpuOverlayConfig':
"""Specifies the driver version being used.
If called multiple times, the same |driver_version| must be used every time.
Args:
driver_version: A string containing the driver version in use.
"""
assert (self._driver_version is None
or self._driver_version == driver_version)
self._driver_version = driver_version
return self
def WithZeroCopyConfig(
self, zero_copy_config: ZeroCopyConfig) -> 'GpuOverlayConfig':
"""Sets the ZeroCopyConfig to use."""
self._zero_copy_config = zero_copy_config
return self
@functools.cached_property
def nv12_overlay_support(self) -> str:
return self._GetOverlaySupport(PixelFormat.NV12)
@functools.cached_property
def yuy2_overlay_support(self) -> str:
return self._GetOverlaySupport(PixelFormat.YUY2)
@functools.cached_property
def bgra8_overlay_support(self) -> str:
return self._GetOverlaySupport(PixelFormat.BGRA8)
def _GetOverlaySupport(self, pixel_format: str) -> str:
assert self._driver_version
if not self.supports_overlays:
return OverlaySupport.NONE
for dc in self._driver_conditionals[pixel_format]:
on_valid_driver = gpu_helper.EvaluateVersionComparison(
self._driver_version, dc.operation, dc.version)
if not on_valid_driver:
return OverlaySupport.SOFTWARE
return self._possible_overlay_support[pixel_format]
@functools.cached_property
def supports_hw_nv12_overlays(self) -> bool:
return self.nv12_overlay_support in OverlaySupport.HARDWARE_OVERLAY_MODES
@functools.cached_property
def supports_hw_yuy2_overlays(self) -> bool:
return self.yuy2_overlay_support in OverlaySupport.HARDWARE_OVERLAY_MODES
@functools.cached_property
def supports_hw_bgra8_overlays(self) -> bool:
return self.bgra8_overlay_support in OverlaySupport.HARDWARE_OVERLAY_MODES
@functools.cached_property
def bgra8_should_be_composed(self) -> bool:
assert self._driver_version
if self.bgra8_overlay_support == OverlaySupport.NONE:
return True
for dc in self._force_composed_bgra8_driver_conditionals:
on_invalid_driver = gpu_helper.EvaluateVersionComparison(
self._driver_version, dc.operation, dc.version)
if on_invalid_driver:
return True
return False
def GetExpectedPixelFormat(self, forced_pixel_format: Optional[str]) -> str:
"""Retrieves the pixel format that is expected to be used for swap chains.
Args:
forced_pixel_format: An optional string specifying the pixel format that
was forced in Chrome via browser arguments. If specified, should be
a PixelFormat value.
Returns:
A string containing the PixelFormat that is expected to be used.
"""
# If no specific pixel format was requested via browser arguments, we
# expect use of NV12 > YUY2 > BGRA8 based on available hardware support.
if forced_pixel_format is None:
if self.supports_hw_nv12_overlays:
return PixelFormat.NV12
if self.supports_hw_yuy2_overlays:
return PixelFormat.YUY2
return PixelFormat.BGRA8
assert forced_pixel_format in PixelFormat.ALL_PIXEL_FORMATS
# If a specific pixel format was requested via browser arguments that the
# browser does not support, we expect a direct fallback to BGRA8.
if (forced_pixel_format == PixelFormat.NV12
and not self.supports_hw_nv12_overlays):
return PixelFormat.BGRA8
if (forced_pixel_format == PixelFormat.YUY2
and not self.supports_hw_yuy2_overlays):
return PixelFormat.BGRA8
return forced_pixel_format
# pylint: disable=too-many-return-statements
def GetExpectedPresentationMode(self, expected_pixel_format: str,
video_rotation: VideoRotation) -> str:
"""Retrieves the presentation mode expected to be used for swap chains.
Args:
expected_pixel_format: A string containing the PixelFormat that is
expected to be used for swap chains. Should be the value returned by
GetExpectedPixelFormat().
video_rotation: The rotation of the video being played.
Returns:
A string containing the PresentationMode that is expected to be used.
"""
supported_rotations = self._supported_rotations[expected_pixel_format]
# We expect NV12/YUY2 to use overlays unless the given video rotation is
# not supported.
if expected_pixel_format == PixelFormat.NV12:
if video_rotation in supported_rotations:
return PresentationMode.OVERLAY
return PresentationMode.COMPOSED
if expected_pixel_format == PixelFormat.YUY2:
if video_rotation in supported_rotations:
return PresentationMode.OVERLAY
return PresentationMode.COMPOSED
# We expect BGRA8 to always use overlays, even without hardware support.
if expected_pixel_format == PixelFormat.BGRA8:
if self.bgra8_should_be_composed:
return PresentationMode.COMPOSED
return PresentationMode.OVERLAY
raise RuntimeError(
f'Pixel format {expected_pixel_format} is known, but does not have '
f'presentation mode logic added')
# pylint: enable=too-many-return-statements
def GetExpectedZeroCopyUsage(self, expected_pixel_format: str,
video_rotation: VideoRotation, fullsize: bool,
codec: ZeroCopyCodec) -> bool:
"""Determines whether the zero copy path is expected to be used or not.
Args:
expected_pixel_format: A string containing the PixelFormat that is
expected to be used for swap chains.
video_rotation: The rotation of the video being played.
fullsize: Whether the video being played is at full size/unscaled or not.
codec: The ZeroCopyCodec of the video being played. Can be UNSPECIFIED,
but will be treated as an error if the codec actually ends up being
needed to determine zero copy usage.
Returns:
True if the zero copy path is expected to be used, otherwise False.
"""
# Rotated videos necessitate a copy.
if video_rotation != VideoRotation.UNROTATED:
return False
# Zero copy path only enabled for NV12.
if expected_pixel_format != PixelFormat.NV12:
return False
# Certain GPUs (namely from Intel) do not support zero copy if the video
# is scaled at all.
if not fullsize and not self._zero_copy_config.supports_scaled_video:
return False
if codec == ZeroCopyCodec.UNSPECIFIED:
raise RuntimeError(
'Test did not specify the codec used when it is relevant to '
'determining zero copy usage')
return codec in self._zero_copy_config.supported_codecs
BasicDirectCompositionConfig = lambda: (GpuOverlayConfig().
WithDirectComposition())
AllHardwareSupportDirectCompositionConfig = lambda: (
BasicDirectCompositionConfig()\
.WithHardwareNV12Support()\
.WithHardwareYUY2Support()\
.WithHardwareBGRA8Support())
OVERLAY_CONFIGS = {
constants.GpuVendor.AMD: {
0x7340: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(supported_rotations=[
VideoRotation.ROT90,
VideoRotation.ROT180,
VideoRotation.ROT270])\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=True,
supported_codecs=[
ZeroCopyCodec.H264,
ZeroCopyCodec.VP9])),
0x6613:
BasicDirectCompositionConfig(),
0x699f:
BasicDirectCompositionConfig(),
},
constants.GpuVendor.INTEL: {
# Hardware overlays are disabled in 26.20.100.8141 per
# crbug.com/1079393#c105
0x5912: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(driver_conditionals=[
DriverConditional('ne', '26.20.100.8141')])\
.WithHardwareYUY2Support()\
.WithHardwareBGRA8Support()\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=False,
supported_codecs=[
ZeroCopyCodec.H264,
ZeroCopyCodec.VP9])),
0x3e92:
AllHardwareSupportDirectCompositionConfig(),
0x9bc5:
AllHardwareSupportDirectCompositionConfig(),
0x4680: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(supported_rotations=[
VideoRotation.ROT180])\
.WithHardwareYUY2Support()\
.WithHardwareBGRA8Support()\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=False,
supported_codecs=[
ZeroCopyCodec.H264])),
},
constants.GpuVendor.NVIDIA: {
# For some reason, software BGRA8 software overlay support changes
# based on driver version.
0x2184: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(driver_conditionals=[
DriverConditional('ge', '31.0.15.4601')])\
.WithHardwareYUY2Support(driver_conditionals=[
DriverConditional('ge', '31.0.15.4601')])
.WithForceComposedBGRA8(driver_conditionals=[
DriverConditional('lt', '31.0.15.4601')])\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=True,
supported_codecs=[
ZeroCopyCodec.H264])),
0x2783: BasicDirectCompositionConfig()\
.WithHardwareNV12Support()\
.WithHardwareYUY2Support()\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=True,
supported_codecs=[
ZeroCopyCodec.H264])),
},
constants.GpuVendor.QUALCOMM: {
0x41333430: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(supported_rotations=[
VideoRotation.ROT180])\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=True)),
0x36333630: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(supported_rotations=[
VideoRotation.ROT180])\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=True)),
0x36334330: BasicDirectCompositionConfig()\
.WithHardwareNV12Support(supported_rotations=[
VideoRotation.ROT180])\
.WithZeroCopyConfig(ZeroCopyConfig(
supports_scaled_video=True)),
},
}
def GetOverlayConfigForGpu(gpu: gpu_device.GPUDevice) -> GpuOverlayConfig:
"""Retrieves the GpuOverlayConfig instance for a particular GPU."""
overlay_config = OVERLAY_CONFIGS.get(gpu.vendor_id,
{}).get(gpu.device_id, None)
if not overlay_config:
raise RuntimeError(
f'GPU with vendor ID {gpu.vendor_id:#02x} and device ID '
f'{gpu.device_id:#02x} does not have an overlay config specified')
return overlay_config.OnDriverVersion(gpu.driver_version)
def ParseOverlayJsonFile(filepath: str) -> None:
"""Parses overlay configs from the specified JSON file and adds to the map.
JSON data must be in the following format:
{
# A string representing the vendor ID in hexadecimal, in this case Intel.
"0x8086": {
# A string representing the device ID in hexadecimal.
"0x1234": [
# An ordered list of functions to call on the GpuOverlayConfig.
{
# Equivalent to .WithDirectComposition()
"function": "WithDirectComposition"
},
{
# Equivalent to:
# .WithHardwareNV12Support(
# driver_conditionals=[DriverConditional('ge', '31.0.15.4601')])
"function": "WithHardwareNV12Support",
"args": {
"driver_conditionals": [
["ge", "31.0.15.4601"]
]
}
},
{
# Equivalent to:
# .WithHardwareYUY2Support(supported_rotations=[VideoRotation.ROT180])
"function": "WithHardwareYUY2Support",
"args": {
"supported_rotations": [
180
]
}
},
{
# Equivalent to:
# .WithZeroCopyConfig(ZeroCopyConfig(
# supports_scaled_video=True,
# supported_codecs=[ZeroCopyCodec.H264]))
"function": "WithZeroCopyConfig",
"args": {
"supports_scaled_video": true,
"supported_codecs": [
'H264'
]
}
},
{
# Equivalent to:
# .WithForceComposedBGRA8(
# driver_conditionals=[DriverConditional('ge', '31.0.15.4601')])
"function": "WithForceComposedBGRA8",
"args": {
"driver_conditionals": [
["ge", "31.0.15.4601"]
]
}
}
],
"0x2345": [
...
]
},
"0x1002": {
...
}
}
Args:
filepath: A string containing a filepath pointing to the JSON file to parse.
"""
with open(filepath, encoding='utf-8') as infile:
json_content = json.load(infile)
for vendor_str, device_map in json_content.items():
assert vendor_str.lower().startswith('0x')
vendor = int(vendor_str, 0)
vendor = constants.GpuVendor(vendor)
for device_str, function_list in device_map.items():
assert device_str.lower().startswith('0x')
device = int(device_str, 0)
_ParseOverlayJsonForDevice(vendor, device, function_list)
def _ParseOverlayJsonForDevice(vendor: constants.GpuVendor, device: int,
function_list: List[dict]) -> None:
"""Helper to parse overlay config JSON for a single device.
Args:
vendor: The GpuVendor value for the device's vendor.
device: An int representing the device's ID.
function_list: A list of dicts, each dict representing a function to call.
"""
if device in OVERLAY_CONFIGS.get(vendor, {}):
logging.warning(
'Config for vendor %#02x and device %#02x already exists, not applying '
'config from JSON file.', vendor, device)
return
overlay_config = GpuOverlayConfig()
for function_call in function_list:
function = function_call['function']
args = function_call.get('args', {})
# Convert any arguments that aren't built-in types before calling the
# provided function with the provided args.
_ConvertDriverConditionals(args)
_ConvertSupportedRotations(args)
_ConvertSupportedCodecs(args)
if function == 'WithZeroCopyConfig':
args = {'zero_copy_config': ZeroCopyConfig(**args)}
getattr(overlay_config, function)(**args)
OVERLAY_CONFIGS.setdefault(vendor, {})[device] = overlay_config
def _ConvertDriverConditionals(args: Dict[str, Any]) -> None:
"""Converts any driver_conditionals arguments in place."""
if 'driver_conditionals' not in args:
return
driver_conditionals = [
DriverConditional(*dc) for dc in args['driver_conditionals']
]
args['driver_conditionals'] = driver_conditionals
def _ConvertSupportedRotations(args: Dict[str, Any]) -> None:
"""Converts any supported_rotations arguments in place."""
if 'supported_rotations' not in args:
return
supported_rotations = [
VideoRotation(sr) for sr in args['supported_rotations']
]
args['supported_rotations'] = supported_rotations
def _ConvertSupportedCodecs(args: Dict[str, Any]) -> None:
"""Converts any supported_codecs arguments in place."""
if 'supported_codecs' not in args:
return
supported_codecs = [ZeroCopyCodec[sc] for sc in args['supported_codecs']]
args['supported_codecs'] = supported_codecs