1 /*
2  * Copyright (C) 2020 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.mediav2.cts;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.os.SystemClock;
22 import android.util.DisplayMetrics;
23 import android.util.Log;
24 import android.view.Surface;
25 import android.view.SurfaceHolder;
26 import android.view.SurfaceView;
27 import android.view.ViewGroup;
28 import android.view.WindowManager;
29 
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.locks.Condition;
32 import java.util.concurrent.locks.Lock;
33 import java.util.concurrent.locks.ReentrantLock;
34 
35 public class CodecTestActivity extends Activity implements SurfaceHolder.Callback {
36     private static final String LOG_TAG = CodecTestActivity.class.getSimpleName();
37     private SurfaceView mSurfaceView;
38     private SurfaceHolder mHolder;
39     private Surface mSurface;
40     private final Lock mLock = new ReentrantLock();
41     private final Condition mCondition = mLock.newCondition();
42 
43     @Override
onCreate(Bundle savedInstanceState)44     protected void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46 
47         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
48         setTurnScreenOn(true);
49         setShowWhenLocked(true);
50 
51         setContentView(R.layout.media_decoder_surface_layout);
52         mSurfaceView = findViewById(R.id.surface);
53         mHolder = mSurfaceView.getHolder();
54         mHolder.addCallback(this);
55     }
56 
57     @Override
surfaceCreated(SurfaceHolder holder)58     public void surfaceCreated(SurfaceHolder holder) {
59         Log.v(LOG_TAG, "surface created");
60         mLock.lock();
61         mSurface = mHolder.getSurface();
62         mLock.unlock();
63     }
64 
65     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)66     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
67         Log.v(LOG_TAG, "surface changed " + format + " " + width + " " + height);
68     }
69 
70     @Override
surfaceDestroyed(SurfaceHolder holder)71     public void surfaceDestroyed(SurfaceHolder holder) {
72         Log.v(LOG_TAG, "surface deleted");
73         mLock.lock();
74         mSurface = null;
75         mLock.unlock();
76     }
77 
waitTillSurfaceIsCreated()78     public void waitTillSurfaceIsCreated() throws InterruptedException {
79         final long mWaitTimeMs = 1000;
80         final int retries = 3;
81         mLock.lock();
82         final long start = SystemClock.elapsedRealtime();
83         while ((SystemClock.elapsedRealtime() - start) < (retries * mWaitTimeMs) &&
84                 mSurface == null) {
85             mCondition.await(mWaitTimeMs, TimeUnit.MILLISECONDS);
86         }
87         mLock.unlock();
88         if (mSurface == null) {
89             throw new InterruptedException("Taking too long to attach a SurfaceView to a window.");
90         }
91     }
92 
getSurface()93     public Surface getSurface() {
94         return mSurface;
95     }
96 
setScreenParams(int width, int height, boolean noStretch)97     public void setScreenParams(int width, int height, boolean noStretch) {
98         ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
99         final DisplayMetrics dm = getResources().getDisplayMetrics();
100         if (noStretch && width <= dm.widthPixels && height <= dm.heightPixels) {
101             lp.width = width;
102             lp.height = height;
103         } else {
104             int a = dm.widthPixels * height / width;
105             if (a <= dm.heightPixels) {
106                 lp.width = dm.widthPixels;
107                 lp.height = a;
108             } else {
109                 lp.width = dm.heightPixels * width / height;
110                 lp.height = dm.heightPixels;
111             }
112         }
113         runOnUiThread(() -> mSurfaceView.setLayoutParams(lp));
114     }
115 }
116