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
17package android.graphics;
18
19import com.android.ide.common.rendering.api.LayoutLog;
20import com.android.layoutlib.bridge.Bridge;
21import com.android.layoutlib.bridge.impl.DelegateManager;
22import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
23
24import android.graphics.Shader.TileMode;
25
26/**
27 * Delegate implementing the native methods of android.graphics.RadialGradient
28 *
29 * Through the layoutlib_create tool, the original native methods of RadialGradient have been
30 * replaced by calls to methods of the same name in this delegate class.
31 *
32 * This class behaves like the original native implementation, but in Java, keeping previously
33 * native data into its own objects and mapping them to int that are sent back and forth between
34 * it and the original RadialGradient class.
35 *
36 * Because this extends {@link Shader_Delegate}, there's no need to use a {@link DelegateManager},
37 * as all the Shader classes will be added to the manager owned by {@link Shader_Delegate}.
38 *
39 * @see Shader_Delegate
40 *
41 */
42public class RadialGradient_Delegate extends Gradient_Delegate {
43
44    // ---- delegate data ----
45    private java.awt.Paint mJavaPaint;
46
47    // ---- Public Helper methods ----
48
49    @Override
50    public java.awt.Paint getJavaPaint() {
51        return mJavaPaint;
52    }
53
54    // ---- native methods ----
55
56    @LayoutlibDelegate
57    /*package*/ static int nativeCreate1(float x, float y, float radius,
58            int colors[], float positions[], int tileMode) {
59        RadialGradient_Delegate newDelegate = new RadialGradient_Delegate(x, y, radius,
60                colors, positions, Shader_Delegate.getTileMode(tileMode));
61        return sManager.addNewDelegate(newDelegate);
62    }
63
64    @LayoutlibDelegate
65    /*package*/ static int nativeCreate2(float x, float y, float radius,
66            int color0, int color1, int tileMode) {
67        return nativeCreate1(x, y, radius, new int[] { color0, color1 }, null /*positions*/,
68                tileMode);
69    }
70
71    @LayoutlibDelegate
72    /*package*/ static int nativePostCreate1(int native_shader, float x, float y, float radius,
73            int colors[], float positions[], int tileMode) {
74        // nothing to be done here.
75        return 0;
76    }
77
78    @LayoutlibDelegate
79    /*package*/ static int nativePostCreate2(int native_shader, float x, float y, float radius,
80            int color0, int color1, int tileMode) {
81        // nothing to be done here.
82        return 0;
83    }
84
85    // ---- Private delegate/helper methods ----
86
87    /**
88     * Create a shader that draws a radial gradient given the center and radius.
89     *
90     * @param x The x-coordinate of the center of the radius
91     * @param y The y-coordinate of the center of the radius
92     * @param radius Must be positive. The radius of the circle for this
93     *            gradient
94     * @param colors The colors to be distributed between the center and edge of
95     *            the circle
96     * @param positions May be NULL. The relative position of each corresponding
97     *            color in the colors array. If this is NULL, the the colors are
98     *            distributed evenly between the center and edge of the circle.
99     * @param tile The Shader tiling mode
100     */
101    private RadialGradient_Delegate(float x, float y, float radius, int colors[], float positions[],
102            TileMode tile) {
103        super(colors, positions);
104        mJavaPaint = new RadialGradientPaint(x, y, radius, mColors, mPositions, tile);
105    }
106
107    private class RadialGradientPaint extends GradientPaint {
108
109        private final float mX;
110        private final float mY;
111        private final float mRadius;
112
113        public RadialGradientPaint(float x, float y, float radius,
114                int[] colors, float[] positions, TileMode mode) {
115            super(colors, positions, mode);
116            mX = x;
117            mY = y;
118            mRadius = radius;
119        }
120
121        @Override
122        public java.awt.PaintContext createContext(
123                java.awt.image.ColorModel     colorModel,
124                java.awt.Rectangle            deviceBounds,
125                java.awt.geom.Rectangle2D     userBounds,
126                java.awt.geom.AffineTransform xform,
127                java.awt.RenderingHints       hints) {
128            precomputeGradientColors();
129
130            java.awt.geom.AffineTransform canvasMatrix;
131            try {
132                canvasMatrix = xform.createInverse();
133            } catch (java.awt.geom.NoninvertibleTransformException e) {
134                Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
135                        "Unable to inverse matrix in RadialGradient", e, null /*data*/);
136                canvasMatrix = new java.awt.geom.AffineTransform();
137            }
138
139            java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
140            try {
141                localMatrix = localMatrix.createInverse();
142            } catch (java.awt.geom.NoninvertibleTransformException e) {
143                Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
144                        "Unable to inverse matrix in RadialGradient", e, null /*data*/);
145                localMatrix = new java.awt.geom.AffineTransform();
146            }
147
148            return new RadialGradientPaintContext(canvasMatrix, localMatrix, colorModel);
149        }
150
151        private class RadialGradientPaintContext implements java.awt.PaintContext {
152
153            private final java.awt.geom.AffineTransform mCanvasMatrix;
154            private final java.awt.geom.AffineTransform mLocalMatrix;
155            private final java.awt.image.ColorModel mColorModel;
156
157            public RadialGradientPaintContext(
158                    java.awt.geom.AffineTransform canvasMatrix,
159                    java.awt.geom.AffineTransform localMatrix,
160                    java.awt.image.ColorModel colorModel) {
161                mCanvasMatrix = canvasMatrix;
162                mLocalMatrix = localMatrix;
163                mColorModel = colorModel;
164            }
165
166            @Override
167            public void dispose() {
168            }
169
170            @Override
171            public java.awt.image.ColorModel getColorModel() {
172                return mColorModel;
173            }
174
175            @Override
176            public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
177                java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(w, h,
178                        java.awt.image.BufferedImage.TYPE_INT_ARGB);
179
180                int[] data = new int[w*h];
181
182                // compute distance from each point to the center, and figure out the distance from
183                // it.
184                int index = 0;
185                float[] pt1 = new float[2];
186                float[] pt2 = new float[2];
187                for (int iy = 0 ; iy < h ; iy++) {
188                    for (int ix = 0 ; ix < w ; ix++) {
189                        // handle the canvas transform
190                        pt1[0] = x + ix;
191                        pt1[1] = y + iy;
192                        mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
193
194                        // handle the local matrix
195                        pt1[0] = pt2[0] - mX;
196                        pt1[1] = pt2[1] - mY;
197                        mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
198
199                        float _x = pt2[0];
200                        float _y = pt2[1];
201                        float distance = (float) Math.sqrt(_x * _x + _y * _y);
202
203                        data[index++] = getGradientColor(distance / mRadius);
204                    }
205                }
206
207                image.setRGB(0 /*startX*/, 0 /*startY*/, w, h, data, 0 /*offset*/, w /*scansize*/);
208
209                return image.getRaster();
210            }
211
212        }
213    }
214
215}
216