1 /* 2 * Copyright 2014 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.hardware.camera2.cts; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.ConditionVariable; 22 import android.os.SystemClock; 23 import android.util.Log; 24 import android.view.SurfaceHolder; 25 import android.view.SurfaceView; 26 import com.android.cts.hardware.R; 27 28 public class Camera2SurfaceViewCtsActivity extends Activity implements SurfaceHolder.Callback { 29 private final static String TAG = "Camera2SurfaceViewCtsActivity"; 30 private final ConditionVariable surfaceChangedDone = new ConditionVariable(); 31 32 private SurfaceView mSurfaceView; 33 private int currentWidth = 0; 34 private int currentHeight = 0; 35 private final Object sizeLock = new Object(); 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 setContentView(R.layout.surface_view_2); 42 mSurfaceView = (SurfaceView) findViewById(R.id.surface_view); 43 mSurfaceView.getHolder().addCallback(this); 44 } 45 getSurfaceView()46 public SurfaceView getSurfaceView() { 47 return mSurfaceView; 48 } 49 waitForSurfaceSizeChanged(int timeOutMs, int expectWidth, int expectHeight)50 public boolean waitForSurfaceSizeChanged(int timeOutMs, int expectWidth, int expectHeight) { 51 if (timeOutMs <= 0 || expectWidth <= 0 || expectHeight <= 0) { 52 throw new IllegalArgumentException( 53 String.format( 54 "timeout(%d), expectWidth(%d), and expectHeight(%d) " + 55 "should all be positive numbers", 56 timeOutMs, expectWidth, expectHeight)); 57 } 58 59 synchronized(sizeLock) { 60 if (expectWidth == currentWidth && expectHeight == currentHeight) { 61 return true; 62 } 63 } 64 65 int waitTimeMs = timeOutMs; 66 boolean changeSucceeded = false; 67 while (!changeSucceeded && waitTimeMs > 0) { 68 long startTimeMs = SystemClock.elapsedRealtime(); 69 changeSucceeded = surfaceChangedDone.block(waitTimeMs); 70 if (!changeSucceeded) { 71 Log.e(TAG, "Wait for surface change timed out after " + timeOutMs + " ms"); 72 return changeSucceeded; 73 } else { 74 // Get a surface change callback, need to check if the size is expected. 75 surfaceChangedDone.close(); 76 if (currentWidth == expectWidth && currentHeight == expectHeight) { 77 return changeSucceeded; 78 } 79 // Do a further iteration surface change check as surfaceChanged could be called 80 // again. 81 changeSucceeded = false; 82 } 83 waitTimeMs -= (SystemClock.elapsedRealtime() - startTimeMs); 84 } 85 86 // Couldn't get expected surface size change. 87 return false; 88 } 89 90 @Override surfaceCreated(SurfaceHolder holder)91 public void surfaceCreated(SurfaceHolder holder) { 92 } 93 94 @Override surfaceChanged(SurfaceHolder holder, int format, int width, int height)95 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 96 Log.i(TAG, "Surface Changed to: " + width + "x" + height); 97 synchronized (sizeLock) { 98 currentWidth = width; 99 currentHeight = height; 100 } 101 surfaceChangedDone.open(); 102 } 103 104 @Override surfaceDestroyed(SurfaceHolder holder)105 public void surfaceDestroyed(SurfaceHolder holder) { 106 } 107 } 108