Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | package android.support.rastermill; |
| 18 | |
| 19 | import android.graphics.Bitmap; |
| 20 | |
| 21 | import java.io.InputStream; |
| 22 | |
| 23 | public class FrameSequence { |
| 24 | static { |
| 25 | System.loadLibrary("framesequence"); |
| 26 | } |
| 27 | |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 28 | private final long mNativeFrameSequence; |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 29 | private final int mWidth; |
| 30 | private final int mHeight; |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 31 | private final boolean mOpaque; |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 32 | private final int mFrameCount; |
| 33 | private final int mDefaultLoopCount; |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 34 | |
| 35 | public int getWidth() { return mWidth; } |
| 36 | public int getHeight() { return mHeight; } |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 37 | public boolean isOpaque() { return mOpaque; } |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 38 | public int getFrameCount() { return mFrameCount; } |
| 39 | public int getDefaultLoopCount() { return mDefaultLoopCount; } |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 40 | |
| 41 | private static native FrameSequence nativeDecodeByteArray(byte[] data, int offset, int length); |
| 42 | private static native FrameSequence nativeDecodeStream(InputStream is, byte[] tempStorage); |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 43 | private static native void nativeDestroyFrameSequence(long nativeFrameSequence); |
| 44 | private static native long nativeCreateState(long nativeFrameSequence); |
| 45 | private static native void nativeDestroyState(long nativeState); |
| 46 | private static native long nativeGetFrame(long nativeState, int frameNr, |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 47 | Bitmap output, int previousFrameNr); |
| 48 | |
| 49 | @SuppressWarnings("unused") // called by native |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 50 | private FrameSequence(long nativeFrameSequence, int width, int height, |
| 51 | boolean opaque, int frameCount, int defaultLoopCount) { |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 52 | mNativeFrameSequence = nativeFrameSequence; |
| 53 | mWidth = width; |
| 54 | mHeight = height; |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 55 | mOpaque = opaque; |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 56 | mFrameCount = frameCount; |
| 57 | mDefaultLoopCount = defaultLoopCount; |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public static FrameSequence decodeByteArray(byte[] data) { |
| 61 | return decodeByteArray(data, 0, data.length); |
| 62 | } |
| 63 | |
| 64 | public static FrameSequence decodeByteArray(byte[] data, int offset, int length) { |
| 65 | if (data == null) throw new IllegalArgumentException(); |
| 66 | if (offset < 0 || length < 0 || (offset + length > data.length)) { |
| 67 | throw new IllegalArgumentException("invalid offset/length parameters"); |
| 68 | } |
| 69 | return nativeDecodeByteArray(data, offset, length); |
| 70 | } |
| 71 | |
| 72 | public static FrameSequence decodeStream(InputStream stream) { |
| 73 | if (stream == null) throw new IllegalArgumentException(); |
| 74 | byte[] tempStorage = new byte[16 * 1024]; // TODO: use buffer pool |
| 75 | return nativeDecodeStream(stream, tempStorage); |
| 76 | } |
| 77 | |
| 78 | State createState() { |
| 79 | if (mNativeFrameSequence == 0) { |
| 80 | throw new IllegalStateException("attempted to use incorrectly built FrameSequence"); |
| 81 | } |
| 82 | |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 83 | long nativeState = nativeCreateState(mNativeFrameSequence); |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 84 | if (nativeState == 0) { |
| 85 | return null; |
| 86 | } |
| 87 | return new State(nativeState); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void finalize() throws Throwable { |
| 92 | try { |
| 93 | if (mNativeFrameSequence != 0) nativeDestroyFrameSequence(mNativeFrameSequence); |
| 94 | } finally { |
| 95 | super.finalize(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Playback state used when moving frames forward in a frame sequence. |
| 101 | * |
| 102 | * Note that this doesn't require contiguous frames to be rendered, it just stores |
| 103 | * information (in the case of gif, a recall buffer) that will be used to construct |
| 104 | * frames based upon data recorded before previousFrameNr. |
| 105 | * |
Chris Craik | 4eb541a | 2014-04-23 15:06:06 -0700 | [diff] [blame] | 106 | * Note: {@link #destroy()} *must* be called before the object is GC'd to free native resources |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 107 | * |
| 108 | * Note: State holds a native ref to its FrameSequence instance, so its FrameSequence should |
| 109 | * remain ref'd while it is in use |
| 110 | */ |
| 111 | static class State { |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 112 | private long mNativeState; |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 113 | |
Chris Craik | e36c5d6 | 2014-01-13 19:37:04 -0800 | [diff] [blame] | 114 | public State(long nativeState) { |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 115 | mNativeState = nativeState; |
| 116 | } |
| 117 | |
Chris Craik | 4eb541a | 2014-04-23 15:06:06 -0700 | [diff] [blame] | 118 | public void destroy() { |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 119 | if (mNativeState != 0) { |
| 120 | nativeDestroyState(mNativeState); |
| 121 | mNativeState = 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // TODO: consider adding alternate API for drawing into a SurfaceTexture |
| 126 | public long getFrame(int frameNr, Bitmap output, int previousFrameNr) { |
| 127 | if (output == null || output.getConfig() != Bitmap.Config.ARGB_8888) { |
| 128 | throw new IllegalArgumentException("Bitmap passed must be non-null and ARGB_8888"); |
| 129 | } |
| 130 | if (mNativeState == 0) { |
Chris Craik | 4eb541a | 2014-04-23 15:06:06 -0700 | [diff] [blame] | 131 | throw new IllegalStateException("attempted to draw destroyed FrameSequenceState"); |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 132 | } |
| 133 | return nativeGetFrame(mNativeState, frameNr, output, previousFrameNr); |
| 134 | } |
| 135 | } |
Chris Craik | a3ac0a2 | 2014-01-06 12:43:42 -0800 | [diff] [blame] | 136 | } |