Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 1 | #include "ScopedLocalRef.h" |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 2 | #include "SkFrontBufferedStream.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | #include "SkMovie.h" |
| 4 | #include "SkStream.h" |
| 5 | #include "GraphicsJNI.h" |
| 6 | #include "SkTemplates.h" |
| 7 | #include "SkUtils.h" |
Leon Scroggins III | ca32021 | 2013-08-20 17:59:39 -0400 | [diff] [blame] | 8 | #include "Utils.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | #include "CreateJavaOutputStreamAdaptor.h" |
| 10 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 11 | #include <androidfw/Asset.h> |
| 12 | #include <androidfw/ResourceTypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13 | #include <netinet/in.h> |
| 14 | |
| 15 | #if 0 |
| 16 | #define TRACE_BITMAP(code) code |
| 17 | #else |
| 18 | #define TRACE_BITMAP(code) |
| 19 | #endif |
| 20 | |
| 21 | static jclass gMovie_class; |
| 22 | static jmethodID gMovie_constructorMethodID; |
| 23 | static jfieldID gMovie_nativeInstanceID; |
| 24 | |
| 25 | jobject create_jmovie(JNIEnv* env, SkMovie* moov) { |
| 26 | if (NULL == moov) { |
| 27 | return NULL; |
| 28 | } |
Elliott Hughes | cf6f7a0 | 2011-04-12 17:50:45 -0700 | [diff] [blame] | 29 | return env->NewObject(gMovie_class, gMovie_constructorMethodID, |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 30 | static_cast<jlong>(reinterpret_cast<uintptr_t>(moov))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static SkMovie* J2Movie(JNIEnv* env, jobject movie) { |
| 34 | SkASSERT(env); |
| 35 | SkASSERT(movie); |
| 36 | SkASSERT(env->IsInstanceOf(movie, gMovie_class)); |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 37 | SkMovie* m = (SkMovie*)env->GetLongField(movie, gMovie_nativeInstanceID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | SkASSERT(m); |
| 39 | return m; |
| 40 | } |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 44 | static jint movie_width(JNIEnv* env, jobject movie) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | NPE_CHECK_RETURN_ZERO(env, movie); |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 46 | return static_cast<jint>(J2Movie(env, movie)->width()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 49 | static jint movie_height(JNIEnv* env, jobject movie) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | NPE_CHECK_RETURN_ZERO(env, movie); |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 51 | return static_cast<jint>(J2Movie(env, movie)->height()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static jboolean movie_isOpaque(JNIEnv* env, jobject movie) { |
| 55 | NPE_CHECK_RETURN_ZERO(env, movie); |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 56 | return J2Movie(env, movie)->isOpaque() ? JNI_TRUE : JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 59 | static jint movie_duration(JNIEnv* env, jobject movie) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | NPE_CHECK_RETURN_ZERO(env, movie); |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 61 | return static_cast<jint>(J2Movie(env, movie)->duration()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 64 | static jboolean movie_setTime(JNIEnv* env, jobject movie, jint ms) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | NPE_CHECK_RETURN_ZERO(env, movie); |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 66 | return J2Movie(env, movie)->setTime(ms) ? JNI_TRUE : JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void movie_draw(JNIEnv* env, jobject movie, jobject canvas, |
| 70 | jfloat fx, jfloat fy, jobject jpaint) { |
| 71 | NPE_CHECK_RETURN_VOID(env, movie); |
| 72 | NPE_CHECK_RETURN_VOID(env, canvas); |
| 73 | // its OK for paint to be null |
| 74 | |
| 75 | SkMovie* m = J2Movie(env, movie); |
| 76 | SkCanvas* c = GraphicsJNI::getNativeCanvas(env, canvas); |
| 77 | SkScalar sx = SkFloatToScalar(fx); |
| 78 | SkScalar sy = SkFloatToScalar(fy); |
| 79 | const SkBitmap& b = m->bitmap(); |
| 80 | const SkPaint* p = jpaint ? GraphicsJNI::getNativePaint(env, jpaint) : NULL; |
| 81 | |
| 82 | c->drawBitmap(b, sx, sy, p); |
| 83 | } |
| 84 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 85 | static jobject movie_decodeAsset(JNIEnv* env, jobject clazz, jlong native_asset) { |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 86 | android::Asset* asset = reinterpret_cast<android::Asset*>(native_asset); |
| 87 | if (asset == NULL) return NULL; |
| 88 | SkAutoTUnref<SkStreamRewindable> stream (new android::AssetStreamAdaptor(asset)); |
| 89 | SkMovie* moov = SkMovie::DecodeStream(stream.get()); |
| 90 | return create_jmovie(env, moov); |
| 91 | } |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | static jobject movie_decodeStream(JNIEnv* env, jobject clazz, jobject istream) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | NPE_CHECK_RETURN_ZERO(env, istream); |
| 96 | |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 97 | jbyteArray byteArray = env->NewByteArray(16*1024); |
| 98 | ScopedLocalRef<jbyteArray> scoper(env, byteArray); |
| 99 | SkStream* strm = CreateJavaInputStreamAdaptor(env, istream, byteArray); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | if (NULL == strm) { |
| 101 | return 0; |
| 102 | } |
| 103 | |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 104 | // Need to buffer enough input to be able to rewind as much as might be read by a decoder |
| 105 | // trying to determine the stream's format. The only decoder for movies is GIF, which |
| 106 | // will only read 6. |
| 107 | // FIXME: Get this number from SkImageDecoder |
| 108 | SkAutoTUnref<SkStreamRewindable> bufferedStream(SkFrontBufferedStream::Create(strm, 6)); |
| 109 | SkASSERT(bufferedStream.get() != NULL); |
| 110 | |
| 111 | SkMovie* moov = SkMovie::DecodeStream(bufferedStream); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | strm->unref(); |
| 113 | return create_jmovie(env, moov); |
| 114 | } |
| 115 | |
| 116 | static jobject movie_decodeByteArray(JNIEnv* env, jobject clazz, |
| 117 | jbyteArray byteArray, |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 118 | jint offset, jint length) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | NPE_CHECK_RETURN_ZERO(env, byteArray); |
| 121 | |
| 122 | int totalLength = env->GetArrayLength(byteArray); |
| 123 | if ((offset | length) < 0 || offset + length > totalLength) { |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 124 | doThrowAIOOBE(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | AutoJavaByteArray ar(env, byteArray); |
| 129 | SkMovie* moov = SkMovie::DecodeMemory(ar.ptr() + offset, length); |
| 130 | return create_jmovie(env, moov); |
| 131 | } |
| 132 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 133 | static void movie_destructor(JNIEnv* env, jobject, jlong movieHandle) { |
| 134 | SkMovie* movie = (SkMovie*) movieHandle; |
Kimiyoshi Kusaka | 08d7778 | 2011-03-28 11:58:51 +0900 | [diff] [blame] | 135 | delete movie; |
| 136 | } |
| 137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | ////////////////////////////////////////////////////////////////////////////////////////////// |
| 139 | |
| 140 | #include <android_runtime/AndroidRuntime.h> |
| 141 | |
| 142 | static JNINativeMethod gMethods[] = { |
| 143 | { "width", "()I", (void*)movie_width }, |
| 144 | { "height", "()I", (void*)movie_height }, |
| 145 | { "isOpaque", "()Z", (void*)movie_isOpaque }, |
| 146 | { "duration", "()I", (void*)movie_duration }, |
| 147 | { "setTime", "(I)Z", (void*)movie_setTime }, |
| 148 | { "draw", "(Landroid/graphics/Canvas;FFLandroid/graphics/Paint;)V", |
| 149 | (void*)movie_draw }, |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 150 | { "nativeDecodeAsset", "(J)Landroid/graphics/Movie;", |
Leon Scroggins III | 7315f1b | 2013-09-10 20:26:05 -0400 | [diff] [blame] | 151 | (void*)movie_decodeAsset }, |
| 152 | { "nativeDecodeStream", "(Ljava/io/InputStream;)Landroid/graphics/Movie;", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | (void*)movie_decodeStream }, |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 154 | { "nativeDestructor","(J)V", (void*)movie_destructor }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | { "decodeByteArray", "([BII)Landroid/graphics/Movie;", |
| 156 | (void*)movie_decodeByteArray }, |
| 157 | }; |
| 158 | |
| 159 | #define kClassPathName "android/graphics/Movie" |
| 160 | |
| 161 | #define RETURN_ERR_IF_NULL(value) do { if (!(value)) { assert(0); return -1; } } while (false) |
| 162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | int register_android_graphics_Movie(JNIEnv* env) |
| 164 | { |
| 165 | gMovie_class = env->FindClass(kClassPathName); |
| 166 | RETURN_ERR_IF_NULL(gMovie_class); |
| 167 | gMovie_class = (jclass)env->NewGlobalRef(gMovie_class); |
Elliott Hughes | 8451b25 | 2011-04-07 19:17:57 -0700 | [diff] [blame] | 168 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 169 | gMovie_constructorMethodID = env->GetMethodID(gMovie_class, "<init>", "(J)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | RETURN_ERR_IF_NULL(gMovie_constructorMethodID); |
| 171 | |
Ashok Bhat | dcaf559 | 2014-01-08 16:47:08 +0000 | [diff] [blame] | 172 | gMovie_nativeInstanceID = env->GetFieldID(gMovie_class, "mNativeMovie", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | RETURN_ERR_IF_NULL(gMovie_nativeInstanceID); |
| 174 | |
| 175 | return android::AndroidRuntime::registerNativeMethods(env, kClassPathName, |
| 176 | gMethods, SK_ARRAY_COUNT(gMethods)); |
| 177 | } |