NinePatch_Delegate.java revision d43909c7503e11eb335a452d296a10804bb01fd6
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 17package android.graphics; 18 19import com.android.layoutlib.bridge.Bridge; 20import com.android.layoutlib.bridge.impl.DelegateManager; 21import com.android.layoutlib.bridge.impl.GcSnapshot; 22import com.android.ninepatch.NinePatchChunk; 23 24import android.graphics.drawable.NinePatchDrawable; 25 26import java.awt.Graphics2D; 27import java.awt.image.BufferedImage; 28import java.io.ByteArrayInputStream; 29import java.io.ByteArrayOutputStream; 30import java.io.IOException; 31import java.io.ObjectInputStream; 32import java.io.ObjectOutputStream; 33import java.lang.ref.SoftReference; 34import java.util.HashMap; 35import java.util.Map; 36 37/** 38 * Delegate implementing the native methods of android.graphics.NinePatch 39 * 40 * Through the layoutlib_create tool, the original native methods of NinePatch have been replaced 41 * by calls to methods of the same name in this delegate class. 42 * 43 * Because it's a stateless class to start with, there's no need to keep a {@link DelegateManager} 44 * around to map int to instance of the delegate. 45 * 46 */ 47public final class NinePatch_Delegate { 48 49 /** 50 * Cache map for {@link NinePatchChunk}. 51 * When the chunks are created they are serialized into a byte[], and both are put 52 * in the cache, using a {@link SoftReference} for the chunk. The default Java classes 53 * for {@link NinePatch} and {@link NinePatchDrawable} only reference to the byte[] data, and 54 * provide this for drawing. 55 * Using the cache map allows us to not have to deserialize the byte[] back into a 56 * {@link NinePatchChunk} every time a rendering is done. 57 */ 58 private final static Map<byte[], SoftReference<NinePatchChunk>> sChunkCache = 59 new HashMap<byte[], SoftReference<NinePatchChunk>>(); 60 61 // ---- Public Helper methods ---- 62 63 /** 64 * Serializes the given chunk. 65 * 66 * @return the serialized data for the chunk. 67 */ 68 public static byte[] serialize(NinePatchChunk chunk) { 69 // serialize the chunk to get a byte[] 70 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 71 ObjectOutputStream oos = null; 72 try { 73 oos = new ObjectOutputStream(baos); 74 oos.writeObject(chunk); 75 } catch (IOException e) { 76 Bridge.getLog().error("Failed to serialize NinePatchChunk.", e); 77 return null; 78 } finally { 79 if (oos != null) { 80 try { 81 oos.close(); 82 } catch (IOException e) { 83 } 84 } 85 } 86 87 // get the array and add it to the cache 88 byte[] array = baos.toByteArray(); 89 sChunkCache.put(array, new SoftReference<NinePatchChunk>(chunk)); 90 return array; 91 } 92 93 // ---- native methods ---- 94 95 /*package*/ static boolean isNinePatchChunk(byte[] chunk) { 96 NinePatchChunk chunkObject = getChunk(chunk); 97 if (chunkObject != null) { 98 return true; 99 } 100 101 return false; 102 } 103 104 /*package*/ static void validateNinePatchChunk(int bitmap, byte[] chunk) { 105 // the default JNI implementation only checks that the byte[] has the same 106 // size as the C struct it represent. Since we cannot do the same check (serialization 107 // will return different size depending on content), we do nothing. 108 } 109 110 /*package*/ static void nativeDraw(int canvas_instance, RectF loc, int bitmap_instance, 111 byte[] c, int paint_instance_or_null, int destDensity, int srcDensity) { 112 draw(canvas_instance, 113 (int) loc.left, (int) loc.top, (int) loc.width(), (int) loc.height(), 114 bitmap_instance, c, paint_instance_or_null, 115 destDensity, srcDensity); 116 } 117 118 /*package*/ static void nativeDraw(int canvas_instance, Rect loc, int bitmap_instance, 119 byte[] c, int paint_instance_or_null, int destDensity, int srcDensity) { 120 draw(canvas_instance, 121 loc.left, loc.top, loc.width(), loc.height(), 122 bitmap_instance, c, paint_instance_or_null, 123 destDensity, srcDensity); 124 } 125 126 private static void draw(int canvas_instance, 127 final int left, final int top, final int right, final int bottom, 128 int bitmap_instance, byte[] c, int paint_instance_or_null, 129 final int destDensity, final int srcDensity) { 130 // get the delegate from the native int. 131 final Bitmap_Delegate bitmap_delegate = Bitmap_Delegate.getDelegate(bitmap_instance); 132 if (bitmap_delegate == null) { 133 return; 134 } 135 136 if (c == null) { 137 // not a 9-patch? 138 BufferedImage image = bitmap_delegate.getImage(); 139 Canvas_Delegate.native_drawBitmap(canvas_instance, bitmap_instance, 140 new Rect(0, 0, image.getWidth(), image.getHeight()), 141 new Rect(left, top, right, bottom), 142 paint_instance_or_null, destDensity, srcDensity); 143 return; 144 } 145 146 final NinePatchChunk chunkObject = getChunk(c); 147 assert chunkObject != null; 148 if (chunkObject == null) { 149 return; 150 } 151 152 Canvas_Delegate canvas_delegate = Canvas_Delegate.getDelegate(canvas_instance); 153 if (canvas_delegate == null) { 154 return; 155 } 156 157 // this one can be null 158 Paint_Delegate paint_delegate = Paint_Delegate.getDelegate(paint_instance_or_null); 159 160 canvas_delegate.getSnapshot().draw(new GcSnapshot.Drawable() { 161 public void draw(Graphics2D graphics, Paint_Delegate paint) { 162 chunkObject.draw(bitmap_delegate.getImage(), graphics, 163 left, top, right - left, bottom - top, destDensity, srcDensity); 164 } 165 }, paint_delegate, true /*compositeOnly*/, false /*forceSrcMode*/); 166 167 } 168 169 /*package*/ static int nativeGetTransparentRegion(int bitmap, byte[] chunk, Rect location) { 170 return 0; 171 } 172 173 // ---- Private Helper methods ---- 174 175 /** 176 * Returns a {@link NinePatchChunk} object for the given serialized representation. 177 * 178 * If the chunk is present in the cache then the object from the cache is returned, otherwise 179 * the array is deserialized into a {@link NinePatchChunk} object. 180 * 181 * @param array the serialized representation of the chunk. 182 * @return the NinePatchChunk or null if deserialization failed. 183 */ 184 private static NinePatchChunk getChunk(byte[] array) { 185 SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array); 186 NinePatchChunk chunk = chunkRef.get(); 187 if (chunk == null) { 188 ByteArrayInputStream bais = new ByteArrayInputStream(array); 189 ObjectInputStream ois = null; 190 try { 191 ois = new ObjectInputStream(bais); 192 chunk = (NinePatchChunk) ois.readObject(); 193 194 // put back the chunk in the cache 195 if (chunk != null) { 196 sChunkCache.put(array, new SoftReference<NinePatchChunk>(chunk)); 197 } 198 } catch (IOException e) { 199 Bridge.getLog().error("Failed to deserialize NinePatchChunk content.", e); 200 return null; 201 } catch (ClassNotFoundException e) { 202 Bridge.getLog().error("Failed to deserialize NinePatchChunk class.", e); 203 return null; 204 } finally { 205 if (ois != null) { 206 try { 207 ois.close(); 208 } catch (IOException e) { 209 } 210 } 211 } 212 } 213 214 return chunk; 215 } 216} 217