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 ignored) {
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 ignored) {
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        return chunkObject != null;
154
155    }
156
157    @LayoutlibDelegate
158    /*package*/ static long validateNinePatchChunk(byte[] chunk) {
159        // the default JNI implementation only checks that the byte[] has the same
160        // size as the C struct it represent. Since we cannot do the same check (serialization
161        // will return different size depending on content), we do nothing.
162        NinePatch_Delegate newDelegate = new NinePatch_Delegate();
163        newDelegate.chunk = chunk;
164        return sManager.addNewDelegate(newDelegate);
165    }
166
167    @LayoutlibDelegate
168    /*package*/ static void nativeFinalize(long chunk) {
169        sManager.removeJavaReferenceFor(chunk);
170    }
171
172    @LayoutlibDelegate
173    /*package*/ static void nativeDraw(long canvas_instance, RectF loc, Bitmap bitmap_instance,
174            long chunk, long paint_instance_or_null, int destDensity, int srcDensity) {
175        draw(canvas_instance,
176                (int) loc.left, (int) loc.top, (int) loc.right, (int) loc.bottom,
177                bitmap_instance, chunk, paint_instance_or_null,
178                destDensity, srcDensity);
179    }
180
181    @LayoutlibDelegate
182    /*package*/ static void nativeDraw(long canvas_instance, Rect loc, Bitmap bitmap_instance,
183            long chunk, long paint_instance_or_null, int destDensity, int srcDensity) {
184        draw(canvas_instance,
185                loc.left, loc.top, loc.right, loc.bottom,
186                bitmap_instance, chunk, paint_instance_or_null,
187                destDensity, srcDensity);
188    }
189
190    @LayoutlibDelegate
191    /*package*/ static long nativeGetTransparentRegion(Bitmap bitmap, long chunk, Rect location) {
192        return 0;
193    }
194
195    // ---- Private Helper methods ----
196
197    private static void draw(long canvas_instance,
198            final int left, final int top, final int right, final int bottom,
199            Bitmap bitmap_instance, long chunk, long paint_instance_or_null,
200            final int destDensity, final int srcDensity) {
201        // get the delegate from the native int.
202        final Bitmap_Delegate bitmap_delegate = Bitmap_Delegate.getDelegate(bitmap_instance);
203        if (bitmap_delegate == null) {
204            return;
205        }
206
207        byte[] c = null;
208        NinePatch_Delegate delegate = sManager.getDelegate(chunk);
209        if (delegate != null) {
210            c = delegate.chunk;
211        }
212        if (c == null) {
213            // not a 9-patch?
214            BufferedImage image = bitmap_delegate.getImage();
215            Canvas_Delegate.native_drawBitmap(null, canvas_instance, bitmap_instance,
216                    0f, 0f, (float)image.getWidth(), (float)image.getHeight(),
217                    (float)left, (float)top, (float)right, (float)bottom,
218                    paint_instance_or_null, destDensity, srcDensity);
219            return;
220        }
221
222        final NinePatchChunk chunkObject = getChunk(c);
223        assert chunkObject != null;
224        if (chunkObject == null) {
225            return;
226        }
227
228        Canvas_Delegate canvas_delegate = Canvas_Delegate.getDelegate(canvas_instance);
229        if (canvas_delegate == null) {
230            return;
231        }
232
233        // this one can be null
234        Paint_Delegate paint_delegate = Paint_Delegate.getDelegate(paint_instance_or_null);
235
236        canvas_delegate.getSnapshot().draw(new GcSnapshot.Drawable() {
237                @Override
238                public void draw(Graphics2D graphics, Paint_Delegate paint) {
239                    chunkObject.draw(bitmap_delegate.getImage(), graphics,
240                            left, top, right - left, bottom - top, destDensity, srcDensity);
241                }
242            }, paint_delegate, true /*compositeOnly*/, false /*forceSrcMode*/);
243
244     }
245}
246