1 /* 2 * Copyright (C) 2010 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.graphics; 18 19 import com.android.layoutlib.bridge.impl.DelegateManager; 20 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 21 22 import android.graphics.Shader.TileMode; 23 24 import libcore.util.NativeAllocationRegistry_Delegate; 25 26 /** 27 * Delegate implementing the native methods of android.graphics.Shader 28 * 29 * Through the layoutlib_create tool, the original native methods of Shader have been replaced 30 * by calls to methods of the same name in this delegate class. 31 * 32 * This class behaves like the original native implementation, but in Java, keeping previously 33 * native data into its own objects and mapping them to int that are sent back and forth between 34 * it and the original Shader class. 35 * 36 * This also serve as a base class for all Shader delegate classes. 37 * 38 * @see DelegateManager 39 * 40 */ 41 public abstract class Shader_Delegate { 42 43 // ---- delegate manager ---- 44 protected static final DelegateManager<Shader_Delegate> sManager = 45 new DelegateManager<Shader_Delegate>(Shader_Delegate.class); 46 private static long sFinalizer = -1; 47 48 // ---- delegate helper data ---- 49 50 // ---- delegate data ---- 51 private Matrix_Delegate mLocalMatrix = null; 52 53 // ---- Public Helper methods ---- 54 getDelegate(long nativeShader)55 public static Shader_Delegate getDelegate(long nativeShader) { 56 return sManager.getDelegate(nativeShader); 57 } 58 59 /** 60 * Returns the {@link TileMode} matching the given int. 61 * @param tileMode the tile mode int value 62 * @return the TileMode enum. 63 */ getTileMode(int tileMode)64 public static TileMode getTileMode(int tileMode) { 65 for (TileMode tm : TileMode.values()) { 66 if (tm.nativeInt == tileMode) { 67 return tm; 68 } 69 } 70 71 assert false; 72 return TileMode.CLAMP; 73 } 74 getJavaPaint()75 public abstract java.awt.Paint getJavaPaint(); isSupported()76 public abstract boolean isSupported(); getSupportMessage()77 public abstract String getSupportMessage(); 78 79 // ---- native methods ---- 80 81 @LayoutlibDelegate nativeGetFinalizer()82 /*package*/ static long nativeGetFinalizer() { 83 synchronized (Shader_Delegate.class) { 84 if (sFinalizer == -1) { 85 sFinalizer = NativeAllocationRegistry_Delegate.createFinalizer( 86 sManager::removeJavaReferenceFor); 87 } 88 } 89 return sFinalizer; 90 } 91 92 // ---- Private delegate/helper methods ---- 93 Shader_Delegate(long nativeMatrix)94 protected Shader_Delegate(long nativeMatrix) { 95 setLocalMatrix(nativeMatrix); 96 } 97 setLocalMatrix(long nativeMatrix)98 public void setLocalMatrix(long nativeMatrix) { 99 mLocalMatrix = Matrix_Delegate.getDelegate(nativeMatrix); 100 } 101 getLocalMatrix()102 protected java.awt.geom.AffineTransform getLocalMatrix() { 103 if (mLocalMatrix != null) { 104 return mLocalMatrix.getAffineTransform(); 105 } 106 107 return new java.awt.geom.AffineTransform(); 108 } 109 110 } 111