1 /* 2 * Copyright (C) 2020 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 com.android.systemui; 18 19 import android.util.Pools; 20 import android.view.SurfaceControl; 21 22 import javax.inject.Inject; 23 import javax.inject.Singleton; 24 25 /** 26 * Provides a synchronized pool of {@link SurfaceControl.Transaction}s to minimize allocations. 27 */ 28 @Singleton 29 public class TransactionPool { 30 private final Pools.SynchronizedPool<SurfaceControl.Transaction> mTransactionPool = 31 new Pools.SynchronizedPool<>(4); 32 33 @Inject TransactionPool()34 TransactionPool() { 35 } 36 37 /** Gets a transaction from the pool. */ acquire()38 public SurfaceControl.Transaction acquire() { 39 SurfaceControl.Transaction t = mTransactionPool.acquire(); 40 if (t == null) { 41 return new SurfaceControl.Transaction(); 42 } 43 return t; 44 } 45 46 /** 47 * Return a transaction to the pool. DO NOT call {@link SurfaceControl.Transaction#close()} if 48 * returning to pool. 49 */ release(SurfaceControl.Transaction t)50 public void release(SurfaceControl.Transaction t) { 51 if (!mTransactionPool.release(t)) { 52 t.close(); 53 } 54 } 55 } 56