1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics.cts; 18 19 import android.app.Activity; 20 import android.opengl.GLSurfaceView; 21 import android.os.Bundle; 22 import android.view.Window; 23 24 import java.util.concurrent.CountDownLatch; 25 import java.util.concurrent.Semaphore; 26 import java.util.concurrent.TimeUnit; 27 28 import javax.microedition.khronos.egl.EGLConfig; 29 import javax.microedition.khronos.opengles.GL10; 30 31 /** 32 * An activity for testing camera output rendering. 33 */ 34 public class CameraGpuCtsActivity extends Activity { 35 36 static { 37 System.loadLibrary("ctsgraphics_jni"); 38 } 39 40 private static final String TAG = "CameraGpuCtsActivity"; 41 42 protected GLSurfaceView mView; 43 protected Semaphore mNativeRendererInitialized; 44 protected long mNativeRenderer; 45 private CountDownLatch mFinishedRendering; 46 47 private class Renderer implements GLSurfaceView.Renderer { onDrawFrame(GL10 gl)48 public void onDrawFrame(GL10 gl) { 49 if (nDrawFrame(mNativeRenderer) == 0) { 50 mFinishedRendering.countDown(); 51 } 52 } 53 onSurfaceChanged(GL10 gl, int width, int height)54 public void onSurfaceChanged(GL10 gl, int width, int height) { 55 // Do nothing. 56 } 57 onSurfaceCreated(GL10 gl, EGLConfig config)58 public void onSurfaceCreated(GL10 gl, EGLConfig config) { 59 mNativeRenderer = nCreateRenderer(); 60 mNativeRendererInitialized.release(); 61 } 62 } 63 64 @Override onCreate(Bundle savedInstanceState)65 public void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 mNativeRendererInitialized = new Semaphore(0); 68 mView = new GLSurfaceView(this); 69 mView.setEGLContextClientVersion(2); 70 mView.setRenderer(new Renderer()); 71 mView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); 72 73 // Wait for 100 frames from camera being rendered. 74 mFinishedRendering = new CountDownLatch(100); 75 76 requestWindowFeature(Window.FEATURE_NO_TITLE); 77 setContentView(mView); 78 } 79 80 @Override onResume()81 protected void onResume() { 82 super.onResume(); 83 mView.onResume(); 84 } 85 86 @Override onPause()87 protected void onPause() { 88 super.onPause(); 89 mView.onPause(); 90 } 91 92 @Override onDestroy()93 protected void onDestroy() { 94 super.onDestroy(); 95 nDestroyRenderer(mNativeRenderer); 96 } 97 waitToFinishRendering()98 public void waitToFinishRendering() throws InterruptedException { 99 // Wait for renderer initialization 100 mNativeRendererInitialized.acquire(); 101 // Skip the test if there is no camera on board. 102 if (!nIsCameraReady(mNativeRenderer)) { 103 return; 104 } 105 106 // Wait long enough so that all frames are captured. 107 if (!mFinishedRendering.await(30, TimeUnit.SECONDS)) { 108 throw new IllegalStateException("Coudn't finish drawing frames!"); 109 } 110 } 111 nCreateRenderer()112 private static native long nCreateRenderer(); nIsCameraReady(long renderer)113 private static native boolean nIsCameraReady(long renderer); nDestroyRenderer(long renderer)114 private static native void nDestroyRenderer(long renderer); nDrawFrame(long renderer)115 private static native int nDrawFrame(long renderer); 116 } 117