18872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger/*
28872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * Copyright (C) 2014 The Android Open Source Project
38872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *
48872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * Licensed under the Apache License, Version 2.0 (the "License");
58872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * you may not use this file except in compliance with the License.
68872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * You may obtain a copy of the License at
78872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *
88872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *      http://www.apache.org/licenses/LICENSE-2.0
98872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger *
108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * Unless required by applicable law or agreed to in writing, software
118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * distributed under the License is distributed on an "AS IS" BASIS,
128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * See the License for the specific language governing permissions and
148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger * limitations under the License.
158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger */
168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#include "jni.h"
188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#include "GraphicsJNI.h"
19ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.h"
208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
214c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger#include <androidfw/ResourceTypes.h>
22dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include <hwui/Canvas.h>
23dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include <hwui/Paint.h>
24bad99183916ba2bac6659efc8a28273e344ba511sergeyv#include <hwui/Typeface.h>
25dccca44ffda4836b56a21da95a046c9708ffd49csergeyv#include <minikin/Layout.h>
264c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
274c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger#include "Bitmap.h"
28acb4099deff72c2c631f7110a405a3331d3e8b27Derek Sollenberger#include "SkDrawFilter.h"
298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger#include "SkGraphics.h"
308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergernamespace android {
328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergernamespace CanvasJNI {
348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic Canvas* get_canvas(jlong canvasHandle) {
368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return reinterpret_cast<Canvas*>(canvasHandle);
378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
39775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhlerstatic void delete_canvas(Canvas* canvas) {
40775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    delete canvas;
41775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler}
42775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler
43775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhlerstatic jlong getNativeFinalizer(JNIEnv* env, jobject clazz) {
44775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    return static_cast<jlong>(reinterpret_cast<uintptr_t>(&delete_canvas));
458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Native wrapper constructor used by Canvas(Bitmap)
48c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reckstatic jlong initRaster(JNIEnv* env, jobject, jobject jbitmap) {
493731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck    SkBitmap bitmap;
50c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    if (jbitmap != NULL) {
513731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck        GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
52c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    }
533731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck    return reinterpret_cast<jlong>(Canvas::create_canvas(bitmap));
548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// Set the given bitmap as the new draw target (wrapped in a new SkCanvas),
578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger// optionally copying canvas matrix & clip state.
58c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reckstatic void setBitmap(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap) {
593731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck    SkBitmap bitmap;
60c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    if (jbitmap != NULL) {
613731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck        GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
62c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    }
633731dc220ed457e0f1e99d7ec2589e0a43872b59John Reck    get_canvas(canvasHandle)->setBitmap(bitmap);
648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean isOpaque(JNIEnv*, jobject, jlong canvasHandle) {
678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return get_canvas(canvasHandle)->isOpaque() ? JNI_TRUE : JNI_FALSE;
688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jint getWidth(JNIEnv*, jobject, jlong canvasHandle) {
718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return static_cast<jint>(get_canvas(canvasHandle)->width());
728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jint getHeight(JNIEnv*, jobject, jlong canvasHandle) {
758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return static_cast<jint>(get_canvas(canvasHandle)->height());
768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
786578a989566e585eee053095dc80e2552e125db2Derek Sollenbergerstatic void setHighContrastText(JNIEnv*, jobject, jlong canvasHandle, jboolean highContrastText) {
796578a989566e585eee053095dc80e2552e125db2Derek Sollenberger    Canvas* canvas = get_canvas(canvasHandle);
806578a989566e585eee053095dc80e2552e125db2Derek Sollenberger    canvas->setHighContrastText(highContrastText);
816578a989566e585eee053095dc80e2552e125db2Derek Sollenberger}
826578a989566e585eee053095dc80e2552e125db2Derek Sollenberger
838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jint getSaveCount(JNIEnv*, jobject, jlong canvasHandle) {
848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return static_cast<jint>(get_canvas(canvasHandle)->getSaveCount());
858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jint save(JNIEnv*, jobject, jlong canvasHandle, jint flagsHandle) {
88eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    SaveFlags::Flags flags = static_cast<SaveFlags::Flags>(flagsHandle);
898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return static_cast<jint>(get_canvas(canvasHandle)->save(flags));
908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jint saveLayer(JNIEnv* env, jobject, jlong canvasHandle, jfloat l, jfloat t,
938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                      jfloat r, jfloat b, jlong paintHandle, jint flagsHandle) {
946ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint  = reinterpret_cast<Paint*>(paintHandle);
95eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    SaveFlags::Flags flags = static_cast<SaveFlags::Flags>(flagsHandle);
968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return static_cast<jint>(get_canvas(canvasHandle)->saveLayer(l, t, r, b, paint, flags));
978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jint saveLayerAlpha(JNIEnv* env, jobject, jlong canvasHandle, jfloat l, jfloat t,
1008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jfloat r, jfloat b, jint alpha, jint flagsHandle) {
101eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita    SaveFlags::Flags flags = static_cast<SaveFlags::Flags>(flagsHandle);
1028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return static_cast<jint>(get_canvas(canvasHandle)->saveLayerAlpha(l, t, r, b, alpha, flags));
1038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1053891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craikstatic void restore(JNIEnv* env, jobject, jlong canvasHandle, jboolean throwOnUnderflow) {
1068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    Canvas* canvas = get_canvas(canvasHandle);
1078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (canvas->getSaveCount() <= 1) {  // cannot restore anymore
1083891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        if (throwOnUnderflow) {
1093891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik            doThrowISE(env, "Underflow in restore - more restores than saves");
1103891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        }
1113891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        return; // compat behavior - return without throwing
1128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
1138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    canvas->restore();
1148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1163891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craikstatic void restoreToCount(JNIEnv* env, jobject, jlong canvasHandle, jint restoreCount,
1173891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        jboolean throwOnUnderflow) {
1188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    Canvas* canvas = get_canvas(canvasHandle);
1198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (restoreCount < 1 || restoreCount > canvas->getSaveCount()) {
1203891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        if (throwOnUnderflow) {
1213891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik            doThrowIAE(env, "Underflow in restoreToCount - more restores than saves");
1223891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik            return;
1233891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        }
1243891f3ad598561d5a82c07795e1fee7f1d3612d1Chris Craik        restoreCount = 1; // compat behavior - restore as far as possible
1258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
1268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    canvas->restoreToCount(restoreCount);
1278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void getCTM(JNIEnv* env, jobject, jlong canvasHandle, jlong matrixHandle) {
1308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
1318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->getMatrix(matrix);
1328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void setMatrix(JNIEnv* env, jobject, jlong canvasHandle, jlong matrixHandle) {
1358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
1368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->setMatrix(matrix ? *matrix : SkMatrix::I());
1378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void concat(JNIEnv* env, jobject, jlong canvasHandle, jlong matrixHandle) {
1408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
1418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->concat(*matrix);
1428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void rotate(JNIEnv*, jobject, jlong canvasHandle, jfloat degrees) {
1458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->rotate(degrees);
1468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void scale(JNIEnv*, jobject, jlong canvasHandle, jfloat sx, jfloat sy) {
1498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->scale(sx, sy);
1508872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void skew(JNIEnv*, jobject, jlong canvasHandle, jfloat sx, jfloat sy) {
1538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->skew(sx, sy);
1548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void translate(JNIEnv*, jobject, jlong canvasHandle, jfloat dx, jfloat dy) {
1578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->translate(dx, dy);
1588872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean getClipBounds(JNIEnv* env, jobject, jlong canvasHandle, jobject bounds) {
1618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRect   r;
1628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkIRect ir;
1638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    bool result = get_canvas(canvasHandle)->getClipBounds(&r);
1648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (!result) {
1668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        r.setEmpty();
1678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
1688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    r.round(&ir);
1698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    (void)GraphicsJNI::irect_to_jrect(ir, env, bounds);
1718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return result ? JNI_TRUE : JNI_FALSE;
1728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean quickRejectRect(JNIEnv* env, jobject, jlong canvasHandle,
1758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                jfloat left, jfloat top, jfloat right, jfloat bottom) {
1768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    bool result = get_canvas(canvasHandle)->quickRejectRect(left, top, right, bottom);
1778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return result ? JNI_TRUE : JNI_FALSE;
1788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean quickRejectPath(JNIEnv* env, jobject, jlong canvasHandle, jlong pathHandle) {
1818872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
1828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    bool result = get_canvas(canvasHandle)->quickRejectPath(*path);
1838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    return result ? JNI_TRUE : JNI_FALSE;
1848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean clipRect(JNIEnv*, jobject, jlong canvasHandle, jfloat l, jfloat t,
1878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jfloat r, jfloat b, jint opHandle) {
1888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRegion::Op op = static_cast<SkRegion::Op>(opHandle);
1895ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    bool nonEmptyClip = get_canvas(canvasHandle)->clipRect(l, t, r, b, op);
1905ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    return nonEmptyClip ? JNI_TRUE : JNI_FALSE;
1918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
1928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
1938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean clipPath(JNIEnv* env, jobject, jlong canvasHandle, jlong pathHandle,
1948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jint opHandle) {
1958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
1968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRegion::Op op = static_cast<SkRegion::Op>(opHandle);
1975ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    bool nonEmptyClip = get_canvas(canvasHandle)->clipPath(path, op);
1985ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    return nonEmptyClip ? JNI_TRUE : JNI_FALSE;
1998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2018872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic jboolean clipRegion(JNIEnv* env, jobject, jlong canvasHandle, jlong deviceRgnHandle,
2028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jint opHandle) {
2038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRegion* deviceRgn = reinterpret_cast<SkRegion*>(deviceRgnHandle);
2048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkRegion::Op op = static_cast<SkRegion::Op>(opHandle);
2055ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    bool nonEmptyClip = get_canvas(canvasHandle)->clipRegion(deviceRgn, op);
2065ec6a2878b2ad933c5da6fe2341c854155acc24cChris Craik    return nonEmptyClip ? JNI_TRUE : JNI_FALSE;
2078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawColor(JNIEnv* env, jobject, jlong canvasHandle, jint color, jint modeHandle) {
2101526a458a30184609f19b05e7334da3cbde81dd1Chris Craik    SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(modeHandle);
2111526a458a30184609f19b05e7334da3cbde81dd1Chris Craik    get_canvas(canvasHandle)->drawColor(color, mode);
2128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawPaint(JNIEnv* env, jobject, jlong canvasHandle, jlong paintHandle) {
2156ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawPaint(*paint);
2178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawPoint(JNIEnv*, jobject, jlong canvasHandle, jfloat x, jfloat y,
2208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                      jlong paintHandle) {
2216ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawPoint(x, y, *paint);
2238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawPoints(JNIEnv* env, jobject, jlong canvasHandle, jfloatArray jptsArray,
2268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                       jint offset, jint count, jlong paintHandle) {
2278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    NPE_CHECK_RETURN_VOID(env, jptsArray);
2288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaFloatArray autoPts(env, jptsArray);
2298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    float* floats = autoPts.ptr();
2308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int length = autoPts.length();
2318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if ((offset | count) < 0 || offset + count > length) {
2338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        doThrowAIOOBE(env);
2348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return;
2358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2376ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawPoints(floats + offset, count, *paint);
2398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawLine(JNIEnv* env, jobject, jlong canvasHandle, jfloat startX, jfloat startY,
2428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                     jfloat stopX, jfloat stopY, jlong paintHandle) {
2436ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawLine(startX, startY, stopX, stopY, *paint);
2458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawLines(JNIEnv* env, jobject, jlong canvasHandle, jfloatArray jptsArray,
2488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                      jint offset, jint count, jlong paintHandle) {
2498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    NPE_CHECK_RETURN_VOID(env, jptsArray);
2508872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaFloatArray autoPts(env, jptsArray);
2518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    float* floats = autoPts.ptr();
2528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int length = autoPts.length();
2538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if ((offset | count) < 0 || offset + count > length) {
2558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        doThrowAIOOBE(env);
2568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return;
2578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
2588872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2596ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawLines(floats + offset, count, *paint);
2618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawRect(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top,
2648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                     jfloat right, jfloat bottom, jlong paintHandle) {
2656ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawRect(left, top, right, bottom, *paint);
2678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
26994394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenbergerstatic void drawRegion(JNIEnv* env, jobject, jlong canvasHandle, jlong regionHandle,
27094394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger                       jlong paintHandle) {
27194394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    const SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle);
27294394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
27394394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger    get_canvas(canvasHandle)->drawRegion(*region, *paint);
27494394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger}
27594394b3fb048d5349a77b57950ab7f6b6e92ce34Derek Sollenberger
2768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawRoundRect(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top,
2778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                          jfloat right, jfloat bottom, jfloat rx, jfloat ry, jlong paintHandle) {
2786ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawRoundRect(left, top, right, bottom, rx, ry, *paint);
2808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2818872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawCircle(JNIEnv* env, jobject, jlong canvasHandle, jfloat cx, jfloat cy,
2838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                       jfloat radius, jlong paintHandle) {
2846ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawCircle(cx, cy, radius, *paint);
2868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawOval(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top,
2898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                     jfloat right, jfloat bottom, jlong paintHandle) {
2906ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawOval(left, top, right, bottom, *paint);
2928872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
2938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
2948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawArc(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top,
2958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                    jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle,
2968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                    jboolean useCenter, jlong paintHandle) {
2976ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
2988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawArc(left, top, right, bottom, startAngle, sweepAngle,
2998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                       useCenter, *paint);
3008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3018872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawPath(JNIEnv* env, jobject, jlong canvasHandle, jlong pathHandle,
3038872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                     jlong paintHandle) {
3048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
3056ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
3068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawPath(*path, *paint);
3078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3088872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawVertices(JNIEnv* env, jobject, jlong canvasHandle,
3108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jint modeHandle, jint vertexCount,
3118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jfloatArray jverts, jint vertIndex,
3128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jfloatArray jtexs, jint texIndex,
3138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jintArray jcolors, jint colorIndex,
3148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jshortArray jindices, jint indexIndex,
3158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                         jint indexCount, jlong paintHandle) {
3168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaFloatArray  vertA(env, jverts, vertIndex + vertexCount);
3178872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaFloatArray  texA(env, jtexs, texIndex + vertexCount);
3188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaIntArray    colorA(env, jcolors, colorIndex + vertexCount);
3198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaShortArray  indexA(env, jindices, indexIndex + indexCount);
3208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const float* verts = vertA.ptr() + vertIndex;
3228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const float* texs = texA.ptr() + vertIndex;
3238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int* colors = NULL;
3248872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const uint16_t* indices = NULL;
3258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (jcolors != NULL) {
3278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        colors = colorA.ptr() + colorIndex;
3288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (jindices != NULL) {
3308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        indices = (const uint16_t*)(indexA.ptr() + indexIndex);
3318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
3328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkCanvas::VertexMode mode = static_cast<SkCanvas::VertexMode>(modeHandle);
3346ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
3358872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawVertices(mode, vertexCount, verts, texs, colors,
3368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                           indices, indexCount, *paint);
3378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
3388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3394c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenbergerstatic void drawNinePatch(JNIEnv* env, jobject, jlong canvasHandle, jlong bitmapHandle,
3404c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        jlong chunkHandle, jfloat left, jfloat top, jfloat right, jfloat bottom,
3414c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        jlong paintHandle, jint dstDensity, jint srcDensity) {
3424c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3434c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    Canvas* canvas = get_canvas(canvasHandle);
3444c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    Bitmap* bitmap = reinterpret_cast<Bitmap*>(bitmapHandle);
3454c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    SkBitmap skiaBitmap;
3464c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    bitmap->getSkBitmap(&skiaBitmap);
3474c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    const android::Res_png_9patch* chunk = reinterpret_cast<android::Res_png_9patch*>(chunkHandle);
3484c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
3494c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3504c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    if (CC_LIKELY(dstDensity == srcDensity || dstDensity == 0 || srcDensity == 0)) {
3514c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        canvas->drawNinePatch(skiaBitmap, *chunk, left, top, right, bottom, paint);
3524c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    } else {
353eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        canvas->save(SaveFlags::MatrixClip);
3544c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3554c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        SkScalar scale = dstDensity / (float)srcDensity;
3564c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        canvas->translate(left, top);
3574c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        canvas->scale(scale, scale);
3584c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3594c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        Paint filteredPaint;
3604c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        if (paint) {
3614c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger            filteredPaint = *paint;
3624c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        }
3634c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        filteredPaint.setFilterQuality(kLow_SkFilterQuality);
3644c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3654c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        canvas->drawNinePatch(skiaBitmap, *chunk, 0, 0, (right-left)/scale, (bottom-top)/scale,
3664c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger                &filteredPaint);
3674c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3684c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger        canvas->restore();
3694c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger    }
3704c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger}
3714c5efe9290543b723b76a8bd48518da1ae1dcb26Derek Sollenberger
3727c103a36f60b690e3fe83c40210e1cb0c76bba43John Reckstatic void drawBitmap(JNIEnv* env, jobject jcanvas, jlong canvasHandle, jobject jbitmap,
3738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                       jfloat left, jfloat top, jlong paintHandle, jint canvasDensity,
3748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                       jint screenDensity, jint bitmapDensity) {
3758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    Canvas* canvas = get_canvas(canvasHandle);
3767c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    SkBitmap bitmap;
3777c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
3786ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
3798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3808872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (canvasDensity == bitmapDensity || canvasDensity == 0 || bitmapDensity == 0) {
3818872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        if (screenDensity != 0 && screenDensity != bitmapDensity) {
3826ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod            Paint filteredPaint;
3838872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            if (paint) {
3848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                filteredPaint = *paint;
3858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            }
3862a1ce8a4e5258b6599cb8e86864eb816d24d69b4Mike Reed            filteredPaint.setFilterQuality(kLow_SkFilterQuality);
3877c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck            canvas->drawBitmap(bitmap, left, top, &filteredPaint);
3888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        } else {
3897c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck            canvas->drawBitmap(bitmap, left, top, paint);
3908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
3918872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    } else {
392eecff56fed5dd5206acfbc5007b4912081b36d3bFlorin Malita        canvas->save(SaveFlags::MatrixClip);
3938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        SkScalar scale = canvasDensity / (float)bitmapDensity;
3948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        canvas->translate(left, top);
3958872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        canvas->scale(scale, scale);
3968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
3976ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod        Paint filteredPaint;
3988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        if (paint) {
3998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            filteredPaint = *paint;
4008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
4012a1ce8a4e5258b6599cb8e86864eb816d24d69b4Mike Reed        filteredPaint.setFilterQuality(kLow_SkFilterQuality);
4028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4037c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck        canvas->drawBitmap(bitmap, 0, 0, &filteredPaint);
4048872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        canvas->restore();
4058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4087c103a36f60b690e3fe83c40210e1cb0c76bba43John Reckstatic void drawBitmapMatrix(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap,
4098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                             jlong matrixHandle, jlong paintHandle) {
4108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
4116ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
4127c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    SkBitmap bitmap;
4137c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
4147c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    get_canvas(canvasHandle)->drawBitmap(bitmap, *matrix, paint);
4158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4177c103a36f60b690e3fe83c40210e1cb0c76bba43John Reckstatic void drawBitmapRect(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap,
4188872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           float srcLeft, float srcTop, float srcRight, float srcBottom,
4198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           float dstLeft, float dstTop, float dstRight, float dstBottom,
4208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jlong paintHandle, jint screenDensity, jint bitmapDensity) {
4218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    Canvas* canvas = get_canvas(canvasHandle);
4226ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
4238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4247c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    SkBitmap bitmap;
4257c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
4268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (screenDensity != 0 && screenDensity != bitmapDensity) {
4276ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod        Paint filteredPaint;
4288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        if (paint) {
4298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger            filteredPaint = *paint;
4308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        }
4312a1ce8a4e5258b6599cb8e86864eb816d24d69b4Mike Reed        filteredPaint.setFilterQuality(kLow_SkFilterQuality);
4327c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck        canvas->drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom,
4338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           dstLeft, dstTop, dstRight, dstBottom, &filteredPaint);
4348872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    } else {
4357c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck        canvas->drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom,
4368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           dstLeft, dstTop, dstRight, dstBottom, paint);
4378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4398872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawBitmapArray(JNIEnv* env, jobject, jlong canvasHandle,
4418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                            jintArray jcolors, jint offset, jint stride,
4428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                            jfloat x, jfloat y, jint width, jint height,
4438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                            jboolean hasAlpha, jlong paintHandle) {
4448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // Note: If hasAlpha is false, kRGB_565_SkColorType will be used, which will
4458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    // correct the alphaType to kOpaque_SkAlphaType.
4468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkImageInfo info = SkImageInfo::Make(width, height,
4478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           hasAlpha ? kN32_SkColorType : kRGB_565_SkColorType,
4488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           kPremul_SkAlphaType);
4498872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkBitmap bitmap;
4503d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    bitmap.setInfo(info);
4513d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    if (!GraphicsJNI::allocatePixels(env, &bitmap, NULL)) {
4528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return;
4538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4558872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    if (!GraphicsJNI::SetPixels(env, jcolors, offset, stride, 0, 0, width, height, bitmap)) {
4568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger        return;
4578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    }
4588872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4596ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
4608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->drawBitmap(bitmap, x, y, paint);
4618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4637c103a36f60b690e3fe83c40210e1cb0c76bba43John Reckstatic void drawBitmapMesh(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap,
4648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jint meshWidth, jint meshHeight, jfloatArray jverts,
4658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jint vertIndex, jintArray jcolors, jint colorIndex, jlong paintHandle) {
4668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int ptCount = (meshWidth + 1) * (meshHeight + 1);
4678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaFloatArray vertA(env, jverts, vertIndex + (ptCount << 1));
4688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    AutoJavaIntArray colorA(env, jcolors, colorIndex + ptCount);
4698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4706ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    const Paint* paint = reinterpret_cast<Paint*>(paintHandle);
4717c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    SkBitmap bitmap;
4727c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap);
4737c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck    get_canvas(canvasHandle)->drawBitmapMesh(bitmap, meshWidth, meshHeight,
4748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                             vertA.ptr(), colorA.ptr(), paint);
4758872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4768872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4778872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawTextChars(JNIEnv* env, jobject, jlong canvasHandle, jcharArray text,
4788872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                          jint index, jint count, jfloat x, jfloat y, jint bidiFlags,
4798872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                          jlong paintHandle, jlong typefaceHandle) {
4806ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
481bad99183916ba2bac6659efc8a28273e344ba511sergeyv    Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
4828872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    jchar* jchars = env->GetCharArrayElements(text, NULL);
483dccca44ffda4836b56a21da95a046c9708ffd49csergeyv    get_canvas(canvasHandle)->drawText(jchars + index, 0, count, count, x, y,
4848872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                       bidiFlags, *paint, typeface);
4858872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    env->ReleaseCharArrayElements(text, jchars, JNI_ABORT);
4868872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4878872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
4888872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawTextString(JNIEnv* env, jobject, jlong canvasHandle, jstring text,
4898872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jint start, jint end, jfloat x, jfloat y, jint bidiFlags,
4908872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                           jlong paintHandle, jlong typefaceHandle) {
4916ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
492bad99183916ba2bac6659efc8a28273e344ba511sergeyv    Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
4938872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int count = end - start;
4948872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const jchar* jchars = env->GetStringChars(text, NULL);
495dccca44ffda4836b56a21da95a046c9708ffd49csergeyv    get_canvas(canvasHandle)->drawText(jchars + start, 0, count, count, x, y,
4968872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                       bidiFlags, *paint, typeface);
4978872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    env->ReleaseStringChars(text, jchars);
4988872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
4998872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5008872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawTextRunChars(JNIEnv* env, jobject, jlong canvasHandle, jcharArray text, jint index,
5018872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                             jint count, jint contextIndex, jint contextCount, jfloat x, jfloat y,
5028872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                             jboolean isRtl, jlong paintHandle, jlong typefaceHandle) {
5036ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
504bad99183916ba2bac6659efc8a28273e344ba511sergeyv    Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
5058872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5068872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR;
5078872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    jchar* jchars = env->GetCharArrayElements(text, NULL);
508dccca44ffda4836b56a21da95a046c9708ffd49csergeyv    get_canvas(canvasHandle)->drawText(jchars + contextIndex, index - contextIndex, count,
5098872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                       contextCount, x, y, bidiFlags, *paint, typeface);
5108872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    env->ReleaseCharArrayElements(text, jchars, JNI_ABORT);
5118872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5128872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5138872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawTextRunString(JNIEnv* env, jobject obj, jlong canvasHandle, jstring text,
5148872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                              jint start, jint end, jint contextStart, jint contextEnd,
5158872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                              jfloat x, jfloat y, jboolean isRtl, jlong paintHandle,
5168872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                              jlong typefaceHandle) {
5176ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
518bad99183916ba2bac6659efc8a28273e344ba511sergeyv    Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
5198872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5208872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR;
5218872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    jint count = end - start;
5228872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    jint contextCount = contextEnd - contextStart;
5238872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const jchar* jchars = env->GetStringChars(text, NULL);
524dccca44ffda4836b56a21da95a046c9708ffd49csergeyv    get_canvas(canvasHandle)->drawText(jchars + contextStart, start - contextStart, count,
5258872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                       contextCount, x, y, bidiFlags, *paint, typeface);
5268872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    env->ReleaseStringChars(text, jchars);
5278872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5288872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5298872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawTextOnPathChars(JNIEnv* env, jobject, jlong canvasHandle, jcharArray text,
5308872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                jint index, jint count, jlong pathHandle, jfloat hOffset,
5318872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                jfloat vOffset, jint bidiFlags, jlong paintHandle,
5328872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                jlong typefaceHandle) {
5338872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
5346ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
535bad99183916ba2bac6659efc8a28273e344ba511sergeyv    Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
5368872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5378872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    jchar* jchars = env->GetCharArrayElements(text, NULL);
5388872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
539dccca44ffda4836b56a21da95a046c9708ffd49csergeyv    get_canvas(canvasHandle)->drawTextOnPath(jchars + index, count, bidiFlags, *path,
5408872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                   hOffset, vOffset, *paint, typeface);
5418872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5428872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    env->ReleaseCharArrayElements(text, jchars, 0);
5438872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5448872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5458872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void drawTextOnPathString(JNIEnv* env, jobject, jlong canvasHandle, jstring text,
5468872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                 jlong pathHandle, jfloat hOffset, jfloat vOffset,
5478872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                                 jint bidiFlags, jlong paintHandle, jlong typefaceHandle) {
5488872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkPath* path = reinterpret_cast<SkPath*>(pathHandle);
5496ba30b85ddfbe37c338ee8dde3dd33322eb38d47Behdad Esfahbod    Paint* paint = reinterpret_cast<Paint*>(paintHandle);
550bad99183916ba2bac6659efc8a28273e344ba511sergeyv    Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle);
5518872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5528872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    const jchar* jchars = env->GetStringChars(text, NULL);
5538872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    int count = env->GetStringLength(text);
5548872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
555dccca44ffda4836b56a21da95a046c9708ffd49csergeyv    get_canvas(canvasHandle)->drawTextOnPath(jchars, count, bidiFlags, *path,
5568872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger                   hOffset, vOffset, *paint, typeface);
5578872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5588872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    env->ReleaseStringChars(text, jchars);
5598872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5608872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5618872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void setDrawFilter(JNIEnv* env, jobject, jlong canvasHandle, jlong filterHandle) {
5628872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    get_canvas(canvasHandle)->setDrawFilter(reinterpret_cast<SkDrawFilter*>(filterHandle));
5638872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5648872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5658872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void freeCaches(JNIEnv* env, jobject) {
5668872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    SkGraphics::PurgeFontCache();
5678872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5688872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5698872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenbergerstatic void freeTextLayoutCaches(JNIEnv* env, jobject) {
5708872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger    Layout::purgeCaches();
5718872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}
5728872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
5738872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger}; // namespace CanvasJNI
5748872b38ef403cc2c44aca07d392f5e9426fd7f54Derek Sollenberger
57576f6a86de25e1bf74717e047e55fd44b089673f3Daniel Micaystatic const JNINativeMethod gMethods[] = {
576775873a66a946fae2b0535abb51df9817bd1b20cRichard Uhler    {"getNativeFinalizer", "()J", (void*) CanvasJNI::getNativeFinalizer},
577c1b33d665c8caf5760f68c45c6ca0baa649b832aJohn Reck    {"initRaster", "(Landroid/graphics/Bitmap;)J", (void*) CanvasJNI::initRaster},
5780dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_setBitmap", "!(JLandroid/graphics/Bitmap;)V", (void*) CanvasJNI::setBitmap},
5790dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_isOpaque","!(J)Z", (void*) CanvasJNI::isOpaque},
5800dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_getWidth","!(J)I", (void*) CanvasJNI::getWidth},
5810dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_getHeight","!(J)I", (void*) CanvasJNI::getHeight},
5820dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_setHighContrastText","!(JZ)V", (void*) CanvasJNI::setHighContrastText},
5830dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_save","!(JI)I", (void*) CanvasJNI::save},
5840dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_saveLayer","!(JFFFFJI)I", (void*) CanvasJNI::saveLayer},
5850dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_saveLayerAlpha","!(JFFFFII)I", (void*) CanvasJNI::saveLayerAlpha},
5860dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_getSaveCount","!(J)I", (void*) CanvasJNI::getSaveCount},
5870dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_restore","!(JZ)V", (void*) CanvasJNI::restore},
5880dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_restoreToCount","!(JIZ)V", (void*) CanvasJNI::restoreToCount},
5890dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_getCTM", "!(JJ)V", (void*)CanvasJNI::getCTM},
5900dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_setMatrix","!(JJ)V", (void*) CanvasJNI::setMatrix},
5910dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_concat","!(JJ)V", (void*) CanvasJNI::concat},
5920dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_rotate","!(JF)V", (void*) CanvasJNI::rotate},
5930dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_scale","!(JFF)V", (void*) CanvasJNI::scale},
5940dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_skew","!(JFF)V", (void*) CanvasJNI::skew},
5950dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_translate","!(JFF)V", (void*) CanvasJNI::translate},
5960dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_getClipBounds","!(JLandroid/graphics/Rect;)Z", (void*) CanvasJNI::getClipBounds},
5970dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_quickReject","!(JJ)Z", (void*) CanvasJNI::quickRejectPath},
5980dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_quickReject","!(JFFFF)Z", (void*)CanvasJNI::quickRejectRect},
5990dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_clipRect","!(JFFFFI)Z", (void*) CanvasJNI::clipRect},
6000dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_clipPath","!(JJI)Z", (void*) CanvasJNI::clipPath},
6010dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_clipRegion","!(JJI)Z", (void*) CanvasJNI::clipRegion},
6020dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_drawColor","!(JII)V", (void*) CanvasJNI::drawColor},
6030dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_drawPaint","!(JJ)V", (void*) CanvasJNI::drawPaint},
6040dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_drawPoint", "!(JFFJ)V", (void*) CanvasJNI::drawPoint},
6050dba1f611410e5075a910fb73ff3d3c703bbc5ceJohn Reck    {"native_drawPoints", "!(J[FIIJ)V", (void*) CanvasJNI::drawPoints},
6060dba1f61141