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.RadialGradient
33  *
34  * Through the layoutlib_create tool, the original native methods of RadialGradient 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 RadialGradient 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 class RadialGradient_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(long matrix, float x, float y, float radius, long[] colors, float[] positions, int tileMode, long colorSpaceHandle)62     /*package*/ static long nativeCreate(long matrix, float x, float y, float radius,
63             long[] colors, float[] positions, int tileMode, long colorSpaceHandle) {
64         RadialGradient_Delegate newDelegate = new RadialGradient_Delegate(matrix, x, y, radius,
65                 colors, positions, Shader_Delegate.getTileMode(tileMode));
66         return sManager.addNewDelegate(newDelegate);
67     }
68 
69     // ---- Private delegate/helper methods ----
70 
71     /**
72      * Create a shader that draws a radial gradient given the center and radius.
73      *
74      * @param nativeMatrix reference to the shader's native transformation matrix
75      * @param x The x-coordinate of the center of the radius
76      * @param y The y-coordinate of the center of the radius
77      * @param radius Must be positive. The radius of the circle for this
78      *            gradient
79      * @param colors The colors to be distributed between the center and edge of
80      *            the circle
81      * @param positions May be NULL. The relative position of each corresponding
82      *            color in the colors array. If this is NULL, the the colors are
83      *            distributed evenly between the center and edge of the circle.
84      * @param tile The Shader tiling mode
85      */
RadialGradient_Delegate(long nativeMatrix, float x, float y, float radius, long[] colors, float[] positions, TileMode tile)86     private RadialGradient_Delegate(long nativeMatrix, float x, float y, float radius,
87             long[] colors, float[] positions, TileMode tile) {
88         super(nativeMatrix, colors, positions);
89         mJavaPaint = new RadialGradientPaint(x, y, radius, mColors, mPositions, tile);
90     }
91 
92     private class RadialGradientPaint extends GradientPaint {
93 
94         private final float mX;
95         private final float mY;
96         private final float mRadius;
97 
RadialGradientPaint(float x, float y, float radius, int[] colors, float[] positions, TileMode mode)98         public RadialGradientPaint(float x, float y, float radius,
99                 int[] colors, float[] positions, TileMode mode) {
100             super(colors, positions, mode);
101             mX = x;
102             mY = y;
103             mRadius = radius;
104         }
105 
106         @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)107         public java.awt.PaintContext createContext(
108                 java.awt.image.ColorModel     colorModel,
109                 java.awt.Rectangle            deviceBounds,
110                 java.awt.geom.Rectangle2D     userBounds,
111                 java.awt.geom.AffineTransform xform,
112                 java.awt.RenderingHints       hints) {
113             precomputeGradientColors();
114 
115             java.awt.geom.AffineTransform canvasMatrix;
116             try {
117                 canvasMatrix = xform.createInverse();
118             } catch (java.awt.geom.NoninvertibleTransformException e) {
119                 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_MATRIX_INVERSE,
120                         "Unable to inverse matrix in RadialGradient", e, null, null /*data*/);
121                 canvasMatrix = new java.awt.geom.AffineTransform();
122             }
123 
124             java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
125             try {
126                 localMatrix = localMatrix.createInverse();
127             } catch (java.awt.geom.NoninvertibleTransformException e) {
128                 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_MATRIX_INVERSE,
129                         "Unable to inverse matrix in RadialGradient", e, null, null /*data*/);
130                 localMatrix = new java.awt.geom.AffineTransform();
131             }
132 
133             return new RadialGradientPaintContext(canvasMatrix, localMatrix, colorModel);
134         }
135 
136         private class RadialGradientPaintContext implements java.awt.PaintContext {
137 
138             private final java.awt.geom.AffineTransform mCanvasMatrix;
139             private final java.awt.geom.AffineTransform mLocalMatrix;
140             private final java.awt.image.ColorModel mColorModel;
141 
RadialGradientPaintContext( java.awt.geom.AffineTransform canvasMatrix, java.awt.geom.AffineTransform localMatrix, java.awt.image.ColorModel colorModel)142             public RadialGradientPaintContext(
143                     java.awt.geom.AffineTransform canvasMatrix,
144                     java.awt.geom.AffineTransform localMatrix,
145                     java.awt.image.ColorModel colorModel) {
146                 mCanvasMatrix = canvasMatrix;
147                 mLocalMatrix = localMatrix;
148                 mColorModel = colorModel.hasAlpha() ? colorModel : ColorModel.getRGBdefault();
149             }
150 
151             @Override
dispose()152             public void dispose() {
153             }
154 
155             @Override
getColorModel()156             public java.awt.image.ColorModel getColorModel() {
157                 return mColorModel;
158             }
159 
160             @Override
getRaster(int x, int y, int w, int h)161             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
162                 int[] data = new int[w*h];
163 
164                 // compute distance from each point to the center, and figure out the distance from
165                 // it.
166                 int index = 0;
167                 float[] pt1 = new float[2];
168                 float[] pt2 = new float[2];
169 
170                 for (int iy = 0 ; iy < h ; iy++) {
171                     for (int ix = 0 ; ix < w ; ix++) {
172                         // handle the canvas transform
173                         pt1[0] = x + ix;
174                         pt1[1] = y + iy;
175                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
176 
177                         // handle the local matrix
178                         pt1[0] = pt2[0];
179                         pt1[1] = pt2[1];
180                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
181 
182                         float _x = pt2[0] - mX;
183                         float _y = pt2[1] - mY;
184                         float distance = (float) Math.hypot(_x, _y);
185 
186                         data[index++] = getGradientColor(distance / mRadius);
187                     }
188                 }
189 
190                 DataBufferInt dataBuffer = new DataBufferInt(data, data.length);
191                 SampleModel colorModel = mColorModel.createCompatibleSampleModel(w, h);
192                 return Raster.createWritableRaster(colorModel, dataBuffer, null);
193             }
194 
195         }
196     }
197 
198 }
199