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