android_graphics_Picture.cpp revision 4b0959d8db20c08ab1fed37f397b303af229162b
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 "jni.h"
18#include "GraphicsJNI.h"
19#include <android_runtime/AndroidRuntime.h>
20
21#include "Picture.h"
22
23#include "SkCanvas.h"
24#include "SkStream.h"
25#include "SkTemplates.h"
26#include "CreateJavaOutputStreamAdaptor.h"
27
28namespace android {
29
30static jlong android_graphics_Picture_newPicture(JNIEnv* env, jobject, jlong srcHandle) {
31    const Picture* src = reinterpret_cast<Picture*>(srcHandle);
32    return reinterpret_cast<jlong>(new Picture(src));
33}
34
35static jlong android_graphics_Picture_deserialize(JNIEnv* env, jobject, jobject jstream,
36                                                  jbyteArray jstorage) {
37    Picture* picture = NULL;
38    SkStream* strm = CreateJavaInputStreamAdaptor(env, jstream, jstorage);
39    if (strm) {
40        picture = Picture::CreateFromStream(strm);
41        delete strm;
42    }
43    return reinterpret_cast<jlong>(picture);
44}
45
46static void android_graphics_Picture_killPicture(JNIEnv* env, jobject, jlong pictureHandle) {
47    Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
48    SkASSERT(picture);
49    delete picture;
50}
51
52static void android_graphics_Picture_draw(JNIEnv* env, jobject, jlong canvasHandle,
53                                          jlong pictureHandle) {
54    SkCanvas* canvas = GraphicsJNI::getNativeCanvas(canvasHandle);
55    Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
56    SkASSERT(canvas);
57    SkASSERT(picture);
58    picture->draw(canvas);
59}
60
61static jboolean android_graphics_Picture_serialize(JNIEnv* env, jobject, jlong pictureHandle,
62                                                   jobject jstream, jbyteArray jstorage) {
63    Picture* picture = reinterpret_cast<Picture*>(pictureHandle);
64    SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
65
66    if (NULL != strm) {
67        picture->serialize(strm);
68        delete strm;
69        return JNI_TRUE;
70    }
71    return JNI_FALSE;
72}
73
74static jint android_graphics_Picture_getWidth(JNIEnv* env, jobject, jlong pictureHandle) {
75    Picture* pict = reinterpret_cast<Picture*>(pictureHandle);
76    return static_cast<jint>(pict->width());
77}
78
79static jint android_graphics_Picture_getHeight(JNIEnv* env, jobject, jlong pictureHandle) {
80    Picture* pict = reinterpret_cast<Picture*>(pictureHandle);
81    return static_cast<jint>(pict->height());
82}
83
84static jlong android_graphics_Picture_beginRecording(JNIEnv* env, jobject, jlong pictHandle,
85                                                     jint w, jint h) {
86    Picture* pict = reinterpret_cast<Picture*>(pictHandle);
87    // beginRecording does not ref its return value, it just returns it.
88    SkCanvas* canvas = pict->beginRecording(w, h);
89    // the java side will wrap this guy in a Canvas.java, which will call
90    // unref in its finalizer, so we have to ref it here, so that both that
91    // Canvas.java and our picture can both be owners
92    canvas->ref();
93    return reinterpret_cast<jlong>(canvas);
94}
95
96static void android_graphics_Picture_endRecording(JNIEnv* env, jobject, jlong pictHandle) {
97    Picture* pict = reinterpret_cast<Picture*>(pictHandle);
98    pict->endRecording();
99}
100
101static JNINativeMethod gMethods[] = {
102    {"nativeGetWidth", "(J)I", (void*) android_graphics_Picture_getWidth},
103    {"nativeGetHeight", "(J)I", (void*) android_graphics_Picture_getHeight},
104    {"nativeConstructor", "(J)J", (void*) android_graphics_Picture_newPicture},
105    {"nativeCreateFromStream", "(Ljava/io/InputStream;[B)J", (void*)android_graphics_Picture_deserialize},
106    {"nativeBeginRecording", "(JII)J", (void*) android_graphics_Picture_beginRecording},
107    {"nativeEndRecording", "(J)V", (void*) android_graphics_Picture_endRecording},
108    {"nativeDraw", "(JJ)V", (void*) android_graphics_Picture_draw},
109    {"nativeWriteToStream", "(JLjava/io/OutputStream;[B)Z", (void*)android_graphics_Picture_serialize},
110    {"nativeDestructor","(J)V", (void*) android_graphics_Picture_killPicture}
111};
112
113int register_android_graphics_Picture(JNIEnv* env) {
114    return AndroidRuntime::registerNativeMethods(env, "android/graphics/Picture", gMethods, NELEM(gMethods));
115}
116
117}; // namespace android
118