|
| 1 | +#include <cstdlib> |
| 2 | +#include <cstdio> |
| 3 | +#include <jni.h> |
| 4 | +#include <android/log.h> |
| 5 | +#include <android/bitmap.h> |
| 6 | +#include <android/hardware_buffer.h> |
| 7 | +#include <android/hardware_buffer_jni.h> |
| 8 | +#include <cstring> |
| 9 | +#include <lz4.h> |
| 10 | + |
| 11 | +#define ASSERT(cond, ...) if (!(cond)) \ |
| 12 | + __android_log_assert(#cond, "DroidCast_raw_log", __VA_ARGS__) |
| 13 | +#define CHECKED(ret, cond, ...) if (!(cond)) do { \ |
| 14 | + __android_log_print(ANDROID_LOG_ERROR, "DroidCast_raw_log", __VA_ARGS__); \ |
| 15 | + return ret; \ |
| 16 | +} while (0) |
| 17 | + |
| 18 | +extern "C" |
| 19 | +JNIEXPORT jint |
| 20 | + |
| 21 | +JNICALL |
| 22 | +Java_ink_mol_droidcast_1raw_ScreenCaptorUtils_copyHardwareBufferToByteBufferNative(JNIEnv *env, |
| 23 | + jobject thiz, |
| 24 | + jobject hardware_buffer, |
| 25 | + jobject byte_buffer, |
| 26 | + jint position, |
| 27 | + jint limit) { |
| 28 | + AHardwareBuffer *hardwareBuffer = AHardwareBuffer_fromHardwareBuffer(env, hardware_buffer); |
| 29 | + CHECKED(-1, hardwareBuffer, "invalid hardware buffer"); |
| 30 | + AHardwareBuffer_Desc desc = {0}; |
| 31 | + AHardwareBuffer_describe(hardwareBuffer, &desc); |
| 32 | + CHECKED(-1, desc.format == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM, |
| 33 | + "native copy does not support format %" PRIu32, desc.format); |
| 34 | + CHECKED(-1, desc.width == desc.stride, |
| 35 | + "native copy width != stride, width = %" PRIu32 ", stride = %" PRIu32, desc.width, |
| 36 | + desc.stride); |
| 37 | + size_t size = desc.width * desc.height * 2; |
| 38 | + CHECKED(-1, limit - position >= size, "buffer size %d is smaller than image size %zu", |
| 39 | + limit - position, size); |
| 40 | + void *bufferPtr = env->GetDirectBufferAddress(byte_buffer); |
| 41 | + ASSERT(bufferPtr, "failed to get direct byte buffer ptr"); |
| 42 | + void *srcPtr; |
| 43 | + int lockResult = AHardwareBuffer_lock(hardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, |
| 44 | + nullptr, &srcPtr); |
| 45 | + ASSERT(lockResult == 0, "failed to lock hw buffer"); |
| 46 | + std::memcpy(bufferPtr, srcPtr, size); |
| 47 | + lockResult = AHardwareBuffer_unlock(hardwareBuffer, nullptr); |
| 48 | + ASSERT(lockResult == 0, "failed to unlock hw buffer"); |
| 49 | + return (int) size; |
| 50 | +} |
| 51 | + |
| 52 | +extern "C" |
| 53 | +JNIEXPORT jint |
| 54 | + |
| 55 | +JNICALL |
| 56 | +Java_ink_mol_droidcast_1raw_ScreenCaptorUtils_copyHardwareBufferToByteBufferNativeLz4(JNIEnv *env, |
| 57 | + jobject thiz, |
| 58 | + jobject hardware_buffer, |
| 59 | + jobject byte_buffer, |
| 60 | + jint position, |
| 61 | + jint limit) { |
| 62 | + AHardwareBuffer *hardwareBuffer = AHardwareBuffer_fromHardwareBuffer(env, hardware_buffer); |
| 63 | + CHECKED(-1, hardwareBuffer, "invalid hardware buffer"); |
| 64 | + AHardwareBuffer_Desc desc; |
| 65 | + AHardwareBuffer_describe(hardwareBuffer, &desc); |
| 66 | + CHECKED(-1, desc.format == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM, |
| 67 | + "native copy does not support format %" PRIu32, desc.format); |
| 68 | + CHECKED(-1, desc.width == desc.stride, |
| 69 | + "native copy width != stride, width = %" PRIu32 ", stride = %" PRIu32, desc.width, |
| 70 | + desc.stride); |
| 71 | + size_t size = desc.width * desc.height * 2; |
| 72 | + void *bufferPtr = env->GetDirectBufferAddress(byte_buffer); |
| 73 | + ASSERT(bufferPtr, "failed to get direct byte buffer ptr"); |
| 74 | + void *srcPtr; |
| 75 | + int lockResult = AHardwareBuffer_lock(hardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_RARELY, -1, |
| 76 | + nullptr, &srcPtr); |
| 77 | + ASSERT(lockResult == 0, "failed to lock hw buffer"); |
| 78 | + int dstSize = LZ4_compress_default((const char *) srcPtr, (char *) bufferPtr, (int) size, |
| 79 | + limit - position); |
| 80 | + lockResult = AHardwareBuffer_unlock(hardwareBuffer, nullptr); |
| 81 | + ASSERT(lockResult == 0, "failed to unlock hw buffer"); |
| 82 | + return (int) dstSize; |
| 83 | +} |
| 84 | + |
| 85 | +extern "C" |
| 86 | +JNIEXPORT jint |
| 87 | + |
| 88 | +JNICALL |
| 89 | +Java_ink_mol_droidcast_1raw_ScreenCaptorUtils_copyBitmapToByteBufferNativeLz4(JNIEnv *env, |
| 90 | + jobject thiz, |
| 91 | + jobject bitmap, |
| 92 | + jobject byte_buffer, |
| 93 | + jint position, |
| 94 | + jint limit) { |
| 95 | + AndroidBitmapInfo info = {0}; |
| 96 | + AndroidBitmap_getInfo(env, bitmap, &info); |
| 97 | + CHECKED(-1, info.format == ANDROID_BITMAP_FORMAT_RGB_565, "invalid RGB565 bitmap"); |
| 98 | + int size = int(info.stride * info.height); |
| 99 | + void *bufferPtr = env->GetDirectBufferAddress(byte_buffer); |
| 100 | + ASSERT(bufferPtr, "failed to get direct byte buffer ptr"); |
| 101 | + void *srcPtr = nullptr; |
| 102 | + AndroidBitmap_lockPixels(env, bitmap, &srcPtr); |
| 103 | + ASSERT(srcPtr, "failed to lock bitmap"); |
| 104 | + int dstSize = LZ4_compress_default((const char *) srcPtr, (char *) bufferPtr, (int) size, |
| 105 | + limit - position); |
| 106 | + AndroidBitmap_unlockPixels(env, bitmap); |
| 107 | + return (int) dstSize; |
| 108 | +} |
| 109 | + |
| 110 | +extern "C" |
| 111 | +JNIEXPORT jint |
| 112 | + |
| 113 | +JNICALL |
| 114 | +Java_ink_mol_droidcast_1raw_ScreenCaptorUtils_lz4CompressBound(JNIEnv *env, jobject thiz, |
| 115 | + jint data_size) { |
| 116 | + return LZ4_compressBound(data_size); |
| 117 | +} |
0 commit comments