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