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