Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #define LOG_TAG "CompositionSamplingListener" |
| 18 | |
| 19 | #include "android_util_Binder.h" |
Louis Chang | e67f654 | 2019-03-22 16:26:10 +0800 | [diff] [blame] | 20 | #include "core_jni_helpers.h" |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 21 | |
| 22 | #include <nativehelper/JNIHelp.h> |
| 23 | |
| 24 | #include <android_runtime/AndroidRuntime.h> |
| 25 | #include <android_runtime/Log.h> |
| 26 | #include <utils/Log.h> |
| 27 | #include <utils/RefBase.h> |
| 28 | #include <binder/IServiceManager.h> |
| 29 | |
| 30 | #include <gui/IRegionSamplingListener.h> |
| 31 | #include <gui/ISurfaceComposer.h> |
Kevin DuBois | 66ed4f8 | 2019-02-18 16:27:11 -0800 | [diff] [blame] | 32 | #include <gui/SurfaceComposerClient.h> |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 33 | #include <ui/Rect.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | struct { |
| 40 | jclass mClass; |
| 41 | jmethodID mDispatchOnSampleCollected; |
| 42 | } gListenerClassInfo; |
| 43 | |
| 44 | struct CompositionSamplingListener : public BnRegionSamplingListener { |
| 45 | CompositionSamplingListener(JNIEnv* env, jobject listener) |
| 46 | : mListener(env->NewGlobalRef(listener)) {} |
| 47 | |
| 48 | void onSampleCollected(float medianLuma) override { |
| 49 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 50 | LOG_ALWAYS_FATAL_IF(env == nullptr, "Unable to retrieve JNIEnv in onSampleCollected."); |
| 51 | |
| 52 | env->CallStaticVoidMethod(gListenerClassInfo.mClass, |
| 53 | gListenerClassInfo.mDispatchOnSampleCollected, mListener, |
| 54 | static_cast<jfloat>(medianLuma)); |
| 55 | if (env->ExceptionCheck()) { |
| 56 | ALOGE("CompositionSamplingListener.onSampleCollected() failed."); |
| 57 | LOGE_EX(env); |
| 58 | env->ExceptionClear(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | virtual ~CompositionSamplingListener() { |
| 64 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 65 | env->DeleteGlobalRef(mListener); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | jobject mListener; |
| 70 | }; |
| 71 | |
| 72 | jlong nativeCreate(JNIEnv* env, jclass clazz, jobject obj) { |
| 73 | CompositionSamplingListener* listener = new CompositionSamplingListener(env, obj); |
| 74 | listener->incStrong((void*)nativeCreate); |
| 75 | return reinterpret_cast<jlong>(listener); |
| 76 | } |
| 77 | |
| 78 | void nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) { |
| 79 | CompositionSamplingListener* listener = reinterpret_cast<CompositionSamplingListener*>(ptr); |
| 80 | listener->decStrong((void*)nativeCreate); |
| 81 | } |
| 82 | |
| 83 | void nativeRegister(JNIEnv* env, jclass clazz, jlong ptr, jobject stopLayerTokenObj, |
| 84 | jint left, jint top, jint right, jint bottom) { |
| 85 | sp<CompositionSamplingListener> listener = reinterpret_cast<CompositionSamplingListener*>(ptr); |
| 86 | sp<IBinder> stopLayerHandle = ibinderForJavaObject(env, stopLayerTokenObj); |
| 87 | |
Kevin DuBois | 66ed4f8 | 2019-02-18 16:27:11 -0800 | [diff] [blame] | 88 | if (SurfaceComposerClient::addRegionSamplingListener( |
| 89 | Rect(left, top, right, bottom), stopLayerHandle, listener) != OK) { |
| 90 | constexpr auto error_msg = "Couldn't addRegionSamplingListener"; |
| 91 | ALOGE(error_msg); |
| 92 | jniThrowRuntimeException(env, error_msg); |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 93 | } |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void nativeUnregister(JNIEnv* env, jclass clazz, jlong ptr) { |
| 97 | sp<CompositionSamplingListener> listener = reinterpret_cast<CompositionSamplingListener*>(ptr); |
| 98 | |
Kevin DuBois | 66ed4f8 | 2019-02-18 16:27:11 -0800 | [diff] [blame] | 99 | if (SurfaceComposerClient::removeRegionSamplingListener(listener) != OK) { |
| 100 | constexpr auto error_msg = "Couldn't removeRegionSamplingListener"; |
| 101 | ALOGE(error_msg); |
| 102 | jniThrowRuntimeException(env, error_msg); |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 103 | } |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | const JNINativeMethod gMethods[] = { |
| 107 | /* name, signature, funcPtr */ |
| 108 | { "nativeCreate", "(Landroid/view/CompositionSamplingListener;)J", |
| 109 | (void*)nativeCreate }, |
| 110 | { "nativeDestroy", "(J)V", |
| 111 | (void*)nativeDestroy }, |
| 112 | { "nativeRegister", "(JLandroid/os/IBinder;IIII)V", |
| 113 | (void*)nativeRegister }, |
| 114 | { "nativeUnregister", "(J)V", |
| 115 | (void*)nativeUnregister } |
| 116 | }; |
| 117 | |
| 118 | } // namespace |
| 119 | |
| 120 | int register_android_view_CompositionSamplingListener(JNIEnv* env) { |
| 121 | int res = jniRegisterNativeMethods(env, "android/view/CompositionSamplingListener", |
| 122 | gMethods, NELEM(gMethods)); |
| 123 | LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods."); |
| 124 | |
| 125 | jclass clazz = env->FindClass("android/view/CompositionSamplingListener"); |
Louis Chang | e67f654 | 2019-03-22 16:26:10 +0800 | [diff] [blame] | 126 | gListenerClassInfo.mClass = MakeGlobalRefOrDie(env, clazz); |
Adrian Roos | 9b963d3 | 2019-02-13 18:39:36 +0000 | [diff] [blame] | 127 | gListenerClassInfo.mDispatchOnSampleCollected = env->GetStaticMethodID( |
| 128 | clazz, "dispatchOnSampleCollected", "(Landroid/view/CompositionSamplingListener;F)V"); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | } // namespace android |