• 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.ide.common.rendering.api.ILayoutLog;
20 import com.android.layoutlib.bridge.Bridge;
21 import com.android.layoutlib.bridge.impl.DelegateManager;
22 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
23 
24 import android.graphics.Shader.TileMode;
25 
26 import java.awt.image.ColorModel;
27 import java.awt.image.DataBufferInt;
28 import java.awt.image.Raster;
29 import java.awt.image.SampleModel;
30 
31 /**
32  * Delegate implementing the native methods of android.graphics.LinearGradient
33  *
34  * Through the layoutlib_create tool, the original native methods of LinearGradient have been
35  * replaced by calls to methods of the same name in this delegate class.
36  *
37  * This class behaves like the original native implementation, but in Java, keeping previously
38  * native data into its own objects and mapping them to int that are sent back and forth between
39  * it and the original LinearGradient class.
40  *
41  * Because this extends {@link Shader_Delegate}, there's no need to use a {@link DelegateManager},
42  * as all the Shader classes will be added to the manager owned by {@link Shader_Delegate}.
43  *
44  * @see Shader_Delegate
45  *
46  */
47 public final class LinearGradient_Delegate extends Gradient_Delegate {
48 
49     // ---- delegate data ----
50     private java.awt.Paint mJavaPaint;
51 
52     // ---- Public Helper methods ----
53 
54     @Override
getJavaPaint()55     public java.awt.Paint getJavaPaint() {
56         return mJavaPaint;
57     }
58 
59     // ---- native methods ----
60 
61     @LayoutlibDelegate
nativeCreate(LinearGradient thisGradient, long matrix, float x0, float y0, float x1, float y1, long[] colors, float[] positions, int tileMode, long colorSpaceHandle)62     /*package*/ static long nativeCreate(LinearGradient thisGradient, long matrix,
63             float x0, float y0, float x1, float y1, long[] colors, float[] positions,
64             int tileMode, long colorSpaceHandle) {
65         LinearGradient_Delegate newDelegate = new LinearGradient_Delegate(matrix, x0, y0,
66                 x1, y1, colors, positions, Shader_Delegate.getTileMode(tileMode));
67         return sManager.addNewDelegate(newDelegate);
68     }
69 
70     // ---- Private delegate/helper methods ----
71 
72     /**
73      * Create a shader that draws a linear gradient along a line.
74      *
75      * @param nativeMatrix reference to the shader's native transformation matrix
76      * @param x0 The x-coordinate for the start of the gradient line
77      * @param y0 The y-coordinate for the start of the gradient line
78      * @param x1 The x-coordinate for the end of the gradient line
79      * @param y1 The y-coordinate for the end of the gradient line
80      * @param colors The colors to be distributed along the gradient line
81      * @param positions May be null. The relative positions [0..1] of each
82      *            corresponding color in the colors array. If this is null, the
83      *            the colors are distributed evenly along the gradient line.
84      * @param tile The Shader tiling mode
85      */
LinearGradient_Delegate(long nativeMatrix, float x0, float y0, float x1, float y1, long[] colors, float[] positions, TileMode tile)86     private LinearGradient_Delegate(long nativeMatrix, float x0, float y0, float x1,
87             float y1, long[] colors, float[] positions, TileMode tile) {
88         super(nativeMatrix, colors, positions);
89         mJavaPaint = new LinearGradientPaint(x0, y0, x1, y1, mColors, mPositions, tile);
90     }
91 
92     // ---- Custom Java Paint ----
93     /**
94      * Linear Gradient (Java) Paint able to handle more than 2 points, as
95      * {@link java.awt.GradientPaint} only supports 2 points and does not support Android's tile
96      * modes.
97      */
98     private class LinearGradientPaint extends GradientPaint {
99 
100         private final float mX0;
101         private final float mY0;
102         private final float mDx;
103         private final float mDy;
104         private final float mDSize2;
105 
LinearGradientPaint(float x0, float y0, float x1, float y1, int[] colors, float[] positions, TileMode tile)106         public LinearGradientPaint(float x0, float y0, float x1, float y1, int[] colors,
107                 float[] positions, TileMode tile) {
108             super(colors, positions, tile);
109             mX0 = x0;
110             mY0 = y0;
111             mDx = x1 - x0;
112             mDy = y1 - y0;
113             mDSize2 = mDx * mDx + mDy * mDy;
114         }
115 
116         @Override
createContext( java.awt.image.ColorModel colorModel, java.awt.Rectangle deviceBounds, java.awt.geom.Rectangle2D userBounds, java.awt.geom.AffineTransform xform, java.awt.RenderingHints hints)117         public java.awt.PaintContext createContext(
118                 java.awt.image.ColorModel      colorModel,
119                 java.awt.Rectangle             deviceBounds,
120                 java.awt.geom.Rectangle2D      userBounds,
121                 java.awt.geom.AffineTransform  xform,
122                 java.awt.RenderingHints        hints) {
123             precomputeGradientColors();
124 
125             java.awt.geom.AffineTransform canvasMatrix;
126             try {
127                 canvasMatrix = xform.createInverse();
128             } catch (java.awt.geom.NoninvertibleTransformException e) {
129                 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_MATRIX_INVERSE,
130                         "Unable to inverse matrix in LinearGradient", e, null, null /*data*/);
131                 canvasMatrix = new java.awt.geom.AffineTransform();
132             }
133 
134             java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
135             try {
136                 localMatrix = localMatrix.createInverse();
137             } catch (java.awt.geom.NoninvertibleTransformException e) {
138                 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_MATRIX_INVERSE,
139                         "Unable to inverse matrix in LinearGradient", e, null, null /*data*/);
140                 localMatrix = new java.awt.geom.AffineTransform();
141             }
142 
143             return new LinearGradientPaintContext(canvasMatrix, localMatrix, colorModel);
144         }
145 
146         private class LinearGradientPaintContext implements java.awt.PaintContext {
147 
148             private final java.awt.geom.AffineTransform mCanvasMatrix;
149             private final java.awt.geom.AffineTransform mLocalMatrix;
150             private final java.awt.image.ColorModel mColorModel;
151 
LinearGradientPaintContext( java.awt.geom.AffineTransform canvasMatrix, java.awt.geom.AffineTransform localMatrix, java.awt.image.ColorModel colorModel)152             private LinearGradientPaintContext(
153                     java.awt.geom.AffineTransform canvasMatrix,
154                     java.awt.geom.AffineTransform localMatrix,
155                     java.awt.image.ColorModel colorModel) {
156                 mCanvasMatrix = canvasMatrix;
157                 mLocalMatrix = localMatrix;
158                 mColorModel = colorModel.hasAlpha() ? colorModel : ColorModel.getRGBdefault();
159             }
160 
161             @Override
dispose()162             public void dispose() {
163             }
164 
165             @Override
getColorModel()166             public java.awt.image.ColorModel getColorModel() {
167                 return mColorModel;
168             }
169 
170             @Override
getRaster(int x, int y, int w, int h)171             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
172                 int[] data = new int[w*h];
173 
174                 int index = 0;
175                 float[] pt1 = new float[2];
176                 float[] pt2 = new float[2];
177                 for (int iy = 0 ; iy < h ; iy++) {
178                     for (int ix = 0 ; ix < w ; ix++) {
179                         // handle the canvas transform
180                         pt1[0] = x + ix;
181                         pt1[1] = y + iy;
182                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
183 
184                         // handle the local matrix.
185                         pt1[0] = pt2[0];
186                         pt1[1] = pt2[1];
187                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
188 
189                         data[index++] = getColor(pt2[0], pt2[1]);
190                     }
191                 }
192 
193                 DataBufferInt dataBuffer = new DataBufferInt(data, data.length);
194                 SampleModel colorModel = mColorModel.createCompatibleSampleModel(w, h);
195                 return Raster.createWritableRaster(colorModel, dataBuffer, null);
196             }
197         }
198 
199         /**
200          * Returns a color for an arbitrary point.
201          */
getColor(float x, float y)202         private int getColor(float x, float y) {
203             float pos;
204             if (mDx == 0) {
205                 pos = (y - mY0) / mDy;
206             } else if (mDy == 0) {
207                 pos = (x - mX0) / mDx;
208             } else {
209                 // find the x position on the gradient vector.
210                 float _x = (mDx*mDy*(y-mY0) + mDy*mDy*mX0 + mDx*mDx*x) / mDSize2;
211                 // from it get the position relative to the vector
212                 pos = (_x - mX0) / mDx;
213             }
214 
215             return getGradientColor(pos);
216         }
217     }
218 }
219