19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006-2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "jni.h"
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "GraphicsJNI.h"
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <android_runtime/AndroidRuntime.h>
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkCanvas.h"
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkDevice.h"
23e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien#include "SkDrawFilter.h"
24caf0df1b7f99736aed1a0b923ef278fc4fd0fccaMike Reed#include "SkGraphics.h"
25caf0df1b7f99736aed1a0b923ef278fc4fd0fccaMike Reed#include "SkImageRef_GlobalPool.h"
2647c0d4eaa926d979c5ea366934750526c20af8ffMike Reed#include "SkPorterDuff.h"
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkShader.h"
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "SkTemplates.h"
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
30f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt#include "TextLayout.h"
31fcf2be1846935e7983ea2fe87fdd4d7af27764b6Fabrice Di Meglio#include "TextLayoutCache.h"
32f7cb1f75fdaedf996cab7c4690b080adc7bc5b97Doug Felt
33f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt#include "unicode/ubidi.h"
340c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt#include "unicode/ushape.h"
350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
360c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt#include <utils/Log.h>
37f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#define TIME_DRAWx
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatic uint32_t get_thread_msec() {
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#if defined(HAVE_POSIX_CLOCKS)
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct timespec tm;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return tm.tv_sec * 1000LL + tm.tv_nsec / 1000000;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#else
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    struct timeval tv;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    gettimeofday(&tv, NULL);
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return tv.tv_sec * 1000LL + tv.tv_usec / 1000;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectclass SkCanvasGlue {
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic:
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void finalizer(JNIEnv* env, jobject clazz, SkCanvas* canvas) {
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->unref();
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static SkCanvas* initRaster(JNIEnv* env, jobject, SkBitmap* bitmap) {
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bitmap ? new SkCanvas(*bitmap) : new SkCanvas;
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
68caf0df1b7f99736aed1a0b923ef278fc4fd0fccaMike Reed    static void freeCaches(JNIEnv* env, jobject) {
69caf0df1b7f99736aed1a0b923ef278fc4fd0fccaMike Reed        // these are called in no particular order
70caf0df1b7f99736aed1a0b923ef278fc4fd0fccaMike Reed        SkImageRef_GlobalPool::SetRAMUsed(0);
71889a3fa6ab9710104af60db5f73d69f253ddf254Derek Sollenberger        SkGraphics::PurgeFontCache();
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7330ca5cd11a23f06f2f8eeaa587685450826f800fFabrice Di Meglio
7430ca5cd11a23f06f2f8eeaa587685450826f800fFabrice Di Meglio    static void freeTextLayoutCaches(JNIEnv* env, jobject) {
7530ca5cd11a23f06f2f8eeaa587685450826f800fFabrice Di Meglio        TextLayoutEngine::getInstance().purgeCaches();
7630ca5cd11a23f06f2f8eeaa587685450826f800fFabrice Di Meglio    }
7730ca5cd11a23f06f2f8eeaa587685450826f800fFabrice Di Meglio
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean isOpaque(JNIEnv* env, jobject jcanvas) {
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->getDevice()->accessBitmap(false).isOpaque();
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int getWidth(JNIEnv* env, jobject jcanvas) {
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->getDevice()->accessBitmap(false).width();
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int getHeight(JNIEnv* env, jobject jcanvas) {
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->getDevice()->accessBitmap(false).height();
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
960965a3244b4c3009d08db2e084cdcb681ef66d26Romain Guy    static void setBitmap(JNIEnv* env, jobject, SkCanvas* canvas, SkBitmap* bitmap) {
970965a3244b4c3009d08db2e084cdcb681ef66d26Romain Guy        if (bitmap) {
980965a3244b4c3009d08db2e084cdcb681ef66d26Romain Guy            canvas->setBitmapDevice(*bitmap);
990965a3244b4c3009d08db2e084cdcb681ef66d26Romain Guy        } else {
1000965a3244b4c3009d08db2e084cdcb681ef66d26Romain Guy            canvas->setDevice(NULL);
1010965a3244b4c3009d08db2e084cdcb681ef66d26Romain Guy        }
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int saveAll(JNIEnv* env, jobject jcanvas) {
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativeCanvas(env, jcanvas)->save();
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int save(JNIEnv* env, jobject jcanvas, SkCanvas::SaveFlags flags) {
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativeCanvas(env, jcanvas)->save(flags);
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int saveLayer(JNIEnv* env, jobject, SkCanvas* canvas, jobject bounds,
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkPaint* paint, int flags) {
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect* bounds_ = NULL;
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect  storage;
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bounds != NULL) {
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            GraphicsJNI::jrectf_to_rect(env, bounds, &storage);
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bounds_ = &storage;
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->saveLayer(bounds_, paint, (SkCanvas::SaveFlags)flags);
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int saveLayer4F(JNIEnv* env, jobject, SkCanvas* canvas,
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           jfloat l, jfloat t, jfloat r, jfloat b,
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           SkPaint* paint, int flags) {
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect bounds;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bounds.set(SkFloatToScalar(l), SkFloatToScalar(t), SkFloatToScalar(r),
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                   SkFloatToScalar(b));
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->saveLayer(&bounds, paint, (SkCanvas::SaveFlags)flags);
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int saveLayerAlpha(JNIEnv* env, jobject, SkCanvas* canvas,
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              jobject bounds, int alpha, int flags) {
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect* bounds_ = NULL;
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect  storage;
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (bounds != NULL) {
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            GraphicsJNI::jrectf_to_rect(env, bounds, &storage);
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bounds_ = &storage;
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->saveLayerAlpha(bounds_, alpha,
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      (SkCanvas::SaveFlags)flags);
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int saveLayerAlpha4F(JNIEnv* env, jobject, SkCanvas* canvas,
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                jfloat l, jfloat t, jfloat r, jfloat b,
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                int alpha, int flags) {
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect  bounds;
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bounds.set(SkFloatToScalar(l), SkFloatToScalar(t), SkFloatToScalar(r),
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                   SkFloatToScalar(b));
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->saveLayerAlpha(&bounds, alpha,
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      (SkCanvas::SaveFlags)flags);
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void restore(JNIEnv* env, jobject jcanvas) {
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (canvas->getSaveCount() <= 1) {  // cannot restore anymore
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            doThrowISE(env, "Underflow in restore");
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->restore();
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static int getSaveCount(JNIEnv* env, jobject jcanvas) {
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativeCanvas(env, jcanvas)->getSaveCount();
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void restoreToCount(JNIEnv* env, jobject jcanvas, int restoreCount) {
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (restoreCount < 1) {
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            doThrowIAE(env, "Underflow in restoreToCount");
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->restoreToCount(restoreCount);
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void translate(JNIEnv* env, jobject jcanvas, jfloat dx, jfloat dy) {
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar dx_ = SkFloatToScalar(dx);
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar dy_ = SkFloatToScalar(dy);
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativeCanvas(env, jcanvas)->translate(dx_, dy_);
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void scale__FF(JNIEnv* env, jobject jcanvas, jfloat sx, jfloat sy) {
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar sx_ = SkFloatToScalar(sx);
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar sy_ = SkFloatToScalar(sy);
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativeCanvas(env, jcanvas)->scale(sx_, sy_);
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void rotate__F(JNIEnv* env, jobject jcanvas, jfloat degrees) {
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar degrees_ = SkFloatToScalar(degrees);
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativeCanvas(env, jcanvas)->rotate(degrees_);
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void skew__FF(JNIEnv* env, jobject jcanvas, jfloat sx, jfloat sy) {
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar sx_ = SkFloatToScalar(sx);
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar sy_ = SkFloatToScalar(sy);
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        (void)GraphicsJNI::getNativeCanvas(env, jcanvas)->skew(sx_, sy_);
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void concat(JNIEnv* env, jobject, SkCanvas* canvas,
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                       const SkMatrix* matrix) {
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->concat(*matrix);
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setMatrix(JNIEnv* env, jobject, SkCanvas* canvas,
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          const SkMatrix* matrix) {
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (NULL == matrix) {
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas->resetMatrix();
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas->setMatrix(*matrix);
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipRect_FFFF(JNIEnv* env, jobject jcanvas, jfloat left,
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                  jfloat top, jfloat right, jfloat bottom) {
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect  r;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        r.set(SkFloatToScalar(left), SkFloatToScalar(top),
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              SkFloatToScalar(right), SkFloatToScalar(bottom));
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* c = GraphicsJNI::getNativeCanvas(env, jcanvas);
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return c->clipRect(r);
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipRect_IIII(JNIEnv* env, jobject jcanvas, jint left,
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                  jint top, jint right, jint bottom) {
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect  r;
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        r.set(SkIntToScalar(left), SkIntToScalar(top),
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              SkIntToScalar(right), SkIntToScalar(bottom));
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return GraphicsJNI::getNativeCanvas(env, jcanvas)->clipRect(r);
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipRect_RectF(JNIEnv* env, jobject jcanvas, jobject rectf) {
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, rectf);
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* c = GraphicsJNI::getNativeCanvas(env, jcanvas);
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect tmp;
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return c->clipRect(*GraphicsJNI::jrectf_to_rect(env, rectf, &tmp));
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipRect_Rect(JNIEnv* env, jobject jcanvas, jobject rect) {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, jcanvas);
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_ZERO(env, rect);
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* c = GraphicsJNI::getNativeCanvas(env, jcanvas);
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect tmp;
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return c->clipRect(*GraphicsJNI::jrect_to_rect(env, rect, &tmp));
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipRect(JNIEnv* env, jobject, SkCanvas* canvas,
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             float left, float top, float right, float bottom,
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int op) {
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect rect;
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        rect.set(SkFloatToScalar(left), SkFloatToScalar(top),
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 SkFloatToScalar(right), SkFloatToScalar(bottom));
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->clipRect(rect, (SkRegion::Op)op);
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipPath(JNIEnv* env, jobject, SkCanvas* canvas,
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             SkPath* path, int op) {
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->clipPath(*path, (SkRegion::Op)op);
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean clipRegion(JNIEnv* env, jobject, SkCanvas* canvas,
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                               SkRegion* deviceRgn, int op) {
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->clipRegion(*deviceRgn, (SkRegion::Op)op);
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void setDrawFilter(JNIEnv* env, jobject, SkCanvas* canvas,
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkDrawFilter* filter) {
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->setDrawFilter(filter);
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean quickReject__RectFI(JNIEnv* env, jobject, SkCanvas* canvas,
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        jobject rect, int edgetype) {
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect rect_;
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrectf_to_rect(env, rect, &rect_);
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->quickReject(rect_, (SkCanvas::EdgeType)edgetype);
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean quickReject__PathI(JNIEnv* env, jobject, SkCanvas* canvas,
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                       SkPath* path, int edgetype) {
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->quickReject(*path, (SkCanvas::EdgeType)edgetype);
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static jboolean quickReject__FFFFI(JNIEnv* env, jobject, SkCanvas* canvas,
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                       jfloat left, jfloat top, jfloat right,
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                       jfloat bottom, int edgetype) {
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect r;
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        r.set(SkFloatToScalar(left), SkFloatToScalar(top),
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project              SkFloatToScalar(right), SkFloatToScalar(bottom));
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return canvas->quickReject(r, (SkCanvas::EdgeType)edgetype);
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawRGB(JNIEnv* env, jobject, SkCanvas* canvas,
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        jint r, jint g, jint b) {
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawARGB(0xFF, r, g, b);
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawARGB(JNIEnv* env, jobject, SkCanvas* canvas,
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         jint a, jint r, jint g, jint b) {
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawARGB(a, r, g, b);
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawColor__I(JNIEnv* env, jobject, SkCanvas* canvas,
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jint color) {
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawColor(color);
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawColor__II(JNIEnv* env, jobject, SkCanvas* canvas,
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              jint color, SkPorterDuff::Mode mode) {
31947c0d4eaa926d979c5ea366934750526c20af8ffMike Reed        canvas->drawColor(color, SkPorterDuff::ToXfermodeMode(mode));
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPaint(JNIEnv* env, jobject, SkCanvas* canvas,
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          SkPaint* paint) {
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawPaint(*paint);
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void doPoints(JNIEnv* env, jobject jcanvas, jfloatArray jptsArray,
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         jint offset, jint count, jobject jpaint,
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkCanvas::PointMode mode) {
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jptsArray);
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jpaint);
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const SkPaint& paint = *GraphicsJNI::getNativePaint(env, jpaint);
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaFloatArray autoPts(env, jptsArray);
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        float* floats = autoPts.ptr();
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const int length = autoPts.length();
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((offset | count) < 0 || offset + count > length) {
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            doThrowAIOOBE(env);
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // now convert the floats into SkPoints
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        count >>= 1;    // now it is the number of points
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkAutoSTMalloc<32, SkPoint> storage(count);
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* pts = storage.get();
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const float* src = floats + offset;
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i = 0; i < count; i++) {
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            pts[i].set(SkFloatToScalar(src[0]), SkFloatToScalar(src[1]));
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            src += 2;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawPoints(mode, count, pts, paint);
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPoints(JNIEnv* env, jobject jcanvas, jfloatArray jptsArray,
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           jint offset, jint count, jobject jpaint) {
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doPoints(env, jcanvas, jptsArray, offset, count, jpaint,
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 SkCanvas::kPoints_PointMode);
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawLines(JNIEnv* env, jobject jcanvas, jfloatArray jptsArray,
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           jint offset, jint count, jobject jpaint) {
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        doPoints(env, jcanvas, jptsArray, offset, count, jpaint,
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                 SkCanvas::kLines_PointMode);
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPoint(JNIEnv* env, jobject jcanvas, float x, float y,
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          jobject jpaint) {
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jcanvas);
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        NPE_CHECK_RETURN_VOID(env, jpaint);
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const SkPaint& paint = *GraphicsJNI::getNativePaint(env, jpaint);
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawPoint(SkFloatToScalar(x), SkFloatToScalar(y), paint);
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawLine__FFFFPaint(JNIEnv* env, jobject, SkCanvas* canvas,
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    jfloat startX, jfloat startY, jfloat stopX,
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    jfloat stopY, SkPaint* paint) {
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawLine(SkFloatToScalar(startX), SkFloatToScalar(startY),
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkFloatToScalar(stopX), SkFloatToScalar(stopY),
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         *paint);
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawRect__RectFPaint(JNIEnv* env, jobject, SkCanvas* canvas,
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                     jobject rect, SkPaint* paint) {
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect rect_;
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrectf_to_rect(env, rect, &rect_);
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawRect(rect_, *paint);
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawRect__FFFFPaint(JNIEnv* env, jobject, SkCanvas* canvas,
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    jfloat left, jfloat top, jfloat right,
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    jfloat bottom, SkPaint* paint) {
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar left_ = SkFloatToScalar(left);
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar top_ = SkFloatToScalar(top);
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar right_ = SkFloatToScalar(right);
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar bottom_ = SkFloatToScalar(bottom);
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawRectCoords(left_, top_, right_, bottom_, *paint);
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawOval(JNIEnv* env, jobject, SkCanvas* canvas, jobject joval,
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkPaint* paint) {
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect oval;
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrectf_to_rect(env, joval, &oval);
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawOval(oval, *paint);
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawCircle(JNIEnv* env, jobject, SkCanvas* canvas, jfloat cx,
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           jfloat cy, jfloat radius, SkPaint* paint) {
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawCircle(SkFloatToScalar(cx), SkFloatToScalar(cy),
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           SkFloatToScalar(radius), *paint);
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawArc(JNIEnv* env, jobject, SkCanvas* canvas, jobject joval,
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        jfloat startAngle, jfloat sweepAngle,
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        jboolean useCenter, SkPaint* paint) {
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect oval;
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrectf_to_rect(env, joval, &oval);
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawArc(oval, SkFloatToScalar(startAngle),
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        SkFloatToScalar(sweepAngle), useCenter, *paint);
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawRoundRect(JNIEnv* env, jobject, SkCanvas* canvas,
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              jobject jrect, jfloat rx, jfloat ry,
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              SkPaint* paint) {
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect rect;
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrectf_to_rect(env, jrect, &rect);
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawRoundRect(rect, SkFloatToScalar(rx), SkFloatToScalar(ry),
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              *paint);
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPath(JNIEnv* env, jobject, SkCanvas* canvas, SkPath* path,
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkPaint* paint) {
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawPath(*path, *paint);
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPicture(JNIEnv* env, jobject, SkCanvas* canvas,
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            SkPicture* picture) {
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkASSERT(canvas);
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkASSERT(picture);
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef TIME_DRAW
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkMSec now = get_thread_msec(); //SkTime::GetMSecs();
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawPicture(*picture);
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef TIME_DRAW
4505baa3a62a97544669fba6d65a11c07f252e654ddSteve Block        ALOGD("---- picture playback %d ms\n", get_thread_msec() - now);
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawBitmap__BitmapFFPaint(JNIEnv* env, jobject jcanvas,
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                          SkCanvas* canvas, SkBitmap* bitmap,
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                          jfloat left, jfloat top,
45711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn                                          SkPaint* paint, jint canvasDensity,
4580d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                                          jint screenDensity, jint bitmapDensity) {
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar left_ = SkFloatToScalar(left);
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkScalar top_ = SkFloatToScalar(top);
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
46211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn        if (canvasDensity == bitmapDensity || canvasDensity == 0
46311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn                || bitmapDensity == 0) {
4640d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            if (screenDensity != 0 && screenDensity != bitmapDensity) {
4650d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                SkPaint filteredPaint;
4660d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                if (paint) {
4670d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                    filteredPaint = *paint;
4680d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                }
4690d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                filteredPaint.setFilterBitmap(true);
4700d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                canvas->drawBitmap(*bitmap, left_, top_, &filteredPaint);
4710d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            } else {
4720d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                canvas->drawBitmap(*bitmap, left_, top_, paint);
4730d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            }
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas->save();
47611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            SkScalar scale = SkFloatToScalar(canvasDensity / (float)bitmapDensity);
47711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            canvas->translate(left_, top_);
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas->scale(scale, scale);
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkPaint filteredPaint;
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (paint) {
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                filteredPaint = *paint;
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            filteredPaint.setFilterBitmap(true);
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
48611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn            canvas->drawBitmap(*bitmap, 0, 0, &filteredPaint);
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            canvas->restore();
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void doDrawBitmap(JNIEnv* env, SkCanvas* canvas, SkBitmap* bitmap,
4930d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                        jobject srcIRect, const SkRect& dst, SkPaint* paint,
4940d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                        jint screenDensity, jint bitmapDensity) {
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkIRect    src, *srcPtr = NULL;
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (NULL != srcIRect) {
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            GraphicsJNI::jrect_to_irect(env, srcIRect, &src);
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            srcPtr = &src;
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5010d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn
5020d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn        if (screenDensity != 0 && screenDensity != bitmapDensity) {
5030d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            SkPaint filteredPaint;
5040d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            if (paint) {
5050d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                filteredPaint = *paint;
5060d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            }
5070d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            filteredPaint.setFilterBitmap(true);
5080d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            canvas->drawBitmapRect(*bitmap, srcPtr, dst, &filteredPaint);
5090d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn        } else {
5100d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn            canvas->drawBitmapRect(*bitmap, srcPtr, dst, paint);
5110d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn        }
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawBitmapRF(JNIEnv* env, jobject, SkCanvas* canvas,
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             SkBitmap* bitmap, jobject srcIRect,
5160d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                             jobject dstRectF, SkPaint* paint,
5170d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                             jint screenDensity, jint bitmapDensity) {
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect      dst;
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrectf_to_rect(env, dstRectF, &dst);
5200d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn        doDrawBitmap(env, canvas, bitmap, srcIRect, dst, paint,
5210d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                screenDensity, bitmapDensity);
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawBitmapRR(JNIEnv* env, jobject, SkCanvas* canvas,
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             SkBitmap* bitmap, jobject srcIRect,
5260d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                             jobject dstRect, SkPaint* paint,
5270d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                             jint screenDensity, jint bitmapDensity) {
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkRect      dst;
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        GraphicsJNI::jrect_to_rect(env, dstRect, &dst);
5300d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn        doDrawBitmap(env, canvas, bitmap, srcIRect, dst, paint,
5310d221012ff5fd314711c00ed30e9b807b9c454c1Dianne Hackborn                screenDensity, bitmapDensity);
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawBitmapArray(JNIEnv* env, jobject, SkCanvas* canvas,
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                jintArray jcolors, int offset, int stride,
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                jfloat x, jfloat y, int width, int height,
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                jboolean hasAlpha, SkPaint* paint)
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkBitmap    bitmap;
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bitmap.setConfig(hasAlpha ? SkBitmap::kARGB_8888_Config :
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                         SkBitmap::kRGB_565_Config, width, height);
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!bitmap.allocPixels()) {
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!GraphicsJNI::SetPixels(env, jcolors, offset, stride,
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    0, 0, width, height, bitmap)) {
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawBitmap(bitmap, SkFloatToScalar(x), SkFloatToScalar(y),
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                           paint);
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawBitmapMatrix(JNIEnv* env, jobject, SkCanvas* canvas,
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 const SkBitmap* bitmap, const SkMatrix* matrix,
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                 const SkPaint* paint) {
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawBitmapMatrix(*bitmap, *matrix, paint);
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawBitmapMesh(JNIEnv* env, jobject, SkCanvas* canvas,
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          const SkBitmap* bitmap, int meshWidth, int meshHeight,
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          jfloatArray jverts, int vertIndex, jintArray jcolors,
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                          int colorIndex, const SkPaint* paint) {
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const int ptCount = (meshWidth + 1) * (meshHeight + 1);
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const int indexCount = meshWidth * meshHeight * 6;
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaFloatArray  vertA(env, jverts, vertIndex + (ptCount << 1));
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaIntArray    colorA(env, jcolors, colorIndex + ptCount);
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /*  Our temp storage holds 2 or 3 arrays.
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            texture points [ptCount * sizeof(SkPoint)]
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                copy to convert from float to fixed
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            indices [ptCount * sizeof(uint16_t)]
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        */
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef SK_SCALAR_IS_FIXED
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        storageSize += ptCount * sizeof(SkPoint);  // storage for verts
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        storageSize += indexCount * sizeof(uint16_t);  // indices[]
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkAutoMalloc storage(storageSize);
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* texs = (SkPoint*)storage.get();
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* verts;
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        uint16_t* indices;
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef SK_SCALAR_IS_FLOAT
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        verts = (SkPoint*)(vertA.ptr() + vertIndex);
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        indices = (uint16_t*)(texs + ptCount);
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#else
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        verts = texs + ptCount;
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        indices = (uint16_t*)(verts + ptCount);
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // convert floats to fixed
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        {
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const float* src = vertA.ptr() + vertIndex;
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i = 0; i < ptCount; i++) {
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                verts[i].set(SkFloatToFixed(src[0]), SkFloatToFixed(src[1]));
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                src += 2;
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // cons up texture coordinates and indices
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        {
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const SkScalar w = SkIntToScalar(bitmap->width());
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const SkScalar h = SkIntToScalar(bitmap->height());
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const SkScalar dx = w / meshWidth;
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            const SkScalar dy = h / meshHeight;
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkPoint* texsPtr = texs;
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkScalar y = 0;
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i = 0; i <= meshHeight; i++) {
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (i == meshHeight) {
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    y = h;  // to ensure numerically we hit h exactly
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                SkScalar x = 0;
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                for (int j = 0; j < meshWidth; j++) {
6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    texsPtr->set(x, y);
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    texsPtr += 1;
6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    x += dx;
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                texsPtr->set(w, y);
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                texsPtr += 1;
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                y += dy;
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT(texsPtr - texs == ptCount);
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // cons up indices
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        {
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            uint16_t* indexPtr = indices;
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int index = 0;
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i = 0; i < meshHeight; i++) {
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                for (int j = 0; j < meshWidth; j++) {
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    // lower-left triangle
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *indexPtr++ = index;
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *indexPtr++ = index + meshWidth + 1;
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *indexPtr++ = index + meshWidth + 2;
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    // upper-right triangle
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *indexPtr++ = index;
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *indexPtr++ = index + meshWidth + 2;
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    *indexPtr++ = index + 1;
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    // bump to the next cell
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    index += 1;
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // bump to the next row
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                index += 1;
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT(indexPtr - indices == indexCount);
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // double-check that we have legal indices
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef SK_DEBUG
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        {
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i = 0; i < indexCount; i++) {
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        // cons-up a shader for the bitmap
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPaint tmpPaint;
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (paint) {
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            tmpPaint = *paint;
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkShader* shader = SkShader::CreateBitmapShader(*bitmap,
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
6716062c5912dc79704b489fc8c289b78a400ed05eeDerek Sollenberger        SkSafeUnref(tmpPaint.setShader(shader));
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, verts,
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             texs, (const SkColor*)colorA.ptr(), NULL, indices,
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             indexCount, tmpPaint);
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawVertices(JNIEnv* env, jobject, SkCanvas* canvas,
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             SkCanvas::VertexMode mode, int vertexCount,
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jfloatArray jverts, int vertIndex,
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jfloatArray jtexs, int texIndex,
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jintArray jcolors, int colorIndex,
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             jshortArray jindices, int indexIndex,
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             int indexCount, const SkPaint* paint) {
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaFloatArray  vertA(env, jverts, vertIndex + vertexCount);
6879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaFloatArray  texA(env, jtexs, texIndex + vertexCount);
6889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaIntArray    colorA(env, jcolors, colorIndex + vertexCount);
6899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        AutoJavaShortArray  indexA(env, jindices, indexIndex + indexCount);
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const int ptCount = vertexCount >> 1;
6929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* verts;
6949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* texs = NULL;
6959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#ifdef SK_SCALAR_IS_FLOAT
6969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        verts = (SkPoint*)(vertA.ptr() + vertIndex);
6979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jtexs != NULL) {
6989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            texs = (SkPoint*)(texA.ptr() + texIndex);
6999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#else
7019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int count = ptCount;    // for verts
7029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jtexs != NULL) {
7039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            count += ptCount;   // += for texs
7049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkAutoMalloc storage(count * sizeof(SkPoint));
7069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        verts = (SkPoint*)storage.get();
7079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const float* src = vertA.ptr() + vertIndex;
7089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i = 0; i < ptCount; i++) {
7099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            verts[i].set(SkFloatToFixed(src[0]), SkFloatToFixed(src[1]));
7109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            src += 2;
7119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jtexs != NULL) {
7139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            texs = verts + ptCount;
7149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            src = texA.ptr() + texIndex;
7159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int i = 0; i < ptCount; i++) {
7169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                texs[i].set(SkFloatToFixed(src[0]), SkFloatToFixed(src[1]));
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                src += 2;
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
7199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#endif
7219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const SkColor* colors = NULL;
7239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const uint16_t* indices = NULL;
7249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jcolors != NULL) {
7259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            colors = (const SkColor*)(colorA.ptr() + colorIndex);
7269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (jindices != NULL) {
7289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            indices = (const uint16_t*)(indexA.ptr() + indexIndex);
7299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
7309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawVertices(mode, ptCount, verts, texs, colors, NULL,
7329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                             indices, indexCount, *paint);
7339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
734f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt
7350c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
736f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt    static void drawText___CIIFFIPaint(JNIEnv* env, jobject, SkCanvas* canvas,
7379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                      jcharArray text, int index, int count,
738f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt                                      jfloat x, jfloat y, int flags, SkPaint* paint) {
7399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jchar* textArray = env->GetCharArrayElements(text, NULL);
740b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio        drawTextWithGlyphs(canvas, textArray + index, 0, count, x, y, flags, paint);
741f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        env->ReleaseCharArrayElements(text, textArray, JNI_ABORT);
7429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7430c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
744f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt    static void drawText__StringIIFFIPaint(JNIEnv* env, jobject,
7450c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt                                          SkCanvas* canvas, jstring text,
746f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt                                          int start, int end,
747f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt                                          jfloat x, jfloat y, int flags, SkPaint* paint) {
748f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        const jchar* textArray = env->GetStringChars(text, NULL);
749b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio        drawTextWithGlyphs(canvas, textArray, start, end, x, y, flags, paint);
750f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        env->ReleaseStringChars(text, textArray);
7519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7520c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
753689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio    static void drawTextWithGlyphs(SkCanvas* canvas, const jchar* textArray,
754689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio            int start, int end,
755689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio            jfloat x, jfloat y, int flags, SkPaint* paint) {
756689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio
757689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio        jint count = end - start;
75879df5323e7ed541b854cea5684a89e8be8c2dfc9Fabrice Di Meglio        drawTextWithGlyphs(canvas, textArray + start, 0, count, count, x, y, flags, paint);
759689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio    }
760689e515ed2b8064c15e54d8ab69d87de54c5e0d6Fabrice Di Meglio
761b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio    static void drawTextWithGlyphs(SkCanvas* canvas, const jchar* textArray,
762b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio            int start, int count, int contextCount,
763b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio            jfloat x, jfloat y, int flags, SkPaint* paint) {
764b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio
765a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
766a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio                textArray, start, count, contextCount, flags);
767b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio        if (value == NULL) {
768a731b082b2c43204e6e9f927ab82fb732934a83bFabrice Di Meglio            return;
769b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio        }
7702301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        SkPaint::Align align = paint->getTextAlign();
7712301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        if (align == SkPaint::kCenter_Align) {
7722301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            x -= 0.5 * value->getTotalAdvance();
7732301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        } else if (align == SkPaint::kRight_Align) {
7742301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            x -= value->getTotalAdvance();
7752301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        }
7762301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        paint->setTextAlign(SkPaint::kLeft_Align);
7772301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        doDrawGlyphsPos(canvas, value->getGlyphs(), value->getPos(), 0, value->getGlyphsCount(), x, y, flags, paint);
7782301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        doDrawTextDecorations(canvas, x, y, value->getTotalAdvance(), paint);
7792301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        paint->setTextAlign(align);
7802301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien    }
7812301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien
7822301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien// Same values used by Skia
7832301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien#define kStdStrikeThru_Offset   (-6.0f / 21.0f)
7842301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien#define kStdUnderline_Offset    (1.0f / 9.0f)
7852301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien#define kStdUnderline_Thickness (1.0f / 18.0f)
7862301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien
7872301d32f7e2ba584abc31ac177dde754385d3c04Raph Levienstatic void doDrawTextDecorations(SkCanvas* canvas, jfloat x, jfloat y, jfloat length, SkPaint* paint) {
788e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien    uint32_t flags;
789e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien    SkDrawFilter* drawFilter = canvas->getDrawFilter();
790e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien    if (drawFilter) {
791e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien        SkPaint paintCopy(*paint);
792e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien        drawFilter->filter(&paintCopy, SkDrawFilter::kText_Type);
793e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien        flags = paintCopy.getFlags();
794e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien    } else {
795e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien        flags = paint->getFlags();
796e174ae210e4756985c17da5bf4457e2009ec5b61Raph Levien    }
7972301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien    if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
7982301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        SkScalar left = SkFloatToScalar(x);
7992301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        SkScalar right = SkFloatToScalar(x + length);
8002301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        float textSize = paint->getTextSize();
8012301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
8022301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        if (flags & SkPaint::kUnderlineText_Flag) {
8032301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            SkScalar top = SkFloatToScalar(y + textSize * kStdUnderline_Offset
8042301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien                    - 0.5f * strokeWidth);
8052301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            SkScalar bottom = SkFloatToScalar(y + textSize * kStdUnderline_Offset
8062301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien                    + 0.5f * strokeWidth);
8072301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            canvas->drawRectCoords(left, top, right, bottom, *paint);
8082301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        }
8092301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        if (flags & SkPaint::kStrikeThruText_Flag) {
8102301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            SkScalar top = SkFloatToScalar(y + textSize * kStdStrikeThru_Offset
8112301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien                    - 0.5f * strokeWidth);
8122301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            SkScalar bottom = SkFloatToScalar(y + textSize * kStdStrikeThru_Offset
8132301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien                    + 0.5f * strokeWidth);
8142301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            canvas->drawRectCoords(left, top, right, bottom, *paint);
8152301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        }
816b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio    }
8172301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien}
818b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio
819fcf2be1846935e7983ea2fe87fdd4d7af27764b6Fabrice Di Meglio    static void doDrawGlyphs(SkCanvas* canvas, const jchar* glyphArray, int index, int count,
820fcf2be1846935e7983ea2fe87fdd4d7af27764b6Fabrice Di Meglio            jfloat x, jfloat y, int flags, SkPaint* paint) {
821c511bee87cda99a252d1a62487f47c8f05aee78cFabrice Di Meglio        // Beware: this needs Glyph encoding (already done on the Paint constructor)
822fcf2be1846935e7983ea2fe87fdd4d7af27764b6Fabrice Di Meglio        canvas->drawText(glyphArray + index * 2, count * 2, x, y, *paint);
823fcf2be1846935e7983ea2fe87fdd4d7af27764b6Fabrice Di Meglio    }
824fcf2be1846935e7983ea2fe87fdd4d7af27764b6Fabrice Di Meglio
8252301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien    static void doDrawGlyphsPos(SkCanvas* canvas, const jchar* glyphArray, const jfloat* posArray,
8262301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            int index, int count, jfloat x, jfloat y, int flags, SkPaint* paint) {
8272301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        SkPoint* posPtr = new SkPoint[count];
8282301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        for (int indx = 0; indx < count; indx++) {
8292301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            posPtr[indx].fX = SkFloatToScalar(x + posArray[indx * 2]);
8302301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien            posPtr[indx].fY = SkFloatToScalar(y + posArray[indx * 2 + 1]);
8312301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        }
8322301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        canvas->drawPosText(glyphArray, count << 1, posPtr, *paint);
8332301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien        delete[] posPtr;
8342301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien    }
8352301d32f7e2ba584abc31ac177dde754385d3c04Raph Levien
8360c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static void drawTextRun___CIIIIFFIPaint(
837f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        JNIEnv* env, jobject, SkCanvas* canvas, jcharArray text, int index,
8380c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        int count, int contextIndex, int contextCount,
8394beb8ff7175ebd14b96942724a658f407d0b9951Doug Felt        jfloat x, jfloat y, int dirFlags, SkPaint* paint) {
840f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt
841f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        jchar* chars = env->GetCharArrayElements(text, NULL);
842b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio        drawTextWithGlyphs(canvas, chars + contextIndex, index - contextIndex,
843b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio                count, contextCount, x, y, dirFlags, paint);
844f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        env->ReleaseCharArrayElements(text, chars, JNI_ABORT);
845f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt    }
846f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt
8470c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt    static void drawTextRun__StringIIIIFFIPaint(
8480c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        JNIEnv* env, jobject obj, SkCanvas* canvas, jstring text, jint start,
8490c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint end, jint contextStart, jint contextEnd,
8504beb8ff7175ebd14b96942724a658f407d0b9951Doug Felt        jfloat x, jfloat y, jint dirFlags, SkPaint* paint) {
851f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt
8520c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint count = end - start;
8530c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt        jint contextCount = contextEnd - contextStart;
854f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        const jchar* chars = env->GetStringChars(text, NULL);
855b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio        drawTextWithGlyphs(canvas, chars + contextStart, start - contextStart,
856b39d89735b8d88669c779b6a8549a179fd0f6f44Fabrice Di Meglio                count, contextCount, x, y, dirFlags, paint);
857f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt        env->ReleaseStringChars(text, chars);
858f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt    }
859f47d7405bbcb25d7cdf89ebb059f41520fe9ab87Doug Felt
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPosText___CII_FPaint(JNIEnv* env, jobject, SkCanvas* canvas,
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                         jcharArray text, int index, int count,
8629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                         jfloatArray pos, SkPaint* paint) {
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jchar* textArray = text ? env->GetCharArrayElements(text, NULL) : NULL;
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        jsize textCount = text ? env->GetArrayLength(text) : NULL;
8659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        float* posArray = pos ? env->GetFloatArrayElements(pos, NULL) : NULL;
8669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int posCount = pos ? env->GetArrayLength(pos) >> 1: 0;
8679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
8689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int indx;
8699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (indx = 0; indx < posCount; indx++) {
8709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            posPtr[indx].fX = SkFloatToScalar(posArray[indx << 1]);
8719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            posPtr[indx].fY = SkFloatToScalar(posArray[(indx << 1) + 1]);
8729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
87362b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy
87462b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy        SkPaint::TextEncoding encoding = paint->getTextEncoding();
87562b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy        paint->setTextEncoding(SkPaint::kUTF16_TextEncoding);
876ad8b8f57a457ff615112b7fa4987f39e75fc5ff6Mike Reed        canvas->drawPosText(textArray + index, count << 1, posPtr, *paint);
87762b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy        paint->setTextEncoding(encoding);
87862b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (text) {
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseCharArrayElements(text, textArray, 0);
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pos) {
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            env->ReleaseFloatArrayElements(pos, posArray, 0);
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        delete[] posPtr;
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8870c702b88c5d0d4380930b920f5be6e66dd95a0d8Doug Felt
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static void drawPosText__String_FPaint(JNIEnv* env, jobject,
8899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                           SkCanvas* canvas, jstring text,
8904beb8ff7175ebd14b96942724a658f407d0b9951Doug Felt                                           jfloatArray pos,
8914beb8ff7175ebd14b96942724a658f407d0b9951Doug Felt                                           SkPaint* paint) {
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const void* text_ = text ? env->GetStringChars(text, NULL) : NULL;
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int byteLength = text ? env->GetStringLength(text) : 0;
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        float* posArray = pos ? env->GetFloatArrayElements(pos, NULL) : NULL;
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int posCount = pos ? env->GetArrayLength(pos) >> 1: 0;
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
8979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int indx = 0; indx < posCount; indx++) {
8999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            posPtr[indx].fX = SkFloatToScalar(posArray[indx << 1]);
9009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            posPtr[indx].fY = SkFloatToScalar(posArray[(indx << 1) + 1]);
9019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
90262b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy
90362b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy        SkPaint::TextEncoding encoding = paint->getTextEncoding();
90462b6eaa7f3a8111311a7ee097f278eb55865a499Romain Guy        paint->setTextEncoding(SkPaint::kUTF16_TextEncoding);
9059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        canvas->drawPosText(text_, byteLength << 1, posPtr, *paint);
906