Picture.java revision 4b0959d8db20c08ab1fed37f397b303af229162b
1/*
2 * Copyright (C) 2007 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 java.io.InputStream;
20import java.io.OutputStream;
21
22/**
23 * A Picture records drawing calls (via the canvas returned by beginRecording)
24 * and can then play them back into Canvas (via {@link Picture#draw(Canvas)} or
25 * {@link Canvas#drawPicture(Picture)}).For most content (e.g. text, lines, rectangles),
26 * drawing a sequence from a picture can be faster than the equivalent API
27 * calls, since the picture performs its playback without incurring any
28 * method-call overhead.
29 */
30public class Picture {
31    private Canvas mRecordingCanvas;
32    private final long mNativePicture;
33
34    private static final int WORKING_STREAM_STORAGE = 16 * 1024;
35
36    /**
37     * Creates an empty picture that is ready to record.
38     */
39    public Picture() {
40        this(nativeConstructor(0));
41    }
42
43    /**
44     * Create a picture by making a copy of what has already been recorded in
45     * src. The contents of src are unchanged, and if src changes later, those
46     * changes will not be reflected in this picture.
47     */
48    public Picture(Picture src) {
49        this(nativeConstructor(src != null ? src.mNativePicture : 0));
50    }
51
52    private Picture(long nativePicture) {
53        if (nativePicture == 0) {
54            throw new RuntimeException();
55        }
56        mNativePicture = nativePicture;
57    }
58
59    @Override
60    protected void finalize() throws Throwable {
61        try {
62            nativeDestructor(mNativePicture);
63        } finally {
64            super.finalize();
65        }
66    }
67
68    /**
69     * To record a picture, call beginRecording() and then draw into the Canvas
70     * that is returned. Nothing we appear on screen, but all of the draw
71     * commands (e.g. {@link Canvas#drawRect(Rect, Paint)}) will be recorded.
72     * To stop recording, call endRecording(). After endRecording() the Canvas
73     * that was returned must no longer be used, and nothing should be drawn
74     * into it.
75     */
76    public Canvas beginRecording(int width, int height) {
77        long ni = nativeBeginRecording(mNativePicture, width, height);
78        mRecordingCanvas = new RecordingCanvas(this, ni);
79        return mRecordingCanvas;
80    }
81
82    /**
83     * Call endRecording when the picture is built. After this call, the picture
84     * may be drawn, but the canvas that was returned by beginRecording must not
85     * be used anymore. This is automatically called if {@link Picture#draw}
86     * or {@link Canvas#drawPicture(Picture)} is called.
87     */
88    public void endRecording() {
89        if (mRecordingCanvas != null) {
90            mRecordingCanvas = null;
91            nativeEndRecording(mNativePicture);
92        }
93    }
94
95    /**
96     * Get the width of the picture as passed to beginRecording. This
97     * does not reflect (per se) the content of the picture.
98     */
99    public int getWidth() {
100      return nativeGetWidth(mNativePicture);
101    }
102
103    /**
104     * Get the height of the picture as passed to beginRecording. This
105     * does not reflect (per se) the content of the picture.
106     */
107    public int getHeight() {
108      return nativeGetHeight(mNativePicture);
109    }
110
111    /**
112     * Draw this picture on the canvas.
113     * <p>
114     * Prior to {@link android.os.Build.VERSION_CODES#L}, this call could
115     * have the side effect of changing the matrix and clip of the canvas
116     * if this picture had imbalanced saves/restores.
117     *
118     * <p>
119     * <strong>Note:</strong> This forces the picture to internally call
120     * {@link Picture#endRecording()} in order to prepare for playback.
121     *
122     * @param canvas  The picture is drawn to this canvas
123     */
124    public void draw(Canvas canvas) {
125        if (mRecordingCanvas != null) {
126            endRecording();
127        }
128        nativeDraw(canvas.getNativeCanvasWrapper(), mNativePicture);
129    }
130
131    /**
132     * Create a new picture (already recorded) from the data in the stream. This
133     * data was generated by a previous call to writeToStream(). Pictures that
134     * have been persisted across device restarts are not guaranteed to decode
135     * properly and are highly discouraged.
136     *
137     * <p>
138     * <strong>Note:</strong> a picture created from an input stream cannot be
139     * replayed on a hardware accelerated canvas.
140     *
141     * @see #writeToStream(java.io.OutputStream)
142     * @deprecated The recommended alternative is to not use writeToStream and
143     * instead draw the picture into a Bitmap from which you can persist it as
144     * raw or compressed pixels.
145     */
146    @Deprecated
147    public static Picture createFromStream(InputStream stream) {
148        return new Picture(nativeCreateFromStream(stream, new byte[WORKING_STREAM_STORAGE]));
149    }
150
151    /**
152     * Write the picture contents to a stream. The data can be used to recreate
153     * the picture in this or another process by calling createFromStream(...)
154     * The resulting stream is NOT to be persisted across device restarts as
155     * there is no guarantee that the Picture can be successfully reconstructed.
156     *
157     * <p>
158     * <strong>Note:</strong> a picture created from an input stream cannot be
159     * replayed on a hardware accelerated canvas.
160     *
161     * @see #createFromStream(java.io.InputStream)
162     * @deprecated The recommended alternative is to draw the picture into a
163     * Bitmap from which you can persist it as raw or compressed pixels.
164     */
165    @Deprecated
166    public void writeToStream(OutputStream stream) {
167        // do explicit check before calling the native method
168        if (stream == null) {
169            throw new NullPointerException();
170        }
171        if (!nativeWriteToStream(mNativePicture, stream,
172                             new byte[WORKING_STREAM_STORAGE])) {
173            throw new RuntimeException();
174        }
175    }
176
177    // return empty picture if src is 0, or a copy of the native src
178    private static native long nativeConstructor(long nativeSrcOr0);
179    private static native long nativeCreateFromStream(InputStream stream, byte[] storage);
180    private static native int nativeGetWidth(long nativePicture);
181    private static native int nativeGetHeight(long nativePicture);
182    private static native long nativeBeginRecording(long nativeCanvas, int w, int h);
183    private static native void nativeEndRecording(long nativeCanvas);
184    private static native void nativeDraw(long nativeCanvas, long nativePicture);
185    private static native boolean nativeWriteToStream(long nativePicture,
186                                           OutputStream stream, byte[] storage);
187    private static native void nativeDestructor(long nativePicture);
188
189    private static class RecordingCanvas extends Canvas {
190        private final Picture mPicture;
191
192        public RecordingCanvas(Picture pict, long nativeCanvas) {
193            super(nativeCanvas);
194            mPicture = pict;
195        }
196
197        @Override
198        public void setBitmap(Bitmap bitmap) {
199            throw new RuntimeException("Cannot call setBitmap on a picture canvas");
200        }
201
202        @Override
203        public void drawPicture(Picture picture) {
204            if (mPicture == picture) {
205                throw new RuntimeException("Cannot draw a picture into its recording canvas");
206            }
207            super.drawPicture(picture);
208        }
209    }
210}
211