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