SurfaceUtils.java revision a7677722304670dc07feef242156b97e6bb51bcd
1/* 2 * Copyright 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 17package android.hardware.camera2.utils; 18 19import android.hardware.camera2.legacy.LegacyCameraDevice; 20import android.hardware.camera2.legacy.LegacyExceptionUtils.BufferQueueAbandonedException; 21import android.util.Size; 22import android.view.Surface; 23 24/** 25 * Various Surface utilities. 26 */ 27public class SurfaceUtils { 28 29 /** 30 * Check if a surface is for preview consumer. 31 * 32 * @param surface The surface to be checked. 33 * @return true if the surface is for preview consumer, false otherwise. 34 */ 35 public static boolean isSurfaceForPreview(Surface surface) { 36 return LegacyCameraDevice.isPreviewConsumer(surface); 37 } 38 39 /** 40 * Check if the surface is for hardware video encoder consumer. 41 * 42 * @param surface The surface to be checked. 43 * @return true if the surface is for hardware video encoder consumer, false otherwise. 44 */ 45 public static boolean isSurfaceForHwVideoEncoder(Surface surface) { 46 return LegacyCameraDevice.isVideoEncoderConsumer(surface); 47 } 48 49 /** 50 * Get the Surface size. 51 * 52 * @param surface The surface to be queried for size. 53 * @return Size of the surface. 54 * 55 * @throw IllegalArgumentException if the surface is already abandoned. 56 */ 57 public static Size getSurfaceSize(Surface surface) { 58 try { 59 return LegacyCameraDevice.getSurfaceSize(surface); 60 } catch (BufferQueueAbandonedException e) { 61 throw new IllegalArgumentException("Surface was abandoned", e); 62 } 63 } 64 65 /** 66 * Get the Surface format. 67 * 68 * @param surface The surface to be queried for format. 69 * @return format of the surface. 70 * 71 * @throw IllegalArgumentException if the surface is already abandoned. 72 */ 73 public static int getSurfaceFormat(Surface surface) { 74 try { 75 return LegacyCameraDevice.detectSurfaceType(surface); 76 } catch (BufferQueueAbandonedException e) { 77 throw new IllegalArgumentException("Surface was abandoned", e); 78 } 79 } 80} 81