blob: 403efb2ab9c986e61d7db3b4900c72d5799eac9d [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"
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040022
John Reck6143e8d2019-10-31 13:51:11 -070023#include <array>
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040024#include "nativehelper/jni_macros.h"
John Reck6143e8d2019-10-31 13:51:11 -070025
Derek Sollenberger4b0959d2014-06-12 12:31:10 -040026namespace 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
John Reck6143e8d2019-10-31 13:51:11 -070094static 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)
Derek Sollenberger4b0959d2014-06-12 12:31:10 -0400104};
105
106int register_android_graphics_Picture(JNIEnv* env) {
John Reck6143e8d2019-10-31 13:51:11 -0700107 return RegisterMethodsOrDie(env, "android/graphics/Picture", gMethods.data(), gMethods.size());
Derek Sollenberger4b0959d2014-06-12 12:31:10 -0400108}
109
110}; // namespace android