SurfaceUtils.java revision e365120aaead97567bdfbc53d3bfc2699bd2f886
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.graphics.ImageFormat; 20import android.hardware.camera2.legacy.LegacyCameraDevice; 21import android.hardware.camera2.legacy.LegacyExceptionUtils.BufferQueueAbandonedException; 22import android.util.Size; 23import android.view.Surface; 24 25/** 26 * Various Surface utilities. 27 */ 28public class SurfaceUtils { 29 30 /** 31 * Check if a surface is for preview consumer based on consumer end point Gralloc usage flags. 32 * 33 * @param surface The surface to be checked. 34 * @return true if the surface is for preview consumer, false otherwise. 35 */ 36 public static boolean isSurfaceForPreview(Surface surface) { 37 return LegacyCameraDevice.isPreviewConsumer(surface); 38 } 39 40 /** 41 * Check if the surface is for hardware video encoder consumer based on consumer end point 42 * Gralloc usage flags. 43 * 44 * @param surface The surface to be checked. 45 * @return true if the surface is for hardware video encoder consumer, false otherwise. 46 */ 47 public static boolean isSurfaceForHwVideoEncoder(Surface surface) { 48 return LegacyCameraDevice.isVideoEncoderConsumer(surface); 49 } 50 51 /** 52 * Get the Surface size. 53 * 54 * @param surface The surface to be queried for size. 55 * @return Size of the surface. 56 * 57 * @throws IllegalArgumentException if the surface is already abandoned. 58 */ 59 public static Size getSurfaceSize(Surface surface) { 60 try { 61 return LegacyCameraDevice.getSurfaceSize(surface); 62 } catch (BufferQueueAbandonedException e) { 63 throw new IllegalArgumentException("Surface was abandoned", e); 64 } 65 } 66 67 /** 68 * Get the Surface format. 69 * 70 * @param surface The surface to be queried for format. 71 * @return format of the surface. 72 * 73 * @throws IllegalArgumentException if the surface is already abandoned. 74 */ 75 public static int getSurfaceFormat(Surface surface) { 76 try { 77 return LegacyCameraDevice.detectSurfaceType(surface); 78 } catch (BufferQueueAbandonedException e) { 79 throw new IllegalArgumentException("Surface was abandoned", e); 80 } 81 } 82 83 /** 84 * Get the Surface dataspace. 85 * 86 * @param surface The surface to be queried for dataspace. 87 * @return dataspace of the surface. 88 * 89 * @throws IllegalArgumentException if the surface is already abandoned. 90 */ 91 public static int getSurfaceDataspace(Surface surface) { 92 try { 93 return LegacyCameraDevice.detectSurfaceDataspace(surface); 94 } catch (BufferQueueAbandonedException e) { 95 throw new IllegalArgumentException("Surface was abandoned", e); 96 } 97 } 98 99 /** 100 * Return true is the consumer is one of the consumers that can accept 101 * producer overrides of the default dimensions and format. 102 * 103 */ 104 public static boolean isFlexibleConsumer(Surface output) { 105 return LegacyCameraDevice.isFlexibleConsumer(output); 106 } 107 108} 109