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 java.awt.image.DataBufferInt;
25 import java.awt.image.Raster;
26 import java.awt.image.SampleModel;
27 
28 /**
29  * Delegate implementing the native methods of android.graphics.SweepGradient
30  *
31  * Through the layoutlib_create tool, the original native methods of SweepGradient have been
32  * replaced by calls to methods of the same name in this delegate class.
33  *
34  * This class behaves like the original native implementation, but in Java, keeping previously
35  * native data into its own objects and mapping them to int that are sent back and forth between
36  * it and the original SweepGradient class.
37  *
38  * Because this extends {@link Shader_Delegate}, there's no need to use a {@link DelegateManager},
39  * as all the Shader classes will be added to the manager owned by {@link Shader_Delegate}.
40  *
41  * @see Shader_Delegate
42  *
43  */
44 public class SweepGradient_Delegate extends Gradient_Delegate {
45 
46     // ---- delegate data ----
47     private java.awt.Paint mJavaPaint;
48 
49     // ---- Public Helper methods ----
50 
51     @Override
getJavaPaint()52     public java.awt.Paint getJavaPaint() {
53         return mJavaPaint;
54     }
55 
56     // ---- native methods ----
57 
58     @LayoutlibDelegate
nativeCreate(long matrix, float x, float y, long[] colors, float[] positions, long colorSpaceHandle)59     /*package*/ static long nativeCreate(long matrix, float x, float y, long[] colors,
60             float[] positions, long colorSpaceHandle) {
61         SweepGradient_Delegate newDelegate = new SweepGradient_Delegate(matrix, x, y, colors,
62                 positions);
63         return sManager.addNewDelegate(newDelegate);
64     }
65 
66     // ---- Private delegate/helper methods ----
67 
68     /**
69      * A subclass of Shader that draws a sweep gradient around a center point.
70      *
71      * @param nativeMatrix reference to the shader's native transformation matrix
72      * @param cx       The x-coordinate of the center
73      * @param cy       The y-coordinate of the center
74      * @param colors   The colors to be distributed between around the center.
75      *                 There must be at least 2 colors in the array.
76      * @param positions May be NULL. The relative position of
77      *                 each corresponding color in the colors array, beginning
78      *                 with 0 and ending with 1.0. If the values are not
79      *                 monotonic, the drawing may produce unexpected results.
80      *                 If positions is NULL, then the colors are automatically
81      *                 spaced evenly.
82      */
SweepGradient_Delegate(long nativeMatrix, float cx, float cy, long[] colors, float[] positions)83     private SweepGradient_Delegate(long nativeMatrix, float cx, float cy,
84             long[] colors, float[] positions) {
85         super(nativeMatrix, colors, positions);
86         mJavaPaint = new SweepGradientPaint(cx, cy, mColors, mPositions);
87     }
88 
89     private class SweepGradientPaint extends GradientPaint {
90 
91         private final float mCx;
92         private final float mCy;
93 
SweepGradientPaint(float cx, float cy, int[] colors, float[] positions)94         public SweepGradientPaint(float cx, float cy, int[] colors,
95                 float[] positions) {
96             super(colors, positions, null /*tileMode*/);
97             mCx = cx;
98             mCy = cy;
99         }
100 
101         @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)102         public java.awt.PaintContext createContext(
103                 java.awt.image.ColorModel     colorModel,
104                 java.awt.Rectangle            deviceBounds,
105                 java.awt.geom.Rectangle2D     userBounds,
106                 java.awt.geom.AffineTransform xform,
107                 java.awt.RenderingHints       hints) {
108             precomputeGradientColors();
109 
110             java.awt.geom.AffineTransform canvasMatrix;
111             try {
112                 canvasMatrix = xform.createInverse();
113             } catch (java.awt.geom.NoninvertibleTransformException e) {
114                 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_MATRIX_INVERSE,
115                         "Unable to inverse matrix in SweepGradient", e, null, null /*data*/);
116                 canvasMatrix = new java.awt.geom.AffineTransform();
117             }
118 
119             java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
120             try {
121                 localMatrix = localMatrix.createInverse();
122             } catch (java.awt.geom.NoninvertibleTransformException e) {
123                 Bridge.getLog().fidelityWarning(ILayoutLog.TAG_MATRIX_INVERSE,
124                         "Unable to inverse matrix in SweepGradient", e, null, null /*data*/);
125                 localMatrix = new java.awt.geom.AffineTransform();
126             }
127 
128             return new SweepGradientPaintContext(canvasMatrix, localMatrix, colorModel);
129         }
130 
131         private class SweepGradientPaintContext implements java.awt.PaintContext {
132 
133             private final java.awt.geom.AffineTransform mCanvasMatrix;
134             private final java.awt.geom.AffineTransform mLocalMatrix;
135             private final java.awt.image.ColorModel mColorModel;
136 
SweepGradientPaintContext( java.awt.geom.AffineTransform canvasMatrix, java.awt.geom.AffineTransform localMatrix, java.awt.image.ColorModel colorModel)137             public SweepGradientPaintContext(
138                     java.awt.geom.AffineTransform canvasMatrix,
139                     java.awt.geom.AffineTransform localMatrix,
140                     java.awt.image.ColorModel colorModel) {
141                 mCanvasMatrix = canvasMatrix;
142                 mLocalMatrix = localMatrix;
143                 mColorModel = colorModel;
144             }
145 
146             @Override
dispose()147             public void dispose() {
148             }
149 
150             @Override
getColorModel()151             public java.awt.image.ColorModel getColorModel() {
152                 return mColorModel;
153             }
154 
155             @Override
getRaster(int x, int y, int w, int h)156             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
157 
158                 int[] data = new int[w*h];
159 
160                 // compute angle from each point to the center, and figure out the distance from
161                 // it.
162                 int index = 0;
163                 float[] pt1 = new float[2];
164                 float[] pt2 = new float[2];
165                 for (int iy = 0 ; iy < h ; iy++) {
166                     for (int ix = 0 ; ix < w ; ix++) {
167                         // handle the canvas transform
168                         pt1[0] = x + ix;
169                         pt1[1] = y + iy;
170                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
171 
172                         // handle the local matrix
173                         pt1[0] = pt2[0] - mCx;
174                         pt1[1] = pt2[1] - mCy;
175                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
176 
177                         float dx = pt2[0];
178                         float dy = pt2[1];
179 
180                         float angle;
181                         if (dx == 0) {
182                             angle = (float) (dy < 0 ? 3 * Math.PI / 2 : Math.PI / 2);
183                         } else if (dy == 0) {
184                             angle = (float) (dx < 0 ? Math.PI : 0);
185                         } else {
186                             angle = (float) Math.atan(dy / dx);
187                             if (dx > 0) {
188                                 if (dy < 0) {
189                                     angle += Math.PI * 2;
190                                 }
191                             } else {
192                                 angle += Math.PI;
193                             }
194                         }
195 
196                         // convert to 0-1. value and get color
197                         data[index++] = getGradientColor((float) (angle / (2 * Math.PI)));
198                     }
199                 }
200 
201                 DataBufferInt dataBuffer = new DataBufferInt(data, data.length);
202                 SampleModel colorModel = mColorModel.createCompatibleSampleModel(w, h);
203                 return Raster.createWritableRaster(colorModel, dataBuffer, null);
204             }
205 
206         }
207     }
208 }
209