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