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