Flutter Engine
The Flutter Engine
ScreenshotUtil.java
Go to the documentation of this file.
1// Copyright 2013 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
5package dev.flutter.scenariosui;
6
7import androidx.annotation.NonNull;
8import dev.flutter.scenarios.TestableFlutterActivity;
9import java.io.IOException;
10import java.io.InputStream;
11import java.io.OutputStream;
12import java.net.Socket;
13import java.nio.ByteBuffer;
14import java.util.concurrent.CountDownLatch;
15import java.util.concurrent.Executor;
16import java.util.concurrent.Executors;
17
18/**
19 * Allows to capture screenshots, and transfers the screenshots to the host where they can be
20 * further proccessed. On a LUCI environment, the screenshots are sent to Skia Gold.
21 */
22public class ScreenshotUtil {
23 private static final String HOST = "localhost";
24 private static final int PORT = 3000;
25
26 private static Connection conn;
27 private static Executor executor;
28
29 private static class Connection {
30 final Socket clientSocket;
31 final OutputStream out;
32 final InputStream in;
33
34 Connection(Socket socket) throws IOException {
35 clientSocket = socket;
36 out = socket.getOutputStream();
37 in = socket.getInputStream();
38 }
39
40 synchronized void writeFile(String name) throws IOException {
41 final ByteBuffer buffer = ByteBuffer.allocate(name.length() + 12);
42 // See ScreenshotBlobTransformer#bind in screenshot_transformer.dart for consumer side.
43 buffer.putInt(name.length());
44 buffer.putInt(0);
45 buffer.putInt(0);
46 buffer.put(name.getBytes());
47 final byte[] bytes = buffer.array();
48 out.write(bytes, 0, bytes.length);
49 out.flush();
50
51 // Wait on run_android_tests.dart to write a single byte into the socket
52 // as a signal that adb screencapture has completed.
53 in.read();
54 }
55
56 synchronized void close() throws IOException {
57 clientSocket.close();
58 }
59 }
60
61 /** Starts the connection with the host. */
62 public static synchronized void onCreate() {
63 if (executor == null) {
64 executor = Executors.newSingleThreadExecutor();
65 }
66 if (conn == null) {
67 executor.execute(
68 () -> {
69 try {
70 final Socket socket = new Socket(HOST, PORT);
71 conn = new Connection(socket);
72 } catch (IOException e) {
73 throw new RuntimeException(e);
74 }
75 });
76 }
77 }
78
79 /** Closes the connection with the host. */
80 public static synchronized void finish() {
81 if (executor != null && conn != null) {
82 executor.execute(
83 () -> {
84 try {
85 conn.close();
86 conn = null;
87 } catch (IOException e) {
88 throw new RuntimeException(e);
89 }
90 });
91 }
92 }
93
94 /**
95 * Sends the file to the host.
96 *
97 * @param filename The file name.
98 * @param fileContent The file content.
99 */
100 public static synchronized void writeFile(
101 @NonNull String filename, @NonNull CountDownLatch latch) {
102 if (executor != null && conn != null) {
103 executor.execute(
104 () -> {
105 try {
106 conn.writeFile(filename);
107 } catch (IOException e) {
108 throw new RuntimeException(e);
109 } finally {
110 latch.countDown();
111 }
112 });
113 }
114 }
115
116 /**
117 * Captures a screenshot of the activity, and sends the screenshot bytes to the host where it is
118 * further processed.
119 *
120 * <p>The activity must be already launched.
121 *
122 * @param activity The target activity.
123 * @param fileName The name of the file.
124 */
125 public static void capture(@NonNull TestableFlutterActivity activity, @NonNull String captureName)
126 throws Exception {
127 CountDownLatch latch = new CountDownLatch(1);
128 activity.waitUntilFlutterRendered();
129 ScreenshotUtil.writeFile(captureName, latch);
130 latch.await();
131 }
132}
static synchronized void finish()
static synchronized void onCreate()
static void capture(@NonNull TestableFlutterActivity activity, @NonNull String captureName)
static synchronized void writeFile( @NonNull String filename, @NonNull CountDownLatch latch)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126