• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
25  * Delegate implementing the native methods of android.graphics.Shader
26  *
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.
29  *
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.
33  *
34  * This also serve as a base class for all Shader delegate classes.
35  *
36  * @see DelegateManager
37  *
38  */
39 public abstract class Shader_Delegate {
40 
41     // ---- delegate manager ----
42     protected static final DelegateManager<Shader_Delegate> sManager =
43             new DelegateManager<Shader_Delegate>(Shader_Delegate.class);
44 
45     // ---- delegate helper data ----
46 
47     // ---- delegate data ----
48     private Matrix_Delegate mLocalMatrix = null;
49 
50     // ---- Public Helper methods ----
51 
getDelegate(int nativeShader)52     public static Shader_Delegate getDelegate(int nativeShader) {
53         return sManager.getDelegate(nativeShader);
54     }
55 
56     /**
57      * Returns the {@link TileMode} matching the given int.
58      * @param tileMode the tile mode int value
59      * @return the TileMode enum.
60      */
getTileMode(int tileMode)61     public static TileMode getTileMode(int tileMode) {
62         for (TileMode tm : TileMode.values()) {
63             if (tm.nativeInt == tileMode) {
64                 return tm;
65             }
66         }
67 
68         assert false;
69         return TileMode.CLAMP;
70     }
71 
getJavaPaint()72     public abstract java.awt.Paint getJavaPaint();
isSupported()73     public abstract boolean isSupported();
getSupportMessage()74     public abstract String getSupportMessage();
75 
isValid()76     public boolean isValid() {
77         if (mLocalMatrix != null && mLocalMatrix.getAffineTransform().getDeterminant() == 0) {
78             return false;
79         }
80 
81         return true;
82     }
83 
84     // ---- native methods ----
85 
86     @LayoutlibDelegate
nativeDestructor(int native_shader)87     /*package*/ static void nativeDestructor(int native_shader) {
88         sManager.removeJavaReferenceFor(native_shader);
89     }
90 
91     @LayoutlibDelegate
nativeGetLocalMatrix(int native_shader, int matrix_instance)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) {
96             return false;
97         }
98 
99         // get the (optional) out matrix.
100         Matrix_Delegate outMatrixDelegate = Matrix_Delegate.getDelegate(matrix_instance);
101 
102         if (shaderDelegate.mLocalMatrix == null || shaderDelegate.mLocalMatrix.isIdentity()) {
103             if (outMatrixDelegate != null) {
104                 outMatrixDelegate.reset();
105             }
106             return false;
107         }
108 
109         if (outMatrixDelegate != null) {
110             outMatrixDelegate.set(shaderDelegate.mLocalMatrix);
111         }
112 
113         return true;
114     }
115 
116 
117     @LayoutlibDelegate
nativeSetLocalMatrix(int native_shader, int matrix_instance)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) {
122             return;
123         }
124 
125         shaderDelegate.mLocalMatrix = Matrix_Delegate.getDelegate(matrix_instance);
126     }
127 
128     // ---- Private delegate/helper methods ----
129 
getLocalMatrix()130     protected java.awt.geom.AffineTransform getLocalMatrix() {
131         if (mLocalMatrix != null) {
132             return mLocalMatrix.getAffineTransform();
133         }
134 
135         return new java.awt.geom.AffineTransform();
136     }
137 }
138