From 9d022ed26d405d55b9b1d7252153862acb05723e Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 7 Apr 2016 12:33:11 -0700 Subject: [PATCH] Add a command that can capture a Skia picture of a Flutter frame and send it to a file or skiaserve debugger (#3165) --- packages/flutter_tools/lib/executable.dart | 2 + .../lib/src/android/android_device.dart | 35 +++++---- .../flutter_tools/lib/src/base/common.dart | 1 + .../flutter_tools/lib/src/commands/drive.dart | 2 +- .../flutter_tools/lib/src/commands/run.dart | 2 +- .../flutter_tools/lib/src/commands/skia.dart | 72 +++++++++++++++++++ packages/flutter_tools/lib/src/device.dart | 3 +- .../flutter_tools/lib/src/ios/devices.dart | 3 +- .../flutter_tools/lib/src/ios/simulators.dart | 9 +-- .../lib/src/service_protocol.dart | 13 ++-- packages/flutter_tools/pubspec.yaml | 1 + .../test/service_protocol_test.dart | 2 +- 12 files changed, 120 insertions(+), 25 deletions(-) create mode 100644 packages/flutter_tools/lib/src/commands/skia.dart diff --git a/packages/flutter_tools/lib/executable.dart b/packages/flutter_tools/lib/executable.dart index 075f2d1944..bed42cff82 100644 --- a/packages/flutter_tools/lib/executable.dart +++ b/packages/flutter_tools/lib/executable.dart @@ -27,6 +27,7 @@ import 'src/commands/refresh.dart'; import 'src/commands/run.dart'; import 'src/commands/run_mojo.dart'; import 'src/commands/screenshot.dart'; +import 'src/commands/skia.dart'; import 'src/commands/stop.dart'; import 'src/commands/test.dart'; import 'src/commands/trace.dart'; @@ -66,6 +67,7 @@ Future main(List args) async { ..addCommand(new RunCommand()) ..addCommand(new RunMojoCommand(hidden: !verboseHelp)) ..addCommand(new ScreenshotCommand()) + ..addCommand(new SkiaCommand()) ..addCommand(new StopCommand()) ..addCommand(new TestCommand()) ..addCommand(new TraceCommand()) diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index 96543978c5..dd758ba9da 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -186,7 +186,7 @@ class AndroidDevice extends Device { return true; } - Future _forwardObservatoryPort(int devicePort, int port) async { + Future _forwardPort(String service, int devicePort, int port) async { bool portWasZero = (port == null) || (port == 0); try { @@ -194,9 +194,9 @@ class AndroidDevice extends Device { port = await portForwarder.forward(devicePort, hostPort: port); if (portWasZero) - printStatus('Observatory listening on http://127.0.0.1:$port'); + printStatus('$service listening on http://127.0.0.1:$port'); } catch (e) { - printError('Unable to forward Observatory port $port: $e'); + printError('Unable to forward port $port: $e'); } } @@ -206,7 +206,8 @@ class AndroidDevice extends Device { String route, bool clearLogs: false, bool startPaused: false, - int debugPort: observatoryDefaultPort + int observatoryPort: observatoryDefaultPort, + int diagnosticPort: diagnosticDefaultPort }) async { printTrace('$this startBundle'); @@ -220,12 +221,15 @@ class AndroidDevice extends Device { runCheckedSync(adbCommandForDevice(['push', bundlePath, _deviceBundlePath])); - ServiceProtocolDiscovery serviceProtocolDiscovery = - new ServiceProtocolDiscovery(logReader); + ServiceProtocolDiscovery observatoryDiscovery = + new ServiceProtocolDiscovery(logReader, ServiceProtocolDiscovery.kObservatoryService); + ServiceProtocolDiscovery diagnosticDiscovery = + new ServiceProtocolDiscovery(logReader, ServiceProtocolDiscovery.kDiagnosticService); // We take this future here but do not wait for completion until *after* we // start the bundle. - Future scrapeServicePort = serviceProtocolDiscovery.nextPort(); + Future> scrapeServicePorts = Future.wait( + >[observatoryDiscovery.nextPort(), diagnosticDiscovery.nextPort()]); List cmd = adbCommandForDevice([ 'shell', 'am', 'start', @@ -255,9 +259,14 @@ class AndroidDevice extends Device { printTrace('Waiting for observatory port to be available...'); try { - int devicePort = await scrapeServicePort.timeout(new Duration(seconds: 12)); - printTrace('service protocol port = $devicePort'); - await _forwardObservatoryPort(devicePort, debugPort); + List devicePorts = await scrapeServicePorts.timeout(new Duration(seconds: 12)); + int observatoryDevicePort = devicePorts[0]; + int diagnosticDevicePort = devicePorts[1]; + printTrace('observatory port = $observatoryDevicePort'); + await _forwardPort(ServiceProtocolDiscovery.kObservatoryService, + observatoryDevicePort, observatoryPort); + await _forwardPort(ServiceProtocolDiscovery.kDiagnosticService, + diagnosticDevicePort, diagnosticPort); return true; } catch (error) { if (error is TimeoutException) @@ -278,7 +287,8 @@ class AndroidDevice extends Device { bool checked: true, bool clearLogs: false, bool startPaused: false, - int debugPort: observatoryDefaultPort, + int observatoryPort: observatoryDefaultPort, + int diagnosticPort: diagnosticDefaultPort, Map platformArgs }) async { if (!_checkForSupportedAdbVersion() || !_checkForSupportedAndroidVersion()) @@ -300,7 +310,8 @@ class AndroidDevice extends Device { route: route, clearLogs: clearLogs, startPaused: startPaused, - debugPort: debugPort + observatoryPort: observatoryPort, + diagnosticPort: diagnosticPort )) { return true; } else { diff --git a/packages/flutter_tools/lib/src/base/common.dart b/packages/flutter_tools/lib/src/base/common.dart index 9da0ec0d53..4481953a69 100644 --- a/packages/flutter_tools/lib/src/base/common.dart +++ b/packages/flutter_tools/lib/src/base/common.dart @@ -3,3 +3,4 @@ // found in the LICENSE file. const int observatoryDefaultPort = 8181; +const int diagnosticDefaultPort = 8182; diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index 142f410835..23809a5133 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -268,7 +268,7 @@ Future startApp(DriveCommand command) async { checked: command.checked, clearLogs: true, startPaused: true, - debugPort: command.debugPort, + observatoryPort: command.debugPort, platformArgs: { 'trace-startup': command.traceStartup, } diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 3d4699b348..88ddcb7b5e 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -221,7 +221,7 @@ Future startApp( checked: checked, clearLogs: clearLogs, startPaused: startPaused, - debugPort: debugPort, + observatoryPort: debugPort, platformArgs: platformArgs ); diff --git a/packages/flutter_tools/lib/src/commands/skia.dart b/packages/flutter_tools/lib/src/commands/skia.dart new file mode 100644 index 0000000000..3aeba2d39d --- /dev/null +++ b/packages/flutter_tools/lib/src/commands/skia.dart @@ -0,0 +1,72 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'dart:io'; +import 'package:http/http.dart' as http; + +import '../base/common.dart'; +import '../globals.dart'; +import '../runner/flutter_command.dart'; + +class SkiaCommand extends FlutterCommand { + @override + final String name = 'skia'; + + @override + final String description = 'Retrieve the last frame rendered by a Flutter app as a Skia picture.'; + + SkiaCommand() { + argParser.addOption('output-file', help: 'Write the Skia picture file to this path.'); + argParser.addOption('skiaserve', help: 'Post the picture to a skiaserve debugger at this URL.'); + argParser.addOption('diagnostic-port', + defaultsTo: diagnosticDefaultPort.toString(), + help: 'Local port where the diagnostic server is listening.'); + } + + @override + Future runInProject() async { + File outputFile; + Uri skiaserveUri; + if (argResults['output-file'] != null) { + outputFile = new File(argResults['output-file']); + } else if (argResults['skiaserve'] != null) { + skiaserveUri = Uri.parse(argResults['skiaserve']); + } else { + printError('Must provide --output-file or --skiaserve'); + return 1; + } + + Uri skpUri = new Uri(scheme: 'http', host: '127.0.0.1', + port: int.parse(argResults['diagnostic-port']), + path: '/skp'); + + http.Request skpRequest = new http.Request('GET', skpUri); + http.StreamedResponse skpResponse = await skpRequest.send(); + if (skpResponse.statusCode != HttpStatus.OK) { + String error = await skpResponse.stream.toStringStream().join(); + printError('Error: $error'); + return 1; + } + + if (outputFile != null) { + IOSink sink = outputFile.openWrite(); + await sink.addStream(skpResponse.stream); + await sink.close(); + } else if (skiaserveUri != null) { + Uri postUri = new Uri.http(skiaserveUri.authority, '/new'); + http.MultipartRequest postRequest = new http.MultipartRequest('POST', postUri); + postRequest.files.add(new http.MultipartFile( + 'file', skpResponse.stream, skpResponse.contentLength)); + + http.StreamedResponse postResponse = await postRequest.send(); + if (postResponse.statusCode != HttpStatus.OK) { + printError('Failed to post Skia picture to skiaserve'); + return 1; + } + } + + return 0; + } +} diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index eaf236213b..637e005b63 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -185,7 +185,8 @@ abstract class Device { bool checked: true, bool clearLogs: false, bool startPaused: false, - int debugPort: observatoryDefaultPort, + int observatoryPort: observatoryDefaultPort, + int diagnosticPort: diagnosticDefaultPort, Map platformArgs }); diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 6d676ca7a4..f15cad6407 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -162,7 +162,8 @@ class IOSDevice extends Device { bool checked: true, bool clearLogs: false, bool startPaused: false, - int debugPort: observatoryDefaultPort, + int observatoryPort: observatoryDefaultPort, + int diagnosticPort: diagnosticDefaultPort, Map platformArgs }) async { // TODO(chinmaygarde): Use checked, mainPath, route, clearLogs. diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 7d28f4eaf0..afe477d664 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -445,7 +445,8 @@ class IOSSimulator extends Device { bool checked: true, bool clearLogs: false, bool startPaused: false, - int debugPort: observatoryDefaultPort, + int observatoryPort: observatoryDefaultPort, + int diagnosticPort: diagnosticDefaultPort, Map platformArgs }) async { printTrace('Building ${app.name} for $id.'); @@ -457,7 +458,7 @@ class IOSSimulator extends Device { return false; ServiceProtocolDiscovery serviceProtocolDiscovery = - new ServiceProtocolDiscovery(logReader); + new ServiceProtocolDiscovery(logReader, ServiceProtocolDiscovery.kObservatoryService); // We take this future here but do not wait for completion until *after* we // start the application. @@ -476,8 +477,8 @@ class IOSSimulator extends Device { if (startPaused) args.add("--start-paused"); - if (debugPort != observatoryDefaultPort) - args.add("--observatory-port=$debugPort"); + if (observatoryPort != observatoryDefaultPort) + args.add("--observatory-port=$observatoryPort"); // Launch the updated application in the simulator. try { diff --git a/packages/flutter_tools/lib/src/service_protocol.dart b/packages/flutter_tools/lib/src/service_protocol.dart index 5df2ebab90..7800dc4aca 100644 --- a/packages/flutter_tools/lib/src/service_protocol.dart +++ b/packages/flutter_tools/lib/src/service_protocol.dart @@ -8,9 +8,13 @@ import 'device.dart'; /// Discover service protocol ports on devices. class ServiceProtocolDiscovery { - /// [logReader] A [DeviceLogReader] to look for Observatory messages in. - ServiceProtocolDiscovery(DeviceLogReader logReader) - : _logReader = logReader { + static const String kObservatoryService = 'Observatory'; + static const String kDiagnosticService = 'Diagnostic server'; + + /// [logReader] A [DeviceLogReader] to look for service messages in. + ServiceProtocolDiscovery(DeviceLogReader logReader, String serviceName) + : _logReader = logReader, + _serviceName = serviceName { assert(_logReader != null); if (!_logReader.isReading) _logReader.start(); @@ -19,6 +23,7 @@ class ServiceProtocolDiscovery { } final DeviceLogReader _logReader; + final String _serviceName; Completer _completer = new Completer(); /// The [Future] returned by this function will complete when the next service @@ -27,7 +32,7 @@ class ServiceProtocolDiscovery { void _onLine(String line) { int portNumber = 0; - if (line.contains('Observatory listening on http://')) { + if (line.contains('$_serviceName listening on http://')) { try { RegExp portExp = new RegExp(r"\d+.\d+.\d+.\d+:(\d+)"); String port = portExp.firstMatch(line).group(1); diff --git a/packages/flutter_tools/pubspec.yaml b/packages/flutter_tools/pubspec.yaml index cb83e6e19f..292085adf3 100644 --- a/packages/flutter_tools/pubspec.yaml +++ b/packages/flutter_tools/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: args: ^0.13.4 crypto: 0.9.2 file: ^0.1.0 + http: ^0.11.3 json_schema: ^1.0.3 mustache4dart: ^1.0.0 package_config: ^0.1.3 diff --git a/packages/flutter_tools/test/service_protocol_test.dart b/packages/flutter_tools/test/service_protocol_test.dart index 621266be0a..f163103b73 100644 --- a/packages/flutter_tools/test/service_protocol_test.dart +++ b/packages/flutter_tools/test/service_protocol_test.dart @@ -14,7 +14,7 @@ void main() { test('Discovery Heartbeat', () async { MockDeviceLogReader logReader = new MockDeviceLogReader(); ServiceProtocolDiscovery discoverer = - new ServiceProtocolDiscovery(logReader); + new ServiceProtocolDiscovery(logReader, ServiceProtocolDiscovery.kObservatoryService); // Get next port future. Future nextPort = discoverer.nextPort(); expect(nextPort, isNotNull);