androiddrawdevice.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. /* AndroidDrawDevice interface */
  23. static jlong
  24. newNativeAndroidDrawDevice(JNIEnv *env, jobject self, fz_context *ctx, jobject obj, jint width, jint height, NativeDeviceLockFn *lock, NativeDeviceUnlockFn *unlock, jint xOrigin, jint yOrigin, jint patchX0, jint patchY0, jint patchX1, jint patchY1, jboolean clear)
  25. {
  26. fz_device *device = NULL;
  27. fz_pixmap *pixmap = NULL;
  28. unsigned char dummy;
  29. NativeDeviceInfo *ninfo = NULL;
  30. NativeDeviceInfo *info;
  31. fz_irect bbox;
  32. int err = 0;
  33. if (!ctx) return 0;
  34. /* Ensure patch fits inside bitmap. */
  35. if (patchX0 < 0) patchX0 = 0;
  36. if (patchY0 < 0) patchY0 = 0;
  37. if (patchX1 > width) patchX1 = width;
  38. if (patchY1 > height) patchY1 = height;
  39. bbox.x0 = xOrigin + patchX0;
  40. bbox.y0 = yOrigin + patchY0;
  41. bbox.x1 = xOrigin + patchX1;
  42. bbox.y1 = yOrigin + patchY1;
  43. fz_var(pixmap);
  44. fz_var(ninfo);
  45. fz_try(ctx)
  46. {
  47. pixmap = fz_new_pixmap_with_bbox_and_data(ctx, fz_device_rgb(ctx), bbox, NULL, 1, &dummy);
  48. pixmap->stride = width * sizeof(int32_t);
  49. ninfo = Memento_label(fz_malloc(ctx, sizeof(*ninfo)), "newNativeAndroidDrawDevice");
  50. ninfo->pixmap = pixmap;
  51. ninfo->lock = lock;
  52. ninfo->unlock = unlock;
  53. ninfo->xOffset = patchX0;
  54. ninfo->yOffset = patchY0;
  55. ninfo->width = width;
  56. ninfo->height = height;
  57. ninfo->object = obj;
  58. (*env)->SetLongField(env, self, fid_NativeDevice_nativeInfo, jlong_cast(ninfo));
  59. (*env)->SetObjectField(env, self, fid_NativeDevice_nativeResource, obj);
  60. if (clear)
  61. {
  62. info = lockNativeDevice(env,self,&err);
  63. if (!err)
  64. {
  65. fz_clear_pixmap_with_value(ctx, pixmap, 0xff);
  66. unlockNativeDevice(env,ninfo);
  67. }
  68. }
  69. if (!err)
  70. device = fz_new_draw_device(ctx, fz_identity, pixmap);
  71. }
  72. fz_catch(ctx)
  73. {
  74. (*env)->SetLongField(env, self, fid_NativeDevice_nativeInfo, 0);
  75. (*env)->SetObjectField(env, self, fid_NativeDevice_nativeResource, NULL);
  76. fz_drop_pixmap(ctx, pixmap);
  77. fz_free(ctx, ninfo);
  78. jni_rethrow(env, ctx);
  79. }
  80. /* lockNativeDevice will already have raised a JNI error if there was one. */
  81. if (err)
  82. {
  83. jthrowable t = (*env)->ExceptionOccurred(env);
  84. (*env)->ExceptionClear(env);
  85. (*env)->SetLongField(env, self, fid_NativeDevice_nativeInfo, 0);
  86. (*env)->SetObjectField(env, self, fid_NativeDevice_nativeResource, NULL);
  87. fz_drop_pixmap(ctx, pixmap);
  88. fz_free(ctx, ninfo);
  89. if ((*env)->Throw(env, t) < 0)
  90. (*env)->ThrowNew(env, cls_RuntimeException, "could not rethrow exception after cleanup when locking failed");
  91. return 0;
  92. }
  93. return jlong_cast(device);
  94. }
  95. static int androidDrawDevice_lock(JNIEnv *env, NativeDeviceInfo *info)
  96. {
  97. uint8_t *pixels;
  98. int ret;
  99. int phase = 0;
  100. fz_context *ctx = get_context(env);
  101. size_t size = info->width * info->height * 4;
  102. if (!ctx)
  103. {
  104. jni_throw_run_imp(env, "no context in DrawDevice call");
  105. return 1;
  106. }
  107. assert(info);
  108. assert(info->object);
  109. while (1)
  110. {
  111. ret = AndroidBitmap_lockPixels(env, info->object, (void **)&pixels);
  112. if (ret == ANDROID_BITMAP_RESULT_SUCCESS)
  113. break;
  114. if (!fz_store_scavenge_external(ctx, size, &phase))
  115. break; /* Failed to free any */
  116. }
  117. if (ret != ANDROID_BITMAP_RESULT_SUCCESS)
  118. {
  119. info->pixmap->samples = NULL;
  120. jni_throw_run_imp(env, "bitmap lock failed in DrawDevice call");
  121. return 1;
  122. }
  123. /* Now offset pixels to allow for the page offsets */
  124. pixels += sizeof(int32_t) * (info->xOffset + info->width * info->yOffset);
  125. info->pixmap->samples = pixels;
  126. return 0;
  127. }
  128. static void androidDrawDevice_unlock(JNIEnv *env, NativeDeviceInfo *info)
  129. {
  130. assert(info);
  131. assert(info->object);
  132. info->pixmap->samples = NULL;
  133. if (AndroidBitmap_unlockPixels(env, info->object) != ANDROID_BITMAP_RESULT_SUCCESS)
  134. jni_throw_run_void(env, "bitmap unlock failed in DrawDevice call");
  135. }
  136. JNIEXPORT void JNICALL
  137. FUN(android_AndroidDrawDevice_invertLuminance)(JNIEnv *env, jobject self)
  138. {
  139. fz_context *ctx = get_context(env);
  140. fz_device *dev = from_Device(env, self);
  141. NativeDeviceInfo *info;
  142. int err;
  143. if (!ctx || !dev) return;
  144. info = lockNativeDevice(env, self, &err);
  145. if (err)
  146. return;
  147. fz_try(ctx)
  148. fz_invert_pixmap_luminance(ctx, info->pixmap);
  149. fz_always(ctx)
  150. unlockNativeDevice(env, info);
  151. fz_catch(ctx)
  152. jni_rethrow_void(env, ctx);
  153. }
  154. JNIEXPORT jlong JNICALL
  155. FUN(android_AndroidDrawDevice_newNative)(JNIEnv *env, jclass self, jobject jbitmap, jint xOrigin, jint yOrigin, jint pX0, jint pY0, jint pX1, jint pY1, jboolean clear)
  156. {
  157. fz_context *ctx = get_context(env);
  158. AndroidBitmapInfo info;
  159. jlong device = 0;
  160. int ret;
  161. if (!ctx) return 0;
  162. if (!jbitmap) jni_throw_arg(env, "bitmap must not be null");
  163. if ((ret = AndroidBitmap_getInfo(env, jbitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS)
  164. jni_throw_run(env, "new DrawDevice failed to get bitmap info");
  165. if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
  166. jni_throw_run(env, "new DrawDevice failed as bitmap format is not RGBA_8888");
  167. if (info.stride != info.width * 4)
  168. jni_throw_run(env, "new DrawDevice failed as bitmap width != stride");
  169. fz_try(ctx)
  170. device = newNativeAndroidDrawDevice(env, self, ctx, jbitmap, info.width, info.height, androidDrawDevice_lock, androidDrawDevice_unlock, xOrigin, yOrigin, pX0, pY0, pX1, pY1, clear);
  171. fz_catch(ctx)
  172. jni_rethrow(env, ctx);
  173. return device;
  174. }