NinePatch_Delegate.java revision b1da1afa7418960b650780250bbd34c81af61aa3
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           assert false;
134           return;
135       }
136
137       if (c == null) {
138           // not a 9-patch?
139           BufferedImage image = bitmap_delegate.getImage();
140           Canvas_Delegate.native_drawBitmap(canvas_instance, bitmap_instance,
141                   new Rect(0, 0, image.getWidth(), image.getHeight()),
142                   new Rect(left, top, right, bottom),
143                   paint_instance_or_null, destDensity, srcDensity);
144           return;
145       }
146
147       final NinePatchChunk chunkObject = getChunk(c);
148       assert chunkObject != null;
149       if (chunkObject == null) {
150           return;
151       }
152
153       Canvas_Delegate canvas_delegate = Canvas_Delegate.getDelegate(canvas_instance);
154       if (canvas_delegate == null) {
155           assert false;
156           return;
157       }
158
159       // this one can be null
160       Paint_Delegate paint_delegate = Paint_Delegate.getDelegate(paint_instance_or_null);
161
162       canvas_delegate.getSnapshot().draw(new GcSnapshot.Drawable() {
163               public void draw(Graphics2D graphics, Paint_Delegate paint) {
164                   chunkObject.draw(bitmap_delegate.getImage(), graphics,
165                           left, top, right - left, bottom - top, destDensity, srcDensity);
166               }
167           }, paint_delegate, true /*compositeOnly*/, false /*forceSrcMode*/);
168
169    }
170
171    /*package*/ static int nativeGetTransparentRegion(int bitmap, byte[] chunk, Rect location) {
172        return 0;
173    }
174
175    // ---- Private Helper methods ----
176
177    /**
178     * Returns a {@link NinePatchChunk} object for the given serialized representation.
179     *
180     * If the chunk is present in the cache then the object from the cache is returned, otherwise
181     * the array is deserialized into a {@link NinePatchChunk} object.
182     *
183     * @param array the serialized representation of the chunk.
184     * @return the NinePatchChunk or null if deserialization failed.
185     */
186    private static NinePatchChunk getChunk(byte[] array) {
187        SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array);
188        NinePatchChunk chunk = chunkRef.get();
189        if (chunk == null) {
190            ByteArrayInputStream bais = new ByteArrayInputStream(array);
191            ObjectInputStream ois = null;
192            try {
193                ois = new ObjectInputStream(bais);
194                chunk = (NinePatchChunk) ois.readObject();
195
196                // put back the chunk in the cache
197                if (chunk != null) {
198                    sChunkCache.put(array, new SoftReference<NinePatchChunk>(chunk));
199                }
200            } catch (IOException e) {
201                Bridge.getLog().error("Failed to deserialize NinePatchChunk content.", e);
202                return null;
203            } catch (ClassNotFoundException e) {
204                Bridge.getLog().error("Failed to deserialize NinePatchChunk class.", e);
205                return null;
206            } finally {
207                if (ois != null) {
208                    try {
209                        ois.close();
210                    } catch (IOException e) {
211                    }
212                }
213            }
214        }
215
216        return chunk;
217    }
218}
219