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.PaintContext; 27 import java.awt.Rectangle; 28 import java.awt.RenderingHints; 29 import java.awt.geom.AffineTransform; 30 import java.awt.geom.NoninvertibleTransformException; 31 import java.awt.geom.Rectangle2D; 32 import java.awt.image.BufferedImage; 33 import java.awt.image.ColorModel; 34 import java.awt.image.DataBufferInt; 35 import java.awt.image.Raster; 36 import java.awt.image.SampleModel; 37 38 /** 39 * Delegate implementing the native methods of android.graphics.BitmapShader 40 * 41 * Through the layoutlib_create tool, the original native methods of BitmapShader have been 42 * replaced by calls to methods of the same name in this delegate class. 43 * 44 * This class behaves like the original native implementation, but in Java, keeping previously 45 * native data into its own objects and mapping them to int that are sent back and forth between 46 * it and the original BitmapShader class. 47 * 48 * Because this extends {@link Shader_Delegate}, there's no need to use a {@link DelegateManager}, 49 * as all the Shader classes will be added to the manager owned by {@link Shader_Delegate}. 50 * 51 * @see Shader_Delegate 52 * 53 */ 54 public class BitmapShader_Delegate extends Shader_Delegate { 55 56 // ---- delegate data ---- 57 private java.awt.Paint mJavaPaint; 58 59 // ---- Public Helper methods ---- 60 61 @Override getJavaPaint()62 public java.awt.Paint getJavaPaint() { 63 return mJavaPaint; 64 } 65 66 @Override isSupported()67 public boolean isSupported() { 68 return true; 69 } 70 71 @Override getSupportMessage()72 public String getSupportMessage() { 73 // no message since isSupported returns true; 74 return null; 75 } 76 77 // ---- native methods ---- 78 79 @LayoutlibDelegate nativeCreate(long nativeMatrix, long bitmapHandle, int shaderTileModeX, int shaderTileModeY)80 /*package*/ static long nativeCreate(long nativeMatrix, long bitmapHandle, 81 int shaderTileModeX, int shaderTileModeY) { 82 Bitmap_Delegate bitmap = Bitmap_Delegate.getDelegate(bitmapHandle); 83 if (bitmap == null) { 84 return 0; 85 } 86 87 BitmapShader_Delegate newDelegate = new BitmapShader_Delegate(nativeMatrix, 88 bitmap.getImage(), 89 Shader_Delegate.getTileMode(shaderTileModeX), 90 Shader_Delegate.getTileMode(shaderTileModeY)); 91 return sManager.addNewDelegate(newDelegate); 92 } 93 94 // ---- Private delegate/helper methods ---- 95 BitmapShader_Delegate(long matrix, BufferedImage image, TileMode tileModeX, TileMode tileModeY)96 private BitmapShader_Delegate(long matrix, BufferedImage image, 97 TileMode tileModeX, TileMode tileModeY) { 98 super(matrix); 99 mJavaPaint = new BitmapShaderPaint(image, tileModeX, tileModeY); 100 } 101 102 private class BitmapShaderPaint implements java.awt.Paint { 103 private final BufferedImage mImage; 104 private final TileMode mTileModeX; 105 private final TileMode mTileModeY; 106 BitmapShaderPaint(BufferedImage image, TileMode tileModeX, TileMode tileModeY)107 BitmapShaderPaint(BufferedImage image, 108 TileMode tileModeX, TileMode tileModeY) { 109 mImage = image; 110 mTileModeX = tileModeX; 111 mTileModeY = tileModeY; 112 } 113 114 @Override createContext(ColorModel colorModel, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints)115 public PaintContext createContext(ColorModel colorModel, Rectangle deviceBounds, 116 Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) { 117 AffineTransform canvasMatrix; 118 try { 119 canvasMatrix = xform.createInverse(); 120 } catch (NoninvertibleTransformException e) { 121 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE, 122 "Unable to inverse matrix in BitmapShader", e, null /*data*/); 123 canvasMatrix = new AffineTransform(); 124 } 125 126 AffineTransform localMatrix = getLocalMatrix(); 127 try { 128 localMatrix = localMatrix.createInverse(); 129 } catch (NoninvertibleTransformException e) { 130 Bridge.getLog().fidelityWarning(LayoutLog.TAG_MATRIX_INVERSE, 131 "Unable to inverse matrix in BitmapShader", e, null /*data*/); 132 localMatrix = new AffineTransform(); 133 } 134 135 if (!colorModel.isCompatibleRaster(mImage.getRaster())) { 136 // Fallback to the default ARGB color model 137 colorModel = ColorModel.getRGBdefault(); 138 } 139 140 return new BitmapShaderContext(canvasMatrix, localMatrix, colorModel); 141 } 142 143 private class BitmapShaderContext implements PaintContext { 144 145 private final AffineTransform mCanvasMatrix; 146 private final AffineTransform mLocalMatrix; 147 private final ColorModel mColorModel; 148 BitmapShaderContext( AffineTransform canvasMatrix, AffineTransform localMatrix, ColorModel colorModel)149 public BitmapShaderContext( 150 AffineTransform canvasMatrix, 151 AffineTransform localMatrix, 152 ColorModel colorModel) { 153 mCanvasMatrix = canvasMatrix; 154 mLocalMatrix = localMatrix; 155 mColorModel = colorModel; 156 } 157 158 @Override dispose()159 public void dispose() { 160 } 161 162 @Override getColorModel()163 public ColorModel getColorModel() { 164 return mColorModel; 165 } 166 167 @Override getRaster(int x, int y, int w, int h)168 public Raster getRaster(int x, int y, int w, int h) { 169 BufferedImage image = new BufferedImage( 170 mColorModel, mColorModel.createCompatibleWritableRaster(w, h), 171 mColorModel.isAlphaPremultiplied(), null); 172 173 int[] data = new int[w*h]; 174 175 int index = 0; 176 float[] pt1 = new float[2]; 177 float[] pt2 = new float[2]; 178 for (int iy = 0 ; iy < h ; iy++) { 179 for (int ix = 0 ; ix < w ; ix++) { 180 // handle the canvas transform 181 pt1[0] = x + ix; 182 pt1[1] = y + iy; 183 mCanvasMatrix.transform(pt1, 0, pt2, 0, 1); 184 185 // handle the local matrix. 186 pt1[0] = pt2[0]; 187 pt1[1] = pt2[1]; 188 mLocalMatrix.transform(pt1, 0, pt2, 0, 1); 189 190 data[index++] = getColor(pt2[0], pt2[1]); 191 } 192 } 193 194 DataBufferInt dataBuffer = new DataBufferInt(data, data.length); 195 SampleModel colorModel = mColorModel.createCompatibleSampleModel(w, h); 196 return Raster.createWritableRaster(colorModel, dataBuffer, null); 197 } 198 } 199 200 /** 201 * Returns a color for an arbitrary point. 202 */ getColor(float fx, float fy)203 private int getColor(float fx, float fy) { 204 int x = getCoordinate(Math.round(fx), mImage.getWidth(), mTileModeX); 205 int y = getCoordinate(Math.round(fy), mImage.getHeight(), mTileModeY); 206 207 return mImage.getRGB(x, y); 208 } 209 getCoordinate(int i, int size, TileMode mode)210 private int getCoordinate(int i, int size, TileMode mode) { 211 if (i < 0) { 212 switch (mode) { 213 case CLAMP: 214 i = 0; 215 break; 216 case REPEAT: 217 i = size - 1 - (-i % size); 218 break; 219 case MIRROR: 220 // this is the same as the positive side, just make the value positive 221 // first. 222 i = -i; 223 int count = i / size; 224 i = i % size; 225 226 if ((count % 2) == 1) { 227 i = size - 1 - i; 228 } 229 break; 230 } 231 } else if (i >= size) { 232 switch (mode) { 233 case CLAMP: 234 i = size - 1; 235 break; 236 case REPEAT: 237 i = i % size; 238 break; 239 case MIRROR: 240 int count = i / size; 241 i = i % size; 242 243 if ((count % 2) == 1) { 244 i = size - 1 - i; 245 } 246 break; 247 } 248 } 249 250 return i; 251 } 252 253 254 @Override getTransparency()255 public int getTransparency() { 256 return java.awt.Paint.TRANSLUCENT; 257 } 258 } 259 } 260