1 /* 2 * Copyright 2017 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 18 #define LOG_TAG "BitmapTest" 19 20 #include <jni.h> 21 #include <android/bitmap.h> 22 #include <cstdlib> 23 #include <cstring> 24 25 // Copied from tests/sensor/jni/nativeTestHelper.h 26 // TODO: Move to a shared location 27 #define ASSERT(condition, format, args...) \ 28 if (!(condition)) { \ 29 fail(env, format, ## args); \ 30 return; \ 31 } 32 33 #define ASSERT_TRUE(a) ASSERT((a), "assert failed on (" #a ") at " __FILE__ ":%d", __LINE__) 34 #define ASSERT_FALSE(a) ASSERT(!(a), "assert failed on (!" #a ") at " __FILE__ ":%d", __LINE__) 35 #define ASSERT_EQ(a, b) \ 36 ASSERT((a) == (b), "assert failed on (" #a " == " #b ") at " __FILE__ ":%d", __LINE__) 37 #define ASSERT_NE(a, b) \ 38 ASSERT((a) != (b), "assert failed on (" #a " != " #b ") at " __FILE__ ":%d", __LINE__) 39 #define ASSERT_GT(a, b) \ 40 ASSERT((a) > (b), "assert failed on (" #a " > " #b ") at " __FILE__ ":%d", __LINE__) 41 #define ASSERT_GE(a, b) \ 42 ASSERT((a) >= (b), "assert failed on (" #a " >= " #b ") at " __FILE__ ":%d", __LINE__) 43 #define ASSERT_LT(a, b) \ 44 ASSERT((a) < (b), "assert failed on (" #a " < " #b ") at " __FILE__ ":%d", __LINE__) 45 #define ASSERT_LE(a, b) \ 46 ASSERT((a) <= (b), "assert failed on (" #a " <= " #b ") at " __FILE__ ":%d", __LINE__) 47 48 static void fail(JNIEnv* env, const char* format, ...) { 49 va_list args; 50 51 va_start(args, format); 52 char *msg; 53 vasprintf(&msg, format, args); 54 va_end(args); 55 56 jclass exClass; 57 const char *className = "java/lang/AssertionError"; 58 exClass = env->FindClass(className); 59 env->ThrowNew(exClass, msg); 60 free(msg); 61 } 62 63 static void validateBitmapInfo(JNIEnv* env, jclass, jobject jbitmap, jint width, jint height, 64 jboolean is565) { 65 AndroidBitmapInfo info; 66 int err = 0; 67 err = AndroidBitmap_getInfo(env, jbitmap, &info); 68 ASSERT_EQ(ANDROID_BITMAP_RESULT_SUCCESS, err); 69 ASSERT_TRUE(width >= 0 && height >= 0); 70 ASSERT_EQ((uint32_t) width, info.width); 71 ASSERT_EQ((uint32_t) height, info.height); 72 int32_t format = is565 ? ANDROID_BITMAP_FORMAT_RGB_565 : ANDROID_BITMAP_FORMAT_RGBA_8888; 73 ASSERT_EQ(format, info.format); 74 } 75 76 static void validateNdkAccessAfterRecycle(JNIEnv* env, jclass, jobject jbitmap) { 77 void* pixels = nullptr; 78 int err = AndroidBitmap_lockPixels(env, jbitmap, &pixels); 79 ASSERT_EQ(err, ANDROID_BITMAP_RESULT_JNI_EXCEPTION); 80 } 81 82 static JNINativeMethod gMethods[] = { 83 { "nValidateBitmapInfo", "(Landroid/graphics/Bitmap;IIZ)V", 84 (void*) validateBitmapInfo }, 85 { "nValidateNdkAccessAfterRecycle", "(Landroid/graphics/Bitmap;)V", 86 (void*) validateNdkAccessAfterRecycle }, 87 }; 88 89 int register_android_graphics_cts_BitmapTest(JNIEnv* env) { 90 jclass clazz = env->FindClass("android/graphics/cts/BitmapTest"); 91 return env->RegisterNatives(clazz, gMethods, 92 sizeof(gMethods) / sizeof(JNINativeMethod)); 93 } 94