2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.graphics;
19 import com.android.layoutlib.bridge.impl.DelegateManager;
20 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22 import android.graphics.Shader.TileMode;
25 * Delegate implementing the native methods of android.graphics.Shader
27 * Through the layoutlib_create tool, the original native methods of Shader have been replaced
28 * by calls to methods of the same name in this delegate class.
30 * This class behaves like the original native implementation, but in Java, keeping previously
31 * native data into its own objects and mapping them to int that are sent back and forth between
32 * it and the original Shader class.
34 * This also serve as a base class for all Shader delegate classes.
36 * @see DelegateManager
39 public abstract class Shader_Delegate {
41 // ---- delegate manager ----
42 protected static final DelegateManager<Shader_Delegate> sManager =
43 new DelegateManager<Shader_Delegate>(Shader_Delegate.class);
45 // ---- delegate helper data ----
47 // ---- delegate data ----
48 private Matrix_Delegate mLocalMatrix = null;
50 // ---- Public Helper methods ----
52 public static Shader_Delegate getDelegate(int nativeShader) {
53 return sManager.getDelegate(nativeShader);
57 * Returns the {@link TileMode} matching the given int.
58 * @param tileMode the tile mode int value
59 * @return the TileMode enum.
61 public static TileMode getTileMode(int tileMode) {
62 for (TileMode tm : TileMode.values()) {
63 if (tm.nativeInt == tileMode) {
69 return TileMode.CLAMP;
72 public abstract java.awt.Paint getJavaPaint();
73 public abstract boolean isSupported();
74 public abstract String getSupportMessage();
76 public boolean isValid() {
77 if (mLocalMatrix != null && mLocalMatrix.getAffineTransform().getDeterminant() == 0) {
84 // ---- native methods ----
87 /*package*/ static void nativeDestructor(int native_shader) {
88 sManager.removeJavaReferenceFor(native_shader);
92 /*package*/ static boolean nativeGetLocalMatrix(int native_shader, int matrix_instance) {
93 // get the delegate from the native int.
94 Shader_Delegate shaderDelegate = sManager.getDelegate(native_shader);
95 if (shaderDelegate == null) {
99 // get the (optional) out matrix.
100 Matrix_Delegate outMatrixDelegate = Matrix_Delegate.getDelegate(matrix_instance);
102 if (shaderDelegate.mLocalMatrix == null || shaderDelegate.mLocalMatrix.isIdentity()) {
103 if (outMatrixDelegate != null) {
104 outMatrixDelegate.reset();
109 if (outMatrixDelegate != null) {
110 outMatrixDelegate.set(shaderDelegate.mLocalMatrix);
118 /*package*/ static void nativeSetLocalMatrix(int native_shader, int matrix_instance) {
119 // get the delegate from the native int.
120 Shader_Delegate shaderDelegate = sManager.getDelegate(native_shader);
121 if (shaderDelegate == null) {
125 shaderDelegate.mLocalMatrix = Matrix_Delegate.getDelegate(matrix_instance);
128 // ---- Private delegate/helper methods ----
130 protected java.awt.geom.AffineTransform getLocalMatrix() {
131 if (mLocalMatrix != null) {
132 return mLocalMatrix.getAffineTransform();
135 return new java.awt.geom.AffineTransform();