1 /*
2  * Copyright (C) 2015 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 com.android.test.uibench;
18 
19 import android.animation.ObjectAnimator;
20 import android.animation.ValueAnimator;
21 import android.graphics.SurfaceTexture;
22 import android.os.Bundle;
23 import androidx.appcompat.app.AppCompatActivity;
24 import android.util.DisplayMetrics;
25 import android.util.Log;
26 import android.view.Gravity;
27 import android.view.TextureView;
28 import android.view.ViewGroup;
29 import android.widget.FrameLayout;
30 
31 import com.android.test.uibench.opengl.ImageFlipRenderThread;
32 
33 public class GlTextureViewActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
34     private ImageFlipRenderThread mRenderThread;
35     private TextureView mTextureView;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     protected void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40 
41         mTextureView = new TextureView(this);
42         mTextureView.setSurfaceTextureListener(this);
43         setContentView(mTextureView, new FrameLayout.LayoutParams(
44                 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
45                 Gravity.CENTER));
46     }
47 
48     @Override
onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)49     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
50         mRenderThread = new ImageFlipRenderThread(getResources(), surface);
51         mRenderThread.start();
52 
53         DisplayMetrics metrics = mTextureView.getContext().getResources().getDisplayMetrics();
54         int distance = Math.max(mTextureView.getWidth(), mTextureView.getHeight());
55         mTextureView.setCameraDistance(distance * metrics.density);
56 
57         ObjectAnimator animator = ObjectAnimator.ofFloat(mTextureView, "rotationY", 0.0f, 360.0f);
58         animator.setRepeatMode(ObjectAnimator.REVERSE);
59         animator.setRepeatCount(ObjectAnimator.INFINITE);
60         animator.setDuration(4000);
61         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
62             @Override
63             public void onAnimationUpdate(ValueAnimator animation) {
64                 mTextureView.invalidate();
65             }
66         });
67         animator.start();
68     }
69 
70     @Override
onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)71     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
72     }
73 
74     @Override
onSurfaceTextureDestroyed(SurfaceTexture surface)75     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
76         mRenderThread.finish();
77         try {
78             mRenderThread.join();
79         } catch (InterruptedException e) {
80             Log.e(ImageFlipRenderThread.LOG_TAG, "Could not wait for render thread");
81         }
82         return true;
83     }
84 
85     @Override
onSurfaceTextureUpdated(SurfaceTexture surface)86     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
87     }
88 
89 }