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
23 #include <array>
24 #include "nativehelper/jni_macros.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 std::array gMethods = {
95 MAKE_JNI_NATIVE_METHOD("nativeGetWidth", "(J)I", android_graphics_Picture_getWidth),
96 MAKE_JNI_NATIVE_METHOD("nativeGetHeight", "(J)I", android_graphics_Picture_getHeight),
97 MAKE_JNI_NATIVE_METHOD("nativeConstructor", "(J)J", android_graphics_Picture_newPicture),
98 MAKE_JNI_NATIVE_METHOD("nativeCreateFromStream", "(Ljava/io/InputStream;[B)J", android_graphics_Picture_deserialize),
99 MAKE_JNI_NATIVE_METHOD("nativeBeginRecording", "(JII)J", android_graphics_Picture_beginRecording),
100 MAKE_JNI_NATIVE_METHOD("nativeEndRecording", "(J)V", android_graphics_Picture_endRecording),
101 MAKE_JNI_NATIVE_METHOD("nativeDraw", "(JJ)V", android_graphics_Picture_draw),
102 MAKE_JNI_NATIVE_METHOD("nativeWriteToStream", "(JLjava/io/OutputStream;[B)Z", android_graphics_Picture_serialize),
103 MAKE_JNI_NATIVE_METHOD("nativeDestructor","(J)V", 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.data(), gMethods.size());
108 }
109
110 }; // namespace android
111