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 private float mAlpha = 1.0f; 53 54 // ---- Public Helper methods ---- 55 getDelegate(long nativeShader)56 public static Shader_Delegate getDelegate(long nativeShader) { 57 return sManager.getDelegate(nativeShader); 58 } 59 60 /** 61 * Returns the {@link TileMode} matching the given int. 62 * @param tileMode the tile mode int value 63 * @return the TileMode enum. 64 */ getTileMode(int tileMode)65 public static TileMode getTileMode(int tileMode) { 66 for (TileMode tm : TileMode.values()) { 67 if (tm.nativeInt == tileMode) { 68 return tm; 69 } 70 } 71 72 assert false; 73 return TileMode.CLAMP; 74 } 75 getJavaPaint()76 public abstract java.awt.Paint getJavaPaint(); isSupported()77 public abstract boolean isSupported(); getSupportMessage()78 public abstract String getSupportMessage(); 79 80 // ---- native methods ---- 81 82 @LayoutlibDelegate nativeGetFinalizer()83 /*package*/ static long nativeGetFinalizer() { 84 synchronized (Shader_Delegate.class) { 85 if (sFinalizer == -1) { 86 sFinalizer = NativeAllocationRegistry_Delegate.createFinalizer( 87 sManager::removeJavaReferenceFor); 88 } 89 } 90 return sFinalizer; 91 } 92 93 // ---- Private delegate/helper methods ---- 94 Shader_Delegate(long nativeMatrix)95 protected Shader_Delegate(long nativeMatrix) { 96 setLocalMatrix(nativeMatrix); 97 } 98 setLocalMatrix(long nativeMatrix)99 public void setLocalMatrix(long nativeMatrix) { 100 mLocalMatrix = Matrix_Delegate.getDelegate(nativeMatrix); 101 } 102 getLocalMatrix()103 protected java.awt.geom.AffineTransform getLocalMatrix() { 104 if (mLocalMatrix != null) { 105 return mLocalMatrix.getAffineTransform(); 106 } 107 108 return new java.awt.geom.AffineTransform(); 109 } 110 setAlpha(float alpha)111 public void setAlpha(float alpha) { 112 mAlpha = alpha; 113 } 114 getAlpha()115 public float getAlpha() { 116 return mAlpha; 117 } 118 } 119