Add integration test of textures (#13122)

This commit is contained in:
Mikkel Nygaard Ravn
2017-11-22 14:16:25 +01:00
committed by GitHub
parent 8ba08718b6
commit d06482cc79
31 changed files with 1361 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
GeneratedPluginRegistrant.java

View File

@@ -0,0 +1,49 @@
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withInputStream { stream ->
localProperties.load(stream)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "com.yourcompany.externalui"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
androidTestCompile 'com.android.support:support-annotations:25.4.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
}

View File

@@ -0,0 +1,21 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourcompany.externalui">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name="io.flutter.app.FlutterApplication"
android:label="external_ui">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,141 @@
// Copyright 2017 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.
package com.yourcompany.externalui;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.os.Bundle;
import android.view.Surface;
import android.view.SurfaceHolder;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.view.TextureRegistry;
import io.flutter.view.TextureRegistry.SurfaceTextureEntry;
public class MainActivity extends FlutterActivity {
private Surface surface;
private SurfaceTexture texture;
private Timer producerTimer;
private Timer consumerTimer;
private long startTime;
private long endTime;
private AtomicInteger framesProduced = new AtomicInteger(0);
private AtomicInteger framesConsumed = new AtomicInteger(0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final MethodChannel channel = new MethodChannel(getFlutterView(), "texture");
channel.setMethodCallHandler(new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, Result result) {
switch (methodCall.method) {
case "start":
framesProduced.set(0);
framesConsumed.set(0);
final int fps = methodCall.arguments();
final FrameRenderer renderer = new FrameRenderer(surface);
producerTimer = new Timer();
producerTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
renderer.drawFrame();
framesProduced.incrementAndGet();
}
}, 0, 1000 / fps);
consumerTimer = new Timer();
consumerTimer.scheduleAtFixedRate(new TimerTask() {
private long lastTimestamp = -1L;
@Override
public void run() {
final long timestamp = texture.getTimestamp();
// The texture's timestamp is updated on consumption.
// We detect the change by asking very frequently.
if (timestamp != lastTimestamp) {
lastTimestamp = timestamp;
framesConsumed.incrementAndGet();
}
}
}, 0, 1);
startTime = System.currentTimeMillis();
result.success(null);
break;
case "stop":
producerTimer.cancel();
consumerTimer.cancel();
endTime = System.currentTimeMillis();
result.success(null);
break;
case "getProducedFrameRate":
result.success(framesProduced.get() * 1000 / (double) (endTime - startTime));
break;
case "getConsumedFrameRate":
result.success(framesConsumed.get() * 1000 / (double) (endTime - startTime));
break;
default: result.notImplemented();
}
}
});
getFlutterView().getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
final SurfaceTextureEntry textureEntry = getFlutterView().createSurfaceTexture();
texture = textureEntry.surfaceTexture();
texture.setDefaultBufferSize(300, 200);
surface = new Surface(texture);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
}
@Override
protected void onDestroy() {
if (surface != null) {
surface.release();
}
super.onDestroy();
}
}
class FrameRenderer {
private final Surface surface;
private final Paint paint;
private int frameCount = 0;
FrameRenderer(Surface surface) {
this.surface = surface;
this.paint = new Paint();
paint.setColor(0xffff0000);
paint.setTextSize(48.0f);
paint.setAntiAlias(true);
}
void drawFrame() {
final Canvas canvas = surface.lockCanvas(null);
canvas.drawColor(0xff000000);
canvas.drawText(String.valueOf(++frameCount), 20, 120, paint);
surface.unlockCanvasAndPost(canvas);
}
}

View File

@@ -0,0 +1,31 @@
buildscript {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}

View File

@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1536M

View File

@@ -0,0 +1,6 @@
#Fri Jun 23 08:50:38 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

View File

@@ -0,0 +1,15 @@
include ':app'
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withInputStream { stream -> plugins.load(stream) }
}
plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}