1 /*
2  * Copyright (C) 2008 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 #include "jni.h"
18 #include "GraphicsJNI.h"
19 #include "core_jni_helpers.h"
20 
21 #include "Picture.h"
22 
23 #include "SkCanvas.h"
24 #include "SkStream.h"
25 #include "SkTemplates.h"
26 #include "CreateJavaOutputStreamAdaptor.h"
27 
28 namespace android {
29 
android_graphics_Picture_newPicture(JNIEnv * env,jobject,jlong srcHandle)30 static jlong android_graphics_Picture_newPicture(JNIEnv* env, jobject, jlong srcHandle) {
31     const Picture* src = reinterpret_cast<Picture*>(srcHandle);
32     return reinterpret_cast<jlong>(new Picture(src));
33 }
34 
android_graphics_Picture_deserialize(JNIEnv * env,jobject,jobject jstream,jbyteArray jstorage)35 static jlong android_graphics_Picture_deserialize(JNIEnv* env, jobject, jobject jstream,
36                                                   jbyteArray jstorage) {
37     Picture* picture = NULL;
38     SkStream* strm = CreateJavaInputStreamAdaptor(env, jstream, jstorage);
39     if (strm) {
40         picture = Picture::CreateFromStream(strm);
41         delete strm;
42     }
43     return reinterpret_cast<jlong>(picture);
44 }
45 
android_graphics_Picture_killPicture(JNIEnv * env,jobject,jlong pictureHandle)46 static void android_graphics_Picture_killPicture(JNIEnv* env, jobject, jlong pictureHandle) {
47     Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
48     SkASSERT(picture);
49     delete picture;
50 }
51 
android_graphics_Picture_draw(JNIEnv * env,jobject,jlong canvasHandle,jlong pictureHandle)52 static void android_graphics_Picture_draw(JNIEnv* env, jobject, jlong canvasHandle,
53                                           jlong pictureHandle) {
54     Canvas* canvas = reinterpret_cast<Canvas*>(canvasHandle);
55     Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
56     SkASSERT(canvas);
57     SkASSERT(picture);
58     picture->draw(canvas);
59 }
60 
android_graphics_Picture_serialize(JNIEnv * env,jobject,jlong pictureHandle,jobject jstream,jbyteArray jstorage)61 static jboolean android_graphics_Picture_serialize(JNIEnv* env, jobject, jlong pictureHandle,
62                                                    jobject jstream, jbyteArray jstorage) {
63     Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
64     SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
65 
66     if (NULL != strm) {
67         picture->serialize(strm);
68         delete strm;
69         return JNI_TRUE;
70     }
71     return JNI_FALSE;
72 }
73 
android_graphics_Picture_getWidth(JNIEnv * env,jobject,jlong pictureHandle)74 static jint android_graphics_Picture_getWidth(JNIEnv* env, jobject, jlong pictureHandle) {
75     Picture* pict = reinterpret_cast<Picture*>(pictureHandle);
76     return static_cast<jint>(pict->width());
77 }
78 
android_graphics_Picture_getHeight(JNIEnv * env,jobject,jlong pictureHandle)79 static jint android_graphics_Picture_getHeight(JNIEnv* env, jobject, jlong pictureHandle) {
80     Picture* pict = reinterpret_cast<Picture*>(pictureHandle);
81     return static_cast<jint>(pict->height());
82 }
83 
android_graphics_Picture_beginRecording(JNIEnv * env,jobject,jlong pictHandle,jint w,jint h)84 static jlong android_graphics_Picture_beginRecording(JNIEnv* env, jobject, jlong pictHandle,
85                                                      jint w, jint h) {
86     Picture* pict = reinterpret_cast<Picture*>(pictHandle);
87     Canvas* canvas = pict->beginRecording(w, h);
88     return reinterpret_cast<jlong>(canvas);
89 }
90 
android_graphics_Picture_endRecording(JNIEnv * env,jobject,jlong pictHandle)91 static void android_graphics_Picture_endRecording(JNIEnv* env, jobject, jlong pictHandle) {
92     Picture* pict = reinterpret_cast<Picture*>(pictHandle);
93     pict->endRecording();
94 }
95 
96 static JNINativeMethod gMethods[] = {
97     {"nativeGetWidth", "(J)I", (void*) android_graphics_Picture_getWidth},
98     {"nativeGetHeight", "(J)I", (void*) android_graphics_Picture_getHeight},
99     {"nativeConstructor", "(J)J", (void*) android_graphics_Picture_newPicture},
100     {"nativeCreateFromStream", "(Ljava/io/InputStream;[B)J", (void*)android_graphics_Picture_deserialize},
101     {"nativeBeginRecording", "(JII)J", (void*) android_graphics_Picture_beginRecording},
102     {"nativeEndRecording", "(J)V", (void*) android_graphics_Picture_endRecording},
103     {"nativeDraw", "(JJ)V", (void*) android_graphics_Picture_draw},
104     {"nativeWriteToStream", "(JLjava/io/OutputStream;[B)Z", (void*)android_graphics_Picture_serialize},
105     {"nativeDestructor","(J)V", (void*) android_graphics_Picture_killPicture}
106 };
107 
register_android_graphics_Picture(JNIEnv * env)108 int register_android_graphics_Picture(JNIEnv* env) {
109     return RegisterMethodsOrDie(env, "android/graphics/Picture", gMethods, NELEM(gMethods));
110 }
111 
112 }; // namespace android
113