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