Home | History | Annotate | Download | only in graphics
      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 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.LinearGradient
     33  *
     34  * Through the layoutlib_create tool, the original native methods of LinearGradient 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 LinearGradient 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 final class LinearGradient_Delegate extends Gradient_Delegate {
     48 
     49     // ---- delegate data ----
     50     private java.awt.Paint mJavaPaint;
     51 
     52     // ---- Public Helper methods ----
     53 
     54     @Override
     55     public java.awt.Paint getJavaPaint() {
     56         return mJavaPaint;
     57     }
     58 
     59     // ---- native methods ----
     60 
     61     @LayoutlibDelegate
     62     /*package*/ static long nativeCreate1(LinearGradient thisGradient, long matrix,
     63             float x0, float y0, float x1, float y1,
     64             int colors[], float positions[], int tileMode) {
     65         LinearGradient_Delegate newDelegate = new LinearGradient_Delegate(matrix, x0, y0,
     66                 x1, y1, colors, positions, Shader_Delegate.getTileMode(tileMode));
     67         return sManager.addNewDelegate(newDelegate);
     68     }
     69 
     70     @LayoutlibDelegate
     71     /*package*/ static long nativeCreate2(LinearGradient thisGradient, long matrix,
     72             float x0, float y0, float x1, float y1,
     73             int color0, int color1, int tileMode) {
     74         return nativeCreate1(thisGradient, matrix, x0, y0, x1, y1, new int[] { color0, color1},
     75                 null /*positions*/, tileMode);
     76     }
     77 
     78     // ---- Private delegate/helper methods ----
     79 
     80     /**
     81      * Create a shader that draws a linear gradient along a line.
     82      *
     83      * @param nativeMatrix reference to the shader's native transformation matrix
     84      * @param x0 The x-coordinate for the start of the gradient line
     85      * @param y0 The y-coordinate for the start of the gradient line
     86      * @param x1 The x-coordinate for the end of the gradient line
     87      * @param y1 The y-coordinate for the end of the gradient line
     88      * @param colors The colors to be distributed along the gradient line
     89      * @param positions May be null. The relative positions [0..1] of each
     90      *            corresponding color in the colors array. If this is null, the
     91      *            the colors are distributed evenly along the gradient line.
     92      * @param tile The Shader tiling mode
     93      */
     94     private LinearGradient_Delegate(long nativeMatrix, float x0, float y0, float x1,
     95             float y1, int colors[], float positions[], TileMode tile) {
     96         super(nativeMatrix, colors, positions);
     97         mJavaPaint = new LinearGradientPaint(x0, y0, x1, y1, mColors, mPositions, tile);
     98     }
     99 
    100     // ---- Custom Java Paint ----
    101     /**
    102      * Linear Gradient (Java) Paint able to handle more than 2 points, as
    103      * {@link java.awt.GradientPaint} only supports 2 points and does not support Android's tile
    104      * modes.
    105      */
    106     private class LinearGradientPaint extends GradientPaint {
    107 
    108         private final float mX0;
    109         private final float mY0;
    110         private final float mDx;
    111         private final float mDy;
    112         private final float mDSize2;
    113 
    114         public LinearGradientPaint(float x0, float y0, float x1, float y1, int colors[],
    115                 float positions[], TileMode tile) {
    116             super(colors, positions, tile);
    117             mX0 = x0;
    118             mY0 = y0;
    119             mDx = x1 - x0;
    120             mDy = y1 - y0;
    121             mDSize2 = mDx * mDx + mDy * mDy;
    122         }
    123 
    124         @Override
    125         public java.awt.PaintContext createContext(
    126                 java.awt.image.ColorModel      colorModel,
    127                 java.awt.Rectangle             deviceBounds,
    128                 java.awt.geom.Rectangle2D      userBounds,
    129                 java.awt.geom.AffineTransform  xform,
    130                 java.awt.RenderingHints        hints) {
    131             precomputeGradientColors();
    132 
    133             java.awt.geom.AffineTransform canvasMatrix;
    134             try {
    135                 canvasMatrix = xform.createInverse();
    136             } catch (java.awt.geom.NoninvertibleTransformException e) {
    137                 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
    138                         "Unable to inverse matrix in LinearGradient", e, null /*data*/);
    139                 canvasMatrix = new java.awt.geom.AffineTransform();
    140             }
    141 
    142             java.awt.geom.AffineTransform localMatrix = getLocalMatrix();
    143             try {
    144                 localMatrix = localMatrix.createInverse();
    145             } catch (java.awt.geom.NoninvertibleTransformException e) {
    146                 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE,
    147                         "Unable to inverse matrix in LinearGradient", e, null /*data*/);
    148                 localMatrix = new java.awt.geom.AffineTransform();
    149             }
    150 
    151             return new LinearGradientPaintContext(canvasMatrix, localMatrix, colorModel);
    152         }
    153 
    154         private class LinearGradientPaintContext implements java.awt.PaintContext {
    155 
    156             private final java.awt.geom.AffineTransform mCanvasMatrix;
    157             private final java.awt.geom.AffineTransform mLocalMatrix;
    158             private final java.awt.image.ColorModel mColorModel;
    159 
    160             private LinearGradientPaintContext(
    161                     java.awt.geom.AffineTransform canvasMatrix,
    162                     java.awt.geom.AffineTransform localMatrix,
    163                     java.awt.image.ColorModel colorModel) {
    164                 mCanvasMatrix = canvasMatrix;
    165                 mLocalMatrix = localMatrix;
    166                 mColorModel = colorModel.hasAlpha() ? colorModel : ColorModel.getRGBdefault();
    167             }
    168 
    169             @Override
    170             public void dispose() {
    171             }
    172 
    173             @Override
    174             public java.awt.image.ColorModel getColorModel() {
    175                 return mColorModel;
    176             }
    177 
    178             @Override
    179             public java.awt.image.Raster getRaster(int x, int y, int w, int h) {
    180                 int[] data = new int[w*h];
    181 
    182                 int index = 0;
    183                 float[] pt1 = new float[2];
    184                 float[] pt2 = new float[2];
    185                 for (int iy = 0 ; iy < h ; iy++) {
    186                     for (int ix = 0 ; ix < w ; ix++) {
    187                         // handle the canvas transform
    188                         pt1[0] = x + ix;
    189                         pt1[1] = y + iy;
    190                         mCanvasMatrix.transform(pt1, 0, pt2, 0, 1);
    191 
    192                         // handle the local matrix.
    193                         pt1[0] = pt2[0];
    194                         pt1[1] = pt2[1];
    195                         mLocalMatrix.transform(pt1, 0, pt2, 0, 1);
    196 
    197                         data[index++] = getColor(pt2[0], pt2[1]);
    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          * Returns a color for an arbitrary point.
    209          */
    210         private int getColor(float x, float y) {
    211             float pos;
    212             if (mDx == 0) {
    213                 pos = (y - mY0) / mDy;
    214             } else if (mDy == 0) {
    215                 pos = (x - mX0) / mDx;
    216             } else {
    217                 // find the x position on the gradient vector.
    218                 float _x = (mDx*mDy*(y-mY0) + mDy*mDy*mX0 + mDx*mDx*x) / mDSize2;
    219                 // from it get the position relative to the vector
    220                 pos = (_x - mX0) / mDx;
    221             }
    222 
    223             return getGradientColor(pos);
    224         }
    225     }
    226 }
    227