| 1 | // What the client process needs of the widget engine's code, compiled into the client library (libtmessages) so the |
| 2 | // client never loads libtwidget.so: that library, QuickJS and the renderer, is mapped only into the isolated engine |
| 3 | // process (WidgetEngineService) and the debug runners' in-process engine. Two pieces, neither of which reads widget |
| 4 | // code: widget-sandbox's gesture policy (gesture_policy.cpp) and twidget's CPU filter pass (core/src/filters.cpp). |
| // Java: org.telegram.utils.twidget.WidgetClientNative, resolved by name (Java_* is exported, exports.map). |
| 6 | #include "filters.h" |
| 7 | #include "twidget/gesture_policy.h" |
| 8 |
| 9 | #include <android/bitmap.h> |
| 10 | #include <jni.h> |
| 11 |
| 12 | #include <cstdint> |
| 13 | #include <optional> |
| 14 | #include <vector> |
| 15 |
| 16 | // widget-sandbox's gesture policy (twidget/gesture_policy.h) for the chat's recognizers over a widget |
| 17 | // (WidgetGestureArbiter): one arbiter per player, on the UI thread. A verdict crosses as bits: owner (0-1), |
| 18 | // gesture (2-4), cancel (5), the bubble's action + 1 (6-8), the widget's event + 1 (9-10), edge (11); -1 for |
| 19 | // no press. |
| 20 | namespace { |
| 21 | jint verdictBits(const twidget::GestureVerdict &v) { |
| 22 | return jint(int(v.owner) | int(v.gesture) << 2 | (v.cancel ? 1 : 0) << 5 | (v.action ? int(*v.action) + 1 : 0) << 6 | |
| 23 | (v.event ? int(*v.event) + 1 : 0) << 9 | (v.edge ? 1 : 0) << 11); |
| 24 | } |
| 25 | jint verdictBits(const std::optional<twidget::GestureVerdict> &v) { return v ? verdictBits(*v) : -1; } |
| 26 | twidget::GestureArbiter *arbiterOf(jlong h) { return reinterpret_cast<twidget::GestureArbiter *>(h); } |
| 27 | } // namespace |
| 28 |
| 29 | extern "C" JNIEXPORT jlong JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterCreate(JNIEnv *, jclass) { |
| 30 | return reinterpret_cast<jlong>(new twidget::GestureArbiter()); |
| 31 | } |
| 32 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterDestroy(JNIEnv *, jclass, |
| 33 | jlong h) { |
| 34 | delete arbiterOf(h); |
| 35 | } |
| 36 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterDown( |
| 37 | JNIEnv *, jclass, jlong h, jdouble t, jdouble x, jdouble y, jboolean claimX, jboolean claimY, jboolean hold, |
| 38 | jboolean mouse, jint button) { |
| 39 | twidget::WidgetGestureClaim claim; |
| 40 | claim.x = claimX; |
| 41 | claim.y = claimY; |
| 42 | claim.hold = hold; |
| 43 | return verdictBits(arbiterOf(h)->down(t, x, y, claim, mouse ? twidget::PointerKind::Mouse : twidget::PointerKind::Touch, button)); |
| 44 | } |
| 45 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterMove(JNIEnv *, jclass, |
| 46 | jlong h, jdouble t, |
| 47 | jdouble x, jdouble y) { |
| 48 | return verdictBits(arbiterOf(h)->move(t, x, y)); |
| 49 | } |
| 50 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterTick(JNIEnv *, jclass, |
| 51 | jlong h, jdouble t) { |
| 52 | return verdictBits(arbiterOf(h)->tick(t)); |
| 53 | } |
| 54 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterUp(JNIEnv *, jclass, |
| 55 | jlong h, jdouble t, |
| 56 | jdouble x, jdouble y) { |
| 57 | return verdictBits(arbiterOf(h)->up(t, x, y)); |
| 58 | } |
| 59 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterCancel(JNIEnv *, jclass, |
| 60 | jlong h) { |
| 61 | arbiterOf(h)->cancel(); |
| 62 | } |
| 63 | extern "C" JNIEXPORT jdouble JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_arbiterHoldDeadline(JNIEnv *, |
| 64 | jclass, |
| 65 | jlong h) { |
| 66 | auto deadline = arbiterOf(h)->holdDeadline(); |
| 67 | return deadline ? *deadline : -1; |
| 68 | } |
| 69 |
| 70 | // CSS filters where RenderEffect is missing (before API 31) or the canvas is a bitmap's: a FilterPush's filters |
| 71 | // (display_list.h's Filter: kind, amount, x, y, ARGB, as parallel arrays of one length) over an ARGB_8888 bitmap's |
| 72 | // premultiplied pixels, in place. `scale`: the bitmap's px per CSS px. |
| 73 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetClientNative_filterPixels( |
| 74 | JNIEnv *e, jclass, jobject bitmap, jintArray kinds, jfloatArray amounts, jfloatArray xs, jfloatArray ys, |
| 75 | jintArray colors, jfloat scale) { |
| 76 | if (!bitmap || !kinds || !amounts || !xs || !ys || !colors) |
| 77 | return; |
| 78 | jsize n = e->GetArrayLength(kinds); |
| 79 | if (n <= 0 || e->GetArrayLength(amounts) != n || e->GetArrayLength(xs) != n || e->GetArrayLength(ys) != n || |
| 80 | e->GetArrayLength(colors) != n) |
| 81 | return; |
| 82 | std::vector<jint> kind(n), color(n); |
| 83 | std::vector<jfloat> amount(n), x(n), y(n); |
| 84 | e->GetIntArrayRegion(kinds, 0, n, kind.data()); |
| 85 | e->GetIntArrayRegion(colors, 0, n, color.data()); |
| 86 | e->GetFloatArrayRegion(amounts, 0, n, amount.data()); |
| 87 | e->GetFloatArrayRegion(xs, 0, n, x.data()); |
| 88 | e->GetFloatArrayRegion(ys, 0, n, y.data()); |
| 89 | twlite::Filters steps; |
| 90 | for (jsize k = 0; k < n; k++) { |
| 91 | if (kind[k] < 0 || kind[k] > int(twlite::Filter::DropShadow)) |
| 92 | continue; |
| 93 | twlite::Filter step; |
| 94 | step.kind = twlite::Filter::Kind(kind[k]); |
| 95 | step.amount = amount[k]; |
| 96 | step.x = x[k]; |
| 97 | step.y = y[k]; |
| 98 | auto argb = uint32_t(color[k]); |
| 99 | step.color = twlite::Color{uint8_t(argb >> 16), uint8_t(argb >> 8), uint8_t(argb), uint8_t(argb >> 24)}; |
| 100 | steps.push_back(step); |
| 101 | } |
| 102 | AndroidBitmapInfo info; |
| 103 | void *pixels = nullptr; |
| 104 | if (steps.empty() || AndroidBitmap_getInfo(e, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 105 | info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) |
| 106 | return; |
| 107 | if (AndroidBitmap_lockPixels(e, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS) |
| 108 | return; |
| 109 | twlite::filter_pixels(static_cast<uint8_t *>(pixels), int(info.width), int(info.height), info.stride, false, |
| 110 | steps, scale); |
| 111 | AndroidBitmap_unlockPixels(e, bitmap); |
| 112 | } |
| 113 |