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