Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 378d9d26 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "cleanup(libgui): remove bq_setframerate flag" into main

parents c69f1c5d b1e81ee1
Loading
Loading
Loading
Loading
+20 −78
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTextur
struct fields_t {
    jfieldID  surfaceTexture;
    jfieldID  producer;
    jfieldID  frameAvailableListener;
    jmethodID postEvent;
    jmethodID postOnSetFrameRateEvent;
};
@@ -108,21 +107,6 @@ static void SurfaceTexture_setProducer(JNIEnv* env, jobject thiz,
    env->SetLongField(thiz, fields.producer, (jlong)producer.get());
}

static void SurfaceTexture_setFrameAvailableListener(JNIEnv* env,
        jobject thiz, sp<SurfaceTexture::FrameAvailableListener> listener)
{
    SurfaceTexture::FrameAvailableListener* const p =
        (SurfaceTexture::FrameAvailableListener*)
            env->GetLongField(thiz, fields.frameAvailableListener);
    if (listener.get()) {
        listener->incStrong((void*)SurfaceTexture_setSurfaceTexture);
    }
    if (p) {
        p->decStrong((void*)SurfaceTexture_setSurfaceTexture);
    }
    env->SetLongField(thiz, fields.frameAvailableListener, (jlong)listener.get());
}

sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) {
    return (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
}
@@ -138,12 +122,12 @@ bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) {

// ----------------------------------------------------------------------------

class JNISurfaceTextureContextCommon {
class JNISurfaceTextureContextListener : public SurfaceTexture::SurfaceTextureListener {
public:
    JNISurfaceTextureContextCommon(JNIEnv* env, jobject weakThiz, jclass clazz)
    JNISurfaceTextureContextListener(JNIEnv* env, jobject weakThiz, jclass clazz)
          : mWeakThiz(env->NewGlobalRef(weakThiz)), mClazz((jclass)env->NewGlobalRef(clazz)) {}

    virtual ~JNISurfaceTextureContextCommon() {
    ~JNISurfaceTextureContextListener() {
        JNIEnv* env = getJNIEnv();
        if (env != NULL) {
            env->DeleteGlobalRef(mWeakThiz);
@@ -153,7 +137,7 @@ public:
        }
    }

    void onFrameAvailable(const BufferItem& item) {
    void onFrameAvailable(const BufferItem& item) override {
        JNIEnv* env = getJNIEnv();
        if (env != NULL) {
            env->CallStaticVoidMethod(mClazz, fields.postEvent, mWeakThiz);
@@ -162,7 +146,18 @@ public:
        }
    }

protected:
    void onSetFrameRate(float frameRate, int8_t compatibility,
                        int8_t changeFrameRateStrategy) override {
        JNIEnv* env = getJNIEnv();
        if (env != NULL) {
            env->CallStaticVoidMethod(mClazz, fields.postOnSetFrameRateEvent, mWeakThiz, frameRate,
                                      compatibility, changeFrameRateStrategy);
        } else {
            ALOGW("onSetFrameRate event will not posted");
        }
    }

private:
    static JNIEnv* getJNIEnv() {
        JNIEnv* env = AndroidRuntime::getJNIEnv();
        if (env == NULL) {
@@ -181,46 +176,11 @@ protected:
    jclass mClazz;
};

class JNISurfaceTextureContextFrameAvailableListener
      : public JNISurfaceTextureContextCommon,
        public SurfaceTexture::FrameAvailableListener {
public:
    JNISurfaceTextureContextFrameAvailableListener(JNIEnv* env, jobject weakThiz, jclass clazz)
          : JNISurfaceTextureContextCommon(env, weakThiz, clazz) {}
    void onFrameAvailable(const BufferItem& item) override {
        JNISurfaceTextureContextCommon::onFrameAvailable(item);
    }
};

class JNISurfaceTextureContextListener : public JNISurfaceTextureContextCommon,
                                         public SurfaceTexture::SurfaceTextureListener {
public:
    JNISurfaceTextureContextListener(JNIEnv* env, jobject weakThiz, jclass clazz)
          : JNISurfaceTextureContextCommon(env, weakThiz, clazz) {}

    void onFrameAvailable(const BufferItem& item) override {
        JNISurfaceTextureContextCommon::onFrameAvailable(item);
    }

    void onSetFrameRate(float frameRate, int8_t compatibility,
                        int8_t changeFrameRateStrategy) override {
        JNIEnv* env = getJNIEnv();
        if (env != NULL) {
            env->CallStaticVoidMethod(mClazz, fields.postOnSetFrameRateEvent, mWeakThiz, frameRate,
                                      compatibility, changeFrameRateStrategy);
        } else {
            ALOGW("onSetFrameRate event will not posted");
        }
    }
};

// ----------------------------------------------------------------------------


#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
#define ANDROID_GRAPHICS_PRODUCER_JNI_ID "mProducer"
#define ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID \
                                         "mFrameAvailableListener"

static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
{
@@ -236,12 +196,6 @@ static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
        ALOGE("can't find android/graphics/SurfaceTexture.%s",
                ANDROID_GRAPHICS_PRODUCER_JNI_ID);
    }
    fields.frameAvailableListener = env->GetFieldID(clazz,
            ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID, "J");
    if (fields.frameAvailableListener == NULL) {
        ALOGE("can't find android/graphics/SurfaceTexture.%s",
                ANDROID_GRAPHICS_FRAMEAVAILABLELISTENER_JNI_ID);
    }

    fields.postEvent = env->GetStaticMethodID(clazz, "postEventFromNative",
            "(Ljava/lang/ref/WeakReference;)V");
@@ -329,27 +283,15 @@ static void SurfaceTexture_init(JNIEnv* env, jobject thiz, jboolean isDetached,
        return;
    }

    if (com::android::graphics::libgui::flags::bq_setframerate()) {
    sp<JNISurfaceTextureContextListener> ctx(
            new JNISurfaceTextureContextListener(env, weakThiz, clazz));
    surfaceTexture->setSurfaceTextureListener(ctx);
    } else {
        sp<JNISurfaceTextureContextFrameAvailableListener> ctx(
                new JNISurfaceTextureContextFrameAvailableListener(env, weakThiz, clazz));
        surfaceTexture->setFrameAvailableListener(ctx);
        SurfaceTexture_setFrameAvailableListener(env, thiz, ctx);
    }
}

static void SurfaceTexture_finalize(JNIEnv* env, jobject thiz)
{
    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
    if (com::android::graphics::libgui::flags::bq_setframerate()) {
    surfaceTexture->setSurfaceTextureListener(0);
    } else {
        surfaceTexture->setFrameAvailableListener(0);
        SurfaceTexture_setFrameAvailableListener(env, thiz, 0);
    }
    SurfaceTexture_setSurfaceTexture(env, thiz, 0);
    SurfaceTexture_setProducer(env, thiz, 0);
}