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