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 "SkCanvas.h" 22 #include "SkPicture.h" 23 #include "SkStream.h" 24 #include "SkTemplates.h" 25 #include "CreateJavaOutputStreamAdaptor.h" 26 27 namespace android { 28 29 class SkPictureGlue { 30 public: 31 static SkPicture* newPicture(JNIEnv* env, jobject, const SkPicture* src) { 32 if (src) { 33 return new SkPicture(*src); 34 } else { 35 return new SkPicture; 36 } 37 } 38 39 static SkPicture* deserialize(JNIEnv* env, jobject, jobject jstream, 40 jbyteArray jstorage) { 41 SkPicture* picture = NULL; 42 SkStream* strm = CreateJavaInputStreamAdaptor(env, jstream, jstorage); 43 if (strm) { 44 picture = SkPicture::CreateFromStream(strm); 45 delete strm; 46 } 47 return picture; 48 } 49 50 static void killPicture(JNIEnv* env, jobject, SkPicture* picture) { 51 SkASSERT(picture); 52 picture->unref(); 53 } 54 55 static void draw(JNIEnv* env, jobject, SkCanvas* canvas, 56 SkPicture* picture) { 57 SkASSERT(canvas); 58 SkASSERT(picture); 59 picture->draw(canvas); 60 } 61 62 static bool serialize(JNIEnv* env, jobject, SkPicture* picture, 63 jobject jstream, jbyteArray jstorage) { 64 SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); 65 66 if (NULL != strm) { 67 picture->serialize(strm); 68 delete strm; 69 return true; 70 } 71 return false; 72 } 73 74 static int getWidth(JNIEnv* env, jobject jpic) { 75 NPE_CHECK_RETURN_ZERO(env, jpic); 76 return GraphicsJNI::getNativePicture(env, jpic)->width(); 77 } 78 79 static int getHeight(JNIEnv* env, jobject jpic) { 80 NPE_CHECK_RETURN_ZERO(env, jpic); 81 return GraphicsJNI::getNativePicture(env, jpic)->height(); 82 } 83 84 static SkCanvas* beginRecording(JNIEnv* env, jobject, SkPicture* pict, 85 int w, int h) { 86 // beginRecording does not ref its return value, it just returns it. 87 SkCanvas* canvas = pict->beginRecording(w, h); 88 // the java side will wrap this guy in a Canvas.java, which will call 89 // unref in its finalizer, so we have to ref it here, so that both that 90 // Canvas.java and our picture can both be owners 91 canvas->ref(); 92 return canvas; 93 } 94 95 static void endRecording(JNIEnv* env, jobject, SkPicture* pict) { 96 pict->endRecording(); 97 } 98 }; 99 100 static JNINativeMethod gPictureMethods[] = { 101 {"getWidth", "()I", (void*) SkPictureGlue::getWidth}, 102 {"getHeight", "()I", (void*) SkPictureGlue::getHeight}, 103 {"nativeConstructor", "(I)I", (void*) SkPictureGlue::newPicture}, 104 {"nativeCreateFromStream", "(Ljava/io/InputStream;[B)I", (void*)SkPictureGlue::deserialize}, 105 {"nativeBeginRecording", "(III)I", (void*) SkPictureGlue::beginRecording}, 106 {"nativeEndRecording", "(I)V", (void*) SkPictureGlue::endRecording}, 107 {"nativeDraw", "(II)V", (void*) SkPictureGlue::draw}, 108 {"nativeWriteToStream", "(ILjava/io/OutputStream;[B)Z", (void*)SkPictureGlue::serialize}, 109 {"nativeDestructor","(I)V", (void*) SkPictureGlue::killPicture} 110 }; 111 112 #include <android_runtime/AndroidRuntime.h> 113 114 #define REG(env, name, array) \ 115 result = android::AndroidRuntime::registerNativeMethods(env, name, array, \ 116 SK_ARRAY_COUNT(array)); \ 117 if (result < 0) return result 118 119 int register_android_graphics_Picture(JNIEnv* env) { 120 int result; 121 122 REG(env, "android/graphics/Picture", gPictureMethods); 123 124 return result; 125 } 126 127 } 128 129 130