Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2012 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 package android.view.cts;
     17 
     18 import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
     19 import static android.opengl.GLES20.glClear;
     20 import static android.opengl.GLES20.glClearColor;
     21 
     22 import android.animation.AnimatorSet;
     23 import android.animation.ObjectAnimator;
     24 import android.app.Activity;
     25 import android.graphics.SurfaceTexture;
     26 import android.os.Bundle;
     27 import android.view.Display;
     28 import android.view.TextureView;
     29 import android.view.WindowManager;
     30 
     31 import junit.framework.Assert;
     32 
     33 import java.util.concurrent.Semaphore;
     34 import java.util.concurrent.TimeUnit;
     35 
     36 public class TextureViewStressTestActivity extends Activity
     37         implements TextureView.SurfaceTextureListener {
     38     public static int mFrames = -1;
     39     public static int mDelayMs = -1;
     40 
     41     private Thread mProducerThread;
     42     private final Semaphore mSemaphore = new Semaphore(0);
     43 
     44     @Override
     45     public void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         Assert.assertTrue(mFrames > 0);
     48         Assert.assertTrue(mDelayMs > 0);
     49         TextureView texView = new TextureView(this);
     50         texView.setSurfaceTextureListener(this);
     51         setContentView(texView);
     52         ObjectAnimator rotate = ObjectAnimator.ofFloat(texView, "rotationY", 180);
     53         ObjectAnimator fadeIn = ObjectAnimator.ofFloat(texView, "alpha", 0.3f, 1f);
     54         ObjectAnimator scaleY = ObjectAnimator.ofFloat(texView, "scaleY", 0.3f, 1f);
     55         AnimatorSet animSet = new AnimatorSet();
     56         animSet.play(rotate).with(fadeIn).with(scaleY);
     57         animSet.setDuration(mFrames * mDelayMs);
     58         animSet.start();
     59     }
     60 
     61     private static int addMargin(int a) {
     62         /* Worst case is 2 * actual refresh rate, in case when the delay pushes the frame off
     63          * VSYNC every frame.
     64          */
     65         return 2 * a;
     66     }
     67 
     68     private static int roundUpFrame(int a, int b) {
     69         /* Need to give time based on (frame duration limited by refresh rate + delay given
     70          * from the test)
     71          */
     72         return (a + b + 1);
     73     }
     74 
     75 
     76     public Boolean waitForCompletion() {
     77         Boolean success = false;
     78         int oneframeMs;
     79 
     80         WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
     81         Display display = wm.getDefaultDisplay();
     82         float rate = display.getRefreshRate();
     83         oneframeMs = roundUpFrame(mDelayMs, (int) (1000.0f / rate));
     84         try {
     85             success = mSemaphore.tryAcquire(addMargin(oneframeMs * mFrames),
     86                     TimeUnit.MILLISECONDS);
     87         } catch (InterruptedException e) {
     88             Assert.fail();
     89         }
     90         return success;
     91     }
     92 
     93     @Override
     94     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
     95         mProducerThread = new GLProducerThread(surface, new GLRendererImpl(),
     96                 mFrames, mDelayMs, mSemaphore);
     97         mProducerThread.start();
     98     }
     99 
    100     @Override
    101     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    102     }
    103 
    104     @Override
    105     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    106         mProducerThread = null;
    107         return true;
    108     }
    109 
    110     @Override
    111     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    112     }
    113 
    114     public static class GLRendererImpl implements GLProducerThread.GLRenderer {
    115         private static final int NUM_COLORS = 4;
    116         private static final float[][] COLOR = {
    117                 { 1.0f, 0.0f, 0.0f },
    118                 { 0.0f, 1.0f, 0.0f },
    119                 { 0.0f, 0.0f, 1.0f },
    120                 { 1.0f, 1.0f, 1.0f }
    121         };
    122 
    123         @Override
    124         public void drawFrame(int frame) {
    125             int index = frame % NUM_COLORS;
    126             glClearColor(COLOR[index][0], COLOR[index][1], COLOR[index][2], 1.0f);
    127             glClear(GL_COLOR_BUFFER_BIT);
    128         }
    129     }
    130 }
    131