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