blob: fd42ddb9226e9a4a6a484d799afcbf3476850a93 [file] [log] [blame]
Derek Sollenberger4b0959d2014-06-12 12:31:10 -04001/*
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
Ben Wagner60126ef2015-08-07 12:13:48 -040017#include "CreateJavaOutputStreamAdaptor.h"
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040018#include "GraphicsJNI.h"
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040019#include "Picture.h"
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040020#include "SkCanvas.h"
21#include "SkStream.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040022#include "core_jni_helpers.h"
23
24#include <jni.h>
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040025
26namespace android {
27
28static 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
33static 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
44static 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
50static void android_graphics_Picture_draw(JNIEnv* env, jobject, jlong canvasHandle,
51 jlong pictureHandle) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040052 Canvas* canvas = reinterpret_cast<Canvas*>(canvasHandle);
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040053 Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
54 SkASSERT(canvas);
55 SkASSERT(picture);
56 picture->draw(canvas);
57}
58
59static 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
72static 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
77static 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
82static jlong android_graphics_Picture_beginRecording(JNIEnv* env, jobject, jlong pictHandle,
83 jint w, jint h) {
84 Picture* pict = reinterpret_cast<Picture*>(pictHandle);
Derek Sollenberger8872b382014-06-23 14:13:53 -040085 Canvas* canvas = pict->beginRecording(w, h);
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040086 return reinterpret_cast<jlong>(canvas);
87}
88
89static void android_graphics_Picture_endRecording(JNIEnv* env, jobject, jlong pictHandle) {
90 Picture* pict = reinterpret_cast<Picture*>(pictHandle);
91 pict->endRecording();
92}
93
94static 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
106int register_android_graphics_Picture(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800107 return RegisterMethodsOrDie(env, "android/graphics/Picture", gMethods, NELEM(gMethods));
Derek Sollenberger4b0959d2014-06-12 12:31:10 -0400108}
109
110}; // namespace android