blob: 1d085e5ccc49a30a0c1a85ce3642721cbefc52d7 [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"
John Reck6143e8d2019-10-31 13:51:11 -070023#include "nativehelper/jni_macros.h"
Ben Wagner60126ef2015-08-07 12:13:48 -040024
25#include <jni.h>
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040026
John Reck6143e8d2019-10-31 13:51:11 -070027#include <array>
28
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040029namespace android {
30
31static jlong android_graphics_Picture_newPicture(JNIEnv* env, jobject, jlong srcHandle) {
32 const Picture* src = reinterpret_cast<Picture*>(srcHandle);
33 return reinterpret_cast<jlong>(new Picture(src));
34}
35
36static jlong android_graphics_Picture_deserialize(JNIEnv* env, jobject, jobject jstream,
37 jbyteArray jstorage) {
38 Picture* picture = NULL;
39 SkStream* strm = CreateJavaInputStreamAdaptor(env, jstream, jstorage);
40 if (strm) {
41 picture = Picture::CreateFromStream(strm);
42 delete strm;
43 }
44 return reinterpret_cast<jlong>(picture);
45}
46
47static void android_graphics_Picture_killPicture(JNIEnv* env, jobject, jlong pictureHandle) {
48 Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
49 SkASSERT(picture);
50 delete picture;
51}
52
53static void android_graphics_Picture_draw(JNIEnv* env, jobject, jlong canvasHandle,
54 jlong pictureHandle) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040055 Canvas* canvas = reinterpret_cast<Canvas*>(canvasHandle);
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040056 Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
57 SkASSERT(canvas);
58 SkASSERT(picture);
59 picture->draw(canvas);
60}
61
62static jboolean android_graphics_Picture_serialize(JNIEnv* env, jobject, jlong pictureHandle,
63 jobject jstream, jbyteArray jstorage) {
64 Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
65 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
66
67 if (NULL != strm) {
68 picture->serialize(strm);
69 delete strm;
70 return JNI_TRUE;
71 }
72 return JNI_FALSE;
73}
74
75static jint android_graphics_Picture_getWidth(JNIEnv* env, jobject, jlong pictureHandle) {
76 Picture* pict = reinterpret_cast<Picture*>(pictureHandle);
77 return static_cast<jint>(pict->width());
78}
79
80static jint android_graphics_Picture_getHeight(JNIEnv* env, jobject, jlong pictureHandle) {
81 Picture* pict = reinterpret_cast<Picture*>(pictureHandle);
82 return static_cast<jint>(pict->height());
83}
84
85static jlong android_graphics_Picture_beginRecording(JNIEnv* env, jobject, jlong pictHandle,
86 jint w, jint h) {
87 Picture* pict = reinterpret_cast<Picture*>(pictHandle);
Derek Sollenberger8872b382014-06-23 14:13:53 -040088 Canvas* canvas = pict->beginRecording(w, h);
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040089 return reinterpret_cast<jlong>(canvas);
90}
91
92static void android_graphics_Picture_endRecording(JNIEnv* env, jobject, jlong pictHandle) {
93 Picture* pict = reinterpret_cast<Picture*>(pictHandle);
94 pict->endRecording();
95}
96
John Reck6143e8d2019-10-31 13:51:11 -070097static const std::array gMethods = {
98 MAKE_JNI_NATIVE_METHOD("nativeGetWidth", "(J)I", android_graphics_Picture_getWidth),
99 MAKE_JNI_NATIVE_METHOD("nativeGetHeight", "(J)I", android_graphics_Picture_getHeight),
100 MAKE_JNI_NATIVE_METHOD("nativeConstructor", "(J)J", android_graphics_Picture_newPicture),
101 MAKE_JNI_NATIVE_METHOD("nativeCreateFromStream", "(Ljava/io/InputStream;[B)J", android_graphics_Picture_deserialize),
102 MAKE_JNI_NATIVE_METHOD("nativeBeginRecording", "(JII)J", android_graphics_Picture_beginRecording),
103 MAKE_JNI_NATIVE_METHOD("nativeEndRecording", "(J)V", android_graphics_Picture_endRecording),
104 MAKE_JNI_NATIVE_METHOD("nativeDraw", "(JJ)V", android_graphics_Picture_draw),
105 MAKE_JNI_NATIVE_METHOD("nativeWriteToStream", "(JLjava/io/OutputStream;[B)Z", android_graphics_Picture_serialize),
106 MAKE_JNI_NATIVE_METHOD("nativeDestructor","(J)V", android_graphics_Picture_killPicture)
Derek Sollenberger4b0959d2014-06-12 12:31:10 -0400107};
108
109int register_android_graphics_Picture(JNIEnv* env) {
John Reck6143e8d2019-10-31 13:51:11 -0700110 return RegisterMethodsOrDie(env, "android/graphics/Picture", gMethods.data(), gMethods.size());
Derek Sollenberger4b0959d2014-06-12 12:31:10 -0400111}
112
113}; // namespace android