1 /* 2 * Copyright 2014 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 android.hardware.camera2.cts.rs; 18 19 import android.content.Context; 20 import android.renderscript.RenderScript; 21 import android.util.Log; 22 23 // TODO : Replace with dependency injection 24 /** 25 * Singleton to hold {@link RenderScript} and {@link AllocationCache} objects. 26 * 27 * <p>The test method must call {@link #setContext} before attempting to retrieve 28 * the underlying objects.</p> * 29 */ 30 public class RenderScriptSingleton { 31 32 private static final String TAG = "RenderScriptSingleton"; 33 34 private static Context sContext; 35 private static RenderScript sRS; 36 private static AllocationCache sCache; 37 38 /** 39 * Initialize the singletons from the given context; the 40 * {@link RenderScript} and {@link AllocationCache} objects are instantiated. 41 * 42 * @param context a non-{@code null} Context. 43 * 44 * @throws IllegalStateException If this was called repeatedly without {@link #clearContext} 45 */ setContext(Context context)46 public static synchronized void setContext(Context context) { 47 if (context.equals(sContext)) { 48 return; 49 } else if (sContext != null) { 50 Log.v(TAG, 51 "Trying to set new context " + context + 52 ", before clearing previous "+ sContext); 53 throw new IllegalStateException( 54 "Call #clearContext before trying to set a new context"); 55 } 56 57 sRS = RenderScript.create(context); 58 sContext = context; 59 sCache = new AllocationCache(sRS); 60 } 61 62 /** 63 * Clean up the singletons from the given context; the 64 * {@link RenderScript} and {@link AllocationCache} objects are destroyed. 65 * 66 * <p>Safe to call multiple times; subsequent invocations have no effect.</p> 67 */ clearContext()68 public static synchronized void clearContext() { 69 if (sContext != null) { 70 sCache.close(); 71 sCache = null; 72 73 sRS.releaseAllContexts(); 74 sRS = null; 75 sContext = null; 76 } 77 } 78 79 /** 80 * Get the current {@link RenderScript} singleton. 81 * 82 * @return A non-{@code null} {@link RenderScript} object. 83 * 84 * @throws IllegalStateException if {@link #setContext} was not called prior to this 85 */ getRS()86 public static synchronized RenderScript getRS() { 87 if (sRS == null) { 88 throw new IllegalStateException("Call #setContext before using #get"); 89 } 90 91 return sRS; 92 } 93 /** 94 * Get the current {@link AllocationCache} singleton. 95 * 96 * @return A non-{@code null} {@link AllocationCache} object. 97 * 98 * @throws IllegalStateException if {@link #setContext} was not called prior to this 99 */ getCache()100 public static synchronized AllocationCache getCache() { 101 if (sCache == null) { 102 throw new IllegalStateException("Call #setContext before using #getCache"); 103 } 104 105 return sCache; 106 } 107 108 // Suppress default constructor for noninstantiability RenderScriptSingleton()109 private RenderScriptSingleton() { throw new AssertionError(); } 110 } 111