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