1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:convert';
6import 'dart:typed_data';
7
8import 'message.dart';
9
10/// Format of data returned by screenshot driver command.
11enum ScreenshotFormat {
12 /// Raw RGBA format.
13 ///
14 /// Unencoded bytes, in RGBA row-primary form with premultiplied alpha, 8 bits per channel.
15 rawRgba,
16
17 /// Raw straight RGBA format.
18 ///
19 /// Unencoded bytes, in RGBA row-primary form with straight alpha, 8 bits per channel.
20 rawStraightRgba,
21
22 /// Raw unmodified format.
23 rawUnmodified,
24
25 /// Raw extended range RGBA format.
26 ///
27 /// Unencoded bytes, in RGBA row-primary form with straight alpha, 32 bit
28 /// float (IEEE 754 binary32) per channel.
29 rawExtendedRgba128,
30
31 /// PNG format.
32 png,
33}
34
35/// A Flutter Driver command that takes a screenshot.
36class ScreenshotCommand extends Command {
37 /// Constructs this command to take a screenshot.
38 ScreenshotCommand({super.timeout, this.format = ScreenshotFormat.png});
39
40 /// Deserializes this command from the value generated by [serialize].
41 ScreenshotCommand.deserialize(super.json)
42 : format = ScreenshotFormat.values[int.tryParse(json['format']!) ?? 4],
43 super.deserialize();
44
45 /// Whether the resulting data is PNG compressed.
46 final ScreenshotFormat format;
47
48 /// Serializes this command to parameter name/value pairs.
49 @override
50 Map<String, String> serialize() {
51 return super.serialize()..addAll(<String, String>{'format': format.index.toString()});
52 }
53
54 @override
55 String get kind => 'screenshot';
56}
57
58/// base64 encode a PNG
59class ScreenshotResult extends Result {
60 /// Constructs a screenshot result with PNG or raw RGBA byte data.
61 ScreenshotResult(this._data);
62
63 final Uint8List _data;
64
65 @override
66 Map<String, Object?> toJson() {
67 return <String, Object?>{'data': base64.encode(_data)};
68 }
69}
70