1 /* 2 * Copyright 2023 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 androidx.camera.camera2.pipe.testing 18 19 import android.graphics.SurfaceTexture 20 import android.util.Size 21 import android.view.Surface 22 import kotlinx.atomicfu.atomic 23 24 /** 25 * Utility class for creating fake Surface objects for tests. 26 * 27 * Close this object to release all surfaces during tests. 28 */ 29 public class FakeSurfaces : AutoCloseable { 30 private val fakeSurfaces = mutableListOf<Surface>() 31 createFakeSurfacenull32 public fun createFakeSurface(size: Size = Size(640, 480)): Surface { 33 val surface = create(size) 34 synchronized(fakeSurfaces) { fakeSurfaces.add(surface) } 35 return surface 36 } 37 closenull38 override fun close() { 39 synchronized(fakeSurfaces) { 40 for (surface in fakeSurfaces) { 41 surface.release() 42 } 43 fakeSurfaces.clear() 44 } 45 } 46 47 public companion object { 48 private val fakeSurfaceTextureNames = atomic(0) 49 createnull50 public fun create(size: Size = Size(640, 480)): Surface { 51 return Surface( 52 SurfaceTexture(fakeSurfaceTextureNames.getAndIncrement()).also { 53 it.setDefaultBufferSize(size.width, size.height) 54 } 55 ) 56 } 57 } 58 } 59