Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "jni.h" |
| 18 | #include "GraphicsJNI.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 19 | #include "core_jni_helpers.h" |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 20 | |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 21 | #include <androidfw/ResourceTypes.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 22 | #include <hwui/Canvas.h> |
| 23 | #include <hwui/Paint.h> |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 24 | #include <hwui/Typeface.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 25 | #include <minikin/Layout.h> |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 26 | |
| 27 | #include "Bitmap.h" |
Derek Sollenberger | acb4099 | 2014-07-21 15:22:10 -0400 | [diff] [blame] | 28 | #include "SkDrawFilter.h" |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 29 | #include "SkGraphics.h" |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | namespace CanvasJNI { |
| 34 | |
| 35 | static Canvas* get_canvas(jlong canvasHandle) { |
| 36 | return reinterpret_cast<Canvas*>(canvasHandle); |
| 37 | } |
| 38 | |
Richard Uhler | 775873a | 2015-12-29 12:37:39 -0800 | [diff] [blame] | 39 | static void delete_canvas(Canvas* canvas) { |
| 40 | delete canvas; |
| 41 | } |
| 42 | |
| 43 | static jlong getNativeFinalizer(JNIEnv* env, jobject clazz) { |
| 44 | return static_cast<jlong>(reinterpret_cast<uintptr_t>(&delete_canvas)); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // Native wrapper constructor used by Canvas(Bitmap) |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 48 | static jlong initRaster(JNIEnv* env, jobject, jobject jbitmap) { |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 49 | SkBitmap bitmap; |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 50 | if (jbitmap != NULL) { |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 51 | GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap); |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 52 | } |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 53 | return reinterpret_cast<jlong>(Canvas::create_canvas(bitmap)); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Set the given bitmap as the new draw target (wrapped in a new SkCanvas), |
| 57 | // optionally copying canvas matrix & clip state. |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 58 | static void setBitmap(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap) { |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 59 | SkBitmap bitmap; |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 60 | if (jbitmap != NULL) { |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 61 | GraphicsJNI::getSkBitmap(env, jbitmap, &bitmap); |
John Reck | c1b33d6 | 2015-04-22 09:04:45 -0700 | [diff] [blame] | 62 | } |
John Reck | 3731dc2 | 2015-04-13 15:20:29 -0700 | [diff] [blame] | 63 | get_canvas(canvasHandle)->setBitmap(bitmap); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 66 | static jboolean isOpaque(jlong canvasHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 67 | return get_canvas(canvasHandle)->isOpaque() ? JNI_TRUE : JNI_FALSE; |
| 68 | } |
| 69 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 70 | static jint getWidth(jlong canvasHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 71 | return static_cast<jint>(get_canvas(canvasHandle)->width()); |
| 72 | } |
| 73 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 74 | static jint getHeight(jlong canvasHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 75 | return static_cast<jint>(get_canvas(canvasHandle)->height()); |
| 76 | } |
| 77 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 78 | static void setHighContrastText(jlong canvasHandle, jboolean highContrastText) { |
Derek Sollenberger | 6578a98 | 2015-07-13 13:24:29 -0400 | [diff] [blame] | 79 | Canvas* canvas = get_canvas(canvasHandle); |
| 80 | canvas->setHighContrastText(highContrastText); |
| 81 | } |
| 82 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 83 | static jint save(jlong canvasHandle, jint flagsHandle) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 84 | SaveFlags::Flags flags = static_cast<SaveFlags::Flags>(flagsHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 85 | return static_cast<jint>(get_canvas(canvasHandle)->save(flags)); |
| 86 | } |
| 87 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 88 | static jint saveLayer(jlong canvasHandle, jfloat l, jfloat t, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 89 | jfloat r, jfloat b, jlong paintHandle, jint flagsHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 90 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 91 | SaveFlags::Flags flags = static_cast<SaveFlags::Flags>(flagsHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 92 | return static_cast<jint>(get_canvas(canvasHandle)->saveLayer(l, t, r, b, paint, flags)); |
| 93 | } |
| 94 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 95 | static jint saveLayerAlpha(jlong canvasHandle, jfloat l, jfloat t, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 96 | jfloat r, jfloat b, jint alpha, jint flagsHandle) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 97 | SaveFlags::Flags flags = static_cast<SaveFlags::Flags>(flagsHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 98 | return static_cast<jint>(get_canvas(canvasHandle)->saveLayerAlpha(l, t, r, b, alpha, flags)); |
| 99 | } |
| 100 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 101 | static bool restore(jlong canvasHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 102 | Canvas* canvas = get_canvas(canvasHandle); |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 103 | if (canvas->getSaveCount() <= 1) { |
| 104 | return false; // cannot restore anymore |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 105 | } |
| 106 | canvas->restore(); |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 107 | return true; // success |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 110 | static void restoreToCount(jlong canvasHandle, jint saveCount) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 111 | Canvas* canvas = get_canvas(canvasHandle); |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 112 | canvas->restoreToCount(saveCount); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 113 | } |
| 114 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 115 | static jint getSaveCount(jlong canvasHandle) { |
| 116 | return static_cast<jint>(get_canvas(canvasHandle)->getSaveCount()); |
| 117 | } |
| 118 | |
| 119 | static void getMatrix(jlong canvasHandle, jlong matrixHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 120 | SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
| 121 | get_canvas(canvasHandle)->getMatrix(matrix); |
| 122 | } |
| 123 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 124 | static void setMatrix(jlong canvasHandle, jlong matrixHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 125 | const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
| 126 | get_canvas(canvasHandle)->setMatrix(matrix ? *matrix : SkMatrix::I()); |
| 127 | } |
| 128 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 129 | static void concat(jlong canvasHandle, jlong matrixHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 130 | const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
| 131 | get_canvas(canvasHandle)->concat(*matrix); |
| 132 | } |
| 133 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 134 | static void rotate(jlong canvasHandle, jfloat degrees) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 135 | get_canvas(canvasHandle)->rotate(degrees); |
| 136 | } |
| 137 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 138 | static void scale(jlong canvasHandle, jfloat sx, jfloat sy) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 139 | get_canvas(canvasHandle)->scale(sx, sy); |
| 140 | } |
| 141 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 142 | static void skew(jlong canvasHandle, jfloat sx, jfloat sy) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 143 | get_canvas(canvasHandle)->skew(sx, sy); |
| 144 | } |
| 145 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 146 | static void translate(jlong canvasHandle, jfloat dx, jfloat dy) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 147 | get_canvas(canvasHandle)->translate(dx, dy); |
| 148 | } |
| 149 | |
| 150 | static jboolean getClipBounds(JNIEnv* env, jobject, jlong canvasHandle, jobject bounds) { |
| 151 | SkRect r; |
| 152 | SkIRect ir; |
| 153 | bool result = get_canvas(canvasHandle)->getClipBounds(&r); |
| 154 | |
| 155 | if (!result) { |
| 156 | r.setEmpty(); |
| 157 | } |
| 158 | r.round(&ir); |
| 159 | |
| 160 | (void)GraphicsJNI::irect_to_jrect(ir, env, bounds); |
| 161 | return result ? JNI_TRUE : JNI_FALSE; |
| 162 | } |
| 163 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 164 | static jboolean quickRejectRect(jlong canvasHandle, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 165 | jfloat left, jfloat top, jfloat right, jfloat bottom) { |
| 166 | bool result = get_canvas(canvasHandle)->quickRejectRect(left, top, right, bottom); |
| 167 | return result ? JNI_TRUE : JNI_FALSE; |
| 168 | } |
| 169 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 170 | static jboolean quickRejectPath(jlong canvasHandle, jlong pathHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 171 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
| 172 | bool result = get_canvas(canvasHandle)->quickRejectPath(*path); |
| 173 | return result ? JNI_TRUE : JNI_FALSE; |
| 174 | } |
| 175 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 176 | // SkRegion::Op and SkClipOp are numerically identical, so we can freely cast |
| 177 | // from one to the other (though SkClipOp is destined to become a strict subset) |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 178 | static_assert(SkRegion::kDifference_Op == static_cast<SkRegion::Op>(SkClipOp::kDifference), ""); |
| 179 | static_assert(SkRegion::kIntersect_Op == static_cast<SkRegion::Op>(SkClipOp::kIntersect), ""); |
Chris Craik | 66b9d44 | 2016-12-16 17:49:48 +0000 | [diff] [blame] | 180 | static_assert(SkRegion::kUnion_Op == static_cast<SkRegion::Op>(SkClipOp::kUnion), ""); |
| 181 | static_assert(SkRegion::kXOR_Op == static_cast<SkRegion::Op>(SkClipOp::kXOR), ""); |
| 182 | static_assert(SkRegion::kReverseDifference_Op == static_cast<SkRegion::Op>(SkClipOp::kReverseDifference), ""); |
| 183 | static_assert(SkRegion::kReplace_Op == static_cast<SkRegion::Op>(SkClipOp::kReplace), ""); |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 184 | |
| 185 | static SkClipOp opHandleToClipOp(jint opHandle) { |
| 186 | // The opHandle is defined in Canvas.java to be Region::Op |
| 187 | SkRegion::Op rgnOp = static_cast<SkRegion::Op>(opHandle); |
| 188 | |
| 189 | // In the future, when we no longer support the wide range of ops (e.g. Union, Xor) |
| 190 | // this function can perform a range check and throw an unsupported-exception. |
| 191 | // e.g. if (rgnOp != kIntersect && rgnOp != kDifference) throw... |
| 192 | |
| 193 | // Skia now takes a different type, SkClipOp, as the parameter to clipping calls |
| 194 | // This type is binary compatible with SkRegion::Op, so a static_cast<> is safe. |
| 195 | return static_cast<SkClipOp>(rgnOp); |
| 196 | } |
| 197 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 198 | static jboolean clipRect(jlong canvasHandle, jfloat l, jfloat t, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 199 | jfloat r, jfloat b, jint opHandle) { |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 200 | bool nonEmptyClip = get_canvas(canvasHandle)->clipRect(l, t, r, b, |
| 201 | opHandleToClipOp(opHandle)); |
Chris Craik | 5ec6a28 | 2015-06-23 15:42:12 -0700 | [diff] [blame] | 202 | return nonEmptyClip ? JNI_TRUE : JNI_FALSE; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 205 | static jboolean clipPath(jlong canvasHandle, jlong pathHandle, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 206 | jint opHandle) { |
| 207 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 208 | bool nonEmptyClip = get_canvas(canvasHandle)->clipPath(path, opHandleToClipOp(opHandle)); |
Chris Craik | 5ec6a28 | 2015-06-23 15:42:12 -0700 | [diff] [blame] | 209 | return nonEmptyClip ? JNI_TRUE : JNI_FALSE; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 212 | static void drawColor(JNIEnv* env, jobject, jlong canvasHandle, jint color, jint modeHandle) { |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 213 | SkBlendMode mode = static_cast<SkBlendMode>(modeHandle); |
Chris Craik | 1526a45 | 2015-03-06 18:42:15 +0000 | [diff] [blame] | 214 | get_canvas(canvasHandle)->drawColor(color, mode); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static void drawPaint(JNIEnv* env, jobject, jlong canvasHandle, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 218 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 219 | get_canvas(canvasHandle)->drawPaint(*paint); |
| 220 | } |
| 221 | |
| 222 | static void drawPoint(JNIEnv*, jobject, jlong canvasHandle, jfloat x, jfloat y, |
| 223 | jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 224 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 225 | get_canvas(canvasHandle)->drawPoint(x, y, *paint); |
| 226 | } |
| 227 | |
| 228 | static void drawPoints(JNIEnv* env, jobject, jlong canvasHandle, jfloatArray jptsArray, |
| 229 | jint offset, jint count, jlong paintHandle) { |
| 230 | NPE_CHECK_RETURN_VOID(env, jptsArray); |
| 231 | AutoJavaFloatArray autoPts(env, jptsArray); |
| 232 | float* floats = autoPts.ptr(); |
| 233 | const int length = autoPts.length(); |
| 234 | |
| 235 | if ((offset | count) < 0 || offset + count > length) { |
| 236 | doThrowAIOOBE(env); |
| 237 | return; |
| 238 | } |
| 239 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 240 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 241 | get_canvas(canvasHandle)->drawPoints(floats + offset, count, *paint); |
| 242 | } |
| 243 | |
| 244 | static void drawLine(JNIEnv* env, jobject, jlong canvasHandle, jfloat startX, jfloat startY, |
| 245 | jfloat stopX, jfloat stopY, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 246 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 247 | get_canvas(canvasHandle)->drawLine(startX, startY, stopX, stopY, *paint); |
| 248 | } |
| 249 | |
| 250 | static void drawLines(JNIEnv* env, jobject, jlong canvasHandle, jfloatArray jptsArray, |
| 251 | jint offset, jint count, jlong paintHandle) { |
| 252 | NPE_CHECK_RETURN_VOID(env, jptsArray); |
| 253 | AutoJavaFloatArray autoPts(env, jptsArray); |
| 254 | float* floats = autoPts.ptr(); |
| 255 | const int length = autoPts.length(); |
| 256 | |
| 257 | if ((offset | count) < 0 || offset + count > length) { |
| 258 | doThrowAIOOBE(env); |
| 259 | return; |
| 260 | } |
| 261 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 262 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 263 | get_canvas(canvasHandle)->drawLines(floats + offset, count, *paint); |
| 264 | } |
| 265 | |
| 266 | static void drawRect(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top, |
| 267 | jfloat right, jfloat bottom, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 268 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 269 | get_canvas(canvasHandle)->drawRect(left, top, right, bottom, *paint); |
| 270 | } |
| 271 | |
Derek Sollenberger | 94394b3 | 2015-07-10 09:58:41 -0400 | [diff] [blame] | 272 | static void drawRegion(JNIEnv* env, jobject, jlong canvasHandle, jlong regionHandle, |
| 273 | jlong paintHandle) { |
| 274 | const SkRegion* region = reinterpret_cast<SkRegion*>(regionHandle); |
| 275 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 276 | get_canvas(canvasHandle)->drawRegion(*region, *paint); |
| 277 | } |
| 278 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 279 | static void drawRoundRect(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top, |
| 280 | jfloat right, jfloat bottom, jfloat rx, jfloat ry, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 281 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 282 | get_canvas(canvasHandle)->drawRoundRect(left, top, right, bottom, rx, ry, *paint); |
| 283 | } |
| 284 | |
| 285 | static void drawCircle(JNIEnv* env, jobject, jlong canvasHandle, jfloat cx, jfloat cy, |
| 286 | jfloat radius, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 287 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 288 | get_canvas(canvasHandle)->drawCircle(cx, cy, radius, *paint); |
| 289 | } |
| 290 | |
| 291 | static void drawOval(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top, |
| 292 | jfloat right, jfloat bottom, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 293 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 294 | get_canvas(canvasHandle)->drawOval(left, top, right, bottom, *paint); |
| 295 | } |
| 296 | |
| 297 | static void drawArc(JNIEnv* env, jobject, jlong canvasHandle, jfloat left, jfloat top, |
| 298 | jfloat right, jfloat bottom, jfloat startAngle, jfloat sweepAngle, |
| 299 | jboolean useCenter, jlong paintHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 300 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 301 | get_canvas(canvasHandle)->drawArc(left, top, right, bottom, startAngle, sweepAngle, |
| 302 | useCenter, *paint); |
| 303 | } |
| 304 | |
| 305 | static void drawPath(JNIEnv* env, jobject, jlong canvasHandle, jlong pathHandle, |
| 306 | jlong paintHandle) { |
| 307 | const SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 308 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 309 | get_canvas(canvasHandle)->drawPath(*path, *paint); |
| 310 | } |
| 311 | |
| 312 | static void drawVertices(JNIEnv* env, jobject, jlong canvasHandle, |
| 313 | jint modeHandle, jint vertexCount, |
| 314 | jfloatArray jverts, jint vertIndex, |
| 315 | jfloatArray jtexs, jint texIndex, |
| 316 | jintArray jcolors, jint colorIndex, |
| 317 | jshortArray jindices, jint indexIndex, |
| 318 | jint indexCount, jlong paintHandle) { |
| 319 | AutoJavaFloatArray vertA(env, jverts, vertIndex + vertexCount); |
| 320 | AutoJavaFloatArray texA(env, jtexs, texIndex + vertexCount); |
| 321 | AutoJavaIntArray colorA(env, jcolors, colorIndex + vertexCount); |
| 322 | AutoJavaShortArray indexA(env, jindices, indexIndex + indexCount); |
| 323 | |
| 324 | const float* verts = vertA.ptr() + vertIndex; |
| 325 | const float* texs = texA.ptr() + vertIndex; |
| 326 | const int* colors = NULL; |
| 327 | const uint16_t* indices = NULL; |
| 328 | |
| 329 | if (jcolors != NULL) { |
| 330 | colors = colorA.ptr() + colorIndex; |
| 331 | } |
| 332 | if (jindices != NULL) { |
| 333 | indices = (const uint16_t*)(indexA.ptr() + indexIndex); |
| 334 | } |
| 335 | |
| 336 | SkCanvas::VertexMode mode = static_cast<SkCanvas::VertexMode>(modeHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 337 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 338 | get_canvas(canvasHandle)->drawVertices(mode, vertexCount, verts, texs, colors, |
| 339 | indices, indexCount, *paint); |
| 340 | } |
| 341 | |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 342 | static void drawNinePatch(JNIEnv* env, jobject, jlong canvasHandle, jlong bitmapHandle, |
| 343 | jlong chunkHandle, jfloat left, jfloat top, jfloat right, jfloat bottom, |
| 344 | jlong paintHandle, jint dstDensity, jint srcDensity) { |
| 345 | |
| 346 | Canvas* canvas = get_canvas(canvasHandle); |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 347 | Bitmap& bitmap = android::bitmap::toBitmap(env, bitmapHandle); |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 348 | const android::Res_png_9patch* chunk = reinterpret_cast<android::Res_png_9patch*>(chunkHandle); |
| 349 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 350 | |
| 351 | if (CC_LIKELY(dstDensity == srcDensity || dstDensity == 0 || srcDensity == 0)) { |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 352 | canvas->drawNinePatch(bitmap, *chunk, left, top, right, bottom, paint); |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 353 | } else { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 354 | canvas->save(SaveFlags::MatrixClip); |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 355 | |
| 356 | SkScalar scale = dstDensity / (float)srcDensity; |
| 357 | canvas->translate(left, top); |
| 358 | canvas->scale(scale, scale); |
| 359 | |
| 360 | Paint filteredPaint; |
| 361 | if (paint) { |
| 362 | filteredPaint = *paint; |
| 363 | } |
| 364 | filteredPaint.setFilterQuality(kLow_SkFilterQuality); |
| 365 | |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 366 | canvas->drawNinePatch(bitmap, *chunk, 0, 0, (right-left)/scale, (bottom-top)/scale, |
Derek Sollenberger | 4c5efe9 | 2015-07-10 13:56:39 -0400 | [diff] [blame] | 367 | &filteredPaint); |
| 368 | |
| 369 | canvas->restore(); |
| 370 | } |
| 371 | } |
| 372 | |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 373 | static void drawBitmap(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 374 | jfloat left, jfloat top, jlong paintHandle, jint canvasDensity, |
| 375 | jint screenDensity, jint bitmapDensity) { |
| 376 | Canvas* canvas = get_canvas(canvasHandle); |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 377 | Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 378 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 379 | |
| 380 | if (canvasDensity == bitmapDensity || canvasDensity == 0 || bitmapDensity == 0) { |
| 381 | if (screenDensity != 0 && screenDensity != bitmapDensity) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 382 | Paint filteredPaint; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 383 | if (paint) { |
| 384 | filteredPaint = *paint; |
| 385 | } |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 386 | filteredPaint.setFilterQuality(kLow_SkFilterQuality); |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 387 | canvas->drawBitmap(bitmap, left, top, &filteredPaint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 388 | } else { |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 389 | canvas->drawBitmap(bitmap, left, top, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 390 | } |
| 391 | } else { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 392 | canvas->save(SaveFlags::MatrixClip); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 393 | SkScalar scale = canvasDensity / (float)bitmapDensity; |
| 394 | canvas->translate(left, top); |
| 395 | canvas->scale(scale, scale); |
| 396 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 397 | Paint filteredPaint; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 398 | if (paint) { |
| 399 | filteredPaint = *paint; |
| 400 | } |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 401 | filteredPaint.setFilterQuality(kLow_SkFilterQuality); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 402 | |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 403 | canvas->drawBitmap(bitmap, 0, 0, &filteredPaint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 404 | canvas->restore(); |
| 405 | } |
| 406 | } |
| 407 | |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 408 | static void drawBitmapMatrix(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 409 | jlong matrixHandle, jlong paintHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 410 | const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 411 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 412 | Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap); |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 413 | get_canvas(canvasHandle)->drawBitmap(bitmap, *matrix, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 414 | } |
| 415 | |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 416 | static void drawBitmapRect(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 417 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 418 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
| 419 | jlong paintHandle, jint screenDensity, jint bitmapDensity) { |
| 420 | Canvas* canvas = get_canvas(canvasHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 421 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 422 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 423 | Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 424 | if (screenDensity != 0 && screenDensity != bitmapDensity) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 425 | Paint filteredPaint; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 426 | if (paint) { |
| 427 | filteredPaint = *paint; |
| 428 | } |
Mike Reed | 2a1ce8a | 2015-03-16 11:16:09 -0400 | [diff] [blame] | 429 | filteredPaint.setFilterQuality(kLow_SkFilterQuality); |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 430 | canvas->drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 431 | dstLeft, dstTop, dstRight, dstBottom, &filteredPaint); |
| 432 | } else { |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 433 | canvas->drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 434 | dstLeft, dstTop, dstRight, dstBottom, paint); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | static void drawBitmapArray(JNIEnv* env, jobject, jlong canvasHandle, |
| 439 | jintArray jcolors, jint offset, jint stride, |
| 440 | jfloat x, jfloat y, jint width, jint height, |
| 441 | jboolean hasAlpha, jlong paintHandle) { |
| 442 | // Note: If hasAlpha is false, kRGB_565_SkColorType will be used, which will |
| 443 | // correct the alphaType to kOpaque_SkAlphaType. |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 444 | SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, |
| 445 | GraphicsJNI::defaultColorSpace()); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 446 | SkBitmap bitmap; |
Derek Sollenberger | 3d4eed7 | 2014-12-04 15:20:29 -0500 | [diff] [blame] | 447 | bitmap.setInfo(info); |
sergeyv | c1c5406 | 2016-10-19 18:47:26 -0700 | [diff] [blame] | 448 | sk_sp<Bitmap> androidBitmap = Bitmap::allocateHeapBitmap(&bitmap, NULL); |
| 449 | if (!androidBitmap) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | |
| 453 | if (!GraphicsJNI::SetPixels(env, jcolors, offset, stride, 0, 0, width, height, bitmap)) { |
| 454 | return; |
| 455 | } |
| 456 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 457 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 458 | get_canvas(canvasHandle)->drawBitmap(*androidBitmap, x, y, paint); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 459 | } |
| 460 | |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 461 | static void drawBitmapMesh(JNIEnv* env, jobject, jlong canvasHandle, jobject jbitmap, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 462 | jint meshWidth, jint meshHeight, jfloatArray jverts, |
| 463 | jint vertIndex, jintArray jcolors, jint colorIndex, jlong paintHandle) { |
| 464 | const int ptCount = (meshWidth + 1) * (meshHeight + 1); |
| 465 | AutoJavaFloatArray vertA(env, jverts, vertIndex + (ptCount << 1)); |
| 466 | AutoJavaIntArray colorA(env, jcolors, colorIndex + ptCount); |
| 467 | |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 468 | const Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | 5fd2a1c | 2016-10-20 15:04:28 -0700 | [diff] [blame] | 469 | Bitmap& bitmap = android::bitmap::toBitmap(env, jbitmap); |
John Reck | 7c103a3 | 2015-04-15 15:52:10 -0700 | [diff] [blame] | 470 | get_canvas(canvasHandle)->drawBitmapMesh(bitmap, meshWidth, meshHeight, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 471 | vertA.ptr(), colorA.ptr(), paint); |
| 472 | } |
| 473 | |
| 474 | static void drawTextChars(JNIEnv* env, jobject, jlong canvasHandle, jcharArray text, |
| 475 | jint index, jint count, jfloat x, jfloat y, jint bidiFlags, |
| 476 | jlong paintHandle, jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 477 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 478 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 479 | jchar* jchars = env->GetCharArrayElements(text, NULL); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 480 | get_canvas(canvasHandle)->drawText(jchars + index, 0, count, count, x, y, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 481 | bidiFlags, *paint, typeface); |
| 482 | env->ReleaseCharArrayElements(text, jchars, JNI_ABORT); |
| 483 | } |
| 484 | |
| 485 | static void drawTextString(JNIEnv* env, jobject, jlong canvasHandle, jstring text, |
| 486 | jint start, jint end, jfloat x, jfloat y, jint bidiFlags, |
| 487 | jlong paintHandle, jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 488 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 489 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 490 | const int count = end - start; |
| 491 | const jchar* jchars = env->GetStringChars(text, NULL); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 492 | get_canvas(canvasHandle)->drawText(jchars + start, 0, count, count, x, y, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 493 | bidiFlags, *paint, typeface); |
| 494 | env->ReleaseStringChars(text, jchars); |
| 495 | } |
| 496 | |
| 497 | static void drawTextRunChars(JNIEnv* env, jobject, jlong canvasHandle, jcharArray text, jint index, |
| 498 | jint count, jint contextIndex, jint contextCount, jfloat x, jfloat y, |
| 499 | jboolean isRtl, jlong paintHandle, jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 500 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 501 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 502 | |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 503 | const int bidiFlags = isRtl ? minikin::kBidi_Force_RTL : minikin::kBidi_Force_LTR; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 504 | jchar* jchars = env->GetCharArrayElements(text, NULL); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 505 | get_canvas(canvasHandle)->drawText(jchars + contextIndex, index - contextIndex, count, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 506 | contextCount, x, y, bidiFlags, *paint, typeface); |
| 507 | env->ReleaseCharArrayElements(text, jchars, JNI_ABORT); |
| 508 | } |
| 509 | |
| 510 | static void drawTextRunString(JNIEnv* env, jobject obj, jlong canvasHandle, jstring text, |
| 511 | jint start, jint end, jint contextStart, jint contextEnd, |
| 512 | jfloat x, jfloat y, jboolean isRtl, jlong paintHandle, |
| 513 | jlong typefaceHandle) { |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 514 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 515 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 516 | |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 517 | int bidiFlags = isRtl ? minikin::kBidi_Force_RTL : minikin::kBidi_Force_LTR; |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 518 | jint count = end - start; |
| 519 | jint contextCount = contextEnd - contextStart; |
| 520 | const jchar* jchars = env->GetStringChars(text, NULL); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 521 | get_canvas(canvasHandle)->drawText(jchars + contextStart, start - contextStart, count, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 522 | contextCount, x, y, bidiFlags, *paint, typeface); |
| 523 | env->ReleaseStringChars(text, jchars); |
| 524 | } |
| 525 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 526 | static void drawTextOnPathChars(JNIEnv* env, jobject, jlong canvasHandle, jcharArray text, |
| 527 | jint index, jint count, jlong pathHandle, jfloat hOffset, |
| 528 | jfloat vOffset, jint bidiFlags, jlong paintHandle, |
| 529 | jlong typefaceHandle) { |
| 530 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 531 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 532 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 533 | |
| 534 | jchar* jchars = env->GetCharArrayElements(text, NULL); |
| 535 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 536 | get_canvas(canvasHandle)->drawTextOnPath(jchars + index, count, bidiFlags, *path, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 537 | hOffset, vOffset, *paint, typeface); |
| 538 | |
| 539 | env->ReleaseCharArrayElements(text, jchars, 0); |
| 540 | } |
| 541 | |
| 542 | static void drawTextOnPathString(JNIEnv* env, jobject, jlong canvasHandle, jstring text, |
| 543 | jlong pathHandle, jfloat hOffset, jfloat vOffset, |
| 544 | jint bidiFlags, jlong paintHandle, jlong typefaceHandle) { |
| 545 | SkPath* path = reinterpret_cast<SkPath*>(pathHandle); |
Behdad Esfahbod | 6ba30b8 | 2014-07-15 16:22:32 -0400 | [diff] [blame] | 546 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 547 | Typeface* typeface = reinterpret_cast<Typeface*>(typefaceHandle); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 548 | |
| 549 | const jchar* jchars = env->GetStringChars(text, NULL); |
| 550 | int count = env->GetStringLength(text); |
| 551 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 552 | get_canvas(canvasHandle)->drawTextOnPath(jchars, count, bidiFlags, *path, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 553 | hOffset, vOffset, *paint, typeface); |
| 554 | |
| 555 | env->ReleaseStringChars(text, jchars); |
| 556 | } |
| 557 | |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 558 | static void setDrawFilter(jlong canvasHandle, jlong filterHandle) { |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 559 | get_canvas(canvasHandle)->setDrawFilter(reinterpret_cast<SkDrawFilter*>(filterHandle)); |
| 560 | } |
| 561 | |
| 562 | static void freeCaches(JNIEnv* env, jobject) { |
| 563 | SkGraphics::PurgeFontCache(); |
| 564 | } |
| 565 | |
| 566 | static void freeTextLayoutCaches(JNIEnv* env, jobject) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame] | 567 | minikin::Layout::purgeCaches(); |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | }; // namespace CanvasJNI |
| 571 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 572 | static const JNINativeMethod gMethods[] = { |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 573 | {"nGetNativeFinalizer", "()J", (void*) CanvasJNI::getNativeFinalizer}, |
| 574 | {"nInitRaster", "(Landroid/graphics/Bitmap;)J", (void*) CanvasJNI::initRaster}, |
| 575 | {"nFreeCaches", "()V", (void*) CanvasJNI::freeCaches}, |
| 576 | {"nFreeTextLayoutCaches", "()V", (void*) CanvasJNI::freeTextLayoutCaches}, |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 577 | |
| 578 | // ------------ @FastNative ---------------- |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 579 | {"nSetBitmap", "(JLandroid/graphics/Bitmap;)V", (void*) CanvasJNI::setBitmap}, |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 580 | {"nGetClipBounds","(JLandroid/graphics/Rect;)Z", (void*) CanvasJNI::getClipBounds}, |
| 581 | |
| 582 | // ------------ @CriticalNative ---------------- |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 583 | {"nIsOpaque","(J)Z", (void*) CanvasJNI::isOpaque}, |
| 584 | {"nGetWidth","(J)I", (void*) CanvasJNI::getWidth}, |
| 585 | {"nGetHeight","(J)I", (void*) CanvasJNI::getHeight}, |
| 586 | {"nSetHighContrastText","(JZ)V", (void*) CanvasJNI::setHighContrastText}, |
| 587 | {"nSave","(JI)I", (void*) CanvasJNI::save}, |
| 588 | {"nSaveLayer","(JFFFFJI)I", (void*) CanvasJNI::saveLayer}, |
| 589 | {"nSaveLayerAlpha","(JFFFFII)I", (void*) CanvasJNI::saveLayerAlpha}, |
| 590 | {"nGetSaveCount","(J)I", (void*) CanvasJNI::getSaveCount}, |
Chris Craik | bfa0b29 | 2017-01-19 19:12:36 -0800 | [diff] [blame] | 591 | {"nRestore","(J)Z", (void*) CanvasJNI::restore}, |
| 592 | {"nRestoreToCount","(JI)V", (void*) CanvasJNI::restoreToCount}, |
| 593 | {"nGetMatrix", "(JJ)V", (void*)CanvasJNI::getMatrix}, |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 594 | {"nSetMatrix","(JJ)V", (void*) CanvasJNI::setMatrix}, |
| 595 | {"nConcat","(JJ)V", (void*) CanvasJNI::concat}, |
| 596 | {"nRotate","(JF)V", (void*) CanvasJNI::rotate}, |
| 597 | {"nScale","(JFF)V", (void*) CanvasJNI::scale}, |
| 598 | {"nSkew","(JFF)V", (void*) CanvasJNI::skew}, |
| 599 | {"nTranslate","(JFF)V", (void*) CanvasJNI::translate}, |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 600 | {"nQuickReject","(JJ)Z", (void*) CanvasJNI::quickRejectPath}, |
| 601 | {"nQuickReject","(JFFFF)Z", (void*)CanvasJNI::quickRejectRect}, |
| 602 | {"nClipRect","(JFFFFI)Z", (void*) CanvasJNI::clipRect}, |
| 603 | {"nClipPath","(JJI)Z", (void*) CanvasJNI::clipPath}, |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 604 | {"nSetDrawFilter", "(JJ)V", (void*) CanvasJNI::setDrawFilter}, |
| 605 | }; |
| 606 | |
| 607 | // If called from Canvas these are regular JNI |
| 608 | // If called from DisplayListCanvas they are @FastNative |
| 609 | static const JNINativeMethod gDrawMethods[] = { |
| 610 | {"nDrawColor","(JII)V", (void*) CanvasJNI::drawColor}, |
| 611 | {"nDrawPaint","(JJ)V", (void*) CanvasJNI::drawPaint}, |
| 612 | {"nDrawPoint", "(JFFJ)V", (void*) CanvasJNI::drawPoint}, |
| 613 | {"nDrawPoints", "(J[FIIJ)V", (void*) CanvasJNI::drawPoints}, |
| 614 | {"nDrawLine", "(JFFFFJ)V", (void*) CanvasJNI::drawLine}, |
| 615 | {"nDrawLines", "(J[FIIJ)V", (void*) CanvasJNI::drawLines}, |
| 616 | {"nDrawRect","(JFFFFJ)V", (void*) CanvasJNI::drawRect}, |
| 617 | {"nDrawRegion", "(JJJ)V", (void*) CanvasJNI::drawRegion }, |
| 618 | {"nDrawRoundRect","(JFFFFFFJ)V", (void*) CanvasJNI::drawRoundRect}, |
| 619 | {"nDrawCircle","(JFFFJ)V", (void*) CanvasJNI::drawCircle}, |
| 620 | {"nDrawOval","(JFFFFJ)V", (void*) CanvasJNI::drawOval}, |
| 621 | {"nDrawArc","(JFFFFFFZJ)V", (void*) CanvasJNI::drawArc}, |
| 622 | {"nDrawPath","(JJJ)V", (void*) CanvasJNI::drawPath}, |
| 623 | {"nDrawVertices", "(JII[FI[FI[II[SIIJ)V", (void*)CanvasJNI::drawVertices}, |
| 624 | {"nDrawNinePatch", "(JJJFFFFJII)V", (void*)CanvasJNI::drawNinePatch}, |
| 625 | {"nDrawBitmapMatrix", "(JLandroid/graphics/Bitmap;JJ)V", (void*)CanvasJNI::drawBitmapMatrix}, |
| 626 | {"nDrawBitmapMesh", "(JLandroid/graphics/Bitmap;II[FI[IIJ)V", (void*)CanvasJNI::drawBitmapMesh}, |
| 627 | {"nDrawBitmap","(JLandroid/graphics/Bitmap;FFJIII)V", (void*) CanvasJNI::drawBitmap}, |
| 628 | {"nDrawBitmap","(JLandroid/graphics/Bitmap;FFFFFFFFJII)V", (void*) CanvasJNI::drawBitmapRect}, |
| 629 | {"nDrawBitmap", "(J[IIIFFIIZJ)V", (void*)CanvasJNI::drawBitmapArray}, |
| 630 | {"nDrawText","(J[CIIFFIJJ)V", (void*) CanvasJNI::drawTextChars}, |
| 631 | {"nDrawText","(JLjava/lang/String;IIFFIJJ)V", (void*) CanvasJNI::drawTextString}, |
| 632 | {"nDrawTextRun","(J[CIIIIFFZJJ)V", (void*) CanvasJNI::drawTextRunChars}, |
| 633 | {"nDrawTextRun","(JLjava/lang/String;IIIIFFZJJ)V", (void*) CanvasJNI::drawTextRunString}, |
| 634 | {"nDrawTextOnPath","(J[CIIJFFIJJ)V", (void*) CanvasJNI::drawTextOnPathChars}, |
| 635 | {"nDrawTextOnPath","(JLjava/lang/String;JFFIJJ)V", (void*) CanvasJNI::drawTextOnPathString}, |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | int register_android_graphics_Canvas(JNIEnv* env) { |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 639 | int ret = 0; |
| 640 | ret |= RegisterMethodsOrDie(env, "android/graphics/Canvas", gMethods, NELEM(gMethods)); |
| 641 | ret |= RegisterMethodsOrDie(env, "android/graphics/BaseCanvas", gDrawMethods, NELEM(gDrawMethods)); |
| 642 | ret |= RegisterMethodsOrDie(env, "android/view/RecordingCanvas", gDrawMethods, NELEM(gDrawMethods)); |
| 643 | return ret; |
| 644 | |
Derek Sollenberger | 8872b38 | 2014-06-23 14:13:53 -0400 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | }; // namespace android |