1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
|
/*
* Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "ImageWriter_JNI"
#include "android_media_Utils.h"
#include <utils/Condition.h>
#include <utils/Log.h>
#include <utils/Mutex.h>
#include <utils/String8.h>
#include <utils/Thread.h>
#include <gui/IProducerListener.h>
#include <gui/Surface.h>
#include <ui/PublicFormat.h>
#include <android_runtime/AndroidRuntime.h>
#include <android_runtime/android_view_Surface.h>
#include <android_runtime/android_hardware_HardwareBuffer.h>
#include <private/android/AHardwareBufferHelpers.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <stdint.h>
#include <inttypes.h>
#include <android/hardware_buffer_jni.h>
#include <deque>
#define IMAGE_BUFFER_JNI_ID "mNativeBuffer"
#define IMAGE_FORMAT_UNKNOWN 0 // This is the same value as ImageFormat#UNKNOWN.
// ----------------------------------------------------------------------------
using namespace android;
static struct {
jmethodID postEventFromNative;
jfieldID mWriterFormat;
} gImageWriterClassInfo;
static struct {
jfieldID mNativeBuffer;
jfieldID mNativeFenceFd;
jfieldID mPlanes;
} gSurfaceImageClassInfo;
static struct {
jclass clazz;
jmethodID ctor;
} gSurfacePlaneClassInfo;
// ----------------------------------------------------------------------------
class JNIImageWriterContext : public BnProducerListener {
public:
JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz);
virtual ~JNIImageWriterContext();
// Implementation of IProducerListener, used to notify the ImageWriter that the consumer
// has returned a buffer and it is ready for ImageWriter to dequeue.
virtual void onBufferReleased();
void setProducer(const sp<Surface>& producer) { mProducer = producer; }
Surface* getProducer() { return mProducer.get(); }
void setBufferFormat(int format) { mFormat = format; }
int getBufferFormat() { return mFormat; }
void setBufferWidth(int width) { mWidth = width; }
int getBufferWidth() { return mWidth; }
void setBufferHeight(int height) { mHeight = height; }
int getBufferHeight() { return mHeight; }
private:
static JNIEnv* getJNIEnv(bool* needsDetach);
static void detachJNI();
sp<Surface> mProducer;
jobject mWeakThiz;
jclass mClazz;
int mFormat;
int mWidth;
int mHeight;
// Class for a shared thread used to detach buffers from buffer queues
// to discard buffers after consumers are done using them.
// This is needed because detaching buffers in onBufferReleased callback
// can lead to deadlock when consumer/producer are on the same process.
class BufferDetacher {
public:
// Called by JNIImageWriterContext ctor. Will start the thread for first ref.
void addRef();
// Called by JNIImageWriterContext dtor. Will stop the thread after ref goes to 0.
void removeRef();
// Called by onBufferReleased to signal this thread to detach a buffer
void detach(wp<Surface>);
private:
class DetachThread : public Thread {
public:
DetachThread() : Thread(/*canCallJava*/false) {};
void detach(wp<Surface>);
virtual void requestExit() override;
private:
virtual bool threadLoop() override;
Mutex mLock;
Condition mCondition;
std::deque<wp<Surface>> mQueue;
static const nsecs_t kWaitDuration = 500000000; // 500 ms
};
sp<DetachThread> mThread;
Mutex mLock;
int mRefCount = 0;
};
static BufferDetacher sBufferDetacher;
};
JNIImageWriterContext::BufferDetacher JNIImageWriterContext::sBufferDetacher;
void JNIImageWriterContext::BufferDetacher::addRef() {
Mutex::Autolock l(mLock);
mRefCount++;
if (mRefCount == 1) {
mThread = new DetachThread();
mThread->run("BufDtchThrd");
}
}
void JNIImageWriterContext::BufferDetacher::removeRef() {
Mutex::Autolock l(mLock);
mRefCount--;
if (mRefCount == 0) {
mThread->requestExit();
mThread->join();
mThread.clear();
}
}
void JNIImageWriterContext::BufferDetacher::detach(wp<Surface> bq) {
Mutex::Autolock l(mLock);
if (mThread == nullptr) {
ALOGE("%s: buffer detach thread is gone!", __FUNCTION__);
return;
}
mThread->detach(bq);
}
void JNIImageWriterContext::BufferDetacher::DetachThread::detach(wp<Surface> bq) {
Mutex::Autolock l(mLock);
mQueue.push_back(bq);
mCondition.signal();
}
void JNIImageWriterContext::BufferDetacher::DetachThread::requestExit() {
Thread::requestExit();
{
Mutex::Autolock l(mLock);
mQueue.clear();
}
mCondition.signal();
}
bool JNIImageWriterContext::BufferDetacher::DetachThread::threadLoop() {
Mutex::Autolock l(mLock);
mCondition.waitRelative(mLock, kWaitDuration);
while (!mQueue.empty()) {
if (exitPending()) {
return false;
}
wp<Surface> wbq = mQueue.front();
mQueue.pop_front();
sp<Surface> bq = wbq.promote();
if (bq != nullptr) {
sp<Fence> fence;
sp<GraphicBuffer> buffer;
ALOGV("%s: One buffer is detached", __FUNCTION__);
mLock.unlock();
bq->detachNextBuffer(&buffer, &fence);
mLock.lock();
}
}
return !exitPending();
}
JNIImageWriterContext::JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz) :
mWeakThiz(env->NewGlobalRef(weakThiz)),
mClazz((jclass)env->NewGlobalRef(clazz)),
mFormat(0),
mWidth(-1),
mHeight(-1) {
sBufferDetacher.addRef();
}
JNIImageWriterContext::~JNIImageWriterContext() {
ALOGV("%s", __FUNCTION__);
bool needsDetach = false;
JNIEnv* env = getJNIEnv(&needsDetach);
if (env != NULL) {
env->DeleteGlobalRef(mWeakThiz);
env->DeleteGlobalRef(mClazz);
} else {
ALOGW("leaking JNI object references");
}
if (needsDetach) {
detachJNI();
}
mProducer.clear();
sBufferDetacher.removeRef();
}
JNIEnv* JNIImageWriterContext::getJNIEnv(bool* needsDetach) {
ALOGV("%s", __FUNCTION__);
LOG_ALWAYS_FATAL_IF(needsDetach == NULL, "needsDetach is null!!!");
*needsDetach = false;
JNIEnv* env = AndroidRuntime::getJNIEnv();
if (env == NULL) {
JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL};
JavaVM* vm = AndroidRuntime::getJavaVM();
int result = vm->AttachCurrentThread(&env, (void*) &args);
if (result != JNI_OK) {
ALOGE("thread attach failed: %#x", result);
return NULL;
}
*needsDetach = true;
}
return env;
}
void JNIImageWriterContext::detachJNI() {
ALOGV("%s", __FUNCTION__);
JavaVM* vm = AndroidRuntime::getJavaVM();
int result = vm->DetachCurrentThread();
if (result != JNI_OK) {
ALOGE("thread detach failed: %#x", result);
}
}
void JNIImageWriterContext::onBufferReleased() {
ALOGV("%s: buffer released", __FUNCTION__);
bool needsDetach = false;
JNIEnv* env = getJNIEnv(&needsDetach);
if (env != NULL) {
// Detach the buffer every time when a buffer consumption is done,
// need let this callback give a BufferItem, then only detach if it was attached to this
// Writer. Do the detach unconditionally for opaque format now. see b/19977520
if (mFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
sBufferDetacher.detach(mProducer);
}
env->CallStaticVoidMethod(mClazz, gImageWriterClassInfo.postEventFromNative, mWeakThiz);
} else {
ALOGW("onBufferReleased event will not posted");
}
if (needsDetach) {
detachJNI();
}
}
// ----------------------------------------------------------------------------
extern "C" {
// -------------------------------Private method declarations--------------
static void Image_setNativeContext(JNIEnv* env, jobject thiz,
sp<GraphicBuffer> buffer, int fenceFd);
static void Image_getNativeContext(JNIEnv* env, jobject thiz,
GraphicBuffer** buffer, int* fenceFd);
static void Image_unlockIfLocked(JNIEnv* env, jobject thiz);
// --------------------------ImageWriter methods---------------------------------------
static void ImageWriter_classInit(JNIEnv* env, jclass clazz) {
ALOGV("%s:", __FUNCTION__);
jclass imageClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage");
LOG_ALWAYS_FATAL_IF(imageClazz == NULL,
"can't find android/media/ImageWriter$WriterSurfaceImage");
gSurfaceImageClassInfo.mNativeBuffer = env->GetFieldID(
imageClazz, IMAGE_BUFFER_JNI_ID, "J");
LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeBuffer == NULL,
"can't find android/media/ImageWriter$WriterSurfaceImage.%s", IMAGE_BUFFER_JNI_ID);
gSurfaceImageClassInfo.mNativeFenceFd = env->GetFieldID(
imageClazz, "mNativeFenceFd", "I");
LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeFenceFd == NULL,
"can't find android/media/ImageWriter$WriterSurfaceImage.mNativeFenceFd");
gSurfaceImageClassInfo.mPlanes = env->GetFieldID(
imageClazz, "mPlanes", "[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;");
LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mPlanes == NULL,
"can't find android/media/ImageWriter$WriterSurfaceImage.mPlanes");
gImageWriterClassInfo.postEventFromNative = env->GetStaticMethodID(
clazz, "postEventFromNative", "(Ljava/lang/Object;)V");
LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.postEventFromNative == NULL,
"can't find android/media/ImageWriter.postEventFromNative");
gImageWriterClassInfo.mWriterFormat = env->GetFieldID(
clazz, "mWriterFormat", "I");
LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.mWriterFormat == NULL,
"can't find android/media/ImageWriter.mWriterFormat");
jclass planeClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage$SurfacePlane");
LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class");
// FindClass only gives a local reference of jclass object.
gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz);
gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>",
"(Landroid/media/ImageWriter$WriterSurfaceImage;IILjava/nio/ByteBuffer;)V");
LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL,
"Can not find SurfacePlane constructor");
}
static jlong ImageWriter_init(JNIEnv* env, jobject thiz, jobject weakThiz, jobject jsurface,
jint maxImages, jint userFormat) {
status_t res;
ALOGV("%s: maxImages:%d", __FUNCTION__, maxImages);
sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
if (surface == NULL) {
jniThrowException(env,
"java/lang/IllegalArgumentException",
"The surface has been released");
return 0;
}
sp<IGraphicBufferProducer> bufferProducer = surface->getIGraphicBufferProducer();
jclass clazz = env->GetObjectClass(thiz);
if (clazz == NULL) {
jniThrowRuntimeException(env, "Can't find android/graphics/ImageWriter");
return 0;
}
sp<JNIImageWriterContext> ctx(new JNIImageWriterContext(env, weakThiz, clazz));
sp<Surface> producer = new Surface(bufferProducer, /*controlledByApp*/false);
ctx->setProducer(producer);
/**
* NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not connectable
* after disconnect. MEDIA or CAMERA are treated the same internally. The producer listener
* will be cleared after disconnect call.
*/
res = producer->connect(/*api*/NATIVE_WINDOW_API_CAMERA, /*listener*/ctx);
if (res != OK) {
ALOGE("%s: Connecting to surface producer interface failed: %s (%d)",
__FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Failed to connect to native window");
return 0;
}
jlong nativeCtx = reinterpret_cast<jlong>(ctx.get());
// Get the dimension and format of the producer.
sp<ANativeWindow> anw = producer;
int32_t width, height, surfaceFormat;
if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
ALOGE("%s: Query Surface width failed: %s (%d)", __FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Failed to query Surface width");
return 0;
}
ctx->setBufferWidth(width);
if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) {
ALOGE("%s: Query Surface height failed: %s (%d)", __FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Failed to query Surface height");
return 0;
}
ctx->setBufferHeight(height);
// Query surface format if no valid user format is specified, otherwise, override surface format
// with user format.
if (userFormat == IMAGE_FORMAT_UNKNOWN) {
if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &surfaceFormat)) != OK) {
ALOGE("%s: Query Surface format failed: %s (%d)", __FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Failed to query Surface format");
return 0;
}
} else {
// Set consumer buffer format to user specified format
PublicFormat publicFormat = static_cast<PublicFormat>(userFormat);
int nativeFormat = mapPublicFormatToHalFormat(publicFormat);
android_dataspace nativeDataspace = mapPublicFormatToHalDataspace(publicFormat);
res = native_window_set_buffers_format(anw.get(), nativeFormat);
if (res != OK) {
ALOGE("%s: Unable to configure consumer native buffer format to %#x",
__FUNCTION__, nativeFormat);
jniThrowRuntimeException(env, "Failed to set Surface format");
return 0;
}
res = native_window_set_buffers_data_space(anw.get(), nativeDataspace);
if (res != OK) {
ALOGE("%s: Unable to configure consumer dataspace %#x",
__FUNCTION__, nativeDataspace);
jniThrowRuntimeException(env, "Failed to set Surface dataspace");
return 0;
}
surfaceFormat = userFormat;
}
ctx->setBufferFormat(surfaceFormat);
env->SetIntField(thiz,
gImageWriterClassInfo.mWriterFormat, reinterpret_cast<jint>(surfaceFormat));
if (!isFormatOpaque(surfaceFormat)) {
res = native_window_set_usage(anw.get(), GRALLOC_USAGE_SW_WRITE_OFTEN);
if (res != OK) {
ALOGE("%s: Configure usage %08x for format %08x failed: %s (%d)",
__FUNCTION__, static_cast<unsigned int>(GRALLOC_USAGE_SW_WRITE_OFTEN),
surfaceFormat, strerror(-res), res);
jniThrowRuntimeException(env, "Failed to SW_WRITE_OFTEN configure usage");
return 0;
}
}
int minUndequeuedBufferCount = 0;
res = anw->query(anw.get(),
NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufferCount);
if (res != OK) {
ALOGE("%s: Query producer undequeued buffer count failed: %s (%d)",
__FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Query producer undequeued buffer count failed");
return 0;
}
size_t totalBufferCount = maxImages + minUndequeuedBufferCount;
res = native_window_set_buffer_count(anw.get(), totalBufferCount);
if (res != OK) {
ALOGE("%s: Set buffer count failed: %s (%d)", __FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Set buffer count failed");
return 0;
}
if (ctx != 0) {
ctx->incStrong((void*)ImageWriter_init);
}
return nativeCtx;
}
static void ImageWriter_dequeueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) {
ALOGV("%s", __FUNCTION__);
JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
if (ctx == NULL || thiz == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"ImageWriterContext is not initialized");
return;
}
sp<ANativeWindow> anw = ctx->getProducer();
android_native_buffer_t *anb = NULL;
int fenceFd = -1;
status_t res = anw->dequeueBuffer(anw.get(), &anb, &fenceFd);
if (res != OK) {
ALOGE("%s: Dequeue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
switch (res) {
case NO_INIT:
jniThrowException(env, "java/lang/IllegalStateException",
"Surface has been abandoned");
break;
default:
// TODO: handle other error cases here.
jniThrowRuntimeException(env, "dequeue buffer failed");
}
return;
}
// New GraphicBuffer object doesn't own the handle, thus the native buffer
// won't be freed when this object is destroyed.
sp<GraphicBuffer> buffer(GraphicBuffer::from(anb));
// Note that:
// 1. No need to lock buffer now, will only lock it when the first getPlanes() is called.
// 2. Fence will be saved to mNativeFenceFd, and will consumed by lock/queue/cancel buffer
// later.
// 3. need use lockAsync here, as it will handle the dequeued fence for us automatically.
// Finally, set the native info into image object.
Image_setNativeContext(env, image, buffer, fenceFd);
}
static void ImageWriter_close(JNIEnv* env, jobject thiz, jlong nativeCtx) {
ALOGV("%s:", __FUNCTION__);
JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
if (ctx == NULL || thiz == NULL) {
// ImageWriter is already closed.
return;
}
ANativeWindow* producer = ctx->getProducer();
if (producer != NULL) {
/**
* NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not
* connectable after disconnect. MEDIA or CAMERA are treated the same internally.
* The producer listener will be cleared after disconnect call.
*/
status_t res = native_window_api_disconnect(producer, /*api*/NATIVE_WINDOW_API_CAMERA);
/**
* This is not an error. if client calling process dies, the window will
* also die and all calls to it will return DEAD_OBJECT, thus it's already
* "disconnected"
*/
if (res == DEAD_OBJECT) {
ALOGW("%s: While disconnecting ImageWriter from native window, the"
" native window died already", __FUNCTION__);
} else if (res != OK) {
ALOGE("%s: native window disconnect failed: %s (%d)",
__FUNCTION__, strerror(-res), res);
jniThrowRuntimeException(env, "Native window disconnect failed");
return;
}
}
ctx->decStrong((void*)ImageWriter_init);
}
static void ImageWriter_cancelImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) {
ALOGV("%s", __FUNCTION__);
JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
if (ctx == NULL || thiz == NULL) {
ALOGW("ImageWriter#close called before Image#close, consider calling Image#close first");
return;
}
sp<ANativeWindow> anw = ctx->getProducer();
GraphicBuffer *buffer = NULL;
int fenceFd = -1;
Image_getNativeContext(env, image, &buffer, &fenceFd);
if (buffer == NULL) {
// Cancel an already cancelled image is harmless.
return;
}
// Unlock the image if it was locked
Image_unlockIfLocked(env, image);
anw->cancelBuffer(anw.get(), buffer, fenceFd);
Image_setNativeContext(env, image, NULL, -1);
}
static void ImageWriter_queueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image,
jlong timestampNs, jint left, jint top, jint right, jint bottom, jint transform,
jint scalingMode) {
ALOGV("%s", __FUNCTION__);
JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
if (ctx == NULL || thiz == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"ImageWriterContext is not initialized");
return;
}
status_t res = OK;
sp<ANativeWindow> anw = ctx->getProducer();
GraphicBuffer *buffer = NULL;
int fenceFd = -1;
Image_getNativeContext(env, image, &buffer, &fenceFd);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return;
}
// Unlock image if it was locked.
Image_unlockIfLocked(env, image);
// Set timestamp
ALOGV("timestamp to be queued: %" PRId64, timestampNs);
res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
if (res != OK) {
jniThrowRuntimeException(env, "Set timestamp failed");
return;
}
// Set crop
android_native_rect_t cropRect;
cropRect.left = left;
cropRect.top = top;
cropRect.right = right;
cropRect.bottom = bottom;
res = native_window_set_crop(anw.get(), &cropRect);
if (res != OK) {
jniThrowRuntimeException(env, "Set crop rect failed");
return;
}
res = native_window_set_buffers_transform(anw.get(), transform);
if (res != OK) {
jniThrowRuntimeException(env, "Set transform failed");
return;
}
res = native_window_set_scaling_mode(anw.get(), scalingMode);
if (res != OK) {
jniThrowRuntimeException(env, "Set scaling mode failed");
return;
}
// Finally, queue input buffer
res = anw->queueBuffer(anw.get(), buffer, fenceFd);
if (res != OK) {
ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
switch (res) {
case NO_INIT:
jniThrowException(env, "java/lang/IllegalStateException",
"Surface has been abandoned");
break;
default:
// TODO: handle other error cases here.
jniThrowRuntimeException(env, "Queue input buffer failed");
}
return;
}
// Clear the image native context: end of this image's lifecycle in public API.
Image_setNativeContext(env, image, NULL, -1);
}
static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nativeCtx,
jlong nativeBuffer, jint imageFormat, jlong timestampNs, jint left, jint top,
jint right, jint bottom, jint transform, jint scalingMode) {
ALOGV("%s", __FUNCTION__);
JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
if (ctx == NULL || thiz == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"ImageWriterContext is not initialized");
return -1;
}
sp<Surface> surface = ctx->getProducer();
status_t res = OK;
if (isFormatOpaque(imageFormat) != isFormatOpaque(ctx->getBufferFormat())) {
jniThrowException(env, "java/lang/IllegalStateException",
"Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa");
return -1;
}
// Image is guaranteed to be from ImageReader at this point, so it is safe to
// cast to BufferItem pointer.
BufferItem* buffer = reinterpret_cast<BufferItem*>(nativeBuffer);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized or already closed");
return -1;
}
// Step 1. Attach Image
res = surface->attachBuffer(buffer->mGraphicBuffer.get());
if (res != OK) {
ALOGE("Attach image failed: %s (%d)", strerror(-res), res);
switch (res) {
case NO_INIT:
jniThrowException(env, "java/lang/IllegalStateException",
"Surface has been abandoned");
break;
default:
// TODO: handle other error cases here.
jniThrowRuntimeException(env, "nativeAttachImage failed!!!");
}
return res;
}
sp < ANativeWindow > anw = surface;
// Step 2. Set timestamp, crop, transform and scaling mode. Note that we do not need unlock the
// image because it was not locked.
ALOGV("timestamp to be queued: %" PRId64, timestampNs);
res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
if (res != OK) {
jniThrowRuntimeException(env, "Set timestamp failed");
return res;
}
android_native_rect_t cropRect;
cropRect.left = left;
cropRect.top = top;
cropRect.right = right;
cropRect.bottom = bottom;
res = native_window_set_crop(anw.get(), &cropRect);
if (res != OK) {
jniThrowRuntimeException(env, "Set crop rect failed");
return res;
}
res = native_window_set_buffers_transform(anw.get(), transform);
if (res != OK) {
jniThrowRuntimeException(env, "Set transform failed");
return res;
}
res = native_window_set_scaling_mode(anw.get(), scalingMode);
if (res != OK) {
jniThrowRuntimeException(env, "Set scaling mode failed");
return res;
}
// Step 3. Queue Image.
res = anw->queueBuffer(anw.get(), buffer->mGraphicBuffer.get(), /*fenceFd*/
-1);
if (res != OK) {
ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
switch (res) {
case NO_INIT:
jniThrowException(env, "java/lang/IllegalStateException",
"Surface has been abandoned");
break;
default:
// TODO: handle other error cases here.
jniThrowRuntimeException(env, "Queue input buffer failed");
}
return res;
}
// Do not set the image native context. Since it would overwrite the existing native context
// of the image that is from ImageReader, the subsequent image close will run into issues.
return res;
}
// --------------------------Image methods---------------------------------------
static void Image_getNativeContext(JNIEnv* env, jobject thiz,
GraphicBuffer** buffer, int* fenceFd) {
ALOGV("%s", __FUNCTION__);
if (buffer != NULL) {
GraphicBuffer *gb = reinterpret_cast<GraphicBuffer *>
(env->GetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer));
*buffer = gb;
}
if (fenceFd != NULL) {
*fenceFd = reinterpret_cast<jint>(env->GetIntField(
thiz, gSurfaceImageClassInfo.mNativeFenceFd));
}
}
static void Image_setNativeContext(JNIEnv* env, jobject thiz,
sp<GraphicBuffer> buffer, int fenceFd) {
ALOGV("%s:", __FUNCTION__);
GraphicBuffer* p = NULL;
Image_getNativeContext(env, thiz, &p, /*fenceFd*/NULL);
if (buffer != 0) {
buffer->incStrong((void*)Image_setNativeContext);
}
if (p) {
p->decStrong((void*)Image_setNativeContext);
}
env->SetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer,
reinterpret_cast<jlong>(buffer.get()));
env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
}
static void Image_unlockIfLocked(JNIEnv* env, jobject thiz) {
ALOGV("%s", __FUNCTION__);
GraphicBuffer* buffer;
Image_getNativeContext(env, thiz, &buffer, NULL);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return;
}
// Is locked?
bool isLocked = false;
jobject planes = NULL;
if (!isFormatOpaque(buffer->getPixelFormat())) {
planes = env->GetObjectField(thiz, gSurfaceImageClassInfo.mPlanes);
}
isLocked = (planes != NULL);
if (isLocked) {
// no need to use fence here, as we it will be consumed by either cancel or queue buffer.
status_t res = buffer->unlock();
if (res != OK) {
jniThrowRuntimeException(env, "unlock buffer failed");
}
ALOGV("Successfully unlocked the image");
}
}
static jint Image_getWidth(JNIEnv* env, jobject thiz) {
ALOGV("%s", __FUNCTION__);
GraphicBuffer* buffer;
Image_getNativeContext(env, thiz, &buffer, NULL);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return -1;
}
return buffer->getWidth();
}
static jint Image_getHeight(JNIEnv* env, jobject thiz) {
ALOGV("%s", __FUNCTION__);
GraphicBuffer* buffer;
Image_getNativeContext(env, thiz, &buffer, NULL);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return -1;
}
return buffer->getHeight();
}
static jint Image_getFormat(JNIEnv* env, jobject thiz) {
ALOGV("%s", __FUNCTION__);
GraphicBuffer* buffer;
Image_getNativeContext(env, thiz, &buffer, NULL);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return 0;
}
// ImageWriter doesn't support data space yet, assuming it is unknown.
PublicFormat publicFmt = android_view_Surface_mapHalFormatDataspaceToPublicFormat(
buffer->getPixelFormat(), HAL_DATASPACE_UNKNOWN);
return static_cast<jint>(publicFmt);
}
static jobject Image_getHardwareBuffer(JNIEnv* env, jobject thiz) {
GraphicBuffer* buffer;
Image_getNativeContext(env, thiz, &buffer, NULL);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return NULL;
}
AHardwareBuffer* b = AHardwareBuffer_from_GraphicBuffer(buffer);
// don't user the public AHardwareBuffer_toHardwareBuffer() because this would force us
// to link against libandroid.so
return android_hardware_HardwareBuffer_createFromAHardwareBuffer(env, b);
}
static void Image_setFenceFd(JNIEnv* env, jobject thiz, int fenceFd) {
ALOGV("%s:", __FUNCTION__);
env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
}
static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image) {
ALOGV("%s", __FUNCTION__);
GraphicBuffer* buffer;
int fenceFd = -1;
Image_getNativeContext(env, thiz, &buffer, &fenceFd);
if (buffer == NULL) {
jniThrowException(env, "java/lang/IllegalStateException",
"Image is not initialized");
return;
}
// ImageWriter doesn't use crop by itself, app sets it, use the no crop version.
const Rect noCrop(buffer->width, buffer->height);
status_t res = lockImageFromBuffer(
buffer, GRALLOC_USAGE_SW_WRITE_OFTEN, noCrop, fenceFd, image);
// Clear the fenceFd as it is already consumed by lock call.
Image_setFenceFd(env, thiz, /*fenceFd*/-1);
if (res != OK) {
jniThrowExceptionFmt(env, "java/lang/RuntimeException",
"lock buffer failed for format 0x%x",
buffer->getPixelFormat());
return;
}
ALOGV("%s: Successfully locked the image", __FUNCTION__);
// crop, transform, scalingMode, timestamp, and frameNumber should be set by producer,
// and we don't set them here.
}
static void Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx,
int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
ALOGV("%s", __FUNCTION__);
status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size,
pixelStride, rowStride);
if (res != OK) {
jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
"Pixel format: 0x%x is unsupported", buffer->flexFormat);
}
}
static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
int numPlanes, int writerFormat) {
ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
int rowStride, pixelStride;
uint8_t *pData;
uint32_t dataSize;
jobject byteBuffer;
int format = Image_getFormat(env, thiz);
if (isFormatOpaque(format) && numPlanes > 0) {
String8 msg;
msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
" must be 0", format, numPlanes);
jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
return NULL;
}
jobjectArray surfacePlanes = env->NewObjectArray(numPlanes, gSurfacePlaneClassInfo.clazz,
/*initial_element*/NULL);
if (surfacePlanes == NULL) {
jniThrowRuntimeException(env, "Failed to create SurfacePlane arrays,"
" probably out of memory");
return NULL;
}
if (isFormatOpaque(format)) {
return surfacePlanes;
}
// Buildup buffer info: rowStride, pixelStride and byteBuffers.
LockedImage lockedImg = LockedImage();
Image_getLockedImage(env, thiz, &lockedImg);
// Create all SurfacePlanes
PublicFormat publicWriterFormat = static_cast<PublicFormat>(writerFormat);
writerFormat = android_view_Surface_mapPublicFormatToHalFormat(publicWriterFormat);
for (int i = 0; i < numPlanes; i++) {
Image_getLockedImageInfo(env, &lockedImg, i, writerFormat,
&pData, &dataSize, &pixelStride, &rowStride);
byteBuffer = env->NewDirectByteBuffer(pData, dataSize);
if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
jniThrowException(env, "java/lang/IllegalStateException",
"Failed to allocate ByteBuffer");
return NULL;
}
// Finally, create this SurfacePlane.
jobject surfacePlane = env->NewObject(gSurfacePlaneClassInfo.clazz,
gSurfacePlaneClassInfo.ctor, thiz, rowStride, pixelStride, byteBuffer);
env->SetObjectArrayElement(surfacePlanes, i, surfacePlane);
}
return surfacePlanes;
}
} // extern "C"
// ----------------------------------------------------------------------------
static JNINativeMethod gImageWriterMethods[] = {
{"nativeClassInit", "()V", (void*)ImageWriter_classInit },
{"nativeInit", "(Ljava/lang/Object;Landroid/view/Surface;II)J",
(void*)ImageWriter_init },
{"nativeClose", "(J)V", (void*)ImageWriter_close },
{"nativeAttachAndQueueImage", "(JJIJIIIIII)I", (void*)ImageWriter_attachAndQueueImage },
{"nativeDequeueInputImage", "(JLandroid/media/Image;)V", (void*)ImageWriter_dequeueImage },
{"nativeQueueInputImage", "(JLandroid/media/Image;JIIIIII)V", (void*)ImageWriter_queueImage },
{"cancelImage", "(JLandroid/media/Image;)V", (void*)ImageWriter_cancelImage },
};
static JNINativeMethod gImageMethods[] = {
{"nativeCreatePlanes", "(II)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;",
(void*)Image_createSurfacePlanes },
{"nativeGetWidth", "()I", (void*)Image_getWidth },
{"nativeGetHeight", "()I", (void*)Image_getHeight },
{"nativeGetFormat", "()I", (void*)Image_getFormat },
{"nativeGetHardwareBuffer", "()Landroid/hardware/HardwareBuffer;",
(void*)Image_getHardwareBuffer },
};
int register_android_media_ImageWriter(JNIEnv *env) {
int ret1 = AndroidRuntime::registerNativeMethods(env,
"android/media/ImageWriter", gImageWriterMethods, NELEM(gImageWriterMethods));
int ret2 = AndroidRuntime::registerNativeMethods(env,
"android/media/ImageWriter$WriterSurfaceImage", gImageMethods, NELEM(gImageMethods));
return (ret1 || ret2);
}
|